1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004-2017 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6 #include "zbuild.h"
7 #include "gzguts.h"
8
9 #if defined(_WIN32)
10 # define LSEEK _lseeki64
11 #else
12 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
13 # define LSEEK lseek64
14 #else
15 # define LSEEK lseek
16 #endif
17 #endif
18
19 /* Local functions */
20 static void gz_reset(gz_state *);
21 static gzFile gz_open(const void *, int, const char *);
22
23 /* Reset gzip file state */
gz_reset(gz_state * state)24 static void gz_reset(gz_state *state) {
25 state->x.have = 0; /* no output data available */
26 if (state->mode == GZ_READ) { /* for reading ... */
27 state->eof = 0; /* not at end of file */
28 state->past = 0; /* have not read past end yet */
29 state->how = LOOK; /* look for gzip header */
30 }
31 else /* for writing ... */
32 state->reset = 0; /* no deflateReset pending */
33 state->seek = 0; /* no seek request pending */
34 gz_error(state, Z_OK, NULL); /* clear error */
35 state->x.pos = 0; /* no uncompressed data yet */
36 state->strm.avail_in = 0; /* no input data yet */
37 }
38
39 /* Open a gzip file either by name or file descriptor. */
gz_open(const void * path,int fd,const char * mode)40 static gzFile gz_open(const void *path, int fd, const char *mode) {
41 gz_state *state;
42 size_t len;
43 int oflag;
44 #ifdef O_CLOEXEC
45 int cloexec = 0;
46 #endif
47 #ifdef O_EXCL
48 int exclusive = 0;
49 #endif
50
51 /* check input */
52 if (path == NULL)
53 return NULL;
54
55 /* allocate gzFile structure to return */
56 state = (gz_state *)malloc(sizeof(gz_state));
57 if (state == NULL)
58 return NULL;
59 state->size = 0; /* no buffers allocated yet */
60 state->want = GZBUFSIZE; /* requested buffer size */
61 state->msg = NULL; /* no error message yet */
62
63 /* interpret mode */
64 state->mode = GZ_NONE;
65 state->level = Z_DEFAULT_COMPRESSION;
66 state->strategy = Z_DEFAULT_STRATEGY;
67 state->direct = 0;
68 while (*mode) {
69 if (*mode >= '0' && *mode <= '9') {
70 state->level = *mode - '0';
71 } else {
72 switch (*mode) {
73 case 'r':
74 state->mode = GZ_READ;
75 break;
76 #ifndef NO_GZCOMPRESS
77 case 'w':
78 state->mode = GZ_WRITE;
79 break;
80 case 'a':
81 state->mode = GZ_APPEND;
82 break;
83 #endif
84 case '+': /* can't read and write at the same time */
85 free(state);
86 return NULL;
87 case 'b': /* ignore -- will request binary anyway */
88 break;
89 #ifdef O_CLOEXEC
90 case 'e':
91 cloexec = 1;
92 break;
93 #endif
94 #ifdef O_EXCL
95 case 'x':
96 exclusive = 1;
97 break;
98 #endif
99 case 'f':
100 state->strategy = Z_FILTERED;
101 break;
102 case 'h':
103 state->strategy = Z_HUFFMAN_ONLY;
104 break;
105 case 'R':
106 state->strategy = Z_RLE;
107 break;
108 case 'F':
109 state->strategy = Z_FIXED;
110 break;
111 case 'T':
112 state->direct = 1;
113 break;
114 default: /* could consider as an error, but just ignore */
115 {}
116 }
117 }
118 mode++;
119 }
120
121 /* must provide an "r", "w", or "a" */
122 if (state->mode == GZ_NONE) {
123 free(state);
124 return NULL;
125 }
126
127 /* can't force transparent read */
128 if (state->mode == GZ_READ) {
129 if (state->direct) {
130 free(state);
131 return NULL;
132 }
133 state->direct = 1; /* for empty file */
134 }
135
136 /* save the path name for error messages */
137 #ifdef WIDECHAR
138 if (fd == -2) {
139 len = wcstombs(NULL, (const wchar_t *)path, 0);
140 if (len == (size_t)-1)
141 len = 0;
142 } else
143 #endif
144 len = strlen((const char *)path);
145 state->path = (char *)malloc(len + 1);
146 if (state->path == NULL) {
147 free(state);
148 return NULL;
149 }
150 #ifdef WIDECHAR
151 if (fd == -2)
152 if (len) {
153 wcstombs(state->path, (const wchar_t *)path, len + 1);
154 } else {
155 *(state->path) = 0;
156 }
157 else
158 #endif
159 (void)snprintf(state->path, len + 1, "%s", (const char *)path);
160
161 /* compute the flags for open() */
162 oflag =
163 #ifdef O_LARGEFILE
164 O_LARGEFILE |
165 #endif
166 #ifdef O_BINARY
167 O_BINARY |
168 #endif
169 #ifdef O_CLOEXEC
170 (cloexec ? O_CLOEXEC : 0) |
171 #endif
172 (state->mode == GZ_READ ?
173 O_RDONLY :
174 (O_WRONLY | O_CREAT |
175 #ifdef O_EXCL
176 (exclusive ? O_EXCL : 0) |
177 #endif
178 (state->mode == GZ_WRITE ?
179 O_TRUNC :
180 O_APPEND)));
181
182 /* open the file with the appropriate flags (or just use fd) */
183 state->fd = fd > -1 ? fd : (
184 #if defined(_WIN32)
185 fd == -2 ? _wopen((const wchar_t *)path, oflag, 0666) :
186 #elif __CYGWIN__
187 fd == -2 ? open(state->path, oflag, 0666) :
188 #endif
189 open((const char *)path, oflag, 0666));
190 if (state->fd == -1) {
191 free(state->path);
192 free(state);
193 return NULL;
194 }
195 if (state->mode == GZ_APPEND) {
196 LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */
197 state->mode = GZ_WRITE; /* simplify later checks */
198 }
199
200 /* save the current position for rewinding (only if reading) */
201 if (state->mode == GZ_READ) {
202 state->start = LSEEK(state->fd, 0, SEEK_CUR);
203 if (state->start == -1) state->start = 0;
204 }
205
206 /* initialize stream */
207 gz_reset(state);
208
209 /* return stream */
210 return (gzFile)state;
211 }
212
213 /* -- see zlib.h -- */
PREFIX(gzopen)214 gzFile ZEXPORT PREFIX(gzopen)(const char *path, const char *mode) {
215 return gz_open(path, -1, mode);
216 }
217
218 #ifdef ZLIB_COMPAT
PREFIX4(gzopen)219 gzFile ZEXPORT PREFIX4(gzopen)(const char *path, const char *mode) {
220 return gz_open(path, -1, mode);
221 }
222 #endif
223
224 /* -- see zlib.h -- */
PREFIX(gzdopen)225 gzFile ZEXPORT PREFIX(gzdopen)(int fd, const char *mode) {
226 char *path; /* identifier for error messages */
227 gzFile gz;
228
229 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
230 return NULL;
231 (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */
232 gz = gz_open(path, fd, mode);
233 free(path);
234 return gz;
235 }
236
237 /* -- see zlib.h -- */
238 #ifdef WIDECHAR
PREFIX(gzopen_w)239 gzFile ZEXPORT PREFIX(gzopen_w)(const wchar_t *path, const char *mode) {
240 return gz_open(path, -2, mode);
241 }
242 #endif
243
244 /* -- see zlib.h -- */
PREFIX(gzbuffer)245 int ZEXPORT PREFIX(gzbuffer)(gzFile file, unsigned size) {
246 gz_state *state;
247
248 /* get internal structure and check integrity */
249 if (file == NULL)
250 return -1;
251 state = (gz_state *)file;
252 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
253 return -1;
254
255 /* make sure we haven't already allocated memory */
256 if (state->size != 0)
257 return -1;
258
259 /* check and set requested size */
260 if ((size << 1) < size)
261 return -1; /* need to be able to double it */
262 if (size < 2)
263 size = 2; /* need two bytes to check magic header */
264 state->want = size;
265 return 0;
266 }
267
268 /* -- see zlib.h -- */
PREFIX(gzrewind)269 int ZEXPORT PREFIX(gzrewind)(gzFile file) {
270 gz_state *state;
271
272 /* get internal structure */
273 if (file == NULL)
274 return -1;
275 state = (gz_state *)file;
276
277 /* check that we're reading and that there's no error */
278 if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
279 return -1;
280
281 /* back up and start over */
282 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
283 return -1;
284 gz_reset(state);
285 return 0;
286 }
287
288 /* -- see zlib.h -- */
PREFIX4(gzseek)289 z_off64_t ZEXPORT PREFIX4(gzseek)(gzFile file, z_off64_t offset, int whence) {
290 unsigned n;
291 z_off64_t ret;
292 gz_state *state;
293
294 /* get internal structure and check integrity */
295 if (file == NULL)
296 return -1;
297 state = (gz_state *)file;
298 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
299 return -1;
300
301 /* check that there's no error */
302 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
303 return -1;
304
305 /* can only seek from start or relative to current position */
306 if (whence != SEEK_SET && whence != SEEK_CUR)
307 return -1;
308
309 /* normalize offset to a SEEK_CUR specification */
310 if (whence == SEEK_SET)
311 offset -= state->x.pos;
312 else if (state->seek)
313 offset += state->skip;
314 state->seek = 0;
315
316 /* if within raw area while reading, just go there */
317 if (state->mode == GZ_READ && state->how == COPY && state->x.pos + offset >= 0) {
318 ret = LSEEK(state->fd, offset - (z_off64_t)state->x.have, SEEK_CUR);
319 if (ret == -1)
320 return -1;
321 state->x.have = 0;
322 state->eof = 0;
323 state->past = 0;
324 state->seek = 0;
325 gz_error(state, Z_OK, NULL);
326 state->strm.avail_in = 0;
327 state->x.pos += offset;
328 return state->x.pos;
329 }
330
331 /* calculate skip amount, rewinding if needed for back seek when reading */
332 if (offset < 0) {
333 if (state->mode != GZ_READ) /* writing -- can't go backwards */
334 return -1;
335 offset += state->x.pos;
336 if (offset < 0) /* before start of file! */
337 return -1;
338 if (PREFIX(gzrewind)(file) == -1) /* rewind, then skip to offset */
339 return -1;
340 }
341
342 /* if reading, skip what's in output buffer (one less gzgetc() check) */
343 if (state->mode == GZ_READ) {
344 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ? (unsigned)offset : state->x.have;
345 state->x.have -= n;
346 state->x.next += n;
347 state->x.pos += n;
348 offset -= n;
349 }
350
351 /* request skip (if not zero) */
352 if (offset) {
353 state->seek = 1;
354 state->skip = offset;
355 }
356 return state->x.pos + offset;
357 }
358
359 /* -- see zlib.h -- */
360 #ifdef ZLIB_COMPAT
PREFIX(gzseek)361 z_off_t ZEXPORT PREFIX(gzseek)(gzFile file, z_off_t offset, int whence) {
362 z_off64_t ret;
363
364 ret = PREFIX4(gzseek)(file, (z_off64_t)offset, whence);
365 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
366 }
367 #endif
368
369 /* -- see zlib.h -- */
PREFIX4(gztell)370 z_off64_t ZEXPORT PREFIX4(gztell)(gzFile file) {
371 gz_state *state;
372
373 /* get internal structure and check integrity */
374 if (file == NULL)
375 return -1;
376 state = (gz_state *)file;
377 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
378 return -1;
379
380 /* return position */
381 return state->x.pos + (state->seek ? state->skip : 0);
382 }
383
384 /* -- see zlib.h -- */
385 #ifdef ZLIB_COMPAT
PREFIX(gztell)386 z_off_t ZEXPORT PREFIX(gztell)(gzFile file) {
387
388 z_off64_t ret;
389
390 ret = PREFIX4(gztell)(file);
391 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
392 }
393 #endif
394
395 /* -- see zlib.h -- */
PREFIX4(gzoffset)396 z_off64_t ZEXPORT PREFIX4(gzoffset)(gzFile file) {
397 z_off64_t offset;
398 gz_state *state;
399
400 /* get internal structure and check integrity */
401 if (file == NULL)
402 return -1;
403 state = (gz_state *)file;
404 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
405 return -1;
406
407 /* compute and return effective offset in file */
408 offset = LSEEK(state->fd, 0, SEEK_CUR);
409 if (offset == -1)
410 return -1;
411 if (state->mode == GZ_READ) /* reading */
412 offset -= state->strm.avail_in; /* don't count buffered input */
413 return offset;
414 }
415
416 /* -- see zlib.h -- */
417 #ifdef ZLIB_COMPAT
PREFIX(gzoffset)418 z_off_t ZEXPORT PREFIX(gzoffset)(gzFile file) {
419 z_off64_t ret;
420
421 ret = PREFIX4(gzoffset)(file);
422 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
423 }
424 #endif
425
426 /* -- see zlib.h -- */
PREFIX(gzeof)427 int ZEXPORT PREFIX(gzeof)(gzFile file) {
428 gz_state *state;
429
430 /* get internal structure and check integrity */
431 if (file == NULL)
432 return 0;
433 state = (gz_state *)file;
434 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
435 return 0;
436
437 /* return end-of-file state */
438 return state->mode == GZ_READ ? state->past : 0;
439 }
440
441 /* -- see zlib.h -- */
PREFIX(gzerror)442 const char * ZEXPORT PREFIX(gzerror)(gzFile file, int *errnum) {
443 gz_state *state;
444
445 /* get internal structure and check integrity */
446 if (file == NULL)
447 return NULL;
448 state = (gz_state *)file;
449 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
450 return NULL;
451
452 /* return error information */
453 if (errnum != NULL)
454 *errnum = state->err;
455 return state->err == Z_MEM_ERROR ? "out of memory" : (state->msg == NULL ? "" : state->msg);
456 }
457
458 /* -- see zlib.h -- */
PREFIX(gzclearerr)459 void ZEXPORT PREFIX(gzclearerr)(gzFile file) {
460 gz_state *state;
461
462 /* get internal structure and check integrity */
463 if (file == NULL)
464 return;
465 state = (gz_state *)file;
466 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
467 return;
468
469 /* clear error and end-of-file */
470 if (state->mode == GZ_READ) {
471 state->eof = 0;
472 state->past = 0;
473 }
474 gz_error(state, Z_OK, NULL);
475 }
476
477 /* Create an error message in allocated memory and set state->err and
478 state->msg accordingly. Free any previous error message already there. Do
479 not try to free or allocate space if the error is Z_MEM_ERROR (out of
480 memory). Simply save the error message as a static string. If there is an
481 allocation failure constructing the error message, then convert the error to
482 out of memory. */
gz_error(gz_state * state,int err,const char * msg)483 void ZLIB_INTERNAL gz_error(gz_state *state, int err, const char *msg) {
484 /* free previously allocated message and clear */
485 if (state->msg != NULL) {
486 if (state->err != Z_MEM_ERROR)
487 free(state->msg);
488 state->msg = NULL;
489 }
490
491 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
492 if (err != Z_OK && err != Z_BUF_ERROR)
493 state->x.have = 0;
494
495 /* set error code, and if no message, then done */
496 state->err = err;
497 if (msg == NULL)
498 return;
499
500 /* for an out of memory error, return literal string when requested */
501 if (err == Z_MEM_ERROR)
502 return;
503
504 /* construct error message with path */
505 if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
506 state->err = Z_MEM_ERROR;
507 return;
508 }
509 (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, "%s%s%s", state->path, ": ", msg);
510 }
511
512 #ifndef INT_MAX
513 /* portably return maximum value for an int (when limits.h presumed not
514 available) -- we need to do this to cover cases where 2's complement not
515 used, since C standard permits 1's complement and sign-bit representations,
516 otherwise we could just use ((unsigned)-1) >> 1 */
gz_intmax()517 unsigned ZLIB_INTERNAL gz_intmax() {
518 unsigned p, q;
519
520 p = 1;
521 do {
522 q = p;
523 p <<= 1;
524 p++;
525 } while (p > q);
526 return q >> 1;
527 }
528 #endif
529