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 ZLIB_INTERNAL *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)
98 /* allocate gzFile structure to return */
99 state = malloc(sizeof(gz_state));
102 state->size = 0; /* no buffers allocated yet */
103 state->want = GZBUFSIZE; /* requested buffer size */
104 state->msg = NULL; /* no error message yet */
107 state->mode = GZ_NONE;
108 state->level = Z_DEFAULT_COMPRESSION;
109 state->strategy = Z_DEFAULT_STRATEGY;
111 if (*mode >= '0' && *mode <= '9')
112 state->level = *mode - '0';
116 state->mode = GZ_READ;
118 #ifndef NO_GZCOMPRESS
120 state->mode = GZ_WRITE;
123 state->mode = GZ_APPEND;
126 case '+': /* can't read and write at the same time */
129 case 'b': /* ignore -- will request binary anyway */
132 state->strategy = Z_FILTERED;
135 state->strategy = Z_HUFFMAN_ONLY;
138 state->strategy = Z_RLE;
141 state->strategy = Z_FIXED;
142 default: /* could consider as an error, but just ignore */
148 /* must provide an "r", "w", or "a" */
149 if (state->mode == GZ_NONE) {
154 /* save the path name for error messages */
155 state->path = malloc(strlen(path) + 1);
156 if (state->path == NULL) {
160 strcpy(state->path, path);
162 /* open the file with the appropriate mode (or just use fd) */
163 state->fd = fd != -1 ? fd :
171 (state->mode == GZ_READ ?
173 (O_WRONLY | O_CREAT | (
174 state->mode == GZ_WRITE ?
178 if (state->fd == -1) {
183 if (state->mode == GZ_APPEND)
184 state->mode = GZ_WRITE; /* simplify later checks */
186 /* save the current position for rewinding (only if reading) */
187 if (state->mode == GZ_READ) {
188 state->start = LSEEK(state->fd, 0, SEEK_CUR);
189 if (state->start == -1) state->start = 0;
192 /* initialize stream */
196 return (gzFile)state;
199 /* -- see zlib.h -- */
200 gzFile ZEXPORT gzopen(path, mode)
204 return gz_open(path, -1, mode);
207 /* -- see zlib.h -- */
208 gzFile ZEXPORT gzopen64(path, mode)
212 return gz_open(path, -1, mode);
215 /* -- see zlib.h -- */
216 gzFile ZEXPORT gzdopen(fd, mode)
220 char *path; /* identifier for error messages */
223 if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
225 sprintf(path, "<fd:%d>", fd); /* for debugging */
226 gz = gz_open(path, fd, mode);
231 /* -- see zlib.h -- */
232 int ZEXPORT gzbuffer(file, size)
238 /* get internal structure and check integrity */
241 state = (gz_statep)file;
242 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
245 /* make sure we haven't already allocated memory */
246 if (state->size != 0)
249 /* check and set requested size */
251 size = 2; /* need two bytes to check magic header */
256 /* -- see zlib.h -- */
257 int ZEXPORT gzrewind(file)
262 /* get internal structure */
265 state = (gz_statep)file;
267 /* check that we're reading and that there's no error */
268 if (state->mode != GZ_READ ||
269 (state->err != Z_OK && state->err != Z_BUF_ERROR))
272 /* back up and start over */
273 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
279 /* -- see zlib.h -- */
280 z_off64_t ZEXPORT gzseek64(file, offset, whence)
289 /* get internal structure and check integrity */
292 state = (gz_statep)file;
293 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
296 /* check that there's no error */
297 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
300 /* can only seek from start or relative to current position */
301 if (whence != SEEK_SET && whence != SEEK_CUR)
304 /* normalize offset to a SEEK_CUR specification */
305 if (whence == SEEK_SET)
306 offset -= state->pos;
307 else if (state->seek)
308 offset += state->skip;
311 /* if within raw area while reading, just go there */
312 if (state->mode == GZ_READ && state->how == COPY &&
313 state->pos + offset >= 0) {
314 ret = LSEEK(state->fd, offset - state->have, SEEK_CUR);
320 gz_error(state, Z_OK, NULL);
321 state->strm.avail_in = 0;
322 state->pos += offset;
326 /* calculate skip amount, rewinding if needed for back seek when reading */
328 if (state->mode != GZ_READ) /* writing -- can't go backwards */
330 offset += state->pos;
331 if (offset < 0) /* before start of file! */
333 if (gzrewind(file) == -1) /* rewind, then skip to offset */
337 /* if reading, skip what's in output buffer (one less gzgetc() check) */
338 if (state->mode == GZ_READ) {
339 n = GT_OFF(state->have) || (z_off64_t)state->have > offset ?
340 (unsigned)offset : state->have;
347 /* request skip (if not zero) */
350 state->skip = offset;
352 return state->pos + offset;
355 /* -- see zlib.h -- */
356 z_off_t ZEXPORT gzseek(file, offset, whence)
363 ret = gzseek64(file, (z_off64_t)offset, whence);
364 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
367 /* -- see zlib.h -- */
368 z_off64_t ZEXPORT gztell64(file)
373 /* get internal structure and check integrity */
376 state = (gz_statep)file;
377 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
380 /* return position */
381 return state->pos + (state->seek ? state->skip : 0);
384 /* -- see zlib.h -- */
385 z_off_t ZEXPORT gztell(file)
390 ret = gztell64(file);
391 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
394 /* -- see zlib.h -- */
395 z_off64_t ZEXPORT gzoffset64(file)
401 /* get internal structure and check integrity */
404 state = (gz_statep)file;
405 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
408 /* compute and return effective offset in file */
409 offset = LSEEK(state->fd, 0, SEEK_CUR);
412 if (state->mode == GZ_READ) /* reading */
413 offset -= state->strm.avail_in; /* don't count buffered input */
417 /* -- see zlib.h -- */
418 z_off_t ZEXPORT gzoffset(file)
423 ret = gzoffset64(file);
424 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
427 /* -- see zlib.h -- */
428 int ZEXPORT gzeof(file)
433 /* get internal structure and check integrity */
436 state = (gz_statep)file;
437 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
440 /* return end-of-file state */
441 return state->mode == GZ_READ ?
442 (state->eof && state->strm.avail_in == 0 && state->have == 0) : 0;
445 /* -- see zlib.h -- */
446 const char * ZEXPORT gzerror(file, errnum)
452 /* get internal structure and check integrity */
455 state = (gz_statep)file;
456 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
459 /* return error information */
461 *errnum = state->err;
462 return state->msg == NULL ? "" : state->msg;
465 /* -- see zlib.h -- */
466 void ZEXPORT gzclearerr(file)
471 /* get internal structure and check integrity */
474 state = (gz_statep)file;
475 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
478 /* clear error and end-of-file */
479 if (state->mode == GZ_READ)
481 gz_error(state, Z_OK, NULL);
484 /* Create an error message in allocated memory and set state->err and
485 state->msg accordingly. Free any previous error message already there. Do
486 not try to free or allocate space if the error is Z_MEM_ERROR (out of
487 memory). Simply save the error message as a static string. If there is an
488 allocation failure constructing the error message, then convert the error to
490 void ZLIB_INTERNAL gz_error(state, err, msg)
495 /* free previously allocated message and clear */
496 if (state->msg != NULL) {
497 if (state->err != Z_MEM_ERROR)
502 /* set error code, and if no message, then done */
507 /* for an out of memory error, save as static string */
508 if (err == Z_MEM_ERROR) {
509 state->msg = (char *)msg;
513 /* construct error message with path */
514 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
515 state->err = Z_MEM_ERROR;
516 state->msg = (char *)"out of memory";
519 strcpy(state->msg, state->path);
520 strcat(state->msg, ": ");
521 strcat(state->msg, msg);
526 /* portably return maximum value for an int (when limits.h presumed not
527 available) -- we need to do this to cover cases where 2's complement not
528 used, since C standard permits 1's complement and sign-bit representations,
529 otherwise we could just use ((unsigned)-1) >> 1 */
530 unsigned ZLIB_INTERNAL gz_intmax()