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
10 #if defined(_WIN32) && !defined(__BORLANDC__)
11 # define LSEEK _lseeki64
13 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
14 # define LSEEK lseek64
22 /* Map the Windows error number in ERROR to a locale-dependent error message
23 string and return a pointer to it. Typically, the values for ERROR come
26 The string pointed to shall not be modified by the application, but may be
27 overwritten by a subsequent call to gz_strwinerror
29 The gz_strwinerror function does not change the current setting of
31 char ZLIB_INTERNAL *gz_strwinerror(DWORD error) {
32 static char buf[1024];
35 DWORD lasterr = GetLastError();
36 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
37 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
40 0, /* Default language */
45 /* If there is an \r\n appended, zap it. */
47 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
52 if (chars > sizeof (buf) - 1) {
53 chars = sizeof (buf) - 1;
57 wcstombs(buf, msgbuf, chars + 1);
61 sprintf(buf, "unknown win32 error (%ld)", error);
64 SetLastError(lasterr);
70 /* Reset gzip file state */
71 local void gz_reset(gz_statep state) {
72 state->x.have = 0; /* no output data available */
73 if (state->mode == GZ_READ) { /* for reading ... */
74 state->eof = 0; /* not at end of file */
75 state->past = 0; /* have not read past end yet */
76 state->how = LOOK; /* look for gzip header */
78 else /* for writing ... */
79 state->reset = 0; /* no deflateReset pending */
80 state->seek = 0; /* no seek request pending */
81 gz_error(state, Z_OK, NULL); /* clear error */
82 state->x.pos = 0; /* no uncompressed data yet */
83 state->strm.avail_in = 0; /* no input data yet */
86 /* Open a gzip file either by name or file descriptor. */
87 local gzFile gz_open(const void *path, int fd, const char *mode) {
102 /* allocate gzFile structure to return */
103 state = (gz_statep)malloc(sizeof(gz_state));
106 state->size = 0; /* no buffers allocated yet */
107 state->want = GZBUFSIZE; /* requested buffer size */
108 state->msg = NULL; /* no error message yet */
111 state->mode = GZ_NONE;
112 state->level = Z_DEFAULT_COMPRESSION;
113 state->strategy = Z_DEFAULT_STRATEGY;
116 if (*mode >= '0' && *mode <= '9')
117 state->level = *mode - '0';
121 state->mode = GZ_READ;
123 #ifndef NO_GZCOMPRESS
125 state->mode = GZ_WRITE;
128 state->mode = GZ_APPEND;
131 case '+': /* can't read and write at the same time */
134 case 'b': /* ignore -- will request binary anyway */
147 state->strategy = Z_FILTERED;
150 state->strategy = Z_HUFFMAN_ONLY;
153 state->strategy = Z_RLE;
156 state->strategy = Z_FIXED;
161 default: /* could consider as an error, but just ignore */
167 /* must provide an "r", "w", or "a" */
168 if (state->mode == GZ_NONE) {
173 /* can't force transparent read */
174 if (state->mode == GZ_READ) {
179 state->direct = 1; /* for empty file */
182 /* save the path name for error messages */
185 len = wcstombs(NULL, path, 0);
186 if (len == (z_size_t)-1)
191 len = strlen((const char *)path);
192 state->path = (char *)malloc(len + 1);
193 if (state->path == NULL) {
200 wcstombs(state->path, path, len + 1);
205 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
206 (void)snprintf(state->path, len + 1, "%s", (const char *)path);
208 strcpy(state->path, path);
211 /* compute the flags for open() */
220 (cloexec ? O_CLOEXEC : 0) |
222 (state->mode == GZ_READ ?
224 (O_WRONLY | O_CREAT |
226 (exclusive ? O_EXCL : 0) |
228 (state->mode == GZ_WRITE ?
232 /* open the file with the appropriate flags (or just use fd) */
233 state->fd = fd > -1 ? fd : (
235 fd == -2 ? _wopen(path, oflag, 0666) :
237 open((const char *)path, oflag, 0666));
238 if (state->fd == -1) {
243 if (state->mode == GZ_APPEND) {
244 LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */
245 state->mode = GZ_WRITE; /* simplify later checks */
248 /* save the current position for rewinding (only if reading) */
249 if (state->mode == GZ_READ) {
250 state->start = LSEEK(state->fd, 0, SEEK_CUR);
251 if (state->start == -1) state->start = 0;
254 /* initialize stream */
258 return (gzFile)state;
261 /* -- see zlib.h -- */
262 gzFile ZEXPORT gzopen(const char *path, const char *mode) {
263 return gz_open(path, -1, mode);
266 /* -- see zlib.h -- */
267 gzFile ZEXPORT gzopen64(const char *path, const char *mode) {
268 return gz_open(path, -1, mode);
271 /* -- see zlib.h -- */
272 gzFile ZEXPORT gzdopen(int fd, const char *mode) {
273 char *path; /* identifier for error messages */
276 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
278 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
279 (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd);
281 sprintf(path, "<fd:%d>", fd); /* for debugging */
283 gz = gz_open(path, fd, mode);
288 /* -- see zlib.h -- */
290 gzFile ZEXPORT gzopen_w(const wchar_t *path, const char *mode) {
291 return gz_open(path, -2, mode);
295 /* -- see zlib.h -- */
296 int ZEXPORT gzbuffer(gzFile file, unsigned size) {
299 /* get internal structure and check integrity */
302 state = (gz_statep)file;
303 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
306 /* make sure we haven't already allocated memory */
307 if (state->size != 0)
310 /* check and set requested size */
311 if ((size << 1) < size)
312 return -1; /* need to be able to double it */
314 size = 8; /* needed to behave well with flushing */
319 /* -- see zlib.h -- */
320 int ZEXPORT gzrewind(gzFile file) {
323 /* get internal structure */
326 state = (gz_statep)file;
328 /* check that we're reading and that there's no error */
329 if (state->mode != GZ_READ ||
330 (state->err != Z_OK && state->err != Z_BUF_ERROR))
333 /* back up and start over */
334 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
340 /* -- see zlib.h -- */
341 z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence) {
346 /* get internal structure and check integrity */
349 state = (gz_statep)file;
350 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
353 /* check that there's no error */
354 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
357 /* can only seek from start or relative to current position */
358 if (whence != SEEK_SET && whence != SEEK_CUR)
361 /* normalize offset to a SEEK_CUR specification */
362 if (whence == SEEK_SET)
363 offset -= state->x.pos;
364 else if (state->seek)
365 offset += state->skip;
368 /* if within raw area while reading, just go there */
369 if (state->mode == GZ_READ && state->how == COPY &&
370 state->x.pos + offset >= 0) {
371 ret = LSEEK(state->fd, offset - (z_off64_t)state->x.have, SEEK_CUR);
378 gz_error(state, Z_OK, NULL);
379 state->strm.avail_in = 0;
380 state->x.pos += offset;
384 /* calculate skip amount, rewinding if needed for back seek when reading */
386 if (state->mode != GZ_READ) /* writing -- can't go backwards */
388 offset += state->x.pos;
389 if (offset < 0) /* before start of file! */
391 if (gzrewind(file) == -1) /* rewind, then skip to offset */
395 /* if reading, skip what's in output buffer (one less gzgetc() check) */
396 if (state->mode == GZ_READ) {
397 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
398 (unsigned)offset : state->x.have;
405 /* request skip (if not zero) */
408 state->skip = offset;
410 return state->x.pos + offset;
413 /* -- see zlib.h -- */
414 z_off_t ZEXPORT gzseek(gzFile file, z_off_t offset, int whence) {
417 ret = gzseek64(file, (z_off64_t)offset, whence);
418 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
421 /* -- see zlib.h -- */
422 z_off64_t ZEXPORT gztell64(gzFile file) {
425 /* get internal structure and check integrity */
428 state = (gz_statep)file;
429 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
432 /* return position */
433 return state->x.pos + (state->seek ? state->skip : 0);
436 /* -- see zlib.h -- */
437 z_off_t ZEXPORT gztell(gzFile file) {
440 ret = gztell64(file);
441 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
444 /* -- see zlib.h -- */
445 z_off64_t ZEXPORT gzoffset64(gzFile file) {
449 /* get internal structure and check integrity */
452 state = (gz_statep)file;
453 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
456 /* compute and return effective offset in file */
457 offset = LSEEK(state->fd, 0, SEEK_CUR);
460 if (state->mode == GZ_READ) /* reading */
461 offset -= state->strm.avail_in; /* don't count buffered input */
465 /* -- see zlib.h -- */
466 z_off_t ZEXPORT gzoffset(gzFile file) {
469 ret = gzoffset64(file);
470 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
473 /* -- see zlib.h -- */
474 int ZEXPORT gzeof(gzFile file) {
477 /* get internal structure and check integrity */
480 state = (gz_statep)file;
481 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
484 /* return end-of-file state */
485 return state->mode == GZ_READ ? state->past : 0;
488 /* -- see zlib.h -- */
489 const char * ZEXPORT gzerror(gzFile file, int *errnum) {
492 /* get internal structure and check integrity */
495 state = (gz_statep)file;
496 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
499 /* return error information */
501 *errnum = state->err;
502 return state->err == Z_MEM_ERROR ? "out of memory" :
503 (state->msg == NULL ? "" : state->msg);
506 /* -- see zlib.h -- */
507 void ZEXPORT gzclearerr(gzFile file) {
510 /* get internal structure and check integrity */
513 state = (gz_statep)file;
514 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
517 /* clear error and end-of-file */
518 if (state->mode == GZ_READ) {
522 gz_error(state, Z_OK, NULL);
525 /* Create an error message in allocated memory and set state->err and
526 state->msg accordingly. Free any previous error message already there. Do
527 not try to free or allocate space if the error is Z_MEM_ERROR (out of
528 memory). Simply save the error message as a static string. If there is an
529 allocation failure constructing the error message, then convert the error to
531 void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg) {
532 /* free previously allocated message and clear */
533 if (state->msg != NULL) {
534 if (state->err != Z_MEM_ERROR)
539 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
540 if (err != Z_OK && err != Z_BUF_ERROR)
543 /* set error code, and if no message, then done */
548 /* for an out of memory error, return literal string when requested */
549 if (err == Z_MEM_ERROR)
552 /* construct error message with path */
553 if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
555 state->err = Z_MEM_ERROR;
558 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
559 (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
560 "%s%s%s", state->path, ": ", msg);
562 strcpy(state->msg, state->path);
563 strcat(state->msg, ": ");
564 strcat(state->msg, msg);
569 /* portably return maximum value for an int (when limits.h presumed not
570 available) -- we need to do this to cover cases where 2's complement not
571 used, since C standard permits 1's complement and sign-bit representations,
572 otherwise we could just use ((unsigned)-1) >> 1 */
573 unsigned ZLIB_INTERNAL gz_intmax(void) {