1 /* gzlib.c contains minimal changes required to be compiled with zlibWrapper:
2 * - gz_statep was converted to union to work with -Wstrict-aliasing=1 */
4 /* gzlib.c -- zlib functions common to reading and writing gzip files
5 * Copyright (C) 2004-2017 Mark Adler
6 * For conditions of distribution and use, see https://www.zlib.net/zlib_license.html
11 #if defined(_WIN32) && !defined(__BORLANDC__) && !defined(__MINGW32__)
12 # define LSEEK _lseeki64
14 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
15 # define LSEEK lseek64
22 local void gz_reset _Z_OF((gz_statep));
23 local gzFile gz_open _Z_OF((const void *, int, const char *));
27 /* Map the Windows error number in ERROR to a locale-dependent error message
28 string and return a pointer to it. Typically, the values for ERROR come
31 The string pointed to shall not be modified by the application, but may be
32 overwritten by a subsequent call to gz_strwinerror
34 The gz_strwinerror function does not change the current setting of
36 char ZLIB_INTERNAL *gz_strwinerror (error)
39 static char buf[1024];
42 DWORD lasterr = GetLastError();
43 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
44 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
47 0, /* Default language */
52 /* If there is an \r\n appended, zap it. */
54 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
59 if (chars > sizeof (buf) - 1) {
60 chars = sizeof (buf) - 1;
64 wcstombs(buf, msgbuf, chars + 1);
68 sprintf(buf, "unknown win32 error (%ld)", error);
71 SetLastError(lasterr);
77 /* Reset gzip file state */
78 local void gz_reset(state)
81 state.state->x.have = 0; /* no output data available */
82 if (state.state->mode == GZ_READ) { /* for reading ... */
83 state.state->eof = 0; /* not at end of file */
84 state.state->past = 0; /* have not read past end yet */
85 state.state->how = LOOK; /* look for gzip header */
87 state.state->seek = 0; /* no seek request pending */
88 gz_error(state, Z_OK, NULL); /* clear error */
89 state.state->x.pos = 0; /* no uncompressed data yet */
90 state.state->strm.avail_in = 0; /* no input data yet */
93 /* Open a gzip file either by name or file descriptor. */
94 local gzFile gz_open(path, fd, mode)
113 /* allocate gzFile structure to return */
114 state.state = (gz_state*)malloc(sizeof(gz_state));
115 if (state.state == NULL)
117 state.state->size = 0; /* no buffers allocated yet */
118 state.state->want = GZBUFSIZE; /* requested buffer size */
119 state.state->msg = NULL; /* no error message yet */
122 state.state->mode = GZ_NONE;
123 state.state->level = Z_DEFAULT_COMPRESSION;
124 state.state->strategy = Z_DEFAULT_STRATEGY;
125 state.state->direct = 0;
127 if (*mode >= '0' && *mode <= '9')
128 state.state->level = *mode - '0';
132 state.state->mode = GZ_READ;
134 #ifndef NO_GZCOMPRESS
136 state.state->mode = GZ_WRITE;
139 state.state->mode = GZ_APPEND;
142 case '+': /* can't read and write at the same time */
145 case 'b': /* ignore -- will request binary anyway */
158 state.state->strategy = Z_FILTERED;
161 state.state->strategy = Z_HUFFMAN_ONLY;
164 state.state->strategy = Z_RLE;
167 state.state->strategy = Z_FIXED;
170 state.state->direct = 1;
172 default: /* could consider as an error, but just ignore */
178 /* must provide an "r", "w", or "a" */
179 if (state.state->mode == GZ_NONE) {
184 /* can't force transparent read */
185 if (state.state->mode == GZ_READ) {
186 if (state.state->direct) {
190 state.state->direct = 1; /* for empty file */
193 /* save the path name for error messages */
196 len = wcstombs(NULL, path, 0);
197 if (len == (z_size_t)-1)
202 len = strlen((const char *)path);
203 state.state->path = (char *)malloc(len + 1);
204 if (state.state->path == NULL) {
211 wcstombs(state.state->path, path, len + 1);
213 *(state.state->path) = 0;
216 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
217 (void)snprintf(state.state->path, len + 1, "%s", (const char *)path);
219 strcpy(state.state->path, (const char*)path);
222 /* compute the flags for open() */
231 (cloexec ? O_CLOEXEC : 0) |
233 (state.state->mode == GZ_READ ?
235 (O_WRONLY | O_CREAT |
237 (exclusive ? O_EXCL : 0) |
239 (state.state->mode == GZ_WRITE ?
243 /* open the file with the appropriate flags (or just use fd) */
244 state.state->fd = fd > -1 ? fd : (
246 fd == -2 ? _wopen(path, oflag, 0666) :
248 open((const char *)path, oflag, 0666));
249 if (state.state->fd == -1) {
250 free(state.state->path);
254 if (state.state->mode == GZ_APPEND) {
255 LSEEK(state.state->fd, 0, SEEK_END); /* so gzoffset() is correct */
256 state.state->mode = GZ_WRITE; /* simplify later checks */
259 /* save the current position for rewinding (only if reading) */
260 if (state.state->mode == GZ_READ) {
261 state.state->start = LSEEK(state.state->fd, 0, SEEK_CUR);
262 if (state.state->start == -1) state.state->start = 0;
265 /* initialize stream */
272 /* -- see zlib.h -- */
273 gzFile ZEXPORT gzopen(path, mode)
277 return gz_open(path, -1, mode);
280 /* -- see zlib.h -- */
281 gzFile ZEXPORT gzopen64(path, mode)
285 return gz_open(path, -1, mode);
288 /* -- see zlib.h -- */
289 gzFile ZEXPORT gzdopen(fd, mode)
293 char *path; /* identifier for error messages */
296 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
298 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
299 (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd);
301 sprintf(path, "<fd:%d>", fd); /* for debugging */
303 gz = gz_open(path, fd, mode);
308 /* -- see zlib.h -- */
310 gzFile ZEXPORT gzopen_w(path, mode)
314 return gz_open(path, -2, mode);
318 /* -- see zlib.h -- */
319 int ZEXPORT gzbuffer(file, size)
325 /* get internal structure and check integrity */
329 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
332 /* make sure we haven't already allocated memory */
333 if (state.state->size != 0)
336 /* check and set requested size */
337 if ((size << 1) < size)
338 return -1; /* need to be able to double it */
340 size = 2; /* need two bytes to check magic header */
341 state.state->want = size;
345 /* -- see zlib.h -- */
346 int ZEXPORT gzrewind(file)
351 /* get internal structure */
356 /* check that we're reading and that there's no error */
357 if (state.state->mode != GZ_READ ||
358 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
361 /* back up and start over */
362 if (LSEEK(state.state->fd, state.state->start, SEEK_SET) == -1)
368 /* -- see zlib.h -- */
369 z_off64_t ZEXPORT gzseek64(file, offset, whence)
378 /* get internal structure and check integrity */
382 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
385 /* check that there's no error */
386 if (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
389 /* can only seek from start or relative to current position */
390 if (whence != SEEK_SET && whence != SEEK_CUR)
393 /* normalize offset to a SEEK_CUR specification */
394 if (whence == SEEK_SET)
395 offset -= state.state->x.pos;
396 else if (state.state->seek)
397 offset += state.state->skip;
398 state.state->seek = 0;
400 /* if within raw area while reading, just go there */
401 if (state.state->mode == GZ_READ && state.state->how == COPY &&
402 state.state->x.pos + offset >= 0) {
403 ret = LSEEK(state.state->fd, offset - state.state->x.have, SEEK_CUR);
406 state.state->x.have = 0;
407 state.state->eof = 0;
408 state.state->past = 0;
409 state.state->seek = 0;
410 gz_error(state, Z_OK, NULL);
411 state.state->strm.avail_in = 0;
412 state.state->x.pos += offset;
413 return state.state->x.pos;
416 /* calculate skip amount, rewinding if needed for back seek when reading */
418 if (state.state->mode != GZ_READ) /* writing -- can't go backwards */
420 offset += state.state->x.pos;
421 if (offset < 0) /* before start of file! */
423 if (gzrewind(file) == -1) /* rewind, then skip to offset */
427 /* if reading, skip what's in output buffer (one less gzgetc() check) */
428 if (state.state->mode == GZ_READ) {
429 n = GT_OFF(state.state->x.have) || (z_off64_t)state.state->x.have > offset ?
430 (unsigned)offset : state.state->x.have;
431 state.state->x.have -= n;
432 state.state->x.next += n;
433 state.state->x.pos += n;
437 /* request skip (if not zero) */
439 state.state->seek = 1;
440 state.state->skip = offset;
442 return state.state->x.pos + offset;
445 /* -- see zlib.h -- */
446 z_off_t ZEXPORT gzseek(file, offset, whence)
453 ret = gzseek64(file, (z_off64_t)offset, whence);
454 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
457 /* -- see zlib.h -- */
458 z_off64_t ZEXPORT gztell64(file)
463 /* get internal structure and check integrity */
467 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
470 /* return position */
471 return state.state->x.pos + (state.state->seek ? state.state->skip : 0);
474 /* -- see zlib.h -- */
475 z_off_t ZEXPORT gztell(file)
480 ret = gztell64(file);
481 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
484 /* -- see zlib.h -- */
485 z_off64_t ZEXPORT gzoffset64(file)
491 /* get internal structure and check integrity */
495 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
498 /* compute and return effective offset in file */
499 offset = LSEEK(state.state->fd, 0, SEEK_CUR);
502 if (state.state->mode == GZ_READ) /* reading */
503 offset -= state.state->strm.avail_in; /* don't count buffered input */
507 /* -- see zlib.h -- */
508 z_off_t ZEXPORT gzoffset(file)
513 ret = gzoffset64(file);
514 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
517 /* -- see zlib.h -- */
518 int ZEXPORT gzeof(file)
523 /* get internal structure and check integrity */
527 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
530 /* return end-of-file state */
531 return state.state->mode == GZ_READ ? state.state->past : 0;
534 /* -- see zlib.h -- */
535 const char * ZEXPORT gzerror(file, errnum)
541 /* get internal structure and check integrity */
545 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
548 /* return error information */
550 *errnum = state.state->err;
551 return state.state->err == Z_MEM_ERROR ? "out of memory" :
552 (state.state->msg == NULL ? "" : state.state->msg);
555 /* -- see zlib.h -- */
556 void ZEXPORT gzclearerr(file)
561 /* get internal structure and check integrity */
565 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
568 /* clear error and end-of-file */
569 if (state.state->mode == GZ_READ) {
570 state.state->eof = 0;
571 state.state->past = 0;
573 gz_error(state, Z_OK, NULL);
576 /* Create an error message in allocated memory and set state.state->err and
577 state.state->msg accordingly. Free any previous error message already there. Do
578 not try to free or allocate space if the error is Z_MEM_ERROR (out of
579 memory). Simply save the error message as a static string. If there is an
580 allocation failure constructing the error message, then convert the error to
582 void ZLIB_INTERNAL gz_error(state, err, msg)
587 /* free previously allocated message and clear */
588 if (state.state->msg != NULL) {
589 if (state.state->err != Z_MEM_ERROR)
590 free(state.state->msg);
591 state.state->msg = NULL;
594 /* if fatal, set state.state->x.have to 0 so that the gzgetc() macro fails */
595 if (err != Z_OK && err != Z_BUF_ERROR)
596 state.state->x.have = 0;
598 /* set error code, and if no message, then done */
599 state.state->err = err;
603 /* for an out of memory error, return literal string when requested */
604 if (err == Z_MEM_ERROR)
607 /* construct error message with path */
608 if ((state.state->msg = (char *)malloc(strlen(state.state->path) + strlen(msg) + 3)) ==
610 state.state->err = Z_MEM_ERROR;
613 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
614 (void)snprintf(state.state->msg, strlen(state.state->path) + strlen(msg) + 3,
615 "%s%s%s", state.state->path, ": ", msg);
617 strcpy(state.state->msg, state.state->path);
618 strcat(state.state->msg, ": ");
619 strcat(state.state->msg, msg);
624 /* portably return maximum value for an int (when limits.h presumed not
625 available) -- we need to do this to cover cases where 2's complement not
626 used, since C standard permits 1's complement and sign-bit representations,
627 otherwise we could just use ((unsigned)-1) >> 1 */
628 unsigned ZLIB_INTERNAL gz_intmax()