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)
109 /* allocate gzFile structure to return */
110 state = malloc(sizeof(gz_state));
113 state->size = 0; /* no buffers allocated yet */
114 state->want = GZBUFSIZE; /* requested buffer size */
115 state->msg = NULL; /* no error message yet */
118 state->mode = GZ_NONE;
119 state->level = Z_DEFAULT_COMPRESSION;
120 state->strategy = Z_DEFAULT_STRATEGY;
123 if (*mode >= '0' && *mode <= '9')
124 state->level = *mode - '0';
128 state->mode = GZ_READ;
130 #ifndef NO_GZCOMPRESS
132 state->mode = GZ_WRITE;
135 state->mode = GZ_APPEND;
138 case '+': /* can't read and write at the same time */
141 case 'b': /* ignore -- will request binary anyway */
154 state->strategy = Z_FILTERED;
157 state->strategy = Z_HUFFMAN_ONLY;
160 state->strategy = Z_RLE;
163 state->strategy = Z_FIXED;
166 default: /* could consider as an error, but just ignore */
172 /* must provide an "r", "w", or "a" */
173 if (state->mode == GZ_NONE) {
178 /* can't force transparent read */
179 if (state->mode == GZ_READ) {
184 state->direct = 1; /* for empty file */
187 /* save the path name for error messages */
188 # define WPATH "<widepath>"
189 state->path = malloc(strlen(fd == -2 ? WPATH : path) + 1);
190 if (state->path == NULL) {
194 strcpy(state->path, fd == -2 ? WPATH : path);
196 /* compute the flags for open() */
205 (cloexec ? O_CLOEXEC : 0) |
207 (state->mode == GZ_READ ?
209 (O_WRONLY | O_CREAT |
211 (exclusive ? O_EXCL : 0) |
213 (state->mode == GZ_WRITE ?
217 /* open the file with the appropriate flags (or just use fd) */
218 state->fd = fd > -1 ? fd : (
220 fd == -2 ? _wopen(path, oflag, 0666) :
222 open(path, oflag, 0666));
223 if (state->fd == -1) {
228 if (state->mode == GZ_APPEND)
229 state->mode = GZ_WRITE; /* simplify later checks */
231 /* save the current position for rewinding (only if reading) */
232 if (state->mode == GZ_READ) {
233 state->start = LSEEK(state->fd, 0, SEEK_CUR);
234 if (state->start == -1) state->start = 0;
237 /* initialize stream */
241 return (gzFile)state;
244 /* -- see zlib.h -- */
245 gzFile ZEXPORT gzopen(path, mode)
249 return gz_open(path, -1, mode);
252 /* -- see zlib.h -- */
253 gzFile ZEXPORT gzopen64(path, mode)
257 return gz_open(path, -1, mode);
260 /* -- see zlib.h -- */
261 gzFile ZEXPORT gzdopen(fd, mode)
265 char *path; /* identifier for error messages */
268 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
270 sprintf(path, "<fd:%d>", fd); /* for debugging */
271 gz = gz_open(path, fd, mode);
276 /* -- see zlib.h -- */
278 gzFile ZEXPORT gzopen_w(path, mode)
282 return gz_open(path, -2, mode);
286 /* -- see zlib.h -- */
287 int ZEXPORT gzbuffer(file, size)
293 /* get internal structure and check integrity */
296 state = (gz_statep)file;
297 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
300 /* make sure we haven't already allocated memory */
301 if (state->size != 0)
304 /* check and set requested size */
306 size = 2; /* need two bytes to check magic header */
311 /* -- see zlib.h -- */
312 int ZEXPORT gzrewind(file)
317 /* get internal structure */
320 state = (gz_statep)file;
322 /* check that we're reading and that there's no error */
323 if (state->mode != GZ_READ ||
324 (state->err != Z_OK && state->err != Z_BUF_ERROR))
327 /* back up and start over */
328 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
334 /* -- see zlib.h -- */
335 z_off64_t ZEXPORT gzseek64(file, offset, whence)
344 /* get internal structure and check integrity */
347 state = (gz_statep)file;
348 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
351 /* check that there's no error */
352 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
355 /* can only seek from start or relative to current position */
356 if (whence != SEEK_SET && whence != SEEK_CUR)
359 /* normalize offset to a SEEK_CUR specification */
360 if (whence == SEEK_SET)
361 offset -= state->x.pos;
362 else if (state->seek)
363 offset += state->skip;
366 /* if within raw area while reading, just go there */
367 if (state->mode == GZ_READ && state->how == COPY &&
368 state->x.pos + offset >= 0) {
369 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
376 gz_error(state, Z_OK, NULL);
377 state->strm.avail_in = 0;
378 state->x.pos += offset;
382 /* calculate skip amount, rewinding if needed for back seek when reading */
384 if (state->mode != GZ_READ) /* writing -- can't go backwards */
386 offset += state->x.pos;
387 if (offset < 0) /* before start of file! */
389 if (gzrewind(file) == -1) /* rewind, then skip to offset */
393 /* if reading, skip what's in output buffer (one less gzgetc() check) */
394 if (state->mode == GZ_READ) {
395 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
396 (unsigned)offset : state->x.have;
403 /* request skip (if not zero) */
406 state->skip = offset;
408 return state->x.pos + offset;
411 /* -- see zlib.h -- */
412 z_off_t ZEXPORT gzseek(file, offset, whence)
419 ret = gzseek64(file, (z_off64_t)offset, whence);
420 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
423 /* -- see zlib.h -- */
424 z_off64_t ZEXPORT gztell64(file)
429 /* get internal structure and check integrity */
432 state = (gz_statep)file;
433 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
436 /* return position */
437 return state->x.pos + (state->seek ? state->skip : 0);
440 /* -- see zlib.h -- */
441 z_off_t ZEXPORT gztell(file)
446 ret = gztell64(file);
447 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
450 /* -- see zlib.h -- */
451 z_off64_t ZEXPORT gzoffset64(file)
457 /* get internal structure and check integrity */
460 state = (gz_statep)file;
461 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
464 /* compute and return effective offset in file */
465 offset = LSEEK(state->fd, 0, SEEK_CUR);
468 if (state->mode == GZ_READ) /* reading */
469 offset -= state->strm.avail_in; /* don't count buffered input */
473 /* -- see zlib.h -- */
474 z_off_t ZEXPORT gzoffset(file)
479 ret = gzoffset64(file);
480 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
483 /* -- see zlib.h -- */
484 int ZEXPORT gzeof(file)
489 /* get internal structure and check integrity */
492 state = (gz_statep)file;
493 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
496 /* return end-of-file state */
497 return state->mode == GZ_READ ? state->past : 0;
500 /* -- see zlib.h -- */
501 const char * ZEXPORT gzerror(file, errnum)
507 /* get internal structure and check integrity */
510 state = (gz_statep)file;
511 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
514 /* return error information */
516 *errnum = state->err;
517 return state->msg == NULL ? "" : state->msg;
520 /* -- see zlib.h -- */
521 void ZEXPORT gzclearerr(file)
526 /* get internal structure and check integrity */
529 state = (gz_statep)file;
530 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
533 /* clear error and end-of-file */
534 if (state->mode == GZ_READ) {
538 gz_error(state, Z_OK, NULL);
541 /* Create an error message in allocated memory and set state->err and
542 state->msg accordingly. Free any previous error message already there. Do
543 not try to free or allocate space if the error is Z_MEM_ERROR (out of
544 memory). Simply save the error message as a static string. If there is an
545 allocation failure constructing the error message, then convert the error to
547 void ZLIB_INTERNAL gz_error(state, err, msg)
552 /* free previously allocated message and clear */
553 if (state->msg != NULL) {
554 if (state->err != Z_MEM_ERROR)
559 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
560 if (err != Z_OK && err != Z_BUF_ERROR)
563 /* set error code, and if no message, then done */
568 /* for an out of memory error, save as static string */
569 if (err == Z_MEM_ERROR) {
570 state->msg = (char *)msg;
574 /* construct error message with path */
575 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
576 state->err = Z_MEM_ERROR;
577 state->msg = (char *)"out of memory";
580 strcpy(state->msg, state->path);
581 strcat(state->msg, ": ");
582 strcat(state->msg, msg);
587 /* portably return maximum value for an int (when limits.h presumed not
588 available) -- we need to do this to cover cases where 2's complement not
589 used, since C standard permits 1's complement and sign-bit representations,
590 otherwise we could just use ((unsigned)-1) >> 1 */
591 unsigned ZLIB_INTERNAL gz_intmax()