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
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 = 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;
167 default: /* could consider as an error, but just ignore */
173 /* must provide an "r", "w", or "a" */
174 if (state->mode == GZ_NONE) {
179 /* can't force transparent read */
180 if (state->mode == GZ_READ) {
185 state->direct = 1; /* for empty file */
188 /* save the path name for error messages */
191 len = wcstombs(NULL, path, 0);
192 if (len == (size_t)-1)
198 state->path = malloc(len + 1);
199 if (state->path == NULL) {
206 wcstombs(state->path, path, len + 1);
211 strcpy(state->path, path);
213 /* compute the flags for open() */
222 (cloexec ? O_CLOEXEC : 0) |
224 (state->mode == GZ_READ ?
226 (O_WRONLY | O_CREAT |
228 (exclusive ? O_EXCL : 0) |
230 (state->mode == GZ_WRITE ?
234 /* open the file with the appropriate flags (or just use fd) */
235 state->fd = fd > -1 ? fd : (
237 fd == -2 ? _wopen(path, oflag, 0666) :
239 open(path, oflag, 0666));
240 if (state->fd == -1) {
245 if (state->mode == GZ_APPEND)
246 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(path, mode)
266 return gz_open(path, -1, mode);
269 /* -- see zlib.h -- */
270 gzFile ZEXPORT gzopen64(path, mode)
274 return gz_open(path, -1, mode);
277 /* -- see zlib.h -- */
278 gzFile ZEXPORT gzdopen(fd, mode)
282 char *path; /* identifier for error messages */
285 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
287 sprintf(path, "<fd:%d>", fd); /* for debugging */
288 gz = gz_open(path, fd, mode);
293 /* -- see zlib.h -- */
295 gzFile ZEXPORT gzopen_w(path, mode)
299 return gz_open(path, -2, mode);
303 /* -- see zlib.h -- */
304 int ZEXPORT gzbuffer(file, size)
310 /* get internal structure and check integrity */
313 state = (gz_statep)file;
314 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
317 /* make sure we haven't already allocated memory */
318 if (state->size != 0)
321 /* check and set requested size */
323 size = 2; /* need two bytes to check magic header */
328 /* -- see zlib.h -- */
329 int ZEXPORT gzrewind(file)
334 /* get internal structure */
337 state = (gz_statep)file;
339 /* check that we're reading and that there's no error */
340 if (state->mode != GZ_READ ||
341 (state->err != Z_OK && state->err != Z_BUF_ERROR))
344 /* back up and start over */
345 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
351 /* -- see zlib.h -- */
352 z_off64_t ZEXPORT gzseek64(file, offset, whence)
361 /* get internal structure and check integrity */
364 state = (gz_statep)file;
365 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
368 /* check that there's no error */
369 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
372 /* can only seek from start or relative to current position */
373 if (whence != SEEK_SET && whence != SEEK_CUR)
376 /* normalize offset to a SEEK_CUR specification */
377 if (whence == SEEK_SET)
378 offset -= state->x.pos;
379 else if (state->seek)
380 offset += state->skip;
383 /* if within raw area while reading, just go there */
384 if (state->mode == GZ_READ && state->how == COPY &&
385 state->x.pos + offset >= 0) {
386 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
393 gz_error(state, Z_OK, NULL);
394 state->strm.avail_in = 0;
395 state->x.pos += offset;
399 /* calculate skip amount, rewinding if needed for back seek when reading */
401 if (state->mode != GZ_READ) /* writing -- can't go backwards */
403 offset += state->x.pos;
404 if (offset < 0) /* before start of file! */
406 if (gzrewind(file) == -1) /* rewind, then skip to offset */
410 /* if reading, skip what's in output buffer (one less gzgetc() check) */
411 if (state->mode == GZ_READ) {
412 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
413 (unsigned)offset : state->x.have;
420 /* request skip (if not zero) */
423 state->skip = offset;
425 return state->x.pos + offset;
428 /* -- see zlib.h -- */
429 z_off_t ZEXPORT gzseek(file, offset, whence)
436 ret = gzseek64(file, (z_off64_t)offset, whence);
437 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
440 /* -- see zlib.h -- */
441 z_off64_t ZEXPORT gztell64(file)
446 /* get internal structure and check integrity */
449 state = (gz_statep)file;
450 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
453 /* return position */
454 return state->x.pos + (state->seek ? state->skip : 0);
457 /* -- see zlib.h -- */
458 z_off_t ZEXPORT gztell(file)
463 ret = gztell64(file);
464 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
467 /* -- see zlib.h -- */
468 z_off64_t ZEXPORT gzoffset64(file)
474 /* get internal structure and check integrity */
477 state = (gz_statep)file;
478 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
481 /* compute and return effective offset in file */
482 offset = LSEEK(state->fd, 0, SEEK_CUR);
485 if (state->mode == GZ_READ) /* reading */
486 offset -= state->strm.avail_in; /* don't count buffered input */
490 /* -- see zlib.h -- */
491 z_off_t ZEXPORT gzoffset(file)
496 ret = gzoffset64(file);
497 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
500 /* -- see zlib.h -- */
501 int ZEXPORT gzeof(file)
506 /* get internal structure and check integrity */
509 state = (gz_statep)file;
510 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
513 /* return end-of-file state */
514 return state->mode == GZ_READ ? state->past : 0;
517 /* -- see zlib.h -- */
518 const char * ZEXPORT gzerror(file, errnum)
524 /* get internal structure and check integrity */
527 state = (gz_statep)file;
528 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
531 /* return error information */
533 *errnum = state->err;
534 return state->msg == NULL ? "" : state->msg;
537 /* -- see zlib.h -- */
538 void ZEXPORT gzclearerr(file)
543 /* get internal structure and check integrity */
546 state = (gz_statep)file;
547 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
550 /* clear error and end-of-file */
551 if (state->mode == GZ_READ) {
555 gz_error(state, Z_OK, NULL);
558 /* Create an error message in allocated memory and set state->err and
559 state->msg accordingly. Free any previous error message already there. Do
560 not try to free or allocate space if the error is Z_MEM_ERROR (out of
561 memory). Simply save the error message as a static string. If there is an
562 allocation failure constructing the error message, then convert the error to
564 void ZLIB_INTERNAL gz_error(state, err, msg)
569 /* free previously allocated message and clear */
570 if (state->msg != NULL) {
571 if (state->err != Z_MEM_ERROR)
576 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
577 if (err != Z_OK && err != Z_BUF_ERROR)
580 /* set error code, and if no message, then done */
585 /* for an out of memory error, save as static string */
586 if (err == Z_MEM_ERROR) {
587 state->msg = (char *)msg;
591 /* construct error message with path */
592 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
593 state->err = Z_MEM_ERROR;
594 state->msg = (char *)"out of memory";
597 strcpy(state->msg, state->path);
598 strcat(state->msg, ": ");
599 strcat(state->msg, msg);
604 /* portably return maximum value for an int (when limits.h presumed not
605 available) -- we need to do this to cover cases where 2's complement not
606 used, since C standard permits 1's complement and sign-bit representations,
607 otherwise we could just use ((unsigned)-1) >> 1 */
608 unsigned ZLIB_INTERNAL gz_intmax()