1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004-2019 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
12 #if defined(_WIN32) && !defined(__BORLANDC__)
13 # define LSEEK _lseeki64
15 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
16 # define LSEEK lseek64
23 local void gz_reset OF((gz_statep));
24 local gzFile gz_open OF((const void *, int, const char *));
28 /* Map the Windows error number in ERROR to a locale-dependent error message
29 string and return a pointer to it. Typically, the values for ERROR come
32 The string pointed to shall not be modified by the application, but may be
33 overwritten by a subsequent call to gz_strwinerror
35 The gz_strwinerror function does not change the current setting of
37 char ZLIB_INTERNAL *gz_strwinerror (error)
40 static char buf[1024];
43 DWORD lasterr = GetLastError();
44 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
45 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
48 0, /* Default language */
53 /* If there is an \r\n appended, zap it. */
55 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
60 if (chars > sizeof (buf) - 1) {
61 chars = sizeof (buf) - 1;
65 wcstombs(buf, msgbuf, chars + 1);
69 sprintf(buf, "unknown win32 error (%ld)", error);
72 SetLastError(lasterr);
78 /* Reset gzip file state */
79 local void gz_reset(state)
82 state->x.have = 0; /* no output data available */
83 if (state->mode == GZ_READ) { /* for reading ... */
84 state->eof = 0; /* not at end of file */
85 state->past = 0; /* have not read past end yet */
86 state->how = LOOK; /* look for gzip header */
88 else /* for writing ... */
89 state->reset = 0; /* no deflateReset pending */
90 state->seek = 0; /* no seek request pending */
91 gz_error(state, Z_OK, NULL); /* clear error */
92 state->x.pos = 0; /* no uncompressed data yet */
93 state->strm.avail_in = 0; /* no input data yet */
96 /* Open a gzip file either by name or file descriptor. */
97 local gzFile gz_open(path, fd, mode)
116 /* allocate gzFile structure to return */
117 state = (gz_statep)malloc(sizeof(gz_state));
120 state->size = 0; /* no buffers allocated yet */
121 state->want = GZBUFSIZE; /* requested buffer size */
122 state->msg = NULL; /* no error message yet */
125 state->mode = GZ_NONE;
126 state->level = Z_DEFAULT_COMPRESSION;
127 state->strategy = Z_DEFAULT_STRATEGY;
130 if (*mode >= '0' && *mode <= '9')
131 state->level = *mode - '0';
135 state->mode = GZ_READ;
137 #ifndef NO_GZCOMPRESS
139 state->mode = GZ_WRITE;
142 state->mode = GZ_APPEND;
145 case '+': /* can't read and write at the same time */
148 case 'b': /* ignore -- will request binary anyway */
161 state->strategy = Z_FILTERED;
164 state->strategy = Z_HUFFMAN_ONLY;
167 state->strategy = Z_RLE;
170 state->strategy = Z_FIXED;
175 default: /* could consider as an error, but just ignore */
181 /* must provide an "r", "w", or "a" */
182 if (state->mode == GZ_NONE) {
187 /* can't force transparent read */
188 if (state->mode == GZ_READ) {
193 state->direct = 1; /* for empty file */
196 /* save the path name for error messages */
199 len = wcstombs(NULL, path, 0);
200 if (len == (z_size_t)-1)
205 len = strlen((const char *)path);
206 state->path = (char *)malloc(len + 1);
207 if (state->path == NULL) {
214 wcstombs(state->path, path, len + 1);
219 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
220 (void)snprintf(state->path, len + 1, "%s", (const char *)path);
222 strcpy(state->path, path);
225 /* compute the flags for open() */
234 (cloexec ? O_CLOEXEC : 0) |
236 (state->mode == GZ_READ ?
238 (O_WRONLY | O_CREAT |
240 (exclusive ? O_EXCL : 0) |
242 (state->mode == GZ_WRITE ?
246 /* open the file with the appropriate flags (or just use fd) */
247 state->fd = fd > -1 ? fd : (
249 fd == -2 ? _wopen(path, oflag, 0666) :
251 open((const char *)path, oflag, 0666));
252 if (state->fd == -1) {
257 if (state->mode == GZ_APPEND) {
258 LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */
259 state->mode = GZ_WRITE; /* simplify later checks */
262 /* save the current position for rewinding (only if reading) */
263 if (state->mode == GZ_READ) {
264 state->start = LSEEK(state->fd, 0, SEEK_CUR);
265 if (state->start == -1) state->start = 0;
268 /* initialize stream */
272 return (gzFile)state;
275 /* -- see zlib.h -- */
276 gzFile ZEXPORT gzopen(path, mode)
280 return gz_open(path, -1, mode);
283 /* -- see zlib.h -- */
284 gzFile ZEXPORT gzopen64(path, mode)
288 return gz_open(path, -1, mode);
291 /* -- see zlib.h -- */
292 gzFile ZEXPORT gzdopen(fd, mode)
296 char *path; /* identifier for error messages */
299 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
301 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
302 (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd);
304 sprintf(path, "<fd:%d>", fd); /* for debugging */
306 gz = gz_open(path, fd, mode);
311 /* -- see zlib.h -- */
313 gzFile ZEXPORT gzopen_w(path, mode)
317 return gz_open(path, -2, mode);
321 /* -- see zlib.h -- */
322 int ZEXPORT gzbuffer(file, size)
328 /* get internal structure and check integrity */
331 state = (gz_statep)file;
332 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
335 /* make sure we haven't already allocated memory */
336 if (state->size != 0)
339 /* check and set requested size */
340 if ((size << 1) < size)
341 return -1; /* need to be able to double it */
343 size = 2; /* need two bytes to check magic header */
348 /* -- see zlib.h -- */
349 int ZEXPORT gzrewind(file)
354 /* get internal structure */
357 state = (gz_statep)file;
359 /* check that we're reading and that there's no error */
360 if (state->mode != GZ_READ ||
361 (state->err != Z_OK && state->err != Z_BUF_ERROR))
364 /* back up and start over */
365 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
371 /* -- see zlib.h -- */
372 z_off64_t ZEXPORT gzseek64(file, offset, whence)
381 /* get internal structure and check integrity */
384 state = (gz_statep)file;
385 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
388 /* check that there's no error */
389 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
392 /* can only seek from start or relative to current position */
393 if (whence != SEEK_SET && whence != SEEK_CUR)
396 /* normalize offset to a SEEK_CUR specification */
397 if (whence == SEEK_SET)
398 offset -= state->x.pos;
399 else if (state->seek)
400 offset += state->skip;
403 /* if within raw area while reading, just go there */
404 if (state->mode == GZ_READ && state->how == COPY &&
405 state->x.pos + offset >= 0) {
406 ret = LSEEK(state->fd, offset - (z_off64_t)state->x.have, SEEK_CUR);
413 gz_error(state, Z_OK, NULL);
414 state->strm.avail_in = 0;
415 state->x.pos += offset;
419 /* calculate skip amount, rewinding if needed for back seek when reading */
421 if (state->mode != GZ_READ) /* writing -- can't go backwards */
423 offset += state->x.pos;
424 if (offset < 0) /* before start of file! */
426 if (gzrewind(file) == -1) /* rewind, then skip to offset */
430 /* if reading, skip what's in output buffer (one less gzgetc() check) */
431 if (state->mode == GZ_READ) {
432 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
433 (unsigned)offset : state->x.have;
440 /* request skip (if not zero) */
443 state->skip = offset;
445 return state->x.pos + offset;
448 /* -- see zlib.h -- */
449 z_off_t ZEXPORT gzseek(file, offset, whence)
456 ret = gzseek64(file, (z_off64_t)offset, whence);
457 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
460 /* -- see zlib.h -- */
461 z_off64_t ZEXPORT gztell64(file)
466 /* get internal structure and check integrity */
469 state = (gz_statep)file;
470 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
473 /* return position */
474 return state->x.pos + (state->seek ? state->skip : 0);
477 /* -- see zlib.h -- */
478 z_off_t ZEXPORT gztell(file)
483 ret = gztell64(file);
484 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
487 /* -- see zlib.h -- */
488 z_off64_t ZEXPORT gzoffset64(file)
494 /* get internal structure and check integrity */
497 state = (gz_statep)file;
498 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
501 /* compute and return effective offset in file */
502 offset = LSEEK(state->fd, 0, SEEK_CUR);
505 if (state->mode == GZ_READ) /* reading */
506 offset -= state->strm.avail_in; /* don't count buffered input */
510 /* -- see zlib.h -- */
511 z_off_t ZEXPORT gzoffset(file)
516 ret = gzoffset64(file);
517 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
520 /* -- see zlib.h -- */
521 int ZEXPORT gzeof(file)
526 /* get internal structure and check integrity */
529 state = (gz_statep)file;
530 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
533 /* return end-of-file state */
534 return state->mode == GZ_READ ? state->past : 0;
537 /* -- see zlib.h -- */
538 const char * ZEXPORT gzerror(file, errnum)
544 /* get internal structure and check integrity */
547 state = (gz_statep)file;
548 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
551 /* return error information */
553 *errnum = state->err;
554 return state->err == Z_MEM_ERROR ? "out of memory" :
555 (state->msg == NULL ? "" : state->msg);
558 /* -- see zlib.h -- */
559 void ZEXPORT gzclearerr(file)
564 /* get internal structure and check integrity */
567 state = (gz_statep)file;
568 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
571 /* clear error and end-of-file */
572 if (state->mode == GZ_READ) {
576 gz_error(state, Z_OK, NULL);
579 /* Create an error message in allocated memory and set state->err and
580 state->msg accordingly. Free any previous error message already there. Do
581 not try to free or allocate space if the error is Z_MEM_ERROR (out of
582 memory). Simply save the error message as a static string. If there is an
583 allocation failure constructing the error message, then convert the error to
585 void ZLIB_INTERNAL gz_error(state, err, msg)
590 /* free previously allocated message and clear */
591 if (state->msg != NULL) {
592 if (state->err != Z_MEM_ERROR)
597 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
598 if (err != Z_OK && err != Z_BUF_ERROR)
601 /* set error code, and if no message, then done */
606 /* for an out of memory error, return literal string when requested */
607 if (err == Z_MEM_ERROR)
610 /* construct error message with path */
611 if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
613 state->err = Z_MEM_ERROR;
616 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
617 (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
618 "%s%s%s", state->path, ": ", msg);
620 strcpy(state->msg, state->path);
621 strcat(state->msg, ": ");
622 strcat(state->msg, msg);
627 /* portably return maximum value for an int (when limits.h presumed not
628 available) -- we need to do this to cover cases where 2's complement not
629 used, since C standard permits 1's complement and sign-bit representations,
630 otherwise we could just use ((unsigned)-1) >> 1 */
631 unsigned ZLIB_INTERNAL gz_intmax()