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
8 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
15 local void gz_reset OF((gz_statep));
16 local gzFile gz_open OF((const char *, int, const char *));
20 /* Map the Windows error number in ERROR to a locale-dependent error message
21 string and return a pointer to it. Typically, the values for ERROR come
24 The string pointed to shall not be modified by the application, but may be
25 overwritten by a subsequent call to gz_strwinerror
27 The gz_strwinerror function does not change the current setting of
29 char ZEXPORT *gz_strwinerror (error)
32 static char buf[1024];
35 DWORD lasterr = GetLastError();
36 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
37 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
40 0, /* Default language */
45 /* If there is an \r\n appended, zap it. */
47 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
52 if (chars > sizeof (buf) - 1) {
53 chars = sizeof (buf) - 1;
57 wcstombs(buf, msgbuf, chars + 1);
61 sprintf(buf, "unknown win32 error (%ld)", error);
64 SetLastError(lasterr);
70 /* Reset gzip file state */
71 local void gz_reset(state)
74 if (state->mode == GZ_READ) { /* for reading ... */
75 state->have = 0; /* no output data available */
76 state->eof = 0; /* not at end of file */
77 state->how = LOOK; /* look for gzip header */
78 state->direct = 1; /* default for empty file */
80 state->seek = 0; /* no seek request pending */
81 gz_error(state, Z_OK, NULL); /* clear error */
82 state->pos = 0; /* no uncompressed data yet */
83 state->strm.avail_in = 0; /* no input data yet */
86 /* Open a gzip file either by name or file descriptor. */
87 local gzFile gz_open(path, fd, mode)
94 /* allocate gzFile structure to return */
95 state = malloc(sizeof(gz_state));
98 state->size = 0; /* no buffers allocated yet */
99 state->want = GZBUFSIZE; /* requested buffer size */
100 state->msg = NULL; /* no error message yet */
103 state->mode = GZ_NONE;
104 state->level = Z_DEFAULT_COMPRESSION;
105 state->strategy = Z_DEFAULT_STRATEGY;
107 if (*mode >= '0' && *mode <= '9')
108 state->level = *mode - '0';
112 state->mode = GZ_READ;
114 #ifndef NO_GZCOMPRESS
116 state->mode = GZ_WRITE;
119 state->mode = GZ_APPEND;
122 case '+': /* can't read and write at the same time */
125 case 'b': /* ignore -- will request binary anyway */
128 state->strategy = Z_FILTERED;
131 state->strategy = Z_HUFFMAN_ONLY;
134 state->strategy = Z_RLE;
137 state->strategy = Z_FIXED;
138 default: /* could consider as an error, but just ignore */
144 /* must provide an "r", "w", or "a" */
145 if (state->mode == GZ_NONE) {
150 /* save the path name for error messages */
151 state->path = malloc(strlen(path) + 1);
152 if (state->path == NULL) {
156 strcpy(state->path, path);
158 /* open the file with the appropriate mode (or just use fd) */
159 state->fd = fd != -1 ? fd :
167 (state->mode == GZ_READ ?
169 (O_WRONLY | O_CREAT | (
170 state->mode == GZ_WRITE ?
174 if (state->fd == -1) {
179 if (state->mode == GZ_APPEND)
180 state->mode = GZ_WRITE; /* simplify later checks */
182 /* save the current position for rewinding (only if reading) */
183 if (state->mode == GZ_READ) {
184 state->start = LSEEK(state->fd, 0, SEEK_CUR);
185 if (state->start == -1) state->start = 0;
188 /* initialize stream */
192 return (gzFile)state;
195 /* -- see zlib.h -- */
196 gzFile ZEXPORT gzopen(path, mode)
200 return gz_open(path, -1, mode);
203 /* -- see zlib.h -- */
204 gzFile ZEXPORT gzopen64(path, mode)
208 return gz_open(path, -1, mode);
211 /* -- see zlib.h -- */
212 gzFile ZEXPORT gzdopen(fd, mode)
216 char *path; /* identifier for error messages */
219 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
221 sprintf(path, "<fd:%d>", fd); /* for debugging */
222 gz = gz_open(path, fd, mode);
227 /* -- see zlib.h -- */
228 int ZEXPORT gzbuffer(file, size)
234 /* get internal structure and check integrity */
237 state = (gz_statep)file;
238 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
241 /* make sure we haven't already allocated memory */
242 if (state->size != 0)
245 /* check and set requested size */
252 /* -- see zlib.h -- */
253 int ZEXPORT gzrewind(file)
258 /* get internal structure */
261 state = (gz_statep)file;
263 /* check that we're reading and that there's no error */
264 if (state->mode != GZ_READ || state->err != Z_OK)
267 /* back up and start over */
268 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
274 /* -- see zlib.h -- */
275 z_off64_t ZEXPORT gzseek64(file, offset, whence)
284 /* get internal structure and check integrity */
287 state = (gz_statep)file;
288 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
291 /* check that there's no error */
292 if (state->err != Z_OK)
295 /* can only seek from start or relative to current position */
296 if (whence != SEEK_SET && whence != SEEK_CUR)
299 /* normalize offset to a SEEK_CUR specification */
300 if (whence == SEEK_SET)
301 offset -= state->pos;
302 else if (state->seek)
303 offset += state->skip;
306 /* if within raw area while reading, just go there */
307 if (state->mode == GZ_READ && state->how == COPY &&
308 state->pos + offset >= state->raw) {
309 ret = LSEEK(state->fd, offset - state->have, SEEK_CUR);
315 gz_error(state, Z_OK, NULL);
316 state->strm.avail_in = 0;
317 state->pos += offset;
321 /* calculate skip amount, rewinding if needed for back seek when reading */
323 if (state->mode != GZ_READ) /* writing -- can't go backwards */
325 offset += state->pos;
326 if (offset < 0) /* before start of file! */
328 if (gzrewind(file) == -1) /* rewind, then skip to offset */
332 /* if reading, skip what's in output buffer (one less gzgetc() check) */
333 if (state->mode == GZ_READ) {
334 n = GT_OFF(state->have) || (z_off64_t)state->have > offset ?
335 (unsigned)offset : state->have;
342 /* request skip (if not zero) */
345 state->skip = offset;
347 return state->pos + offset;
350 /* -- see zlib.h -- */
351 z_off_t ZEXPORT gzseek(file, offset, whence)
358 ret = gzseek64(file, (z_off64_t)offset, whence);
359 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
362 /* -- see zlib.h -- */
363 z_off64_t ZEXPORT gztell64(file)
368 /* get internal structure and check integrity */
371 state = (gz_statep)file;
372 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
375 /* return position */
376 return state->pos + (state->seek ? state->skip : 0);
379 /* -- see zlib.h -- */
380 z_off_t ZEXPORT gztell(file)
385 ret = gztell64(file);
386 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
389 /* -- see zlib.h -- */
390 z_off64_t ZEXPORT gzoffset64(file)
396 /* get internal structure and check integrity */
399 state = (gz_statep)file;
400 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
403 /* compute and return effective offset in file */
404 offset = LSEEK(state->fd, 0, SEEK_CUR);
407 if (state->mode == GZ_READ) /* reading */
408 offset -= state->strm.avail_in; /* don't count buffered input */
412 /* -- see zlib.h -- */
413 z_off_t ZEXPORT gzoffset(file)
418 ret = gzoffset64(file);
419 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
422 /* -- see zlib.h -- */
423 int ZEXPORT gzeof(file)
428 /* get internal structure and check integrity */
431 state = (gz_statep)file;
432 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
435 /* return end-of-file state */
436 return state->mode == GZ_READ ?
437 (state->eof && state->strm.avail_in == 0 && state->have == 0) : 0;
440 /* -- see zlib.h -- */
441 const char * ZEXPORT gzerror(file, errnum)
447 /* get internal structure and check integrity */
450 state = (gz_statep)file;
451 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
454 /* return error information */
456 *errnum = state->err;
457 return state->msg == NULL ? "" : state->msg;
460 /* -- see zlib.h -- */
461 void ZEXPORT gzclearerr(file)
466 /* get internal structure and check integrity */
469 state = (gz_statep)file;
470 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
473 /* clear error and end-of-file */
474 if (state->mode == GZ_READ)
476 gz_error(state, Z_OK, NULL);
479 /* Create an error message in allocated memory and set state->err and
480 state->msg accordingly. Free any previous error message already there. Do
481 not try to free or allocate space if the error is Z_MEM_ERROR (out of
482 memory). Simply save the error message as a static string. If there is an
483 allocation failure constructing the error message, then convert the error to
485 void ZEXPORT gz_error(state, err, msg)
490 /* free previously allocated message and clear */
491 if (state->msg != NULL) {
492 if (state->err != Z_MEM_ERROR)
497 /* set error code, and if no message, then done */
502 /* for an out of memory error, save as static string */
503 if (err == Z_MEM_ERROR) {
504 state->msg = (char *)msg;
508 /* construct error message with path */
509 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
510 state->err = Z_MEM_ERROR;
511 state->msg = (char *)"out of memory";
514 strcpy(state->msg, state->path);
515 strcat(state->msg, ": ");
516 strcat(state->msg, msg);
521 /* portably return maximum value for an int (when limits.h presumed not
522 available) -- we need to do this to cover cases where 2's complement not
523 used, since C standard permits 1's complement and sign-bit representations,
524 otherwise we could just use ((unsigned)-1) >> 1 */
525 unsigned ZEXPORT gz_intmax()