1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004, 2010, 2011, 2012, 2013 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
8 #if defined(_WIN32) && !defined(__BORLANDC__)
9 # define LSEEK _lseeki64
11 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
12 # define LSEEK lseek64
19 local void gz_reset OF((gz_statep));
20 local gzFile gz_open OF((const void *, int, const char *));
24 /* Map the Windows error number in ERROR to a locale-dependent error message
25 string and return a pointer to it. Typically, the values for ERROR come
28 The string pointed to shall not be modified by the application, but may be
29 overwritten by a subsequent call to gz_strwinerror
31 The gz_strwinerror function does not change the current setting of
33 char ZLIB_INTERNAL *gz_strwinerror (error)
36 static char buf[1024];
39 DWORD lasterr = GetLastError();
40 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
41 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
44 0, /* Default language */
49 /* If there is an \r\n appended, zap it. */
51 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
56 if (chars > sizeof (buf) - 1) {
57 chars = sizeof (buf) - 1;
61 wcstombs(buf, msgbuf, chars + 1);
65 sprintf(buf, "unknown win32 error (%ld)", error);
68 SetLastError(lasterr);
74 /* Reset gzip file state */
75 local void gz_reset(state)
78 state->x.have = 0; /* no output data available */
79 if (state->mode == GZ_READ) { /* for reading ... */
80 state->eof = 0; /* not at end of file */
81 state->past = 0; /* have not read past end yet */
82 state->how = LOOK; /* look for gzip header */
84 state->seek = 0; /* no seek request pending */
85 gz_error(state, Z_OK, NULL); /* clear error */
86 state->x.pos = 0; /* no uncompressed data yet */
87 state->strm.avail_in = 0; /* no input data yet */
90 /* Open a gzip file either by name or file descriptor. */
91 local gzFile gz_open(path, fd, mode)
110 /* allocate gzFile structure to return */
111 state = (gz_statep)malloc(sizeof(gz_state));
114 state->size = 0; /* no buffers allocated yet */
115 state->want = GZBUFSIZE; /* requested buffer size */
116 state->msg = NULL; /* no error message yet */
119 state->mode = GZ_NONE;
120 state->level = Z_DEFAULT_COMPRESSION;
121 state->strategy = Z_DEFAULT_STRATEGY;
124 if (*mode >= '0' && *mode <= '9')
125 state->level = *mode - '0';
129 state->mode = GZ_READ;
131 #ifndef NO_GZCOMPRESS
133 state->mode = GZ_WRITE;
136 state->mode = GZ_APPEND;
139 case '+': /* can't read and write at the same time */
142 case 'b': /* ignore -- will request binary anyway */
155 state->strategy = Z_FILTERED;
158 state->strategy = Z_HUFFMAN_ONLY;
161 state->strategy = Z_RLE;
164 state->strategy = Z_FIXED;
169 default: /* could consider as an error, but just ignore */
175 /* must provide an "r", "w", or "a" */
176 if (state->mode == GZ_NONE) {
181 /* can't force transparent read */
182 if (state->mode == GZ_READ) {
187 state->direct = 1; /* for empty file */
190 /* save the path name for error messages */
193 len = wcstombs(NULL, path, 0);
194 if (len == (size_t)-1)
199 len = strlen((const char *)path);
200 state->path = (char *)malloc(len + 1);
201 if (state->path == NULL) {
208 wcstombs(state->path, path, len + 1);
213 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
214 snprintf(state->path, len + 1, "%s", (const char *)path);
216 strcpy(state->path, path);
219 /* compute the flags for open() */
228 (cloexec ? O_CLOEXEC : 0) |
230 (state->mode == GZ_READ ?
232 (O_WRONLY | O_CREAT |
234 (exclusive ? O_EXCL : 0) |
236 (state->mode == GZ_WRITE ?
240 /* open the file with the appropriate flags (or just use fd) */
241 state->fd = fd > -1 ? fd : (
243 fd == -2 ? _wopen(path, oflag, 0666) :
245 open((const char *)path, oflag, 0666));
246 if (state->fd == -1) {
251 if (state->mode == GZ_APPEND)
252 state->mode = GZ_WRITE; /* simplify later checks */
254 /* save the current position for rewinding (only if reading) */
255 if (state->mode == GZ_READ) {
256 state->start = LSEEK(state->fd, 0, SEEK_CUR);
257 if (state->start == -1) state->start = 0;
260 /* initialize stream */
264 return (gzFile)state;
267 /* -- see zlib.h -- */
268 gzFile ZEXPORT gzopen(path, mode)
272 return gz_open(path, -1, mode);
275 /* -- see zlib.h -- */
276 gzFile ZEXPORT gzopen64(path, mode)
280 return gz_open(path, -1, mode);
283 /* -- see zlib.h -- */
284 gzFile ZEXPORT gzdopen(fd, mode)
288 char *path; /* identifier for error messages */
291 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
293 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
294 snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */
296 sprintf(path, "<fd:%d>", fd); /* for debugging */
298 gz = gz_open(path, fd, mode);
303 /* -- see zlib.h -- */
305 gzFile ZEXPORT gzopen_w(path, mode)
309 return gz_open(path, -2, mode);
313 /* -- see zlib.h -- */
314 int ZEXPORT gzbuffer(file, size)
320 /* get internal structure and check integrity */
323 state = (gz_statep)file;
324 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
327 /* make sure we haven't already allocated memory */
328 if (state->size != 0)
331 /* check and set requested size */
333 size = 2; /* need two bytes to check magic header */
338 /* -- see zlib.h -- */
339 int ZEXPORT gzrewind(file)
344 /* get internal structure */
347 state = (gz_statep)file;
349 /* check that we're reading and that there's no error */
350 if (state->mode != GZ_READ ||
351 (state->err != Z_OK && state->err != Z_BUF_ERROR))
354 /* back up and start over */
355 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
361 /* -- see zlib.h -- */
362 z_off64_t ZEXPORT gzseek64(file, offset, whence)
371 /* get internal structure and check integrity */
374 state = (gz_statep)file;
375 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
378 /* check that there's no error */
379 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
382 /* can only seek from start or relative to current position */
383 if (whence != SEEK_SET && whence != SEEK_CUR)
386 /* normalize offset to a SEEK_CUR specification */
387 if (whence == SEEK_SET)
388 offset -= state->x.pos;
389 else if (state->seek)
390 offset += state->skip;
393 /* if within raw area while reading, just go there */
394 if (state->mode == GZ_READ && state->how == COPY &&
395 state->x.pos + offset >= 0) {
396 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
403 gz_error(state, Z_OK, NULL);
404 state->strm.avail_in = 0;
405 state->x.pos += offset;
409 /* calculate skip amount, rewinding if needed for back seek when reading */
411 if (state->mode != GZ_READ) /* writing -- can't go backwards */
413 offset += state->x.pos;
414 if (offset < 0) /* before start of file! */
416 if (gzrewind(file) == -1) /* rewind, then skip to offset */
420 /* if reading, skip what's in output buffer (one less gzgetc() check) */
421 if (state->mode == GZ_READ) {
422 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
423 (unsigned)offset : state->x.have;
430 /* request skip (if not zero) */
433 state->skip = offset;
435 return state->x.pos + offset;
438 /* -- see zlib.h -- */
439 z_off_t ZEXPORT gzseek(file, offset, whence)
446 ret = gzseek64(file, (z_off64_t)offset, whence);
447 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
450 /* -- see zlib.h -- */
451 z_off64_t ZEXPORT gztell64(file)
456 /* get internal structure and check integrity */
459 state = (gz_statep)file;
460 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
463 /* return position */
464 return state->x.pos + (state->seek ? state->skip : 0);
467 /* -- see zlib.h -- */
468 z_off_t ZEXPORT gztell(file)
473 ret = gztell64(file);
474 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
477 /* -- see zlib.h -- */
478 z_off64_t ZEXPORT gzoffset64(file)
484 /* get internal structure and check integrity */
487 state = (gz_statep)file;
488 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
491 /* compute and return effective offset in file */
492 offset = LSEEK(state->fd, 0, SEEK_CUR);
495 if (state->mode == GZ_READ) /* reading */
496 offset -= state->strm.avail_in; /* don't count buffered input */
500 /* -- see zlib.h -- */
501 z_off_t ZEXPORT gzoffset(file)
506 ret = gzoffset64(file);
507 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
510 /* -- see zlib.h -- */
511 int ZEXPORT gzeof(file)
516 /* get internal structure and check integrity */
519 state = (gz_statep)file;
520 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
523 /* return end-of-file state */
524 return state->mode == GZ_READ ? state->past : 0;
527 /* -- see zlib.h -- */
528 const char * ZEXPORT gzerror(file, errnum)
534 /* get internal structure and check integrity */
537 state = (gz_statep)file;
538 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
541 /* return error information */
543 *errnum = state->err;
544 return state->err == Z_MEM_ERROR ? "out of memory" :
545 (state->msg == NULL ? "" : state->msg);
548 /* -- see zlib.h -- */
549 void ZEXPORT gzclearerr(file)
554 /* get internal structure and check integrity */
557 state = (gz_statep)file;
558 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
561 /* clear error and end-of-file */
562 if (state->mode == GZ_READ) {
566 gz_error(state, Z_OK, NULL);
569 /* Create an error message in allocated memory and set state->err and
570 state->msg accordingly. Free any previous error message already there. Do
571 not try to free or allocate space if the error is Z_MEM_ERROR (out of
572 memory). Simply save the error message as a static string. If there is an
573 allocation failure constructing the error message, then convert the error to
575 void ZLIB_INTERNAL gz_error(state, err, msg)
580 /* free previously allocated message and clear */
581 if (state->msg != NULL) {
582 if (state->err != Z_MEM_ERROR)
587 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
588 if (err != Z_OK && err != Z_BUF_ERROR)
591 /* set error code, and if no message, then done */
596 /* for an out of memory error, return literal string when requested */
597 if (err == Z_MEM_ERROR)
600 /* construct error message with path */
601 if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
603 state->err = Z_MEM_ERROR;
606 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
607 snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
608 "%s%s%s", state->path, ": ", msg);
610 strcpy(state->msg, state->path);
611 strcat(state->msg, ": ");
612 strcat(state->msg, msg);
618 /* portably return maximum value for an int (when limits.h presumed not
619 available) -- we need to do this to cover cases where 2's complement not
620 used, since C standard permits 1's complement and sign-bit representations,
621 otherwise we could just use ((unsigned)-1) >> 1 */
622 unsigned ZLIB_INTERNAL gz_intmax()