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
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 OF((gz_statep));
23 local gzFile gz_open 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->x.have = 0; /* no output data available */
82 if (state->mode == GZ_READ) { /* for reading ... */
83 state->eof = 0; /* not at end of file */
84 state->past = 0; /* have not read past end yet */
85 state->how = LOOK; /* look for gzip header */
87 state->seek = 0; /* no seek request pending */
88 gz_error(state, Z_OK, NULL); /* clear error */
89 state->x.pos = 0; /* no uncompressed data yet */
90 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 = (gz_statep)malloc(sizeof(gz_state));
117 state->size = 0; /* no buffers allocated yet */
118 state->want = GZBUFSIZE; /* requested buffer size */
119 state->msg = NULL; /* no error message yet */
122 state->mode = GZ_NONE;
123 state->level = Z_DEFAULT_COMPRESSION;
124 state->strategy = Z_DEFAULT_STRATEGY;
127 if (*mode >= '0' && *mode <= '9')
128 state->level = *mode - '0';
132 state->mode = GZ_READ;
134 #ifndef NO_GZCOMPRESS
136 state->mode = GZ_WRITE;
139 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->strategy = Z_FILTERED;
161 state->strategy = Z_HUFFMAN_ONLY;
164 state->strategy = Z_RLE;
167 state->strategy = Z_FIXED;
172 default: /* could consider as an error, but just ignore */
178 /* must provide an "r", "w", or "a" */
179 if (state->mode == GZ_NONE) {
184 /* can't force transparent read */
185 if (state->mode == GZ_READ) {
190 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->path = (char *)malloc(len + 1);
204 if (state->path == NULL) {
211 wcstombs(state->path, path, len + 1);
216 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
217 (void)snprintf(state->path, len + 1, "%s", (const char *)path);
219 strcpy(state->path, path);
222 /* compute the flags for open() */
231 (cloexec ? O_CLOEXEC : 0) |
233 (state->mode == GZ_READ ?
235 (O_WRONLY | O_CREAT |
237 (exclusive ? O_EXCL : 0) |
239 (state->mode == GZ_WRITE ?
243 /* open the file with the appropriate flags (or just use fd) */
244 state->fd = fd > -1 ? fd : (
246 fd == -2 ? _wopen(path, oflag, 0666) :
248 open((const char *)path, oflag, 0666));
249 if (state->fd == -1) {
254 if (state->mode == GZ_APPEND) {
255 LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */
256 state->mode = GZ_WRITE; /* simplify later checks */
259 /* save the current position for rewinding (only if reading) */
260 if (state->mode == GZ_READ) {
261 state->start = LSEEK(state->fd, 0, SEEK_CUR);
262 if (state->start == -1) state->start = 0;
265 /* initialize stream */
269 return (gzFile)state;
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 */
328 state = (gz_statep)file;
329 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
332 /* make sure we haven't already allocated memory */
333 if (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 */
345 /* -- see zlib.h -- */
346 int ZEXPORT gzrewind(file)
351 /* get internal structure */
354 state = (gz_statep)file;
356 /* check that we're reading and that there's no error */
357 if (state->mode != GZ_READ ||
358 (state->err != Z_OK && state->err != Z_BUF_ERROR))
361 /* back up and start over */
362 if (LSEEK(state->fd, 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 */
381 state = (gz_statep)file;
382 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
385 /* check that there's no error */
386 if (state->err != Z_OK && 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->x.pos;
396 else if (state->seek)
397 offset += state->skip;
400 /* if within raw area while reading, just go there */
401 if (state->mode == GZ_READ && state->how == COPY &&
402 state->x.pos + offset >= 0) {
403 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
410 gz_error(state, Z_OK, NULL);
411 state->strm.avail_in = 0;
412 state->x.pos += offset;
416 /* calculate skip amount, rewinding if needed for back seek when reading */
418 if (state->mode != GZ_READ) /* writing -- can't go backwards */
420 offset += 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->mode == GZ_READ) {
429 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
430 (unsigned)offset : state->x.have;
437 /* request skip (if not zero) */
440 state->skip = offset;
442 return 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 */
466 state = (gz_statep)file;
467 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
470 /* return position */
471 return state->x.pos + (state->seek ? 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 */
494 state = (gz_statep)file;
495 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
498 /* compute and return effective offset in file */
499 offset = LSEEK(state->fd, 0, SEEK_CUR);
502 if (state->mode == GZ_READ) /* reading */
503 offset -= 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 */
526 state = (gz_statep)file;
527 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
530 /* return end-of-file state */
531 return state->mode == GZ_READ ? state->past : 0;
534 /* -- see zlib.h -- */
535 const char * ZEXPORT gzerror(file, errnum)
541 /* get internal structure and check integrity */
544 state = (gz_statep)file;
545 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
548 /* return error information */
550 *errnum = state->err;
551 return state->err == Z_MEM_ERROR ? "out of memory" :
552 (state->msg == NULL ? "" : state->msg);
555 /* -- see zlib.h -- */
556 void ZEXPORT gzclearerr(file)
561 /* get internal structure and check integrity */
564 state = (gz_statep)file;
565 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
568 /* clear error and end-of-file */
569 if (state->mode == GZ_READ) {
573 gz_error(state, Z_OK, NULL);
576 /* Create an error message in allocated memory and set state->err and
577 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->msg != NULL) {
589 if (state->err != Z_MEM_ERROR)
594 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
595 if (err != Z_OK && err != Z_BUF_ERROR)
598 /* set error code, and if no message, then done */
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->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
610 state->err = Z_MEM_ERROR;
613 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
614 (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
615 "%s%s%s", state->path, ": ", msg);
617 strcpy(state->msg, state->path);
618 strcat(state->msg, ": ");
619 strcat(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()