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 */
83 state->seek = 0; /* no seek request pending */
84 gz_error(state, Z_OK, NULL); /* clear error */
85 state->x.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)
101 /* allocate gzFile structure to return */
102 state = malloc(sizeof(gz_state));
105 state->size = 0; /* no buffers allocated yet */
106 state->want = GZBUFSIZE; /* requested buffer size */
107 state->msg = NULL; /* no error message yet */
110 state->mode = GZ_NONE;
111 state->level = Z_DEFAULT_COMPRESSION;
112 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;
148 default: /* could consider as an error, but just ignore */
154 /* must provide an "r", "w", or "a" */
155 if (state->mode == GZ_NONE) {
160 /* can't force transparent read */
161 if (state->mode == GZ_READ) {
166 state->direct = 1; /* for empty file */
169 /* save the path name for error messages */
170 state->path = malloc(strlen(path) + 1);
171 if (state->path == NULL) {
175 strcpy(state->path, path);
177 /* open the file with the appropriate mode (or just use fd) */
178 state->fd = fd != -1 ? fd :
186 (state->mode == GZ_READ ?
188 (O_WRONLY | O_CREAT | (
189 state->mode == GZ_WRITE ?
193 if (state->fd == -1) {
198 if (state->mode == GZ_APPEND)
199 state->mode = GZ_WRITE; /* simplify later checks */
201 /* save the current position for rewinding (only if reading) */
202 if (state->mode == GZ_READ) {
203 state->start = LSEEK(state->fd, 0, SEEK_CUR);
204 if (state->start == -1) state->start = 0;
207 /* initialize stream */
211 return (gzFile)state;
214 /* -- see zlib.h -- */
215 gzFile ZEXPORT gzopen(path, mode)
219 return gz_open(path, -1, mode);
222 /* -- see zlib.h -- */
223 gzFile ZEXPORT gzopen64(path, mode)
227 return gz_open(path, -1, mode);
230 /* -- see zlib.h -- */
231 gzFile ZEXPORT gzdopen(fd, mode)
235 char *path; /* identifier for error messages */
238 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
240 sprintf(path, "<fd:%d>", fd); /* for debugging */
241 gz = gz_open(path, fd, mode);
246 /* -- see zlib.h -- */
247 int ZEXPORT gzbuffer(file, size)
253 /* get internal structure and check integrity */
256 state = (gz_statep)file;
257 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
260 /* make sure we haven't already allocated memory */
261 if (state->size != 0)
264 /* check and set requested size */
266 size = 2; /* need two bytes to check magic header */
271 /* -- see zlib.h -- */
272 int ZEXPORT gzrewind(file)
277 /* get internal structure */
280 state = (gz_statep)file;
282 /* check that we're reading and that there's no error */
283 if (state->mode != GZ_READ ||
284 (state->err != Z_OK && state->err != Z_BUF_ERROR))
287 /* back up and start over */
288 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
294 /* -- see zlib.h -- */
295 z_off64_t ZEXPORT gzseek64(file, offset, whence)
304 /* get internal structure and check integrity */
307 state = (gz_statep)file;
308 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
311 /* check that there's no error */
312 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
315 /* can only seek from start or relative to current position */
316 if (whence != SEEK_SET && whence != SEEK_CUR)
319 /* normalize offset to a SEEK_CUR specification */
320 if (whence == SEEK_SET)
321 offset -= state->x.pos;
322 else if (state->seek)
323 offset += state->skip;
326 /* if within raw area while reading, just go there */
327 if (state->mode == GZ_READ && state->how == COPY &&
328 state->x.pos + offset >= 0) {
329 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
335 gz_error(state, Z_OK, NULL);
336 state->strm.avail_in = 0;
337 state->x.pos += offset;
341 /* calculate skip amount, rewinding if needed for back seek when reading */
343 if (state->mode != GZ_READ) /* writing -- can't go backwards */
345 offset += state->x.pos;
346 if (offset < 0) /* before start of file! */
348 if (gzrewind(file) == -1) /* rewind, then skip to offset */
352 /* if reading, skip what's in output buffer (one less gzgetc() check) */
353 if (state->mode == GZ_READ) {
354 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
355 (unsigned)offset : state->x.have;
362 /* request skip (if not zero) */
365 state->skip = offset;
367 return state->x.pos + offset;
370 /* -- see zlib.h -- */
371 z_off_t ZEXPORT gzseek(file, offset, whence)
378 ret = gzseek64(file, (z_off64_t)offset, whence);
379 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
382 /* -- see zlib.h -- */
383 z_off64_t ZEXPORT gztell64(file)
388 /* get internal structure and check integrity */
391 state = (gz_statep)file;
392 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
395 /* return position */
396 return state->x.pos + (state->seek ? state->skip : 0);
399 /* -- see zlib.h -- */
400 z_off_t ZEXPORT gztell(file)
405 ret = gztell64(file);
406 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
409 /* -- see zlib.h -- */
410 z_off64_t ZEXPORT gzoffset64(file)
416 /* get internal structure and check integrity */
419 state = (gz_statep)file;
420 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
423 /* compute and return effective offset in file */
424 offset = LSEEK(state->fd, 0, SEEK_CUR);
427 if (state->mode == GZ_READ) /* reading */
428 offset -= state->strm.avail_in; /* don't count buffered input */
432 /* -- see zlib.h -- */
433 z_off_t ZEXPORT gzoffset(file)
438 ret = gzoffset64(file);
439 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
442 /* -- see zlib.h -- */
443 int ZEXPORT gzeof(file)
448 /* get internal structure and check integrity */
451 state = (gz_statep)file;
452 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
455 /* return end-of-file state */
456 return state->mode == GZ_READ ?
457 (state->eof && state->strm.avail_in == 0 && state->x.have == 0) : 0;
460 /* -- see zlib.h -- */
461 const char * ZEXPORT gzerror(file, errnum)
467 /* get internal structure and check integrity */
470 state = (gz_statep)file;
471 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
474 /* return error information */
476 *errnum = state->err;
477 return state->msg == NULL ? "" : state->msg;
480 /* -- see zlib.h -- */
481 void ZEXPORT gzclearerr(file)
486 /* get internal structure and check integrity */
489 state = (gz_statep)file;
490 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
493 /* clear error and end-of-file */
494 if (state->mode == GZ_READ)
496 gz_error(state, Z_OK, NULL);
499 /* Create an error message in allocated memory and set state->err and
500 state->msg accordingly. Free any previous error message already there. Do
501 not try to free or allocate space if the error is Z_MEM_ERROR (out of
502 memory). Simply save the error message as a static string. If there is an
503 allocation failure constructing the error message, then convert the error to
505 void ZLIB_INTERNAL gz_error(state, err, msg)
510 /* free previously allocated message and clear */
511 if (state->msg != NULL) {
512 if (state->err != Z_MEM_ERROR)
517 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
518 if (err != Z_OK && err != Z_BUF_ERROR)
521 /* set error code, and if no message, then done */
526 /* for an out of memory error, save as static string */
527 if (err == Z_MEM_ERROR) {
528 state->msg = (char *)msg;
532 /* construct error message with path */
533 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
534 state->err = Z_MEM_ERROR;
535 state->msg = (char *)"out of memory";
538 strcpy(state->msg, state->path);
539 strcat(state->msg, ": ");
540 strcat(state->msg, msg);
545 /* portably return maximum value for an int (when limits.h presumed not
546 available) -- we need to do this to cover cases where 2's complement not
547 used, since C standard permits 1's complement and sign-bit representations,
548 otherwise we could just use ((unsigned)-1) >> 1 */
549 unsigned ZLIB_INTERNAL gz_intmax()