1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004-2017 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
8 #if defined(_WIN32) && !defined(__BORLANDC__) && !defined(__MINGW32__)
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 state
->seek
= 0; /* no seek request pending */
85 gz_error(state
, Z_OK
, NULL
); /* clear error */
86 state
->x
.pos
= 0; /* no uncompressed data yet */
87 state
->strm
.avail_in
= 0; /* no input data yet */
90 /* Open a gzip file either by name or file descriptor. */
91 local gzFile
gz_open(path
, fd
, mode
)
110 /* allocate gzFile structure to return */
111 state
= (gz_statep
)malloc(sizeof(gz_state
));
114 state
->size
= 0; /* no buffers allocated yet */
115 state
->want
= GZBUFSIZE
; /* requested buffer size */
116 state
->msg
= NULL
; /* no error message yet */
119 state
->mode
= GZ_NONE
;
120 state
->level
= Z_DEFAULT_COMPRESSION
;
121 state
->strategy
= Z_DEFAULT_STRATEGY
;
124 if (*mode
>= '0' && *mode
<= '9')
125 state
->level
= *mode
- '0';
129 state
->mode
= GZ_READ
;
131 #ifndef NO_GZCOMPRESS
133 state
->mode
= GZ_WRITE
;
136 state
->mode
= GZ_APPEND
;
139 case '+': /* can't read and write at the same time */
142 case 'b': /* ignore -- will request binary anyway */
155 state
->strategy
= Z_FILTERED
;
158 state
->strategy
= Z_HUFFMAN_ONLY
;
161 state
->strategy
= Z_RLE
;
164 state
->strategy
= Z_FIXED
;
169 default: /* could consider as an error, but just ignore */
175 /* must provide an "r", "w", or "a" */
176 if (state
->mode
== GZ_NONE
) {
181 /* can't force transparent read */
182 if (state
->mode
== GZ_READ
) {
187 state
->direct
= 1; /* for empty file */
190 /* save the path name for error messages */
193 len
= wcstombs(NULL
, path
, 0);
194 if (len
== (z_size_t
)-1)
199 len
= strlen((const char *)path
);
200 state
->path
= (char *)malloc(len
+ 1);
201 if (state
->path
== NULL
) {
208 wcstombs(state
->path
, path
, len
+ 1);
213 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
214 (void)snprintf(state
->path
, len
+ 1, "%s", (const char *)path
);
216 strcpy(state
->path
, path
);
219 /* compute the flags for open() */
228 (cloexec
? O_CLOEXEC
: 0) |
230 (state
->mode
== GZ_READ
?
232 (O_WRONLY
| O_CREAT
|
234 (exclusive
? O_EXCL
: 0) |
236 (state
->mode
== GZ_WRITE
?
240 /* open the file with the appropriate flags (or just use fd) */
241 state
->fd
= fd
> -1 ? fd
: (
243 fd
== -2 ? _wopen(path
, oflag
, 0666) :
245 open((const char *)path
, oflag
, 0666));
246 if (state
->fd
== -1) {
251 if (state
->mode
== GZ_APPEND
) {
252 LSEEK(state
->fd
, 0, SEEK_END
); /* so gzoffset() is correct */
253 state
->mode
= GZ_WRITE
; /* simplify later checks */
256 /* save the current position for rewinding (only if reading) */
257 if (state
->mode
== GZ_READ
) {
258 state
->start
= LSEEK(state
->fd
, 0, SEEK_CUR
);
259 if (state
->start
== -1) state
->start
= 0;
262 /* initialize stream */
266 return (gzFile
)state
;
269 /* -- see zlib.h -- */
270 gzFile ZEXPORT
gzopen(path
, mode
)
274 return gz_open(path
, -1, mode
);
277 /* -- see zlib.h -- */
278 gzFile ZEXPORT
gzopen64(path
, mode
)
282 return gz_open(path
, -1, mode
);
285 /* -- see zlib.h -- */
286 gzFile ZEXPORT
gzdopen(fd
, mode
)
290 char *path
; /* identifier for error messages */
293 if (fd
== -1 || (path
= (char *)malloc(7 + 3 * sizeof(int))) == NULL
)
295 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
296 (void)snprintf(path
, 7 + 3 * sizeof(int), "<fd:%d>", fd
);
298 sprintf(path
, "<fd:%d>", fd
); /* for debugging */
300 gz
= gz_open(path
, fd
, mode
);
305 /* -- see zlib.h -- */
307 gzFile ZEXPORT
gzopen_w(path
, mode
)
311 return gz_open(path
, -2, mode
);
315 /* -- see zlib.h -- */
316 int ZEXPORT
gzbuffer(file
, size
)
322 /* get internal structure and check integrity */
325 state
= (gz_statep
)file
;
326 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
329 /* make sure we haven't already allocated memory */
330 if (state
->size
!= 0)
333 /* check and set requested size */
334 if ((size
<< 1) < size
)
335 return -1; /* need to be able to double it */
337 size
= 2; /* need two bytes to check magic header */
342 /* -- see zlib.h -- */
343 int ZEXPORT
gzrewind(file
)
348 /* get internal structure */
351 state
= (gz_statep
)file
;
353 /* check that we're reading and that there's no error */
354 if (state
->mode
!= GZ_READ
||
355 (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
))
358 /* back up and start over */
359 if (LSEEK(state
->fd
, state
->start
, SEEK_SET
) == -1)
365 /* -- see zlib.h -- */
366 z_off64_t ZEXPORT
gzseek64(file
, offset
, whence
)
375 /* get internal structure and check integrity */
378 state
= (gz_statep
)file
;
379 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
382 /* check that there's no error */
383 if (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
)
386 /* can only seek from start or relative to current position */
387 if (whence
!= SEEK_SET
&& whence
!= SEEK_CUR
)
390 /* normalize offset to a SEEK_CUR specification */
391 if (whence
== SEEK_SET
)
392 offset
-= state
->x
.pos
;
393 else if (state
->seek
)
394 offset
+= state
->skip
;
397 /* if within raw area while reading, just go there */
398 if (state
->mode
== GZ_READ
&& state
->how
== COPY
&&
399 state
->x
.pos
+ offset
>= 0) {
400 ret
= LSEEK(state
->fd
, offset
- state
->x
.have
, SEEK_CUR
);
407 gz_error(state
, Z_OK
, NULL
);
408 state
->strm
.avail_in
= 0;
409 state
->x
.pos
+= offset
;
413 /* calculate skip amount, rewinding if needed for back seek when reading */
415 if (state
->mode
!= GZ_READ
) /* writing -- can't go backwards */
417 offset
+= state
->x
.pos
;
418 if (offset
< 0) /* before start of file! */
420 if (gzrewind(file
) == -1) /* rewind, then skip to offset */
424 /* if reading, skip what's in output buffer (one less gzgetc() check) */
425 if (state
->mode
== GZ_READ
) {
426 n
= GT_OFF(state
->x
.have
) || (z_off64_t
)state
->x
.have
> offset
?
427 (unsigned)offset
: state
->x
.have
;
434 /* request skip (if not zero) */
437 state
->skip
= offset
;
439 return state
->x
.pos
+ offset
;
442 /* -- see zlib.h -- */
443 z_off_t ZEXPORT
gzseek(file
, offset
, whence
)
450 ret
= gzseek64(file
, (z_off64_t
)offset
, whence
);
451 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
454 /* -- see zlib.h -- */
455 z_off64_t ZEXPORT
gztell64(file
)
460 /* get internal structure and check integrity */
463 state
= (gz_statep
)file
;
464 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
467 /* return position */
468 return state
->x
.pos
+ (state
->seek
? state
->skip
: 0);
471 /* -- see zlib.h -- */
472 z_off_t ZEXPORT
gztell(file
)
477 ret
= gztell64(file
);
478 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
481 /* -- see zlib.h -- */
482 z_off64_t ZEXPORT
gzoffset64(file
)
488 /* get internal structure and check integrity */
491 state
= (gz_statep
)file
;
492 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
495 /* compute and return effective offset in file */
496 offset
= LSEEK(state
->fd
, 0, SEEK_CUR
);
499 if (state
->mode
== GZ_READ
) /* reading */
500 offset
-= state
->strm
.avail_in
; /* don't count buffered input */
504 /* -- see zlib.h -- */
505 z_off_t ZEXPORT
gzoffset(file
)
510 ret
= gzoffset64(file
);
511 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
514 /* -- see zlib.h -- */
515 int ZEXPORT
gzeof(file
)
520 /* get internal structure and check integrity */
523 state
= (gz_statep
)file
;
524 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
527 /* return end-of-file state */
528 return state
->mode
== GZ_READ
? state
->past
: 0;
531 /* -- see zlib.h -- */
532 const char * ZEXPORT
gzerror(file
, errnum
)
538 /* get internal structure and check integrity */
541 state
= (gz_statep
)file
;
542 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
545 /* return error information */
547 *errnum
= state
->err
;
548 return state
->err
== Z_MEM_ERROR
? "out of memory" :
549 (state
->msg
== NULL
? "" : state
->msg
);
552 /* -- see zlib.h -- */
553 void ZEXPORT
gzclearerr(file
)
558 /* get internal structure and check integrity */
561 state
= (gz_statep
)file
;
562 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
565 /* clear error and end-of-file */
566 if (state
->mode
== GZ_READ
) {
570 gz_error(state
, Z_OK
, NULL
);
573 /* Create an error message in allocated memory and set state->err and
574 state->msg accordingly. Free any previous error message already there. Do
575 not try to free or allocate space if the error is Z_MEM_ERROR (out of
576 memory). Simply save the error message as a static string. If there is an
577 allocation failure constructing the error message, then convert the error to
579 void ZLIB_INTERNAL
gz_error(state
, err
, msg
)
584 /* free previously allocated message and clear */
585 if (state
->msg
!= NULL
) {
586 if (state
->err
!= Z_MEM_ERROR
)
591 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
592 if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
)
595 /* set error code, and if no message, then done */
600 /* for an out of memory error, return literal string when requested */
601 if (err
== Z_MEM_ERROR
)
604 /* construct error message with path */
605 if ((state
->msg
= (char *)malloc(strlen(state
->path
) + strlen(msg
) + 3)) ==
607 state
->err
= Z_MEM_ERROR
;
610 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
611 (void)snprintf(state
->msg
, strlen(state
->path
) + strlen(msg
) + 3,
612 "%s%s%s", state
->path
, ": ", msg
);
614 strcpy(state
->msg
, state
->path
);
615 strcat(state
->msg
, ": ");
616 strcat(state
->msg
, msg
);
621 /* portably return maximum value for an int (when limits.h presumed not
622 available) -- we need to do this to cover cases where 2's complement not
623 used, since C standard permits 1's complement and sign-bit representations,
624 otherwise we could just use ((unsigned)-1) >> 1 */
625 unsigned ZLIB_INTERNAL
gz_intmax()