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)
108 /* allocate gzFile structure to return */
109 state = malloc(sizeof(gz_state));
112 state->size = 0; /* no buffers allocated yet */
113 state->want = GZBUFSIZE; /* requested buffer size */
114 state->msg = NULL; /* no error message yet */
117 state->mode = GZ_NONE;
118 state->level = Z_DEFAULT_COMPRESSION;
119 state->strategy = Z_DEFAULT_STRATEGY;
122 if (*mode >= '0' && *mode <= '9')
123 state->level = *mode - '0';
127 state->mode = GZ_READ;
129 #ifndef NO_GZCOMPRESS
131 state->mode = GZ_WRITE;
134 state->mode = GZ_APPEND;
137 case '+': /* can't read and write at the same time */
140 case 'b': /* ignore -- will request binary anyway */
153 state->strategy = Z_FILTERED;
156 state->strategy = Z_HUFFMAN_ONLY;
159 state->strategy = Z_RLE;
162 state->strategy = Z_FIXED;
165 default: /* could consider as an error, but just ignore */
171 /* must provide an "r", "w", or "a" */
172 if (state->mode == GZ_NONE) {
177 /* can't force transparent read */
178 if (state->mode == GZ_READ) {
183 state->direct = 1; /* for empty file */
186 /* save the path name for error messages */
187 state->path = malloc(strlen(path) + 1);
188 if (state->path == NULL) {
192 strcpy(state->path, path);
194 /* open the file with the appropriate mode (or just use fd) */
195 state->fd = fd != -1 ? fd :
204 (cloexec ? O_CLOEXEC : 0) |
206 (state->mode == GZ_READ ?
208 (O_WRONLY | O_CREAT |
210 (exclusive ? O_EXCL : 0) |
212 (state->mode == GZ_WRITE ?
216 if (state->fd == -1) {
221 if (state->mode == GZ_APPEND)
222 state->mode = GZ_WRITE; /* simplify later checks */
224 /* save the current position for rewinding (only if reading) */
225 if (state->mode == GZ_READ) {
226 state->start = LSEEK(state->fd, 0, SEEK_CUR);
227 if (state->start == -1) state->start = 0;
230 /* initialize stream */
234 return (gzFile)state;
237 /* -- see zlib.h -- */
238 gzFile ZEXPORT gzopen(path, mode)
242 return gz_open(path, -1, mode);
245 /* -- see zlib.h -- */
246 gzFile ZEXPORT gzopen64(path, mode)
250 return gz_open(path, -1, mode);
253 /* -- see zlib.h -- */
254 gzFile ZEXPORT gzdopen(fd, mode)
258 char *path; /* identifier for error messages */
261 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
263 sprintf(path, "<fd:%d>", fd); /* for debugging */
264 gz = gz_open(path, fd, mode);
269 /* -- see zlib.h -- */
270 int ZEXPORT gzbuffer(file, size)
276 /* get internal structure and check integrity */
279 state = (gz_statep)file;
280 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
283 /* make sure we haven't already allocated memory */
284 if (state->size != 0)
287 /* check and set requested size */
289 size = 2; /* need two bytes to check magic header */
294 /* -- see zlib.h -- */
295 int ZEXPORT gzrewind(file)
300 /* get internal structure */
303 state = (gz_statep)file;
305 /* check that we're reading and that there's no error */
306 if (state->mode != GZ_READ ||
307 (state->err != Z_OK && state->err != Z_BUF_ERROR))
310 /* back up and start over */
311 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
317 /* -- see zlib.h -- */
318 z_off64_t ZEXPORT gzseek64(file, offset, whence)
327 /* get internal structure and check integrity */
330 state = (gz_statep)file;
331 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
334 /* check that there's no error */
335 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
338 /* can only seek from start or relative to current position */
339 if (whence != SEEK_SET && whence != SEEK_CUR)
342 /* normalize offset to a SEEK_CUR specification */
343 if (whence == SEEK_SET)
344 offset -= state->x.pos;
345 else if (state->seek)
346 offset += state->skip;
349 /* if within raw area while reading, just go there */
350 if (state->mode == GZ_READ && state->how == COPY &&
351 state->x.pos + offset >= 0) {
352 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
359 gz_error(state, Z_OK, NULL);
360 state->strm.avail_in = 0;
361 state->x.pos += offset;
365 /* calculate skip amount, rewinding if needed for back seek when reading */
367 if (state->mode != GZ_READ) /* writing -- can't go backwards */
369 offset += state->x.pos;
370 if (offset < 0) /* before start of file! */
372 if (gzrewind(file) == -1) /* rewind, then skip to offset */
376 /* if reading, skip what's in output buffer (one less gzgetc() check) */
377 if (state->mode == GZ_READ) {
378 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
379 (unsigned)offset : state->x.have;
386 /* request skip (if not zero) */
389 state->skip = offset;
391 return state->x.pos + offset;
394 /* -- see zlib.h -- */
395 z_off_t ZEXPORT gzseek(file, offset, whence)
402 ret = gzseek64(file, (z_off64_t)offset, whence);
403 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
406 /* -- see zlib.h -- */
407 z_off64_t ZEXPORT gztell64(file)
412 /* get internal structure and check integrity */
415 state = (gz_statep)file;
416 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
419 /* return position */
420 return state->x.pos + (state->seek ? state->skip : 0);
423 /* -- see zlib.h -- */
424 z_off_t ZEXPORT gztell(file)
429 ret = gztell64(file);
430 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
433 /* -- see zlib.h -- */
434 z_off64_t ZEXPORT gzoffset64(file)
440 /* get internal structure and check integrity */
443 state = (gz_statep)file;
444 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
447 /* compute and return effective offset in file */
448 offset = LSEEK(state->fd, 0, SEEK_CUR);
451 if (state->mode == GZ_READ) /* reading */
452 offset -= state->strm.avail_in; /* don't count buffered input */
456 /* -- see zlib.h -- */
457 z_off_t ZEXPORT gzoffset(file)
462 ret = gzoffset64(file);
463 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
466 /* -- see zlib.h -- */
467 int ZEXPORT gzeof(file)
472 /* get internal structure and check integrity */
475 state = (gz_statep)file;
476 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
479 /* return end-of-file state */
480 return state->mode == GZ_READ ? state->past : 0;
483 /* -- see zlib.h -- */
484 const char * ZEXPORT gzerror(file, errnum)
490 /* get internal structure and check integrity */
493 state = (gz_statep)file;
494 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
497 /* return error information */
499 *errnum = state->err;
500 return state->msg == NULL ? "" : state->msg;
503 /* -- see zlib.h -- */
504 void ZEXPORT gzclearerr(file)
509 /* get internal structure and check integrity */
512 state = (gz_statep)file;
513 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
516 /* clear error and end-of-file */
517 if (state->mode == GZ_READ) {
521 gz_error(state, Z_OK, NULL);
524 /* Create an error message in allocated memory and set state->err and
525 state->msg accordingly. Free any previous error message already there. Do
526 not try to free or allocate space if the error is Z_MEM_ERROR (out of
527 memory). Simply save the error message as a static string. If there is an
528 allocation failure constructing the error message, then convert the error to
530 void ZLIB_INTERNAL gz_error(state, err, msg)
535 /* free previously allocated message and clear */
536 if (state->msg != NULL) {
537 if (state->err != Z_MEM_ERROR)
542 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
543 if (err != Z_OK && err != Z_BUF_ERROR)
546 /* set error code, and if no message, then done */
551 /* for an out of memory error, save as static string */
552 if (err == Z_MEM_ERROR) {
553 state->msg = (char *)msg;
557 /* construct error message with path */
558 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
559 state->err = Z_MEM_ERROR;
560 state->msg = (char *)"out of memory";
563 strcpy(state->msg, state->path);
564 strcat(state->msg, ": ");
565 strcat(state->msg, msg);
570 /* portably return maximum value for an int (when limits.h presumed not
571 available) -- we need to do this to cover cases where 2's complement not
572 used, since C standard permits 1's complement and sign-bit representations,
573 otherwise we could just use ((unsigned)-1) >> 1 */
574 unsigned ZLIB_INTERNAL gz_intmax()