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 #ifdef _LARGEFILE64_SOURCE
15 local void gz_reset OF((gz_statep));
16 local gzFile gz_open OF((const char *, int, const char *));
18 #if defined UNDER_CE && defined NO_ERRNO_H
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);
68 #endif /* UNDER_CE && NO_ERRNO_H */
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 /* open the file with the appropriate mode (or just use fd) */
151 state->fd = fd != -1 ? fd :
159 (state->mode == GZ_READ ?
161 (O_WRONLY | O_CREAT | (
162 state->mode == GZ_WRITE ?
166 if (state->fd == -1) {
170 if (state->mode == GZ_APPEND)
171 state->mode = GZ_WRITE; /* simplify later checks */
173 /* save the path name for error messages */
174 state->path = malloc(strlen(path) + 1);
175 if (state->path == NULL) {
179 strcpy(state->path, path);
181 /* save the current position for rewinding (only if reading) */
182 if (state->mode == GZ_READ) {
183 state->start = LSEEK(state->fd, 0, SEEK_CUR);
184 if (state->start == -1) state->start = 0;
187 /* initialize stream */
191 return (gzFile)state;
194 /* -- see zlib.h -- */
195 gzFile ZEXPORT gzopen(path, mode)
199 return gz_open(path, -1, mode);
202 /* -- see zlib.h -- */
203 gzFile ZEXPORT gzopen64(path, mode)
207 return gz_open(path, -1, mode);
210 /* -- see zlib.h -- */
211 gzFile ZEXPORT gzdopen(fd, mode)
215 char *path; /* identifier for error messages */
218 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
220 sprintf(path, "<fd:%d>", fd);
221 gz = gz_open(path, fd, mode);
226 /* -- see zlib.h -- */
227 int ZEXPORT gzbuffer(file, size)
233 /* get internal structure and check integrity */
236 state = (gz_statep)file;
237 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
240 /* make sure we haven't already allocated memory */
241 if (state->size != 0)
244 /* check and set requested size */
251 /* -- see zlib.h -- */
252 int ZEXPORT gzrewind(file)
257 /* get internal structure */
260 state = (gz_statep)file;
262 /* check that we're reading and that there's no error */
263 if (state->mode != GZ_READ || state->err != Z_OK)
266 /* back up and start over */
267 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
273 /* -- see zlib.h -- */
274 z_off64_t ZEXPORT gzseek64(file, offset, whence)
283 /* get internal structure and check integrity */
286 state = (gz_statep)file;
287 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
290 /* check that there's no error */
291 if (state->err != Z_OK)
294 /* can only seek from start or relative to current position */
295 if (whence != SEEK_SET && whence != SEEK_CUR)
298 /* normalize offset to a SEEK_CUR specification */
299 if (whence == SEEK_SET)
300 offset -= state->pos;
301 else if (state->seek)
302 offset += state->skip;
305 /* if within raw area while reading, just go there */
306 if (state->mode == GZ_READ && state->how == COPY &&
307 state->pos + offset >= state->raw) {
308 ret = LSEEK(state->fd, offset, SEEK_CUR);
314 gz_error(state, Z_OK, NULL);
315 state->strm.avail_in = 0;
316 state->pos += offset;
320 /* calculate skip amount, rewinding if needed for back seek when reading */
322 if (state->mode != GZ_READ) /* writing -- can't go backwards */
324 offset += state->pos;
325 if (offset < 0) /* before start of file! */
327 if (gzrewind(file) == -1) /* rewind, then skip to offset */
331 /* if reading, skip what's in output buffer (one less gzgetc() check) */
332 if (state->mode == GZ_READ) {
333 n = GT_OFF(state->have) || (z_off64_t)state->have > offset ?
334 (unsigned)offset : state->have;
341 /* request skip (if not zero) */
344 state->skip = offset;
346 return state->pos + offset;
349 /* -- see zlib.h -- */
350 z_off_t ZEXPORT gzseek(file, offset, whence)
357 ret = gzseek64(file, (z_off64_t)offset, whence);
358 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
361 /* -- see zlib.h -- */
362 z_off64_t ZEXPORT gztell64(file)
367 /* get internal structure and check integrity */
370 state = (gz_statep)file;
371 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
374 /* return position */
375 return state->pos + (state->seek ? state->skip : 0);
378 /* -- see zlib.h -- */
379 z_off_t ZEXPORT gztell(file)
384 ret = gztell64(file);
385 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
388 /* -- see zlib.h -- */
389 z_off64_t ZEXPORT gzoffset64(file)
395 /* get internal structure and check integrity */
398 state = (gz_statep)file;
399 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
402 /* compute and return effective offset in file */
403 offset = LSEEK(state->fd, 0, SEEK_CUR);
406 if (state->mode == GZ_READ) /* reading */
407 offset -= state->strm.avail_in; /* don't count buffered input */
411 /* -- see zlib.h -- */
412 z_off_t ZEXPORT gzoffset(file)
417 ret = gzoffset64(file);
418 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
421 /* -- see zlib.h -- */
422 int ZEXPORT gzeof(file)
427 /* get internal structure and check integrity */
430 state = (gz_statep)file;
431 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
434 /* return end-of-file state */
435 return state->mode == GZ_READ ? (state->eof && state->have == 0) : 0;
438 /* -- see zlib.h -- */
439 const char * ZEXPORT gzerror(file, errnum)
445 /* get internal structure and check integrity */
448 state = (gz_statep)file;
449 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
452 /* return error information */
453 *errnum = state->err;
454 return state->msg == NULL ? "" : state->msg;
457 /* -- see zlib.h -- */
458 void ZEXPORT gzclearerr(file)
463 /* get internal structure and check integrity */
466 state = (gz_statep)file;
467 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
470 /* clear error and end-of-file */
471 if (state->mode == GZ_READ)
473 gz_error(state, Z_OK, NULL);
476 /* Create an error message in allocated memory and set state->err and
477 state->msg accordingly. Free any previous error message already there. Do
478 not try to free or allocate space if the error is Z_MEM_ERROR (out of
479 memory). Simply save the error message as a static string. If there is an
480 allocation failure constructing the error message, then convert the error to
482 void ZEXPORT gz_error(state, err, msg)
487 /* free previously allocated message and clear */
488 if (state->msg != NULL) {
489 if (state->err != Z_MEM_ERROR)
494 /* set error code, and if no message, then done */
499 /* for an out of memory error, save as static string */
500 if (err == Z_MEM_ERROR) {
501 state->msg = (char *)msg;
505 /* construct error message with path */
506 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
507 state->err = Z_MEM_ERROR;
508 state->msg = (char *)"out of memory";
511 strcpy(state->msg, state->path);
512 strcat(state->msg, ": ");
513 strcat(state->msg, msg);
518 /* portably return maximum value for an int (when limits.h presumed not
519 available) -- we need to do this to cover cases where 2's complement not
520 used, since C standard permits 1's complement and sign-bit representations,
521 otherwise we could just use ((unsigned)-1) >> 1 */
522 unsigned ZEXPORT gz_intmax()