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
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->how = LOOK; /* look for gzip header */
82 state->direct = 1; /* default for empty file */
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)
102 /* allocate gzFile structure to return */
103 state = malloc(sizeof(gz_state));
106 state->size = 0; /* no buffers allocated yet */
107 state->want = GZBUFSIZE; /* requested buffer size */
108 state->msg = NULL; /* no error message yet */
111 state->mode = GZ_NONE;
112 state->level = Z_DEFAULT_COMPRESSION;
113 state->strategy = Z_DEFAULT_STRATEGY;
115 if (*mode >= '0' && *mode <= '9')
116 state->level = *mode - '0';
120 state->mode = GZ_READ;
122 #ifndef NO_GZCOMPRESS
124 state->mode = GZ_WRITE;
127 state->mode = GZ_APPEND;
130 case '+': /* can't read and write at the same time */
133 case 'b': /* ignore -- will request binary anyway */
136 state->strategy = Z_FILTERED;
139 state->strategy = Z_HUFFMAN_ONLY;
142 state->strategy = Z_RLE;
145 state->strategy = Z_FIXED;
146 default: /* could consider as an error, but just ignore */
152 /* must provide an "r", "w", or "a" */
153 if (state->mode == GZ_NONE) {
158 /* save the path name for error messages */
159 state->path = malloc(strlen(path) + 1);
160 if (state->path == NULL) {
164 strcpy(state->path, path);
166 /* open the file with the appropriate mode (or just use fd) */
167 state->fd = fd != -1 ? fd :
175 (state->mode == GZ_READ ?
177 (O_WRONLY | O_CREAT | (
178 state->mode == GZ_WRITE ?
182 if (state->fd == -1) {
187 if (state->mode == GZ_APPEND)
188 state->mode = GZ_WRITE; /* simplify later checks */
190 /* save the current position for rewinding (only if reading) */
191 if (state->mode == GZ_READ) {
192 state->start = LSEEK(state->fd, 0, SEEK_CUR);
193 if (state->start == -1) state->start = 0;
196 /* initialize stream */
200 return (gzFile)state;
203 /* -- see zlib.h -- */
204 gzFile ZEXPORT gzopen(path, mode)
208 return gz_open(path, -1, mode);
211 /* -- see zlib.h -- */
212 gzFile ZEXPORT gzopen64(path, mode)
216 return gz_open(path, -1, mode);
219 /* -- see zlib.h -- */
220 gzFile ZEXPORT gzdopen(fd, mode)
224 char *path; /* identifier for error messages */
227 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
229 sprintf(path, "<fd:%d>", fd); /* for debugging */
230 gz = gz_open(path, fd, mode);
235 /* -- see zlib.h -- */
236 int ZEXPORT gzbuffer(file, size)
242 /* get internal structure and check integrity */
245 state = (gz_statep)file;
246 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
249 /* make sure we haven't already allocated memory */
250 if (state->size != 0)
253 /* check and set requested size */
255 size = 2; /* need two bytes to check magic header */
260 /* -- see zlib.h -- */
261 int ZEXPORT gzrewind(file)
266 /* get internal structure */
269 state = (gz_statep)file;
271 /* check that we're reading and that there's no error */
272 if (state->mode != GZ_READ ||
273 (state->err != Z_OK && state->err != Z_BUF_ERROR))
276 /* back up and start over */
277 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
283 /* -- see zlib.h -- */
284 z_off64_t ZEXPORT gzseek64(file, offset, whence)
293 /* get internal structure and check integrity */
296 state = (gz_statep)file;
297 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
300 /* check that there's no error */
301 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
304 /* can only seek from start or relative to current position */
305 if (whence != SEEK_SET && whence != SEEK_CUR)
308 /* normalize offset to a SEEK_CUR specification */
309 if (whence == SEEK_SET)
310 offset -= state->x.pos;
311 else if (state->seek)
312 offset += state->skip;
315 /* if within raw area while reading, just go there */
316 if (state->mode == GZ_READ && state->how == COPY &&
317 state->x.pos + offset >= 0) {
318 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
324 gz_error(state, Z_OK, NULL);
325 state->strm.avail_in = 0;
326 state->x.pos += offset;
330 /* calculate skip amount, rewinding if needed for back seek when reading */
332 if (state->mode != GZ_READ) /* writing -- can't go backwards */
334 offset += state->x.pos;
335 if (offset < 0) /* before start of file! */
337 if (gzrewind(file) == -1) /* rewind, then skip to offset */
341 /* if reading, skip what's in output buffer (one less gzgetc() check) */
342 if (state->mode == GZ_READ) {
343 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
344 (unsigned)offset : state->x.have;
351 /* request skip (if not zero) */
354 state->skip = offset;
356 return state->x.pos + offset;
359 /* -- see zlib.h -- */
360 z_off_t ZEXPORT gzseek(file, offset, whence)
367 ret = gzseek64(file, (z_off64_t)offset, whence);
368 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
371 /* -- see zlib.h -- */
372 z_off64_t ZEXPORT gztell64(file)
377 /* get internal structure and check integrity */
380 state = (gz_statep)file;
381 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
384 /* return position */
385 return state->x.pos + (state->seek ? state->skip : 0);
388 /* -- see zlib.h -- */
389 z_off_t ZEXPORT gztell(file)
394 ret = gztell64(file);
395 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
398 /* -- see zlib.h -- */
399 z_off64_t ZEXPORT gzoffset64(file)
405 /* get internal structure and check integrity */
408 state = (gz_statep)file;
409 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
412 /* compute and return effective offset in file */
413 offset = LSEEK(state->fd, 0, SEEK_CUR);
416 if (state->mode == GZ_READ) /* reading */
417 offset -= state->strm.avail_in; /* don't count buffered input */
421 /* -- see zlib.h -- */
422 z_off_t ZEXPORT gzoffset(file)
427 ret = gzoffset64(file);
428 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
431 /* -- see zlib.h -- */
432 int ZEXPORT gzeof(file)
437 /* get internal structure and check integrity */
440 state = (gz_statep)file;
441 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
444 /* return end-of-file state */
445 return state->mode == GZ_READ ?
446 (state->eof && state->strm.avail_in == 0 && state->x.have == 0) : 0;
449 /* -- see zlib.h -- */
450 const char * ZEXPORT gzerror(file, errnum)
456 /* get internal structure and check integrity */
459 state = (gz_statep)file;
460 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
463 /* return error information */
465 *errnum = state->err;
466 return state->msg == NULL ? "" : state->msg;
469 /* -- see zlib.h -- */
470 void ZEXPORT gzclearerr(file)
475 /* get internal structure and check integrity */
478 state = (gz_statep)file;
479 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
482 /* clear error and end-of-file */
483 if (state->mode == GZ_READ)
485 gz_error(state, Z_OK, NULL);
488 /* Create an error message in allocated memory and set state->err and
489 state->msg accordingly. Free any previous error message already there. Do
490 not try to free or allocate space if the error is Z_MEM_ERROR (out of
491 memory). Simply save the error message as a static string. If there is an
492 allocation failure constructing the error message, then convert the error to
494 void ZLIB_INTERNAL gz_error(state, err, msg)
499 /* free previously allocated message and clear */
500 if (state->msg != NULL) {
501 if (state->err != Z_MEM_ERROR)
506 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
507 if (err != Z_OK && err != Z_BUF_ERROR)
510 /* set error code, and if no message, then done */
515 /* for an out of memory error, save as static string */
516 if (err == Z_MEM_ERROR) {
517 state->msg = (char *)msg;
521 /* construct error message with path */
522 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
523 state->err = Z_MEM_ERROR;
524 state->msg = (char *)"out of memory";
527 strcpy(state->msg, state->path);
528 strcat(state->msg, ": ");
529 strcat(state->msg, msg);
534 /* portably return maximum value for an int (when limits.h presumed not
535 available) -- we need to do this to cover cases where 2's complement not
536 used, since C standard permits 1's complement and sign-bit representations,
537 otherwise we could just use ((unsigned)-1) >> 1 */
538 unsigned ZLIB_INTERNAL gz_intmax()