1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004-2024 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
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(DWORD error
) {
30 static char buf
[1024];
33 DWORD lasterr
= GetLastError();
34 DWORD chars
= FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
35 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
38 0, /* Default language */
43 /* If there is an \r\n appended, zap it. */
45 && msgbuf
[chars
- 2] == '\r' && msgbuf
[chars
- 1] == '\n') {
50 if (chars
> sizeof (buf
) - 1) {
51 chars
= sizeof (buf
) - 1;
55 wcstombs(buf
, msgbuf
, chars
+ 1);
59 sprintf(buf
, "unknown win32 error (%ld)", error
);
62 SetLastError(lasterr
);
68 /* Reset gzip file state */
69 local
void gz_reset(gz_statep state
) {
70 state
->x
.have
= 0; /* no output data available */
71 if (state
->mode
== GZ_READ
) { /* for reading ... */
72 state
->eof
= 0; /* not at end of file */
73 state
->past
= 0; /* have not read past end yet */
74 state
->how
= LOOK
; /* look for gzip header */
76 else /* for writing ... */
77 state
->reset
= 0; /* no deflateReset pending */
78 state
->seek
= 0; /* no seek request pending */
79 gz_error(state
, Z_OK
, NULL
); /* clear error */
80 state
->x
.pos
= 0; /* no uncompressed data yet */
81 state
->strm
.avail_in
= 0; /* no input data yet */
84 /* Open a gzip file either by name or file descriptor. */
85 local gzFile
gz_open(const void *path
, int fd
, const char *mode
) {
100 /* allocate gzFile structure to return */
101 state
= (gz_statep
)malloc(sizeof(gz_state
));
104 state
->size
= 0; /* no buffers allocated yet */
105 state
->want
= GZBUFSIZE
; /* requested buffer size */
106 state
->msg
= NULL
; /* no error message yet */
109 state
->mode
= GZ_NONE
;
110 state
->level
= Z_DEFAULT_COMPRESSION
;
111 state
->strategy
= Z_DEFAULT_STRATEGY
;
114 if (*mode
>= '0' && *mode
<= '9')
115 state
->level
= *mode
- '0';
119 state
->mode
= GZ_READ
;
121 #ifndef NO_GZCOMPRESS
123 state
->mode
= GZ_WRITE
;
126 state
->mode
= GZ_APPEND
;
129 case '+': /* can't read and write at the same time */
132 case 'b': /* ignore -- will request binary anyway */
145 state
->strategy
= Z_FILTERED
;
148 state
->strategy
= Z_HUFFMAN_ONLY
;
151 state
->strategy
= Z_RLE
;
154 state
->strategy
= Z_FIXED
;
159 default: /* could consider as an error, but just ignore */
165 /* must provide an "r", "w", or "a" */
166 if (state
->mode
== GZ_NONE
) {
171 /* can't force transparent read */
172 if (state
->mode
== GZ_READ
) {
177 state
->direct
= 1; /* for empty file */
180 /* save the path name for error messages */
183 len
= wcstombs(NULL
, path
, 0);
184 if (len
== (z_size_t
)-1)
189 len
= strlen((const char *)path
);
190 state
->path
= (char *)malloc(len
+ 1);
191 if (state
->path
== NULL
) {
198 wcstombs(state
->path
, path
, len
+ 1);
203 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
204 (void)snprintf(state
->path
, len
+ 1, "%s", (const char *)path
);
206 strcpy(state
->path
, path
);
209 /* compute the flags for open() */
218 (cloexec
? O_CLOEXEC
: 0) |
220 (state
->mode
== GZ_READ
?
222 (O_WRONLY
| O_CREAT
|
224 (exclusive
? O_EXCL
: 0) |
226 (state
->mode
== GZ_WRITE
?
230 /* open the file with the appropriate flags (or just use fd) */
231 state
->fd
= fd
> -1 ? fd
: (
233 fd
== -2 ? _wopen(path
, oflag
, 0666) :
235 open((const char *)path
, oflag
, 0666));
236 if (state
->fd
== -1) {
241 if (state
->mode
== GZ_APPEND
) {
242 LSEEK(state
->fd
, 0, SEEK_END
); /* so gzoffset() is correct */
243 state
->mode
= GZ_WRITE
; /* simplify later checks */
246 /* save the current position for rewinding (only if reading) */
247 if (state
->mode
== GZ_READ
) {
248 state
->start
= LSEEK(state
->fd
, 0, SEEK_CUR
);
249 if (state
->start
== -1) state
->start
= 0;
252 /* initialize stream */
256 return (gzFile
)state
;
259 /* -- see zlib.h -- */
260 gzFile ZEXPORT
gzopen(const char *path
, const char *mode
) {
261 return gz_open(path
, -1, mode
);
264 /* -- see zlib.h -- */
265 gzFile ZEXPORT
gzopen64(const char *path
, const char *mode
) {
266 return gz_open(path
, -1, mode
);
269 /* -- see zlib.h -- */
270 gzFile ZEXPORT
gzdopen(int fd
, const char *mode
) {
271 char *path
; /* identifier for error messages */
274 if (fd
== -1 || (path
= (char *)malloc(7 + 3 * sizeof(int))) == NULL
)
276 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
277 (void)snprintf(path
, 7 + 3 * sizeof(int), "<fd:%d>", fd
);
279 sprintf(path
, "<fd:%d>", fd
); /* for debugging */
281 gz
= gz_open(path
, fd
, mode
);
286 /* -- see zlib.h -- */
288 gzFile ZEXPORT
gzopen_w(const wchar_t *path
, const char *mode
) {
289 return gz_open(path
, -2, mode
);
293 /* -- see zlib.h -- */
294 int ZEXPORT
gzbuffer(gzFile file
, unsigned size
) {
297 /* get internal structure and check integrity */
300 state
= (gz_statep
)file
;
301 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
304 /* make sure we haven't already allocated memory */
305 if (state
->size
!= 0)
308 /* check and set requested size */
309 if ((size
<< 1) < size
)
310 return -1; /* need to be able to double it */
312 size
= 8; /* needed to behave well with flushing */
317 /* -- see zlib.h -- */
318 int ZEXPORT
gzrewind(gzFile file
) {
321 /* get internal structure */
324 state
= (gz_statep
)file
;
326 /* check that we're reading and that there's no error */
327 if (state
->mode
!= GZ_READ
||
328 (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
))
331 /* back up and start over */
332 if (LSEEK(state
->fd
, state
->start
, SEEK_SET
) == -1)
338 /* -- see zlib.h -- */
339 z_off64_t ZEXPORT
gzseek64(gzFile file
, z_off64_t offset
, int whence
) {
344 /* get internal structure and check integrity */
347 state
= (gz_statep
)file
;
348 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
351 /* check that there's no error */
352 if (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
)
355 /* can only seek from start or relative to current position */
356 if (whence
!= SEEK_SET
&& whence
!= SEEK_CUR
)
359 /* normalize offset to a SEEK_CUR specification */
360 if (whence
== SEEK_SET
)
361 offset
-= state
->x
.pos
;
362 else if (state
->seek
)
363 offset
+= state
->skip
;
366 /* if within raw area while reading, just go there */
367 if (state
->mode
== GZ_READ
&& state
->how
== COPY
&&
368 state
->x
.pos
+ offset
>= 0) {
369 ret
= LSEEK(state
->fd
, offset
- (z_off64_t
)state
->x
.have
, SEEK_CUR
);
376 gz_error(state
, Z_OK
, NULL
);
377 state
->strm
.avail_in
= 0;
378 state
->x
.pos
+= offset
;
382 /* calculate skip amount, rewinding if needed for back seek when reading */
384 if (state
->mode
!= GZ_READ
) /* writing -- can't go backwards */
386 offset
+= state
->x
.pos
;
387 if (offset
< 0) /* before start of file! */
389 if (gzrewind(file
) == -1) /* rewind, then skip to offset */
393 /* if reading, skip what's in output buffer (one less gzgetc() check) */
394 if (state
->mode
== GZ_READ
) {
395 n
= GT_OFF(state
->x
.have
) || (z_off64_t
)state
->x
.have
> offset
?
396 (unsigned)offset
: state
->x
.have
;
403 /* request skip (if not zero) */
406 state
->skip
= offset
;
408 return state
->x
.pos
+ offset
;
411 /* -- see zlib.h -- */
412 z_off_t ZEXPORT
gzseek(gzFile file
, z_off_t offset
, int whence
) {
415 ret
= gzseek64(file
, (z_off64_t
)offset
, whence
);
416 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
419 /* -- see zlib.h -- */
420 z_off64_t ZEXPORT
gztell64(gzFile file
) {
423 /* get internal structure and check integrity */
426 state
= (gz_statep
)file
;
427 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
430 /* return position */
431 return state
->x
.pos
+ (state
->seek
? state
->skip
: 0);
434 /* -- see zlib.h -- */
435 z_off_t ZEXPORT
gztell(gzFile file
) {
438 ret
= gztell64(file
);
439 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
442 /* -- see zlib.h -- */
443 z_off64_t ZEXPORT
gzoffset64(gzFile file
) {
447 /* get internal structure and check integrity */
450 state
= (gz_statep
)file
;
451 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
454 /* compute and return effective offset in file */
455 offset
= LSEEK(state
->fd
, 0, SEEK_CUR
);
458 if (state
->mode
== GZ_READ
) /* reading */
459 offset
-= state
->strm
.avail_in
; /* don't count buffered input */
463 /* -- see zlib.h -- */
464 z_off_t ZEXPORT
gzoffset(gzFile file
) {
467 ret
= gzoffset64(file
);
468 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
471 /* -- see zlib.h -- */
472 int ZEXPORT
gzeof(gzFile file
) {
475 /* get internal structure and check integrity */
478 state
= (gz_statep
)file
;
479 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
482 /* return end-of-file state */
483 return state
->mode
== GZ_READ
? state
->past
: 0;
486 /* -- see zlib.h -- */
487 const char * ZEXPORT
gzerror(gzFile file
, int *errnum
) {
490 /* get internal structure and check integrity */
493 state
= (gz_statep
)file
;
494 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
497 /* return error information */
499 *errnum
= state
->err
;
500 return state
->err
== Z_MEM_ERROR
? "out of memory" :
501 (state
->msg
== NULL
? "" : state
->msg
);
504 /* -- see zlib.h -- */
505 void ZEXPORT
gzclearerr(gzFile file
) {
508 /* get internal structure and check integrity */
511 state
= (gz_statep
)file
;
512 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
515 /* clear error and end-of-file */
516 if (state
->mode
== GZ_READ
) {
520 gz_error(state
, Z_OK
, NULL
);
523 /* Create an error message in allocated memory and set state->err and
524 state->msg accordingly. Free any previous error message already there. Do
525 not try to free or allocate space if the error is Z_MEM_ERROR (out of
526 memory). Simply save the error message as a static string. If there is an
527 allocation failure constructing the error message, then convert the error to
529 void ZLIB_INTERNAL
gz_error(gz_statep state
, int err
, const char *msg
) {
530 /* free previously allocated message and clear */
531 if (state
->msg
!= NULL
) {
532 if (state
->err
!= Z_MEM_ERROR
)
537 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
538 if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
)
541 /* set error code, and if no message, then done */
546 /* for an out of memory error, return literal string when requested */
547 if (err
== Z_MEM_ERROR
)
550 /* construct error message with path */
551 if ((state
->msg
= (char *)malloc(strlen(state
->path
) + strlen(msg
) + 3)) ==
553 state
->err
= Z_MEM_ERROR
;
556 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
557 (void)snprintf(state
->msg
, strlen(state
->path
) + strlen(msg
) + 3,
558 "%s%s%s", state
->path
, ": ", msg
);
560 strcpy(state
->msg
, state
->path
);
561 strcat(state
->msg
, ": ");
562 strcat(state
->msg
, msg
);
566 /* portably return maximum value for an int (when limits.h presumed not
567 available) -- we need to do this to cover cases where 2's complement not
568 used, since C standard permits 1's complement and sign-bit representations,
569 otherwise we could just use ((unsigned)-1) >> 1 */
570 unsigned ZLIB_INTERNAL
gz_intmax(void) {