1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004, 2010, 2011, 2012 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
11 #if defined(_WIN32) && !defined(__BORLANDC__)
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 = 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;
170 default: /* could consider as an error, but just ignore */
176 /* must provide an "r", "w", or "a" */
177 if (state->mode == GZ_NONE) {
182 /* can't force transparent read */
183 if (state->mode == GZ_READ) {
188 state->direct = 1; /* for empty file */
191 /* save the path name for error messages */
194 len = wcstombs(NULL, path, 0);
195 if (len == (size_t)-1)
201 state->path = malloc(len + 1);
202 if (state->path == NULL) {
209 wcstombs(state->path, path, len + 1);
214 strcpy(state->path, path);
216 /* compute the flags for open() */
225 (cloexec ? O_CLOEXEC : 0) |
227 (state->mode == GZ_READ ?
229 (O_WRONLY | O_CREAT |
231 (exclusive ? O_EXCL : 0) |
233 (state->mode == GZ_WRITE ?
237 /* open the file with the appropriate flags (or just use fd) */
238 state->fd = fd > -1 ? fd : (
240 fd == -2 ? _wopen(path, oflag, 0666) :
242 open(path, oflag, 0666));
243 if (state->fd == -1) {
248 if (state->mode == GZ_APPEND)
249 state->mode = GZ_WRITE; /* simplify later checks */
251 /* save the current position for rewinding (only if reading) */
252 if (state->mode == GZ_READ) {
253 state->start = LSEEK(state->fd, 0, SEEK_CUR);
254 if (state->start == -1) state->start = 0;
257 /* initialize stream */
261 return (gzFile)state;
264 /* -- see zlib.h -- */
265 gzFile ZEXPORT gzopen(path, mode)
269 return gz_open(path, -1, mode);
272 /* -- see zlib.h -- */
273 gzFile ZEXPORT gzopen64(path, mode)
277 return gz_open(path, -1, mode);
280 /* -- see zlib.h -- */
281 gzFile ZEXPORT gzdopen(fd, mode)
285 char *path; /* identifier for error messages */
288 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
290 sprintf(path, "<fd:%d>", fd); /* for debugging */
291 gz = gz_open(path, fd, mode);
296 /* -- see zlib.h -- */
298 gzFile ZEXPORT gzopen_w(path, mode)
302 return gz_open(path, -2, mode);
306 /* -- see zlib.h -- */
307 int ZEXPORT gzbuffer(file, size)
313 /* get internal structure and check integrity */
316 state = (gz_statep)file;
317 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
320 /* make sure we haven't already allocated memory */
321 if (state->size != 0)
324 /* check and set requested size */
326 size = 2; /* need two bytes to check magic header */
331 /* -- see zlib.h -- */
332 int ZEXPORT gzrewind(file)
337 /* get internal structure */
340 state = (gz_statep)file;
342 /* check that we're reading and that there's no error */
343 if (state->mode != GZ_READ ||
344 (state->err != Z_OK && state->err != Z_BUF_ERROR))
347 /* back up and start over */
348 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
354 /* -- see zlib.h -- */
355 z_off64_t ZEXPORT gzseek64(file, offset, whence)
364 /* get internal structure and check integrity */
367 state = (gz_statep)file;
368 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
371 /* check that there's no error */
372 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
375 /* can only seek from start or relative to current position */
376 if (whence != SEEK_SET && whence != SEEK_CUR)
379 /* normalize offset to a SEEK_CUR specification */
380 if (whence == SEEK_SET)
381 offset -= state->x.pos;
382 else if (state->seek)
383 offset += state->skip;
386 /* if within raw area while reading, just go there */
387 if (state->mode == GZ_READ && state->how == COPY &&
388 state->x.pos + offset >= 0) {
389 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
396 gz_error(state, Z_OK, NULL);
397 state->strm.avail_in = 0;
398 state->x.pos += offset;
402 /* calculate skip amount, rewinding if needed for back seek when reading */
404 if (state->mode != GZ_READ) /* writing -- can't go backwards */
406 offset += state->x.pos;
407 if (offset < 0) /* before start of file! */
409 if (gzrewind(file) == -1) /* rewind, then skip to offset */
413 /* if reading, skip what's in output buffer (one less gzgetc() check) */
414 if (state->mode == GZ_READ) {
415 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
416 (unsigned)offset : state->x.have;
423 /* request skip (if not zero) */
426 state->skip = offset;
428 return state->x.pos + offset;
431 /* -- see zlib.h -- */
432 z_off_t ZEXPORT gzseek(file, offset, whence)
439 ret = gzseek64(file, (z_off64_t)offset, whence);
440 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
443 /* -- see zlib.h -- */
444 z_off64_t ZEXPORT gztell64(file)
449 /* get internal structure and check integrity */
452 state = (gz_statep)file;
453 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
456 /* return position */
457 return state->x.pos + (state->seek ? state->skip : 0);
460 /* -- see zlib.h -- */
461 z_off_t ZEXPORT gztell(file)
466 ret = gztell64(file);
467 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
470 /* -- see zlib.h -- */
471 z_off64_t ZEXPORT gzoffset64(file)
477 /* get internal structure and check integrity */
480 state = (gz_statep)file;
481 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
484 /* compute and return effective offset in file */
485 offset = LSEEK(state->fd, 0, SEEK_CUR);
488 if (state->mode == GZ_READ) /* reading */
489 offset -= state->strm.avail_in; /* don't count buffered input */
493 /* -- see zlib.h -- */
494 z_off_t ZEXPORT gzoffset(file)
499 ret = gzoffset64(file);
500 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
503 /* -- see zlib.h -- */
504 int ZEXPORT gzeof(file)
509 /* get internal structure and check integrity */
512 state = (gz_statep)file;
513 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
516 /* return end-of-file state */
517 return state->mode == GZ_READ ? state->past : 0;
520 /* -- see zlib.h -- */
521 const char * ZEXPORT gzerror(file, errnum)
527 /* get internal structure and check integrity */
530 state = (gz_statep)file;
531 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
534 /* return error information */
536 *errnum = state->err;
537 return state->msg == NULL ? "" : state->msg;
540 /* -- see zlib.h -- */
541 void ZEXPORT gzclearerr(file)
546 /* get internal structure and check integrity */
549 state = (gz_statep)file;
550 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
553 /* clear error and end-of-file */
554 if (state->mode == GZ_READ) {
558 gz_error(state, Z_OK, NULL);
561 /* Create an error message in allocated memory and set state->err and
562 state->msg accordingly. Free any previous error message already there. Do
563 not try to free or allocate space if the error is Z_MEM_ERROR (out of
564 memory). Simply save the error message as a static string. If there is an
565 allocation failure constructing the error message, then convert the error to
567 void ZLIB_INTERNAL gz_error(state, err, msg)
572 /* free previously allocated message and clear */
573 if (state->msg != NULL) {
574 if (state->err != Z_MEM_ERROR)
579 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
580 if (err != Z_OK && err != Z_BUF_ERROR)
583 /* set error code, and if no message, then done */
588 /* for an out of memory error, save as static string */
589 if (err == Z_MEM_ERROR) {
590 state->msg = (char *)msg;
594 /* construct error message with path */
595 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
596 state->err = Z_MEM_ERROR;
597 state->msg = (char *)"out of memory";
600 strcpy(state->msg, state->path);
601 strcat(state->msg, ": ");
602 strcat(state->msg, msg);
607 /* portably return maximum value for an int (when limits.h presumed not
608 available) -- we need to do this to cover cases where 2's complement not
609 used, since C standard permits 1's complement and sign-bit representations,
610 otherwise we could just use ((unsigned)-1) >> 1 */
611 unsigned ZLIB_INTERNAL gz_intmax()