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
10 #ifdef _LARGEFILE64_SOURCE
11 # define LSEEK lseek64
17 local void gz_reset OF((gz_statep));
18 local gzFile gz_open OF((const char *, int, const char *, int));
20 #if defined UNDER_CE && defined NO_ERRNO_H
22 /* Map the Windows error number in ERROR to a locale-dependent error message
23 string and return a pointer to it. Typically, the values for ERROR come
26 The string pointed to shall not be modified by the application, but may be
27 overwritten by a subsequent call to gz_strwinerror
29 The gz_strwinerror function does not change the current setting of
31 char ZEXPORT *gz_strwinerror (error)
34 static char buf[1024];
37 DWORD lasterr = GetLastError();
38 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
39 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
42 0, /* Default language */
47 /* If there is an \r\n appended, zap it. */
49 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
54 if (chars > sizeof (buf) - 1) {
55 chars = sizeof (buf) - 1;
59 wcstombs(buf, msgbuf, chars + 1);
63 sprintf(buf, "unknown win32 error (%ld)", error);
66 SetLastError(lasterr);
70 #endif /* UNDER_CE && NO_ERRNO_H */
72 /* Reset gzip file state */
73 local void gz_reset(state)
76 state->how = 0; /* look for gzip header */
77 if (state->mode == GZ_READ) { /* for reading ... */
78 state->have = 0; /* no output data available */
79 state->eof = 0; /* not at end of file */
81 state->seek = 0; /* no seek request pending */
82 gz_error(state, Z_OK, NULL); /* clear error */
83 state->pos = 0; /* no uncompressed data yet */
84 state->strm.avail_in = 0; /* no input data yet */
87 /* Open a gzip file either by name or file descriptor. */
88 local gzFile gz_open(path, fd, mode, large)
96 /* allocate gzFile structure to return */
97 state = malloc(sizeof(gz_state));
100 state->size = 0; /* no buffers allocated yet */
101 state->want = GZBUFSIZE; /* requested buffer size */
102 state->msg = NULL; /* no error message yet */
105 state->mode = GZ_NONE;
106 state->level = Z_DEFAULT_COMPRESSION;
107 state->strategy = Z_DEFAULT_STRATEGY;
109 if (*mode >= '0' && *mode <= '9')
110 state->level = *mode - '0';
114 state->mode = GZ_READ;
116 #ifndef NO_GZCOMPRESS
118 state->mode = GZ_WRITE;
121 state->mode = GZ_APPEND;
124 case '+': /* can't read and write at the same time */
127 case 'b': /* ignore -- will request binary anyway */
130 state->strategy = Z_FILTERED;
133 state->strategy = Z_HUFFMAN_ONLY;
136 state->strategy = Z_RLE;
139 state->strategy = Z_FIXED;
140 default: /* could consider as an error, but just ignore */
146 /* must provide an "r", "w", or "a" */
147 if (state->mode == GZ_NONE) {
152 /* open the file with the appropriate mode (or just use fd) */
153 state->fd = fd != -1 ? fd :
165 (state->mode == GZ_READ ?
167 (O_WRONLY | O_CREAT | (
168 state->mode == GZ_WRITE ?
172 if (state->fd == -1) {
176 if (state->mode == GZ_APPEND)
177 state->mode = GZ_WRITE; /* simplify later checks */
179 /* save the path name for error messages */
180 state->path = malloc(strlen(path) + 1);
181 strcpy(state->path, path);
183 /* save the current position for rewinding (only if reading) */
184 if (state->mode == GZ_READ) {
185 state->start = LSEEK(state->fd, 0, SEEK_CUR);
186 if (state->start == -1) state->start = 0;
189 /* initialize stream */
193 return (gzFile)state;
196 /* -- see zlib.h -- */
197 gzFile ZEXPORT gzopen(path, mode)
201 return gz_open(path, -1, mode, 0);
204 /* -- see zlib.h -- */
205 gzFile ZEXPORT gzopen64(path, mode)
209 return gz_open(path, -1, mode, 1);
212 /* -- see zlib.h -- */
213 gzFile ZEXPORT gzdopen(fd, mode)
217 char path[46]; /* identifier for error messages */
222 sprintf(path, "<fd:%d>", fd); /* big enough for 128-bit integers */
224 snprintf(path, sizeof(path), "<fd:%d>", fd);
226 return gz_open(path, fd, mode, 0);
229 /* -- see zlib.h -- */
230 int ZEXPORT gzbuffer(file, size)
236 /* get internal structure and check integrity */
239 state = (gz_statep)file;
240 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
243 /* make sure we haven't already allocated memory */
244 if (state->size != 0)
247 /* check and set requested size */
254 /* -- see zlib.h -- */
255 int ZEXPORT gzrewind(file)
260 /* get internal structure */
263 state = (gz_statep)file;
265 /* check that we're reading and that there's no error */
266 if (state->mode != GZ_READ || state->err != Z_OK)
269 /* back up and start over */
270 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
276 /* -- see zlib.h -- */
277 z_off64_t ZEXPORT gzseek64(file, offset, whence)
286 /* get internal structure and check integrity */
289 state = (gz_statep)file;
290 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
293 /* check that there's no error */
294 if (state->err != Z_OK)
297 /* can only seek from start or relative to current position */
298 if (whence != SEEK_SET && whence != SEEK_CUR)
301 /* normalize offset to a SEEK_CUR specification */
302 if (whence == SEEK_SET)
303 offset -= state->pos;
305 /* if within raw area while reading, just go there */
306 if (state->mode == GZ_READ && state->how == 1 &&
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 = state->have > offset ? (unsigned)offset : state->have;
340 /* request skip (if not zero) */
343 state->skip = offset;
345 return state->pos + offset;
348 /* -- see zlib.h -- */
349 z_off_t ZEXPORT gzseek(file, offset, whence)
356 ret = gzseek64(file, (z_off64_t)offset, whence);
357 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
360 /* -- see zlib.h -- */
361 z_off64_t ZEXPORT gztell64(file)
366 /* get internal structure and check integrity */
369 state = (gz_statep)file;
370 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
373 /* return position */
374 return state->pos + (state->seek ? state->skip : 0);
377 /* -- see zlib.h -- */
378 z_off_t ZEXPORT gztell(file)
383 ret = gztell64(file);
384 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
387 /* -- see zlib.h -- */
388 z_off64_t ZEXPORT gzoffset64(file)
394 /* get internal structure and check integrity */
397 state = (gz_statep)file;
398 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
401 /* compute and return effective offset in file */
402 offset = LSEEK(state->fd, 0, SEEK_CUR);
405 if (state->mode == GZ_READ) /* reading */
406 offset -= state->strm.avail_in; /* don't count buffered input */
410 /* -- see zlib.h -- */
411 z_off_t ZEXPORT gzoffset(file)
416 ret = gzoffset64(file);
417 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
420 /* -- see zlib.h -- */
421 int ZEXPORT gzeof(file)
426 /* get internal structure and check integrity */
429 state = (gz_statep)file;
430 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
433 /* return end-of-file state */
434 return state->mode == GZ_READ ? (state->eof && state->have == 0) : 0;
437 /* -- see zlib.h -- */
438 const char * ZEXPORT gzerror(file, errnum)
444 /* get internal structure and check integrity */
447 state = (gz_statep)file;
448 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
451 /* return error information */
452 *errnum = state->err;
453 return state->msg == NULL ? "" : state->msg;
456 /* -- see zlib.h -- */
457 void ZEXPORT gzclearerr(file)
462 /* get internal structure and check integrity */
465 state = (gz_statep)file;
466 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
469 /* clear error and end-of-file */
470 if (state->mode == GZ_READ)
472 gz_error(state, Z_OK, NULL);
475 /* Create an error message in allocated memory and set state->err and
476 state->msg accordingly. Free any previous error message already there. Do
477 not try to free or allocate space if the error is Z_MEM_ERROR (out of
478 memory). Simply save the error message as a static string. If there is an
479 allocation failure constructing the error message, then convert the error to
481 void ZEXPORT gz_error(state, err, msg)
486 /* free previously allocated message and clear */
487 if (state->msg != NULL) {
488 if (state->err != Z_MEM_ERROR)
493 /* set error code, and if no message, then done */
498 /* for an out of memory error, save as static string */
499 if (err == Z_MEM_ERROR) {
500 state->msg = (char *)msg;
504 /* construct error message with path */
505 if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
506 state->err = Z_MEM_ERROR;
507 state->msg = (char *)"out of memory";
510 strcpy(state->msg, state->path);
511 strcat(state->msg, ": ");
512 strcat(state->msg, msg);
516 #endif /* !OLD_GZIO */