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 || state->err != Z_OK)
271 /* back up and start over */
272 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
278 /* -- see zlib.h -- */
279 z_off64_t ZEXPORT gzseek64(file, offset, whence)
288 /* get internal structure and check integrity */
291 state = (gz_statep)file;
292 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
295 /* check that there's no error */
296 if (state->err != Z_OK)
299 /* can only seek from start or relative to current position */
300 if (whence != SEEK_SET && whence != SEEK_CUR)
303 /* normalize offset to a SEEK_CUR specification */
304 if (whence == SEEK_SET)
305 offset -= state->pos;
306 else if (state->seek)
307 offset += state->skip;
310 /* if within raw area while reading, just go there */
311 if (state->mode == GZ_READ && state->how == COPY &&
312 state->pos + offset >= state->raw) {
313 ret = LSEEK(state->fd, offset - state->have, SEEK_CUR);
319 gz_error(state, Z_OK, NULL);
320 state->strm.avail_in = 0;
321 state->pos += offset;
325 /* calculate skip amount, rewinding if needed for back seek when reading */
327 if (state->mode != GZ_READ) /* writing -- can't go backwards */
329 offset += state->pos;
330 if (offset < 0) /* before start of file! */
332 if (gzrewind(file) == -1) /* rewind, then skip to offset */
336 /* if reading, skip what's in output buffer (one less gzgetc() check) */
337 if (state->mode == GZ_READ) {
338 n = GT_OFF(state->have) || (z_off64_t)state->have > offset ?
339 (unsigned)offset : state->have;
346 /* request skip (if not zero) */
349 state->skip = offset;
351 return state->pos + offset;
354 /* -- see zlib.h -- */
355 z_off_t ZEXPORT gzseek(file, offset, whence)
362 ret = gzseek64(file, (z_off64_t)offset, whence);
363 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
366 /* -- see zlib.h -- */
367 z_off64_t ZEXPORT gztell64(file)
372 /* get internal structure and check integrity */
375 state = (gz_statep)file;
376 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
379 /* return position */
380 return state->pos + (state->seek ? state->skip : 0);
383 /* -- see zlib.h -- */
384 z_off_t ZEXPORT gztell(file)
389 ret = gztell64(file);
390 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
393 /* -- see zlib.h -- */
394 z_off64_t ZEXPORT gzoffset64(file)
400 /* get internal structure and check integrity */
403 state = (gz_statep)file;
404 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
407 /* compute and return effective offset in file */
408 offset = LSEEK(state->fd, 0, SEEK_CUR);
411 if (state->mode == GZ_READ) /* reading */
412 offset -= state->strm.avail_in; /* don't count buffered input */
416 /* -- see zlib.h -- */
417 z_off_t ZEXPORT gzoffset(file)
422 ret = gzoffset64(file);
423 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
426 /* -- see zlib.h -- */
427 int ZEXPORT gzeof(file)
432 /* get internal structure and check integrity */
435 state = (gz_statep)file;
436 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
439 /* return end-of-file state */
440 return state->mode == GZ_READ ?
441 (state->eof && state->strm.avail_in == 0 && state->have == 0) : 0;
444 /* -- see zlib.h -- */
445 const char * ZEXPORT gzerror(file, errnum)
451 /* get internal structure and check integrity */
454 state = (gz_statep)file;
455 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
458 /* return error information */
460 *errnum = state->err;
461 return state->msg == NULL ? "" : state->msg;
464 /* -- see zlib.h -- */
465 void ZEXPORT gzclearerr(file)
470 /* get internal structure and check integrity */
473 state = (gz_statep)file;
474 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
477 /* clear error and end-of-file */
478 if (state->mode == GZ_READ)
480 gz_error(state, Z_OK, NULL);
483 /* Create an error message in allocated memory and set state->err and
484 state->msg accordingly. Free any previous error message already there. Do
485 not try to free or allocate space if the error is Z_MEM_ERROR (out of
486 memory). Simply save the error message as a static string. If there is an
487 allocation failure constructing the error message, then convert the error to
489 void ZLIB_INTERNAL gz_error(state, err, msg)
494 /* free previously allocated message and clear */
495 if (state->msg != NULL) {
496 if (state->err != Z_MEM_ERROR)
501 /* set error code, and if no message, then done */
506 /* for an out of memory error, save as static string */
507 if (err == Z_MEM_ERROR) {
508 state->msg = (char *)msg;
512 /* construct error message with path */
513 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
514 state->err = Z_MEM_ERROR;
515 state->msg = (char *)"out of memory";
518 strcpy(state->msg, state->path);
519 strcat(state->msg, ": ");
520 strcat(state->msg, msg);
525 /* portably return maximum value for an int (when limits.h presumed not
526 available) -- we need to do this to cover cases where 2's complement not
527 used, since C standard permits 1's complement and sign-bit representations,
528 otherwise we could just use ((unsigned)-1) >> 1 */
529 unsigned ZLIB_INTERNAL gz_intmax()