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(DWORD error) {
37 static char buf[1024];
40 DWORD lasterr = GetLastError();
41 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
42 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
45 0, /* Default language */
50 /* If there is an \r\n appended, zap it. */
52 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
57 if (chars > sizeof (buf) - 1) {
58 chars = sizeof (buf) - 1;
62 wcstombs(buf, msgbuf, chars + 1);
66 sprintf(buf, "unknown win32 error (%ld)", error);
69 SetLastError(lasterr);
75 /* Reset gzip file state */
76 local void gz_reset(gz_statep state) {
77 state.state->x.have = 0; /* no output data available */
78 if (state.state->mode == GZ_READ) { /* for reading ... */
79 state.state->eof = 0; /* not at end of file */
80 state.state->past = 0; /* have not read past end yet */
81 state.state->how = LOOK; /* look for gzip header */
83 state.state->seek = 0; /* no seek request pending */
84 gz_error(state, Z_OK, NULL); /* clear error */
85 state.state->x.pos = 0; /* no uncompressed data yet */
86 state.state->strm.avail_in = 0; /* no input data yet */
89 /* Open a gzip file either by name or file descriptor. */
90 local gzFile gz_open(const void *path, int fd, const char *mode) {
105 /* allocate gzFile structure to return */
106 state.state = (gz_state*)malloc(sizeof(gz_state));
107 if (state.state == NULL)
109 state.state->size = 0; /* no buffers allocated yet */
110 state.state->want = GZBUFSIZE; /* requested buffer size */
111 state.state->msg = NULL; /* no error message yet */
114 state.state->mode = GZ_NONE;
115 state.state->level = Z_DEFAULT_COMPRESSION;
116 state.state->strategy = Z_DEFAULT_STRATEGY;
117 state.state->direct = 0;
119 if (*mode >= '0' && *mode <= '9')
120 state.state->level = *mode - '0';
124 state.state->mode = GZ_READ;
126 #ifndef NO_GZCOMPRESS
128 state.state->mode = GZ_WRITE;
131 state.state->mode = GZ_APPEND;
134 case '+': /* can't read and write at the same time */
137 case 'b': /* ignore -- will request binary anyway */
150 state.state->strategy = Z_FILTERED;
153 state.state->strategy = Z_HUFFMAN_ONLY;
156 state.state->strategy = Z_RLE;
159 state.state->strategy = Z_FIXED;
162 state.state->direct = 1;
164 default: /* could consider as an error, but just ignore */
170 /* must provide an "r", "w", or "a" */
171 if (state.state->mode == GZ_NONE) {
176 /* can't force transparent read */
177 if (state.state->mode == GZ_READ) {
178 if (state.state->direct) {
182 state.state->direct = 1; /* for empty file */
185 /* save the path name for error messages */
188 len = wcstombs(NULL, path, 0);
189 if (len == (z_size_t)-1)
194 len = strlen((const char *)path);
195 state.state->path = (char *)malloc(len + 1);
196 if (state.state->path == NULL) {
203 wcstombs(state.state->path, path, len + 1);
205 *(state.state->path) = 0;
208 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
209 (void)snprintf(state.state->path, len + 1, "%s", (const char *)path);
211 strcpy(state.state->path, (const char*)path);
214 /* compute the flags for open() */
223 (cloexec ? O_CLOEXEC : 0) |
225 (state.state->mode == GZ_READ ?
227 (O_WRONLY | O_CREAT |
229 (exclusive ? O_EXCL : 0) |
231 (state.state->mode == GZ_WRITE ?
235 /* open the file with the appropriate flags (or just use fd) */
236 state.state->fd = fd > -1 ? fd : (
238 fd == -2 ? _wopen(path, oflag, 0666) :
240 open((const char *)path, oflag, 0666));
241 if (state.state->fd == -1) {
242 free(state.state->path);
246 if (state.state->mode == GZ_APPEND) {
247 LSEEK(state.state->fd, 0, SEEK_END); /* so gzoffset() is correct */
248 state.state->mode = GZ_WRITE; /* simplify later checks */
251 /* save the current position for rewinding (only if reading) */
252 if (state.state->mode == GZ_READ) {
253 state.state->start = LSEEK(state.state->fd, 0, SEEK_CUR);
254 if (state.state->start == -1) state.state->start = 0;
257 /* initialize stream */
264 /* -- see zlib.h -- */
265 gzFile ZEXPORT gzopen(const char *path, const char *mode) {
266 return gz_open(path, -1, mode);
269 /* -- see zlib.h -- */
270 gzFile ZEXPORT gzopen64(const char *path, const char *mode) {
271 return gz_open(path, -1, mode);
274 /* -- see zlib.h -- */
275 gzFile ZEXPORT gzdopen(int fd, const char *mode) {
276 char *path; /* identifier for error messages */
279 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
281 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
282 (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd);
284 sprintf(path, "<fd:%d>", fd); /* for debugging */
286 gz = gz_open(path, fd, mode);
291 /* -- see zlib.h -- */
293 gzFile ZEXPORT gzopen_w(const wchar_t *path, const char *mode) {
294 return gz_open(path, -2, mode);
298 /* -- see zlib.h -- */
299 int ZEXPORT gzbuffer(gzFile file, unsigned size) {
302 /* get internal structure and check integrity */
306 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
309 /* make sure we haven't already allocated memory */
310 if (state.state->size != 0)
313 /* check and set requested size */
314 if ((size << 1) < size)
315 return -1; /* need to be able to double it */
317 size = 2; /* need two bytes to check magic header */
318 state.state->want = size;
322 /* -- see zlib.h -- */
323 int ZEXPORT gzrewind(gzFile file) {
326 /* get internal structure */
331 /* check that we're reading and that there's no error */
332 if (state.state->mode != GZ_READ ||
333 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
336 /* back up and start over */
337 if (LSEEK(state.state->fd, state.state->start, SEEK_SET) == -1)
343 /* -- see zlib.h -- */
344 z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence) {
349 /* get internal structure and check integrity */
353 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
356 /* check that there's no error */
357 if (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
360 /* can only seek from start or relative to current position */
361 if (whence != SEEK_SET && whence != SEEK_CUR)
364 /* normalize offset to a SEEK_CUR specification */
365 if (whence == SEEK_SET)
366 offset -= state.state->x.pos;
367 else if (state.state->seek)
368 offset += state.state->skip;
369 state.state->seek = 0;
371 /* if within raw area while reading, just go there */
372 if (state.state->mode == GZ_READ && state.state->how == COPY &&
373 state.state->x.pos + offset >= 0) {
374 ret = LSEEK(state.state->fd, offset - state.state->x.have, SEEK_CUR);
377 state.state->x.have = 0;
378 state.state->eof = 0;
379 state.state->past = 0;
380 state.state->seek = 0;
381 gz_error(state, Z_OK, NULL);
382 state.state->strm.avail_in = 0;
383 state.state->x.pos += offset;
384 return state.state->x.pos;
387 /* calculate skip amount, rewinding if needed for back seek when reading */
389 if (state.state->mode != GZ_READ) /* writing -- can't go backwards */
391 offset += state.state->x.pos;
392 if (offset < 0) /* before start of file! */
394 if (gzrewind(file) == -1) /* rewind, then skip to offset */
398 /* if reading, skip what's in output buffer (one less gzgetc() check) */
399 if (state.state->mode == GZ_READ) {
400 n = GT_OFF(state.state->x.have) || (z_off64_t)state.state->x.have > offset ?
401 (unsigned)offset : state.state->x.have;
402 state.state->x.have -= n;
403 state.state->x.next += n;
404 state.state->x.pos += n;
408 /* request skip (if not zero) */
410 state.state->seek = 1;
411 state.state->skip = offset;
413 return state.state->x.pos + offset;
416 /* -- see zlib.h -- */
417 z_off_t ZEXPORT gzseek(gzFile file, z_off_t offset, int whence) {
420 ret = gzseek64(file, (z_off64_t)offset, whence);
421 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
424 /* -- see zlib.h -- */
425 z_off64_t ZEXPORT gztell64(gzFile file) {
428 /* get internal structure and check integrity */
432 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
435 /* return position */
436 return state.state->x.pos + (state.state->seek ? state.state->skip : 0);
439 /* -- see zlib.h -- */
440 z_off_t ZEXPORT gztell(gzFile file) {
443 ret = gztell64(file);
444 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
447 /* -- see zlib.h -- */
448 z_off64_t ZEXPORT gzoffset64(gzFile file) {
452 /* get internal structure and check integrity */
456 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
459 /* compute and return effective offset in file */
460 offset = LSEEK(state.state->fd, 0, SEEK_CUR);
463 if (state.state->mode == GZ_READ) /* reading */
464 offset -= state.state->strm.avail_in; /* don't count buffered input */
468 /* -- see zlib.h -- */
469 z_off_t ZEXPORT gzoffset(gzFile file) {
472 ret = gzoffset64(file);
473 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
476 /* -- see zlib.h -- */
477 int ZEXPORT gzeof(gzFile file) {
480 /* get internal structure and check integrity */
484 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
487 /* return end-of-file state */
488 return state.state->mode == GZ_READ ? state.state->past : 0;
491 /* -- see zlib.h -- */
492 const char * ZEXPORT gzerror(gzFile file, int *errnum) {
495 /* get internal structure and check integrity */
499 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
502 /* return error information */
504 *errnum = state.state->err;
505 return state.state->err == Z_MEM_ERROR ? "out of memory" :
506 (state.state->msg == NULL ? "" : state.state->msg);
509 /* -- see zlib.h -- */
510 void ZEXPORT gzclearerr(gzFile file) {
513 /* get internal structure and check integrity */
517 if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
520 /* clear error and end-of-file */
521 if (state.state->mode == GZ_READ) {
522 state.state->eof = 0;
523 state.state->past = 0;
525 gz_error(state, Z_OK, NULL);
528 /* Create an error message in allocated memory and set state.state->err and
529 state.state->msg accordingly. Free any previous error message already there. Do
530 not try to free or allocate space if the error is Z_MEM_ERROR (out of
531 memory). Simply save the error message as a static string. If there is an
532 allocation failure constructing the error message, then convert the error to
534 void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg) {
535 /* free previously allocated message and clear */
536 if (state.state->msg != NULL) {
537 if (state.state->err != Z_MEM_ERROR)
538 free(state.state->msg);
539 state.state->msg = NULL;
542 /* if fatal, set state.state->x.have to 0 so that the gzgetc() macro fails */
543 if (err != Z_OK && err != Z_BUF_ERROR)
544 state.state->x.have = 0;
546 /* set error code, and if no message, then done */
547 state.state->err = err;
551 /* for an out of memory error, return literal string when requested */
552 if (err == Z_MEM_ERROR)
555 /* construct error message with path */
556 if ((state.state->msg = (char *)malloc(strlen(state.state->path) + strlen(msg) + 3)) ==
558 state.state->err = Z_MEM_ERROR;
561 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
562 (void)snprintf(state.state->msg, strlen(state.state->path) + strlen(msg) + 3,
563 "%s%s%s", state.state->path, ": ", msg);
565 strcpy(state.state->msg, state.state->path);
566 strcat(state.state->msg, ": ");
567 strcat(state.state->msg, msg);
572 /* portably return maximum value for an int (when limits.h presumed not
573 available) -- we need to do this to cover cases where 2's complement not
574 used, since C standard permits 1's complement and sign-bit representations,
575 otherwise we could just use ((unsigned)-1) >> 1 */
576 unsigned ZLIB_INTERNAL gz_intmax() {