1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004, 2010, 2011, 2012, 2013 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
18 /* Forward declarations */
19 z_off_t ZEXPORT gzoffset(gzFile file);
20 int ZEXPORT gzbuffer(gzFile file, unsigned size);
23 local void gz_reset OF((gz_statep));
24 local gzFile gz_open OF((const void *, int, const char *));
28 /* Map the Windows error number in ERROR to a locale-dependent error message
29 string and return a pointer to it. Typically, the values for ERROR come
32 The string pointed to shall not be modified by the application, but may be
33 overwritten by a subsequent call to gz_strwinerror
35 The gz_strwinerror function does not change the current setting of
37 char ZLIB_INTERNAL *gz_strwinerror (error)
40 static char buf[1024];
43 DWORD lasterr = GetLastError();
44 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
45 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
48 0, /* Default language */
53 /* If there is an \r\n appended, zap it. */
55 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
60 if (chars > sizeof (buf) - 1) {
61 chars = sizeof (buf) - 1;
65 wcstombs(buf, msgbuf, chars + 1);
69 sprintf(buf, "unknown win32 error (%ld)", error);
72 SetLastError(lasterr);
78 /* Reset gzip file state */
79 local void gz_reset(gz_statep 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(const void *path, int fd, const char *mode)
110 /* allocate gzFile structure to return */
111 state = (gz_statep)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;
169 default: /* could consider as an error, but just ignore */
175 /* must provide an "r", "w", or "a" */
176 if (state->mode == GZ_NONE) {
181 /* can't force transparent read */
182 if (state->mode == GZ_READ) {
187 state->direct = 1; /* for empty file */
190 /* save the path name for error messages */
193 len = wcstombs(NULL, (const wchar_t*)path, 0);
194 if (len == (size_t)-1)
199 len = strlen((const char *)path);
200 state->path = (char *)malloc(len + 1);
201 if (state->path == NULL) {
208 wcstombs(state->path, (const wchar_t*)path, len + 1);
213 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
214 snprintf(state->path, len + 1, "%s", (const char *)path);
216 strlcpy(state->path, path, sizeof(state->path));
219 /* compute the flags for open() */
228 (cloexec ? O_CLOEXEC : 0) |
230 (state->mode == GZ_READ ?
232 (O_WRONLY | O_CREAT |
234 (exclusive ? O_EXCL : 0) |
236 (state->mode == GZ_WRITE ?
240 /* open the file with the appropriate flags (or just use fd) */
241 state->fd = fd > -1 ? fd : (
243 fd == -2 ? _wopen((const wchar_t*)path, oflag, 0666) :
245 open((const char *)path, oflag, 0666));
246 if (state->fd == -1) {
251 if (state->mode == GZ_APPEND)
252 state->mode = GZ_WRITE; /* simplify later checks */
254 /* save the current position for rewinding (only if reading) */
255 if (state->mode == GZ_READ) {
256 state->start = LSEEK(state->fd, 0, SEEK_CUR);
257 if (state->start == -1) state->start = 0;
260 /* initialize stream */
264 return (gzFile)state;
267 /* -- see zlib.h -- */
268 gzFile ZEXPORT gzopen(const char *path, const char *mode)
270 return gz_open(path, -1, mode);
273 /* -- see zlib.h -- */
274 gzFile ZEXPORT gzopen64(const char *path, const char *mode)
276 return gz_open(path, -1, mode);
279 /* -- see zlib.h -- */
280 gzFile ZEXPORT gzdopen(int fd, const char *mode)
282 char *path; /* identifier for error messages */
285 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
287 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
288 snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */
290 sprintf(path, "<fd:%d>", fd); /* for debugging */
292 gz = gz_open(path, fd, mode);
297 /* -- see zlib.h -- */
299 gzFile ZEXPORT gzopen_w(const wchar_t *path, const char *mode)
301 return gz_open(path, -2, mode);
305 /* -- see zlib.h -- */
306 int ZEXPORT gzbuffer(gzFile file, unsigned 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(gzFile file)
333 /* get internal structure */
336 state = (gz_statep)file;
338 /* check that we're reading and that there's no error */
339 if (state->mode != GZ_READ ||
340 (state->err != Z_OK && state->err != Z_BUF_ERROR))
343 /* back up and start over */
344 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
350 /* -- see zlib.h -- */
351 z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence)
357 /* get internal structure and check integrity */
360 state = (gz_statep)file;
361 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
364 /* check that there's no error */
365 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
368 /* can only seek from start or relative to current position */
369 if (whence != SEEK_SET && whence != SEEK_CUR)
372 /* normalize offset to a SEEK_CUR specification */
373 if (whence == SEEK_SET)
374 offset -= state->x.pos;
375 else if (state->seek)
376 offset += state->skip;
379 /* if within raw area while reading, just go there */
380 if (state->mode == GZ_READ && state->how == MODE_COPY &&
381 state->x.pos + offset >= 0) {
382 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
389 gz_error(state, Z_OK, NULL);
390 state->strm.avail_in = 0;
391 state->x.pos += offset;
395 /* calculate skip amount, rewinding if needed for back seek when reading */
397 if (state->mode != GZ_READ) /* writing -- can't go backwards */
399 offset += state->x.pos;
400 if (offset < 0) /* before start of file! */
402 if (gzrewind(file) == -1) /* rewind, then skip to offset */
406 /* if reading, skip what's in output buffer (one less gzgetc() check) */
407 if (state->mode == GZ_READ) {
408 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
409 (unsigned)offset : state->x.have;
416 /* request skip (if not zero) */
419 state->skip = offset;
421 return state->x.pos + offset;
424 /* -- see zlib.h -- */
425 z_off_t ZEXPORT gzseek(gzFile file, z_off_t offset, int whence)
429 ret = gzseek64(file, (z_off64_t)offset, whence);
430 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
433 /* -- see zlib.h -- */
434 z_off64_t ZEXPORT gztell64(gzFile file)
438 /* get internal structure and check integrity */
441 state = (gz_statep)file;
442 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
445 /* return position */
446 return state->x.pos + (state->seek ? state->skip : 0);
449 /* -- see zlib.h -- */
450 z_off_t ZEXPORT gztell(gzFile file)
454 ret = gztell64(file);
455 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
458 /* -- see zlib.h -- */
459 z_off64_t ZEXPORT gzoffset64(gzFile file)
464 /* get internal structure and check integrity */
467 state = (gz_statep)file;
468 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
471 /* compute and return effective offset in file */
472 offset = LSEEK(state->fd, 0, SEEK_CUR);
475 if (state->mode == GZ_READ) /* reading */
476 offset -= state->strm.avail_in; /* don't count buffered input */
480 /* -- see zlib.h -- */
481 z_off_t ZEXPORT gzoffset(gzFile file)
483 z_off64_t ret = gzoffset64(file);
484 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
487 /* -- see zlib.h -- */
488 int ZEXPORT gzeof(gzFile file)
492 /* get internal structure and check integrity */
495 state = (gz_statep)file;
496 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
499 /* return end-of-file state */
500 return state->mode == GZ_READ ? state->past : 0;
503 /* -- see zlib.h -- */
504 const char * ZEXPORT gzerror(gzFile file, int *errnum)
508 /* get internal structure and check integrity */
511 state = (gz_statep)file;
512 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
515 /* return error information */
517 *errnum = state->err;
518 return state->err == Z_MEM_ERROR ? "out of memory" :
519 (state->msg == NULL ? "" : state->msg);
522 /* -- see zlib.h -- */
523 void ZEXPORT gzclearerr(gzFile file)
527 /* get internal structure and check integrity */
530 state = (gz_statep)file;
531 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
534 /* clear error and end-of-file */
535 if (state->mode == GZ_READ) {
539 gz_error(state, Z_OK, NULL);
542 /* Create an error message in allocated memory and set state->err and
543 state->msg accordingly. Free any previous error message already there. Do
544 not try to free or allocate space if the error is Z_MEM_ERROR (out of
545 memory). Simply save the error message as a static string. If there is an
546 allocation failure constructing the error message, then convert the error to
548 void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
550 /* free previously allocated message and clear */
551 if (state->msg != NULL) {
552 if (state->err != Z_MEM_ERROR)
557 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
558 if (err != Z_OK && err != Z_BUF_ERROR)
561 /* set error code, and if no message, then done */
566 /* for an out of memory error, return literal string when requested */
567 if (err == Z_MEM_ERROR)
570 /* construct error message with path */
571 if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
573 state->err = Z_MEM_ERROR;
576 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
577 snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
578 "%s%s%s", state->path, ": ", msg);
580 strlcpy(state->msg, state->path, sizeof(state->msg));
581 strlcat(state->msg, ": ", sizeof(state->msg));
582 strlcat(state->msg, msg, sizeof(state->msg));
588 /* portably return maximum value for an int (when limits.h presumed not
589 available) -- we need to do this to cover cases where 2's complement not
590 used, since C standard permits 1's complement and sign-bit representations,
591 otherwise we could just use ((unsigned)-1) >> 1 */
592 unsigned ZLIB_INTERNAL gz_intmax()