1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004, 2010 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
11 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
12 # define LSEEK lseek64
18 local void gz_reset OF((gz_statep));
19 local gzFile gz_open OF((const char *, int, const char *));
23 /* Map the Windows error number in ERROR to a locale-dependent error message
24 string and return a pointer to it. Typically, the values for ERROR come
27 The string pointed to shall not be modified by the application, but may be
28 overwritten by a subsequent call to gz_strwinerror
30 The gz_strwinerror function does not change the current setting of
32 char ZLIB_INTERNAL *gz_strwinerror (error)
35 static char buf[1024];
38 DWORD lasterr = GetLastError();
39 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
40 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
43 0, /* Default language */
48 /* If there is an \r\n appended, zap it. */
50 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
55 if (chars > sizeof (buf) - 1) {
56 chars = sizeof (buf) - 1;
60 wcstombs(buf, msgbuf, chars + 1);
64 sprintf(buf, "unknown win32 error (%ld)", error);
67 SetLastError(lasterr);
73 /* Reset gzip file state */
74 local void gz_reset(state)
77 if (state->mode == GZ_READ) { /* for reading ... */
78 state->have = 0; /* no output data available */
79 state->eof = 0; /* not at end of file */
80 state->how = LOOK; /* look for gzip header */
81 state->direct = 1; /* default for empty file */
83 state->seek = 0; /* no seek request pending */
84 gz_error(state, Z_OK, NULL); /* clear error */
85 state->pos = 0; /* no uncompressed data yet */
86 state->strm.avail_in = 0; /* no input data yet */
89 /* Open a gzip file either by name or file descriptor. */
90 local gzFile gz_open(path, fd, mode)
97 /* allocate gzFile structure to return */
98 state = malloc(sizeof(gz_state));
101 state->size = 0; /* no buffers allocated yet */
102 state->want = GZBUFSIZE; /* requested buffer size */
103 state->msg = NULL; /* no error message yet */
106 state->mode = GZ_NONE;
107 state->level = Z_DEFAULT_COMPRESSION;
108 state->strategy = Z_DEFAULT_STRATEGY;
110 if (*mode >= '0' && *mode <= '9')
111 state->level = *mode - '0';
115 state->mode = GZ_READ;
117 #ifndef NO_GZCOMPRESS
119 state->mode = GZ_WRITE;
122 state->mode = GZ_APPEND;
125 case '+': /* can't read and write at the same time */
128 case 'b': /* ignore -- will request binary anyway */
131 state->strategy = Z_FILTERED;
134 state->strategy = Z_HUFFMAN_ONLY;
137 state->strategy = Z_RLE;
140 state->strategy = Z_FIXED;
141 default: /* could consider as an error, but just ignore */
147 /* must provide an "r", "w", or "a" */
148 if (state->mode == GZ_NONE) {
153 /* save the path name for error messages */
154 state->path = malloc(strlen(path) + 1);
155 if (state->path == NULL) {
159 strcpy(state->path, path);
161 /* open the file with the appropriate mode (or just use fd) */
162 state->fd = fd != -1 ? fd :
170 (state->mode == GZ_READ ?
172 (O_WRONLY | O_CREAT | (
173 state->mode == GZ_WRITE ?
177 if (state->fd == -1) {
182 if (state->mode == GZ_APPEND)
183 state->mode = GZ_WRITE; /* simplify later checks */
185 /* save the current position for rewinding (only if reading) */
186 if (state->mode == GZ_READ) {
187 state->start = LSEEK(state->fd, 0, SEEK_CUR);
188 if (state->start == -1) state->start = 0;
191 /* initialize stream */
195 return (gzFile)state;
198 /* -- see zlib.h -- */
199 gzFile ZEXPORT gzopen(path, mode)
203 return gz_open(path, -1, mode);
206 /* -- see zlib.h -- */
207 gzFile ZEXPORT gzopen64(path, mode)
211 return gz_open(path, -1, mode);
214 /* -- see zlib.h -- */
215 gzFile ZEXPORT gzdopen(fd, mode)
219 char *path; /* identifier for error messages */
222 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
224 sprintf(path, "<fd:%d>", fd); /* for debugging */
225 gz = gz_open(path, fd, mode);
230 /* -- see zlib.h -- */
231 int ZEXPORT gzbuffer(file, size)
237 /* get internal structure and check integrity */
240 state = (gz_statep)file;
241 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
244 /* make sure we haven't already allocated memory */
245 if (state->size != 0)
248 /* check and set requested size */
255 /* -- see zlib.h -- */
256 int ZEXPORT gzrewind(file)
261 /* get internal structure */
264 state = (gz_statep)file;
266 /* check that we're reading and that there's no error */
267 if (state->mode != GZ_READ || state->err != Z_OK)
270 /* back up and start over */
271 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
277 /* -- see zlib.h -- */
278 z_off64_t ZEXPORT gzseek64(file, offset, whence)
287 /* get internal structure and check integrity */
290 state = (gz_statep)file;
291 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
294 /* check that there's no error */
295 if (state->err != Z_OK)
298 /* can only seek from start or relative to current position */
299 if (whence != SEEK_SET && whence != SEEK_CUR)
302 /* normalize offset to a SEEK_CUR specification */
303 if (whence == SEEK_SET)
304 offset -= state->pos;
305 else if (state->seek)
306 offset += state->skip;
309 /* if within raw area while reading, just go there */
310 if (state->mode == GZ_READ && state->how == COPY &&
311 state->pos + offset >= state->raw) {
312 ret = LSEEK(state->fd, offset - state->have, SEEK_CUR);
318 gz_error(state, Z_OK, NULL);
319 state->strm.avail_in = 0;
320 state->pos += offset;
324 /* calculate skip amount, rewinding if needed for back seek when reading */
326 if (state->mode != GZ_READ) /* writing -- can't go backwards */
328 offset += state->pos;
329 if (offset < 0) /* before start of file! */
331 if (gzrewind(file) == -1) /* rewind, then skip to offset */
335 /* if reading, skip what's in output buffer (one less gzgetc() check) */
336 if (state->mode == GZ_READ) {
337 n = GT_OFF(state->have) || (z_off64_t)state->have > offset ?
338 (unsigned)offset : state->have;
345 /* request skip (if not zero) */
348 state->skip = offset;
350 return state->pos + offset;
353 /* -- see zlib.h -- */
354 z_off_t ZEXPORT gzseek(file, offset, whence)
361 ret = gzseek64(file, (z_off64_t)offset, whence);
362 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
365 /* -- see zlib.h -- */
366 z_off64_t ZEXPORT gztell64(file)
371 /* get internal structure and check integrity */
374 state = (gz_statep)file;
375 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
378 /* return position */
379 return state->pos + (state->seek ? state->skip : 0);
382 /* -- see zlib.h -- */
383 z_off_t ZEXPORT gztell(file)
388 ret = gztell64(file);
389 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
392 /* -- see zlib.h -- */
393 z_off64_t ZEXPORT gzoffset64(file)
399 /* get internal structure and check integrity */
402 state = (gz_statep)file;
403 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
406 /* compute and return effective offset in file */
407 offset = LSEEK(state->fd, 0, SEEK_CUR);
410 if (state->mode == GZ_READ) /* reading */
411 offset -= state->strm.avail_in; /* don't count buffered input */
415 /* -- see zlib.h -- */
416 z_off_t ZEXPORT gzoffset(file)
421 ret = gzoffset64(file);
422 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
425 /* -- see zlib.h -- */
426 int ZEXPORT gzeof(file)
431 /* get internal structure and check integrity */
434 state = (gz_statep)file;
435 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
438 /* return end-of-file state */
439 return state->mode == GZ_READ ?
440 (state->eof && state->strm.avail_in == 0 && state->have == 0) : 0;
443 /* -- see zlib.h -- */
444 const char * ZEXPORT gzerror(file, errnum)
450 /* get internal structure and check integrity */
453 state = (gz_statep)file;
454 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
457 /* return error information */
459 *errnum = state->err;
460 return state->msg == NULL ? "" : state->msg;
463 /* -- see zlib.h -- */
464 void ZEXPORT gzclearerr(file)
469 /* get internal structure and check integrity */
472 state = (gz_statep)file;
473 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
476 /* clear error and end-of-file */
477 if (state->mode == GZ_READ)
479 gz_error(state, Z_OK, NULL);
482 /* Create an error message in allocated memory and set state->err and
483 state->msg accordingly. Free any previous error message already there. Do
484 not try to free or allocate space if the error is Z_MEM_ERROR (out of
485 memory). Simply save the error message as a static string. If there is an
486 allocation failure constructing the error message, then convert the error to
488 void ZLIB_INTERNAL gz_error(state, err, msg)
493 /* free previously allocated message and clear */
494 if (state->msg != NULL) {
495 if (state->err != Z_MEM_ERROR)
500 /* set error code, and if no message, then done */
505 /* for an out of memory error, save as static string */
506 if (err == Z_MEM_ERROR) {
507 state->msg = (char *)msg;
511 /* construct error message with path */
512 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
513 state->err = Z_MEM_ERROR;
514 state->msg = (char *)"out of memory";
517 strcpy(state->msg, state->path);
518 strcat(state->msg, ": ");
519 strcat(state->msg, msg);
524 /* portably return maximum value for an int (when limits.h presumed not
525 available) -- we need to do this to cover cases where 2's complement not
526 used, since C standard permits 1's complement and sign-bit representations,
527 otherwise we could just use ((unsigned)-1) >> 1 */
528 unsigned ZLIB_INTERNAL gz_intmax()