-
Notifications
You must be signed in to change notification settings - Fork 1
/
cfile_gzip.c
250 lines (220 loc) · 7.98 KB
/
cfile_gzip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* cfile.c
* This file is part of The PaulWay Libraries
*
* Copyright (C) 2006 Paul Wayper <[email protected]>
* Copyright (C) 2012 Peter Miller
*
* The PaulWay Libraries are free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* The PaulWay Libraries are distributed in the hope that they will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <zlib.h>
#include <talloc.h>
#include <stdint.h>
#include "cfile_private.h"
#include "cfile_gzip.h"
/* Predeclare function calls */
off_t gzip_size(cfile *fp);
bool gzip_eof(cfile *fp);
char *gzip_gets(cfile *fp, char *str, size_t len);
ssize_t gzip_read(cfile *fp, void *ptr, size_t size, size_t num);
ssize_t gzip_write(cfile *fp, const void *ptr, size_t size, size_t num);
int gzip_flush(cfile *fp);
int gzip_close(cfile *fp);
/*! \brief The gzip file structure
*
* We only need to store the actual (zlib) file pointer.
*/
typedef struct cfile_gzip {
cfile inherited; /*< our inherited function table */
gzFile gp; /*< the actual zlib file pointer */
} cfile_gzip;
static const cfile_vtable gzip_cfile_table;
/*! \brief Open a file for reading or writing
*
* Open the given file using the given mode. Opens the file and
* returns a cfile handle to it. Mode must start with 'r' or 'w'
* to read or write (respectively) - other modes are not expected
* to work.
*
* \return A successfully created file handle, or NULL on failure.
*/
cfile *gzip_open(const char *name, /*!< The name of the file to open.
At this stage we don't attempt to pick up reading stdin or
writing stdout as gzip compressed streams. */
const char *mode) /*!< "r" to specify reading, "w" for writing. */
{
cfile_gzip *cfzp;
gzFile own_file = gzopen(name, mode);
if (!own_file) {
/* Keep any errno set by gzopen - let it handle any invalid modes,
etc. */
return NULL;
}
cfzp = (cfile_gzip *)cfile_alloc(&gzip_cfile_table, name, mode);
if (!cfzp) {
errno = EINVAL;
gzclose(own_file);
return NULL;
}
cfzp->gp = own_file;
return (cfile *)cfzp;
}
/* We don't, as yet, support opening a file descriptor as a gzip stream. */
/*! \brief Returns the _uncompressed_ file size
*
* Determining the uncompressed file size is fairly
* easy with gzip files - the size is a 32-bit little-endian signed
* int (I think) at the end of the file.
*
* \param fp The file handle to check
* \return The number of bytes in the uncompressed file.
*/
off_t gzip_size(cfile *fp) {
uint32_t size; /* Make sure this is a 32-bit int! */
FILE *rawfp = fopen(fp->filename,"rb"); /* open the compressed file directly */
if (!rawfp) {
return 0;
}
fseek(rawfp,-4,SEEK_END);
fread(&size,4,1,rawfp);
fclose(rawfp);
return (off_t)size;
}
/*! \brief Returns true if we've reached the end of the file being read.
*
* This passes through the state of the lower-level's EOF checking.
*
* \param fp The file handle to check.
* \return True (1) if the file has reached EOF, False (0) if not.
*/
bool gzip_eof(cfile *fp) {
cfile_gzip *cfzp = (cfile_gzip *)fp;
return gzeof(cfzp->gp);
}
/*! \brief Get a string from the file, up to a maximum length or newline.
*
* For gzipped files this simply uses zlib's fgets implementation.
*
* \param fp The file handle to read from.
* \param str An array of characters to read the file contents into.
* \param len The maximum length, plus one, of the string to read. In
* other words, if this is 10, then fgets will read a maximum of nine
* characters from the file. The character after the last character
* read is always set to \\0 to terminate the string. The newline
* character is kept on the line if there was room to read it.
* \return A pointer to the string thus read.
*/
char *gzip_gets(cfile *fp, char *str, size_t len) {
cfile_gzip *cfzp = (cfile_gzip *)fp;
return gzgets(cfzp->gp, str, len);
}
/*! \brief Print a formatted string to the file, from another function
*
* The standard vfprintf implementation. For those people that have
* to receive a '...' argument in their own function and send it to
* a cfile.
*
* \param fp The file handle to write to.
* \param fmt The format string to print.
* \param ap The compiled va_list of parameters to print.
* \return The success of the file write operation.
* \todo Should we be reusing a buffer rather than allocating one each time?
*/
int gzip_vprintf(cfile *fp, const char *fmt, va_list ap)
__attribute ((format (printf, 2, 0)));
int gzip_vprintf(cfile *fp, const char *fmt, va_list ap) {
cfile_gzip *cfzp = (cfile_gzip *)fp;
int rtn;
char *buf = talloc_vasprintf(fp, fmt, ap);
/* Problem in zlib forbids gzprintf of more than 4095 characters
* at a time. Use gzwrite to get around this, assuming that it
* doesn't have the same problem... */
rtn = gzwrite(cfzp->gp, buf, strlen(buf));
talloc_free(buf);
return rtn;
}
/*! \brief Read a block of data from the file.
*
* Reads a given number of structures of a specified size from the
* file into the memory pointer given. The destination memory must
* be allocated first. Some read functions only specify one size,
* we use two here because that's what fread requires (and it's
* better for the programmer anyway IMHO).
* \param fp The file handle to read from.
* \param ptr The memory to write into.
* \param size The size of each structure in bytes.
* \param num The number of structures to read.
* \return The success of the file read operation.
*/
ssize_t gzip_read(cfile *fp, void *ptr, size_t size, size_t num) {
cfile_gzip *cfzp = (cfile_gzip *)fp;
return gzread(cfzp->gp, ptr, size * num);
}
/*! \brief Write a block of data from the file.
*
* Writes a given number of structures of a specified size into the
* file from the memory pointer given.
* \param fp The file handle to write into.
* \param ptr The memory to read from.
* \param size The size of each structure in bytes.
* \param num The number of structures to write.
* \return The success of the file write operation.
*/
ssize_t gzip_write(cfile *fp, const void *ptr, size_t size, size_t num) {
cfile_gzip *cfzp = (cfile_gzip *)fp;
return gzwrite(cfzp->gp, ptr, size * num);
}
/*! \brief Flush the file's output buffer.
*
* This function flushes any data passed to write or printf but not
* yet written to disk. If the file is being read, it has no effect.
* \param fp The file handle to flush.
* \return the success of the file flush operation.
* \note for gzip files, under certain compression methods, flushing
* may result in lower compression performance. We use Z_SYNC_FLUSH
* to write to the nearest byte boundary without unduly impacting
* compression.
*/
int gzip_flush(cfile *fp) {
cfile_gzip *cfzp = (cfile_gzip *)fp;
return gzflush(cfzp->gp, Z_SYNC_FLUSH);
}
/*! \brief Close the given file handle.
*
* This function frees the memory allocated for the file handle and
* closes the associated file.
* \param fp The file handle to close.
* \return the success of the file close operation.
*/
int gzip_close(cfile *fp) {
cfile_gzip *cfzp = (cfile_gzip *)fp;
return gzclose(cfzp->gp);
}
/*! \brief The function dispatch table for gzip files */
static const cfile_vtable gzip_cfile_table = {
sizeof(cfile_gzip),
gzip_size,
gzip_eof,
gzip_gets,
gzip_vprintf,
gzip_read,
gzip_write,
gzip_flush,
gzip_close,
"GZip file"
};