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