1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004-2019 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 void *, 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 else /* for writing ... */
85 state
->reset
= 0; /* no deflateReset pending */
86 state
->seek
= 0; /* no seek request pending */
87 gz_error(state
, Z_OK
, NULL
); /* clear error */
88 state
->x
.pos
= 0; /* no uncompressed data yet */
89 state
->strm
.avail_in
= 0; /* no input data yet */
92 /* Open a gzip file either by name or file descriptor. */
93 local gzFile
gz_open(path
, fd
, mode
)
112 /* allocate gzFile structure to return */
113 state
= (gz_statep
)malloc(sizeof(gz_state
));
116 state
->size
= 0; /* no buffers allocated yet */
117 state
->want
= GZBUFSIZE
; /* requested buffer size */
118 state
->msg
= NULL
; /* no error message yet */
121 state
->mode
= GZ_NONE
;
122 state
->level
= Z_DEFAULT_COMPRESSION
;
123 state
->strategy
= Z_DEFAULT_STRATEGY
;
126 if (*mode
>= '0' && *mode
<= '9')
127 state
->level
= *mode
- '0';
131 state
->mode
= GZ_READ
;
133 #ifndef NO_GZCOMPRESS
135 state
->mode
= GZ_WRITE
;
138 state
->mode
= GZ_APPEND
;
141 case '+': /* can't read and write at the same time */
144 case 'b': /* ignore -- will request binary anyway */
157 state
->strategy
= Z_FILTERED
;
160 state
->strategy
= Z_HUFFMAN_ONLY
;
163 state
->strategy
= Z_RLE
;
166 state
->strategy
= Z_FIXED
;
171 default: /* could consider as an error, but just ignore */
177 /* must provide an "r", "w", or "a" */
178 if (state
->mode
== GZ_NONE
) {
183 /* can't force transparent read */
184 if (state
->mode
== GZ_READ
) {
189 state
->direct
= 1; /* for empty file */
192 /* save the path name for error messages */
195 len
= wcstombs(NULL
, path
, 0);
196 if (len
== (z_size_t
)-1)
201 len
= strlen((const char *)path
);
202 state
->path
= (char *)malloc(len
+ 1);
203 if (state
->path
== NULL
) {
210 wcstombs(state
->path
, path
, len
+ 1);
215 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
216 (void)snprintf(state
->path
, len
+ 1, "%s", (const char *)path
);
218 strcpy(state
->path
, path
);
221 /* compute the flags for open() */
230 (cloexec
? O_CLOEXEC
: 0) |
232 (state
->mode
== GZ_READ
?
234 (O_WRONLY
| O_CREAT
|
236 (exclusive
? O_EXCL
: 0) |
238 (state
->mode
== GZ_WRITE
?
242 /* open the file with the appropriate flags (or just use fd) */
243 state
->fd
= fd
> -1 ? fd
: (
245 fd
== -2 ? _wopen(path
, oflag
, 0666) :
247 open((const char *)path
, oflag
, 0666));
248 if (state
->fd
== -1) {
253 if (state
->mode
== GZ_APPEND
) {
254 LSEEK(state
->fd
, 0, SEEK_END
); /* so gzoffset() is correct */
255 state
->mode
= GZ_WRITE
; /* simplify later checks */
258 /* save the current position for rewinding (only if reading) */
259 if (state
->mode
== GZ_READ
) {
260 state
->start
= LSEEK(state
->fd
, 0, SEEK_CUR
);
261 if (state
->start
== -1) state
->start
= 0;
264 /* initialize stream */
268 return (gzFile
)state
;
271 /* -- see zlib.h -- */
272 gzFile ZEXPORT
gzopen(path
, mode
)
276 return gz_open(path
, -1, mode
);
279 /* -- see zlib.h -- */
280 gzFile ZEXPORT
gzopen64(path
, mode
)
284 return gz_open(path
, -1, mode
);
287 /* -- see zlib.h -- */
288 gzFile ZEXPORT
gzdopen(fd
, mode
)
292 char *path
; /* identifier for error messages */
295 if (fd
== -1 || (path
= (char *)malloc(7 + 3 * sizeof(int))) == NULL
)
297 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
298 (void)snprintf(path
, 7 + 3 * sizeof(int), "<fd:%d>", fd
);
300 sprintf(path
, "<fd:%d>", fd
); /* for debugging */
302 gz
= gz_open(path
, fd
, mode
);
307 /* -- see zlib.h -- */
309 gzFile ZEXPORT
gzopen_w(path
, mode
)
313 return gz_open(path
, -2, mode
);
317 /* -- see zlib.h -- */
318 int ZEXPORT
gzbuffer(file
, size
)
324 /* get internal structure and check integrity */
327 state
= (gz_statep
)file
;
328 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
331 /* make sure we haven't already allocated memory */
332 if (state
->size
!= 0)
335 /* check and set requested size */
336 if ((size
<< 1) < size
)
337 return -1; /* need to be able to double it */
339 size
= 2; /* need two bytes to check magic header */
344 /* -- see zlib.h -- */
345 int ZEXPORT
gzrewind(file
)
350 /* get internal structure */
353 state
= (gz_statep
)file
;
355 /* check that we're reading and that there's no error */
356 if (state
->mode
!= GZ_READ
||
357 (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
))
360 /* back up and start over */
361 if (LSEEK(state
->fd
, state
->start
, SEEK_SET
) == -1)
367 /* -- see zlib.h -- */
368 z_off64_t ZEXPORT
gzseek64(file
, offset
, whence
)
377 /* get internal structure and check integrity */
380 state
= (gz_statep
)file
;
381 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
384 /* check that there's no error */
385 if (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
)
388 /* can only seek from start or relative to current position */
389 if (whence
!= SEEK_SET
&& whence
!= SEEK_CUR
)
392 /* normalize offset to a SEEK_CUR specification */
393 if (whence
== SEEK_SET
)
394 offset
-= state
->x
.pos
;
395 else if (state
->seek
)
396 offset
+= state
->skip
;
399 /* if within raw area while reading, just go there */
400 if (state
->mode
== GZ_READ
&& state
->how
== COPY
&&
401 state
->x
.pos
+ offset
>= 0) {
402 ret
= LSEEK(state
->fd
, offset
- (z_off64_t
)state
->x
.have
, SEEK_CUR
);
409 gz_error(state
, Z_OK
, NULL
);
410 state
->strm
.avail_in
= 0;
411 state
->x
.pos
+= offset
;
415 /* calculate skip amount, rewinding if needed for back seek when reading */
417 if (state
->mode
!= GZ_READ
) /* writing -- can't go backwards */
419 offset
+= state
->x
.pos
;
420 if (offset
< 0) /* before start of file! */
422 if (gzrewind(file
) == -1) /* rewind, then skip to offset */
426 /* if reading, skip what's in output buffer (one less gzgetc() check) */
427 if (state
->mode
== GZ_READ
) {
428 n
= GT_OFF(state
->x
.have
) || (z_off64_t
)state
->x
.have
> offset
?
429 (unsigned)offset
: state
->x
.have
;
436 /* request skip (if not zero) */
439 state
->skip
= offset
;
441 return state
->x
.pos
+ offset
;
444 /* -- see zlib.h -- */
445 z_off_t ZEXPORT
gzseek(file
, offset
, whence
)
452 ret
= gzseek64(file
, (z_off64_t
)offset
, whence
);
453 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
456 /* -- see zlib.h -- */
457 z_off64_t ZEXPORT
gztell64(file
)
462 /* get internal structure and check integrity */
465 state
= (gz_statep
)file
;
466 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
469 /* return position */
470 return state
->x
.pos
+ (state
->seek
? state
->skip
: 0);
473 /* -- see zlib.h -- */
474 z_off_t ZEXPORT
gztell(file
)
479 ret
= gztell64(file
);
480 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
483 /* -- see zlib.h -- */
484 z_off64_t ZEXPORT
gzoffset64(file
)
490 /* get internal structure and check integrity */
493 state
= (gz_statep
)file
;
494 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
497 /* compute and return effective offset in file */
498 offset
= LSEEK(state
->fd
, 0, SEEK_CUR
);
501 if (state
->mode
== GZ_READ
) /* reading */
502 offset
-= state
->strm
.avail_in
; /* don't count buffered input */
506 /* -- see zlib.h -- */
507 z_off_t ZEXPORT
gzoffset(file
)
512 ret
= gzoffset64(file
);
513 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
516 /* -- see zlib.h -- */
517 int ZEXPORT
gzeof(file
)
522 /* get internal structure and check integrity */
525 state
= (gz_statep
)file
;
526 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
529 /* return end-of-file state */
530 return state
->mode
== GZ_READ
? state
->past
: 0;
533 /* -- see zlib.h -- */
534 const char * ZEXPORT
gzerror(file
, errnum
)
540 /* get internal structure and check integrity */
543 state
= (gz_statep
)file
;
544 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
547 /* return error information */
549 *errnum
= state
->err
;
550 return state
->err
== Z_MEM_ERROR
? "out of memory" :
551 (state
->msg
== NULL
? "" : state
->msg
);
554 /* -- see zlib.h -- */
555 void ZEXPORT
gzclearerr(file
)
560 /* get internal structure and check integrity */
563 state
= (gz_statep
)file
;
564 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
567 /* clear error and end-of-file */
568 if (state
->mode
== GZ_READ
) {
572 gz_error(state
, Z_OK
, NULL
);
575 /* Create an error message in allocated memory and set state->err and
576 state->msg accordingly. Free any previous error message already there. Do
577 not try to free or allocate space if the error is Z_MEM_ERROR (out of
578 memory). Simply save the error message as a static string. If there is an
579 allocation failure constructing the error message, then convert the error to
581 void ZLIB_INTERNAL
gz_error(state
, err
, msg
)
586 /* free previously allocated message and clear */
587 if (state
->msg
!= NULL
) {
588 if (state
->err
!= Z_MEM_ERROR
)
593 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
594 if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
)
597 /* set error code, and if no message, then done */
602 /* for an out of memory error, return literal string when requested */
603 if (err
== Z_MEM_ERROR
)
606 /* construct error message with path */
607 if ((state
->msg
= (char *)malloc(strlen(state
->path
) + strlen(msg
) + 3)) ==
609 state
->err
= Z_MEM_ERROR
;
612 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
613 (void)snprintf(state
->msg
, strlen(state
->path
) + strlen(msg
) + 3,
614 "%s%s%s", state
->path
, ": ", msg
);
616 strcpy(state
->msg
, state
->path
);
617 strcat(state
->msg
, ": ");
618 strcat(state
->msg
, msg
);
623 /* portably return maximum value for an int (when limits.h presumed not
624 available) -- we need to do this to cover cases where 2's complement not
625 used, since C standard permits 1's complement and sign-bit representations,
626 otherwise we could just use ((unsigned)-1) >> 1 */
627 unsigned ZLIB_INTERNAL
gz_intmax()