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
10 #if defined(_WIN32) && !defined(__BORLANDC__)
11 # define LSEEK _lseeki64
13 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
14 # define LSEEK lseek64
21 local void gz_reset OF((gz_statep));
22 local gzFile gz_open OF((const void *, int, const char *));
26 /* Map the Windows error number in ERROR to a locale-dependent error message
27 string and return a pointer to it. Typically, the values for ERROR come
30 The string pointed to shall not be modified by the application, but may be
31 overwritten by a subsequent call to gz_strwinerror
33 The gz_strwinerror function does not change the current setting of
35 char ZLIB_INTERNAL *gz_strwinerror (error)
38 static char buf[1024];
41 DWORD lasterr = GetLastError();
42 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
43 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
46 0, /* Default language */
51 /* If there is an \r\n appended, zap it. */
53 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
58 if (chars > sizeof (buf) - 1) {
59 chars = sizeof (buf) - 1;
63 wcstombs(buf, msgbuf, chars + 1);
67 sprintf(buf, "unknown win32 error (%ld)", error);
70 SetLastError(lasterr);
76 /* Reset gzip file state */
77 local void gz_reset(state)
80 state->x.have = 0; /* no output data available */
81 if (state->mode == GZ_READ) { /* for reading ... */
82 state->eof = 0; /* not at end of file */
83 state->past = 0; /* have not read past end yet */
84 state->how = LOOK; /* look for gzip header */
86 state->seek = 0; /* no seek request pending */
87 gz_error(state, Z_OK, NULL); /* clear error */
88 state->x.pos = 0; /* no uncompressed data yet */
89 state->strm.avail_in = 0; /* no input data yet */
92 /* Open a gzip file either by name or file descriptor. */
93 local gzFile gz_open(path, fd, mode)
112 /* allocate gzFile structure to return */
113 state = (gz_statep)malloc(sizeof(gz_state));
116 state->size = 0; /* no buffers allocated yet */
117 state->want = GZBUFSIZE; /* requested buffer size */
118 state->msg = NULL; /* no error message yet */
121 state->mode = GZ_NONE;
122 state->level = Z_DEFAULT_COMPRESSION;
123 state->strategy = Z_DEFAULT_STRATEGY;
126 if (*mode >= '0' && *mode <= '9')
127 state->level = *mode - '0';
131 state->mode = GZ_READ;
133 #ifndef NO_GZCOMPRESS
135 state->mode = GZ_WRITE;
138 state->mode = GZ_APPEND;
141 case '+': /* can't read and write at the same time */
144 case 'b': /* ignore -- will request binary anyway */
157 state->strategy = Z_FILTERED;
160 state->strategy = Z_HUFFMAN_ONLY;
163 state->strategy = Z_RLE;
166 state->strategy = Z_FIXED;
171 default: /* could consider as an error, but just ignore */
177 /* must provide an "r", "w", or "a" */
178 if (state->mode == GZ_NONE) {
183 /* can't force transparent read */
184 if (state->mode == GZ_READ) {
189 state->direct = 1; /* for empty file */
192 /* save the path name for error messages */
195 len = wcstombs(NULL, path, 0);
196 if (len == (size_t)-1)
201 len = strlen((const char *)path);
202 state->path = (char *)malloc(len + 1);
203 if (state->path == NULL) {
210 wcstombs(state->path, path, len + 1);
215 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
216 snprintf(state->path, len + 1, "%s", (const char *)path);
218 strcpy(state->path, path);
221 /* compute the flags for open() */
230 (cloexec ? O_CLOEXEC : 0) |
232 (state->mode == GZ_READ ?
234 (O_WRONLY | O_CREAT |
236 (exclusive ? O_EXCL : 0) |
238 (state->mode == GZ_WRITE ?
242 /* open the file with the appropriate flags (or just use fd) */
243 state->fd = fd > -1 ? fd : (
245 fd == -2 ? _wopen(path, oflag, 0666) :
247 open((const char *)path, oflag, 0666));
248 if (state->fd == -1) {
253 if (state->mode == GZ_APPEND)
254 state->mode = GZ_WRITE; /* simplify later checks */
256 /* save the current position for rewinding (only if reading) */
257 if (state->mode == GZ_READ) {
258 state->start = LSEEK(state->fd, 0, SEEK_CUR);
259 if (state->start == -1) state->start = 0;
262 /* initialize stream */
266 return (gzFile)state;
269 /* -- see zlib.h -- */
270 gzFile ZEXPORT gzopen(path, mode)
274 return gz_open(path, -1, mode);
277 /* -- see zlib.h -- */
278 gzFile ZEXPORT gzopen64(path, mode)
282 return gz_open(path, -1, mode);
285 /* -- see zlib.h -- */
286 gzFile ZEXPORT gzdopen(fd, mode)
290 char *path; /* identifier for error messages */
293 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
295 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
296 snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */
298 sprintf(path, "<fd:%d>", fd); /* for debugging */
300 gz = gz_open(path, fd, mode);
305 /* -- see zlib.h -- */
307 gzFile ZEXPORT gzopen_w(path, mode)
311 return gz_open(path, -2, mode);
315 /* -- see zlib.h -- */
316 int ZEXPORT gzbuffer(file, size)
322 /* get internal structure and check integrity */
325 state = (gz_statep)file;
326 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
329 /* make sure we haven't already allocated memory */
330 if (state->size != 0)
333 /* check and set requested size */
335 size = 2; /* need two bytes to check magic header */
340 /* -- see zlib.h -- */
341 int ZEXPORT gzrewind(file)
346 /* get internal structure */
349 state = (gz_statep)file;
351 /* check that we're reading and that there's no error */
352 if (state->mode != GZ_READ ||
353 (state->err != Z_OK && state->err != Z_BUF_ERROR))
356 /* back up and start over */
357 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
363 /* -- see zlib.h -- */
364 z_off64_t ZEXPORT gzseek64(file, offset, whence)
373 /* get internal structure and check integrity */
376 state = (gz_statep)file;
377 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
380 /* check that there's no error */
381 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
384 /* can only seek from start or relative to current position */
385 if (whence != SEEK_SET && whence != SEEK_CUR)
388 /* normalize offset to a SEEK_CUR specification */
389 if (whence == SEEK_SET)
390 offset -= state->x.pos;
391 else if (state->seek)
392 offset += state->skip;
395 /* if within raw area while reading, just go there */
396 if (state->mode == GZ_READ && state->how == COPY &&
397 state->x.pos + offset >= 0) {
398 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
405 gz_error(state, Z_OK, NULL);
406 state->strm.avail_in = 0;
407 state->x.pos += offset;
411 /* calculate skip amount, rewinding if needed for back seek when reading */
413 if (state->mode != GZ_READ) /* writing -- can't go backwards */
415 offset += state->x.pos;
416 if (offset < 0) /* before start of file! */
418 if (gzrewind(file) == -1) /* rewind, then skip to offset */
422 /* if reading, skip what's in output buffer (one less gzgetc() check) */
423 if (state->mode == GZ_READ) {
424 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
425 (unsigned)offset : state->x.have;
432 /* request skip (if not zero) */
435 state->skip = offset;
437 return state->x.pos + offset;
440 /* -- see zlib.h -- */
441 z_off_t ZEXPORT gzseek(file, offset, whence)
448 ret = gzseek64(file, (z_off64_t)offset, whence);
449 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
452 /* -- see zlib.h -- */
453 z_off64_t ZEXPORT gztell64(file)
458 /* get internal structure and check integrity */
461 state = (gz_statep)file;
462 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
465 /* return position */
466 return state->x.pos + (state->seek ? state->skip : 0);
469 /* -- see zlib.h -- */
470 z_off_t ZEXPORT gztell(file)
475 ret = gztell64(file);
476 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
479 /* -- see zlib.h -- */
480 z_off64_t ZEXPORT gzoffset64(file)
486 /* get internal structure and check integrity */
489 state = (gz_statep)file;
490 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
493 /* compute and return effective offset in file */
494 offset = LSEEK(state->fd, 0, SEEK_CUR);
497 if (state->mode == GZ_READ) /* reading */
498 offset -= state->strm.avail_in; /* don't count buffered input */
502 /* -- see zlib.h -- */
503 z_off_t ZEXPORT gzoffset(file)
508 ret = gzoffset64(file);
509 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
512 /* -- see zlib.h -- */
513 int ZEXPORT gzeof(file)
518 /* get internal structure and check integrity */
521 state = (gz_statep)file;
522 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
525 /* return end-of-file state */
526 return state->mode == GZ_READ ? state->past : 0;
529 /* -- see zlib.h -- */
530 const char * ZEXPORT gzerror(file, errnum)
536 /* get internal structure and check integrity */
539 state = (gz_statep)file;
540 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
543 /* return error information */
545 *errnum = state->err;
546 return state->err == Z_MEM_ERROR ? "out of memory" :
547 (state->msg == NULL ? "" : state->msg);
550 /* -- see zlib.h -- */
551 void ZEXPORT gzclearerr(file)
556 /* get internal structure and check integrity */
559 state = (gz_statep)file;
560 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
563 /* clear error and end-of-file */
564 if (state->mode == GZ_READ) {
568 gz_error(state, Z_OK, NULL);
571 /* Create an error message in allocated memory and set state->err and
572 state->msg accordingly. Free any previous error message already there. Do
573 not try to free or allocate space if the error is Z_MEM_ERROR (out of
574 memory). Simply save the error message as a static string. If there is an
575 allocation failure constructing the error message, then convert the error to
577 void ZLIB_INTERNAL gz_error(state, err, msg)
582 /* free previously allocated message and clear */
583 if (state->msg != NULL) {
584 if (state->err != Z_MEM_ERROR)
589 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
590 if (err != Z_OK && err != Z_BUF_ERROR)
593 /* set error code, and if no message, then done */
598 /* for an out of memory error, return literal string when requested */
599 if (err == Z_MEM_ERROR)
602 /* construct error message with path */
603 if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
605 state->err = Z_MEM_ERROR;
608 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
609 snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
610 "%s%s%s", state->path, ": ", msg);
612 strcpy(state->msg, state->path);
613 strcat(state->msg, ": ");
614 strcat(state->msg, msg);
620 /* portably return maximum value for an int (when limits.h presumed not
621 available) -- we need to do this to cover cases where 2's complement not
622 used, since C standard permits 1's complement and sign-bit representations,
623 otherwise we could just use ((unsigned)-1) >> 1 */
624 unsigned ZLIB_INTERNAL gz_intmax()