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
8 #if defined(_WIN32) && !defined(__BORLANDC__)
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->past = 0; /* have not read past end yet */
82 state->how = LOOK; /* look for gzip header */
84 state->seek = 0; /* no seek request pending */
85 gz_error(state, Z_OK, NULL); /* clear error */
86 state->x.pos = 0; /* no uncompressed data yet */
87 state->strm.avail_in = 0; /* no input data yet */
90 /* Open a gzip file either by name or file descriptor. */
91 local gzFile gz_open(path, fd, mode)
102 /* allocate gzFile structure to return */
103 state = malloc(sizeof(gz_state));
106 state->size = 0; /* no buffers allocated yet */
107 state->want = GZBUFSIZE; /* requested buffer size */
108 state->msg = NULL; /* no error message yet */
111 state->mode = GZ_NONE;
112 state->level = Z_DEFAULT_COMPRESSION;
113 state->strategy = Z_DEFAULT_STRATEGY;
116 if (*mode >= '0' && *mode <= '9')
117 state->level = *mode - '0';
121 state->mode = GZ_READ;
123 #ifndef NO_GZCOMPRESS
125 state->mode = GZ_WRITE;
128 state->mode = GZ_APPEND;
131 case '+': /* can't read and write at the same time */
134 case 'b': /* ignore -- will request binary anyway */
137 state->strategy = Z_FILTERED;
140 state->strategy = Z_HUFFMAN_ONLY;
143 state->strategy = Z_RLE;
146 state->strategy = Z_FIXED;
149 default: /* could consider as an error, but just ignore */
155 /* must provide an "r", "w", or "a" */
156 if (state->mode == GZ_NONE) {
161 /* can't force transparent read */
162 if (state->mode == GZ_READ) {
167 state->direct = 1; /* for empty file */
170 /* save the path name for error messages */
171 state->path = malloc(strlen(path) + 1);
172 if (state->path == NULL) {
176 strcpy(state->path, path);
178 /* open the file with the appropriate mode (or just use fd) */
179 state->fd = fd != -1 ? fd :
187 (state->mode == GZ_READ ?
189 (O_WRONLY | O_CREAT | (
190 state->mode == GZ_WRITE ?
194 if (state->fd == -1) {
199 if (state->mode == GZ_APPEND)
200 state->mode = GZ_WRITE; /* simplify later checks */
202 /* save the current position for rewinding (only if reading) */
203 if (state->mode == GZ_READ) {
204 state->start = LSEEK(state->fd, 0, SEEK_CUR);
205 if (state->start == -1) state->start = 0;
208 /* initialize stream */
212 return (gzFile)state;
215 /* -- see zlib.h -- */
216 gzFile ZEXPORT gzopen(path, mode)
220 return gz_open(path, -1, mode);
223 /* -- see zlib.h -- */
224 gzFile ZEXPORT gzopen64(path, mode)
228 return gz_open(path, -1, mode);
231 /* -- see zlib.h -- */
232 gzFile ZEXPORT gzdopen(fd, mode)
236 char *path; /* identifier for error messages */
239 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
241 sprintf(path, "<fd:%d>", fd); /* for debugging */
242 gz = gz_open(path, fd, mode);
247 /* -- see zlib.h -- */
248 int ZEXPORT gzbuffer(file, size)
254 /* get internal structure and check integrity */
257 state = (gz_statep)file;
258 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
261 /* make sure we haven't already allocated memory */
262 if (state->size != 0)
265 /* check and set requested size */
267 size = 2; /* need two bytes to check magic header */
272 /* -- see zlib.h -- */
273 int ZEXPORT gzrewind(file)
278 /* get internal structure */
281 state = (gz_statep)file;
283 /* check that we're reading and that there's no error */
284 if (state->mode != GZ_READ ||
285 (state->err != Z_OK && state->err != Z_BUF_ERROR))
288 /* back up and start over */
289 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
295 /* -- see zlib.h -- */
296 z_off64_t ZEXPORT gzseek64(file, offset, whence)
305 /* get internal structure and check integrity */
308 state = (gz_statep)file;
309 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
312 /* check that there's no error */
313 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
316 /* can only seek from start or relative to current position */
317 if (whence != SEEK_SET && whence != SEEK_CUR)
320 /* normalize offset to a SEEK_CUR specification */
321 if (whence == SEEK_SET)
322 offset -= state->x.pos;
323 else if (state->seek)
324 offset += state->skip;
327 /* if within raw area while reading, just go there */
328 if (state->mode == GZ_READ && state->how == COPY &&
329 state->x.pos + offset >= 0) {
330 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
337 gz_error(state, Z_OK, NULL);
338 state->strm.avail_in = 0;
339 state->x.pos += offset;
343 /* calculate skip amount, rewinding if needed for back seek when reading */
345 if (state->mode != GZ_READ) /* writing -- can't go backwards */
347 offset += state->x.pos;
348 if (offset < 0) /* before start of file! */
350 if (gzrewind(file) == -1) /* rewind, then skip to offset */
354 /* if reading, skip what's in output buffer (one less gzgetc() check) */
355 if (state->mode == GZ_READ) {
356 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
357 (unsigned)offset : state->x.have;
364 /* request skip (if not zero) */
367 state->skip = offset;
369 return state->x.pos + offset;
372 /* -- see zlib.h -- */
373 z_off_t ZEXPORT gzseek(file, offset, whence)
380 ret = gzseek64(file, (z_off64_t)offset, whence);
381 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
384 /* -- see zlib.h -- */
385 z_off64_t ZEXPORT gztell64(file)
390 /* get internal structure and check integrity */
393 state = (gz_statep)file;
394 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
397 /* return position */
398 return state->x.pos + (state->seek ? state->skip : 0);
401 /* -- see zlib.h -- */
402 z_off_t ZEXPORT gztell(file)
407 ret = gztell64(file);
408 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
411 /* -- see zlib.h -- */
412 z_off64_t ZEXPORT gzoffset64(file)
418 /* get internal structure and check integrity */
421 state = (gz_statep)file;
422 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
425 /* compute and return effective offset in file */
426 offset = LSEEK(state->fd, 0, SEEK_CUR);
429 if (state->mode == GZ_READ) /* reading */
430 offset -= state->strm.avail_in; /* don't count buffered input */
434 /* -- see zlib.h -- */
435 z_off_t ZEXPORT gzoffset(file)
440 ret = gzoffset64(file);
441 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
444 /* -- see zlib.h -- */
445 int ZEXPORT gzeof(file)
450 /* get internal structure and check integrity */
453 state = (gz_statep)file;
454 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
457 /* return end-of-file state */
458 return state->mode == GZ_READ ? state->past : 0;
461 /* -- see zlib.h -- */
462 const char * ZEXPORT gzerror(file, errnum)
468 /* get internal structure and check integrity */
471 state = (gz_statep)file;
472 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
475 /* return error information */
477 *errnum = state->err;
478 return state->msg == NULL ? "" : state->msg;
481 /* -- see zlib.h -- */
482 void ZEXPORT gzclearerr(file)
487 /* get internal structure and check integrity */
490 state = (gz_statep)file;
491 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
494 /* clear error and end-of-file */
495 if (state->mode == GZ_READ) {
499 gz_error(state, Z_OK, NULL);
502 /* Create an error message in allocated memory and set state->err and
503 state->msg accordingly. Free any previous error message already there. Do
504 not try to free or allocate space if the error is Z_MEM_ERROR (out of
505 memory). Simply save the error message as a static string. If there is an
506 allocation failure constructing the error message, then convert the error to
508 void ZLIB_INTERNAL gz_error(state, err, msg)
513 /* free previously allocated message and clear */
514 if (state->msg != NULL) {
515 if (state->err != Z_MEM_ERROR)
520 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
521 if (err != Z_OK && err != Z_BUF_ERROR)
524 /* set error code, and if no message, then done */
529 /* for an out of memory error, save as static string */
530 if (err == Z_MEM_ERROR) {
531 state->msg = (char *)msg;
535 /* construct error message with path */
536 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
537 state->err = Z_MEM_ERROR;
538 state->msg = (char *)"out of memory";
541 strcpy(state->msg, state->path);
542 strcat(state->msg, ": ");
543 strcat(state->msg, msg);
548 /* portably return maximum value for an int (when limits.h presumed not
549 available) -- we need to do this to cover cases where 2's complement not
550 used, since C standard permits 1's complement and sign-bit representations,
551 otherwise we could just use ((unsigned)-1) >> 1 */
552 unsigned ZLIB_INTERNAL gz_intmax()