1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004, 2010, 2011 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 char *, 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)
97 int cloexec = 0, exclusive = 0;
103 /* allocate gzFile structure to return */
104 state = malloc(sizeof(gz_state));
107 state->size = 0; /* no buffers allocated yet */
108 state->want = GZBUFSIZE; /* requested buffer size */
109 state->msg = NULL; /* no error message yet */
112 state->mode = GZ_NONE;
113 state->level = Z_DEFAULT_COMPRESSION;
114 state->strategy = Z_DEFAULT_STRATEGY;
117 if (*mode >= '0' && *mode <= '9')
118 state->level = *mode - '0';
122 state->mode = GZ_READ;
124 #ifndef NO_GZCOMPRESS
126 state->mode = GZ_WRITE;
129 state->mode = GZ_APPEND;
132 case '+': /* can't read and write at the same time */
135 case 'b': /* ignore -- will request binary anyway */
144 state->strategy = Z_FILTERED;
147 state->strategy = Z_HUFFMAN_ONLY;
150 state->strategy = Z_RLE;
153 state->strategy = Z_FIXED;
156 default: /* could consider as an error, but just ignore */
162 /* must provide an "r", "w", or "a" */
163 if (state->mode == GZ_NONE) {
168 /* can't force transparent read */
169 if (state->mode == GZ_READ) {
174 state->direct = 1; /* for empty file */
177 /* save the path name for error messages */
178 state->path = malloc(strlen(path) + 1);
179 if (state->path == NULL) {
183 strcpy(state->path, path);
185 /* open the file with the appropriate mode (or just use fd) */
186 state->fd = fd != -1 ? fd :
195 (cloexec ? O_CLOEXEC : 0) |
197 (state->mode == GZ_READ ?
199 (O_WRONLY | O_CREAT |
201 (exclusive ? O_EXCL : 0) |
203 (state->mode == GZ_WRITE ?
207 if (state->fd == -1) {
212 if (state->mode == GZ_APPEND)
213 state->mode = GZ_WRITE; /* simplify later checks */
215 /* save the current position for rewinding (only if reading) */
216 if (state->mode == GZ_READ) {
217 state->start = LSEEK(state->fd, 0, SEEK_CUR);
218 if (state->start == -1) state->start = 0;
221 /* initialize stream */
225 return (gzFile)state;
228 /* -- see zlib.h -- */
229 gzFile ZEXPORT gzopen(path, mode)
233 return gz_open(path, -1, mode);
236 /* -- see zlib.h -- */
237 gzFile ZEXPORT gzopen64(path, mode)
241 return gz_open(path, -1, mode);
244 /* -- see zlib.h -- */
245 gzFile ZEXPORT gzdopen(fd, mode)
249 char *path; /* identifier for error messages */
252 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
254 sprintf(path, "<fd:%d>", fd); /* for debugging */
255 gz = gz_open(path, fd, mode);
260 /* -- see zlib.h -- */
261 int ZEXPORT gzbuffer(file, size)
267 /* get internal structure and check integrity */
270 state = (gz_statep)file;
271 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
274 /* make sure we haven't already allocated memory */
275 if (state->size != 0)
278 /* check and set requested size */
280 size = 2; /* need two bytes to check magic header */
285 /* -- see zlib.h -- */
286 int ZEXPORT gzrewind(file)
291 /* get internal structure */
294 state = (gz_statep)file;
296 /* check that we're reading and that there's no error */
297 if (state->mode != GZ_READ ||
298 (state->err != Z_OK && state->err != Z_BUF_ERROR))
301 /* back up and start over */
302 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
308 /* -- see zlib.h -- */
309 z_off64_t ZEXPORT gzseek64(file, offset, whence)
318 /* get internal structure and check integrity */
321 state = (gz_statep)file;
322 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
325 /* check that there's no error */
326 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
329 /* can only seek from start or relative to current position */
330 if (whence != SEEK_SET && whence != SEEK_CUR)
333 /* normalize offset to a SEEK_CUR specification */
334 if (whence == SEEK_SET)
335 offset -= state->x.pos;
336 else if (state->seek)
337 offset += state->skip;
340 /* if within raw area while reading, just go there */
341 if (state->mode == GZ_READ && state->how == COPY &&
342 state->x.pos + offset >= 0) {
343 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
350 gz_error(state, Z_OK, NULL);
351 state->strm.avail_in = 0;
352 state->x.pos += offset;
356 /* calculate skip amount, rewinding if needed for back seek when reading */
358 if (state->mode != GZ_READ) /* writing -- can't go backwards */
360 offset += state->x.pos;
361 if (offset < 0) /* before start of file! */
363 if (gzrewind(file) == -1) /* rewind, then skip to offset */
367 /* if reading, skip what's in output buffer (one less gzgetc() check) */
368 if (state->mode == GZ_READ) {
369 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
370 (unsigned)offset : state->x.have;
377 /* request skip (if not zero) */
380 state->skip = offset;
382 return state->x.pos + offset;
385 /* -- see zlib.h -- */
386 z_off_t ZEXPORT gzseek(file, offset, whence)
393 ret = gzseek64(file, (z_off64_t)offset, whence);
394 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
397 /* -- see zlib.h -- */
398 z_off64_t ZEXPORT gztell64(file)
403 /* get internal structure and check integrity */
406 state = (gz_statep)file;
407 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
410 /* return position */
411 return state->x.pos + (state->seek ? state->skip : 0);
414 /* -- see zlib.h -- */
415 z_off_t ZEXPORT gztell(file)
420 ret = gztell64(file);
421 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
424 /* -- see zlib.h -- */
425 z_off64_t ZEXPORT gzoffset64(file)
431 /* get internal structure and check integrity */
434 state = (gz_statep)file;
435 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
438 /* compute and return effective offset in file */
439 offset = LSEEK(state->fd, 0, SEEK_CUR);
442 if (state->mode == GZ_READ) /* reading */
443 offset -= state->strm.avail_in; /* don't count buffered input */
447 /* -- see zlib.h -- */
448 z_off_t ZEXPORT gzoffset(file)
453 ret = gzoffset64(file);
454 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
457 /* -- see zlib.h -- */
458 int ZEXPORT gzeof(file)
463 /* get internal structure and check integrity */
466 state = (gz_statep)file;
467 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
470 /* return end-of-file state */
471 return state->mode == GZ_READ ? state->past : 0;
474 /* -- see zlib.h -- */
475 const char * ZEXPORT gzerror(file, errnum)
481 /* get internal structure and check integrity */
484 state = (gz_statep)file;
485 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
488 /* return error information */
490 *errnum = state->err;
491 return state->msg == NULL ? "" : state->msg;
494 /* -- see zlib.h -- */
495 void ZEXPORT gzclearerr(file)
500 /* get internal structure and check integrity */
503 state = (gz_statep)file;
504 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
507 /* clear error and end-of-file */
508 if (state->mode == GZ_READ) {
512 gz_error(state, Z_OK, NULL);
515 /* Create an error message in allocated memory and set state->err and
516 state->msg accordingly. Free any previous error message already there. Do
517 not try to free or allocate space if the error is Z_MEM_ERROR (out of
518 memory). Simply save the error message as a static string. If there is an
519 allocation failure constructing the error message, then convert the error to
521 void ZLIB_INTERNAL gz_error(state, err, msg)
526 /* free previously allocated message and clear */
527 if (state->msg != NULL) {
528 if (state->err != Z_MEM_ERROR)
533 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
534 if (err != Z_OK && err != Z_BUF_ERROR)
537 /* set error code, and if no message, then done */
542 /* for an out of memory error, save as static string */
543 if (err == Z_MEM_ERROR) {
544 state->msg = (char *)msg;
548 /* construct error message with path */
549 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
550 state->err = Z_MEM_ERROR;
551 state->msg = (char *)"out of memory";
554 strcpy(state->msg, state->path);
555 strcat(state->msg, ": ");
556 strcat(state->msg, msg);
561 /* portably return maximum value for an int (when limits.h presumed not
562 available) -- we need to do this to cover cases where 2's complement not
563 used, since C standard permits 1's complement and sign-bit representations,
564 otherwise we could just use ((unsigned)-1) >> 1 */
565 unsigned ZLIB_INTERNAL gz_intmax()