1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004, 2010, 2011, 2012, 2013 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
11 #if defined(_WIN32) && !defined(__BORLANDC__)
12 # define LSEEK _lseeki64
14 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
15 # define LSEEK lseek64
22 local
void gz_reset
OF((gz_statep
));
23 local gzFile gz_open
OF((const void *, int, const char *));
27 /* Map the Windows error number in ERROR to a locale-dependent error message
28 string and return a pointer to it. Typically, the values for ERROR come
31 The string pointed to shall not be modified by the application, but may be
32 overwritten by a subsequent call to gz_strwinerror
34 The gz_strwinerror function does not change the current setting of
36 char ZLIB_INTERNAL
*gz_strwinerror (error
)
39 static char buf
[1024];
42 DWORD lasterr
= GetLastError();
43 DWORD chars
= FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
44 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
47 0, /* Default language */
52 /* If there is an \r\n appended, zap it. */
54 && msgbuf
[chars
- 2] == '\r' && msgbuf
[chars
- 1] == '\n') {
59 if (chars
> sizeof (buf
) - 1) {
60 chars
= sizeof (buf
) - 1;
64 wcstombs(buf
, msgbuf
, chars
+ 1);
68 sprintf(buf
, "unknown win32 error (%ld)", error
);
71 SetLastError(lasterr
);
77 /* Reset gzip file state */
78 local
void gz_reset(state
)
81 state
->x
.have
= 0; /* no output data available */
82 if (state
->mode
== GZ_READ
) { /* for reading ... */
83 state
->eof
= 0; /* not at end of file */
84 state
->past
= 0; /* have not read past end yet */
85 state
->how
= LOOK
; /* look for gzip header */
87 state
->seek
= 0; /* no seek request pending */
88 gz_error(state
, Z_OK
, NULL
); /* clear error */
89 state
->x
.pos
= 0; /* no uncompressed data yet */
90 state
->strm
.avail_in
= 0; /* no input data yet */
93 /* Open a gzip file either by name or file descriptor. */
94 local gzFile
gz_open(path
, fd
, mode
)
113 /* allocate gzFile structure to return */
114 state
= (gz_statep
)malloc(sizeof(gz_state
));
117 state
->size
= 0; /* no buffers allocated yet */
118 state
->want
= GZBUFSIZE
; /* requested buffer size */
119 state
->msg
= NULL
; /* no error message yet */
122 state
->mode
= GZ_NONE
;
123 state
->level
= Z_DEFAULT_COMPRESSION
;
124 state
->strategy
= Z_DEFAULT_STRATEGY
;
127 if (*mode
>= '0' && *mode
<= '9')
128 state
->level
= *mode
- '0';
132 state
->mode
= GZ_READ
;
134 #ifndef NO_GZCOMPRESS
136 state
->mode
= GZ_WRITE
;
139 state
->mode
= GZ_APPEND
;
142 case '+': /* can't read and write at the same time */
145 case 'b': /* ignore -- will request binary anyway */
158 state
->strategy
= Z_FILTERED
;
161 state
->strategy
= Z_HUFFMAN_ONLY
;
164 state
->strategy
= Z_RLE
;
167 state
->strategy
= Z_FIXED
;
172 default: /* could consider as an error, but just ignore */
178 /* must provide an "r", "w", or "a" */
179 if (state
->mode
== GZ_NONE
) {
184 /* can't force transparent read */
185 if (state
->mode
== GZ_READ
) {
190 state
->direct
= 1; /* for empty file */
193 /* save the path name for error messages */
196 len
= wcstombs(NULL
, path
, 0);
197 if (len
== (size_t)-1)
202 len
= strlen((const char *)path
);
203 state
->path
= (char *)malloc(len
+ 1);
204 if (state
->path
== NULL
) {
211 wcstombs(state
->path
, path
, len
+ 1);
216 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
217 snprintf(state
->path
, len
+ 1, "%s", (const char *)path
);
219 strcpy(state
->path
, path
);
222 /* compute the flags for open() */
231 (cloexec
? O_CLOEXEC
: 0) |
233 (state
->mode
== GZ_READ
?
235 (O_WRONLY
| O_CREAT
|
237 (exclusive
? O_EXCL
: 0) |
239 (state
->mode
== GZ_WRITE
?
243 /* open the file with the appropriate flags (or just use fd) */
244 state
->fd
= fd
> -1 ? fd
: (
246 fd
== -2 ? _wopen(path
, oflag
, 0666) :
248 open((const char *)path
, oflag
, 0666));
249 if (state
->fd
== -1) {
254 if (state
->mode
== GZ_APPEND
)
255 state
->mode
= GZ_WRITE
; /* simplify later checks */
257 /* save the current position for rewinding (only if reading) */
258 if (state
->mode
== GZ_READ
) {
259 state
->start
= LSEEK(state
->fd
, 0, SEEK_CUR
);
260 if (state
->start
== -1) state
->start
= 0;
263 /* initialize stream */
267 return (gzFile
)state
;
270 /* -- see zlib.h -- */
271 gzFile ZEXPORT
gzopen(path
, mode
)
275 return gz_open(path
, -1, mode
);
278 /* -- see zlib.h -- */
279 gzFile ZEXPORT
gzopen64(path
, mode
)
283 return gz_open(path
, -1, mode
);
286 /* -- see zlib.h -- */
287 gzFile ZEXPORT
gzdopen(fd
, mode
)
291 char *path
; /* identifier for error messages */
294 if (fd
== -1 || (path
= (char *)malloc(7 + 3 * sizeof(int))) == NULL
)
296 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
297 snprintf(path
, 7 + 3 * sizeof(int), "<fd:%d>", fd
); /* for debugging */
299 sprintf(path
, "<fd:%d>", fd
); /* for debugging */
301 gz
= gz_open(path
, fd
, mode
);
306 /* -- see zlib.h -- */
308 gzFile ZEXPORT
gzopen_w(path
, mode
)
312 return gz_open(path
, -2, mode
);
316 /* -- see zlib.h -- */
317 int ZEXPORT
gzbuffer(file
, size
)
323 /* get internal structure and check integrity */
326 state
= (gz_statep
)file
;
327 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
330 /* make sure we haven't already allocated memory */
331 if (state
->size
!= 0)
334 /* check and set requested size */
336 size
= 2; /* need two bytes to check magic header */
341 /* -- see zlib.h -- */
342 int ZEXPORT
gzrewind(file
)
347 /* get internal structure */
350 state
= (gz_statep
)file
;
352 /* check that we're reading and that there's no error */
353 if (state
->mode
!= GZ_READ
||
354 (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
))
357 /* back up and start over */
358 if (LSEEK(state
->fd
, state
->start
, SEEK_SET
) == -1)
364 /* -- see zlib.h -- */
365 z_off64_t ZEXPORT
gzseek64(file
, offset
, whence
)
374 /* get internal structure and check integrity */
377 state
= (gz_statep
)file
;
378 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
381 /* check that there's no error */
382 if (state
->err
!= Z_OK
&& state
->err
!= Z_BUF_ERROR
)
385 /* can only seek from start or relative to current position */
386 if (whence
!= SEEK_SET
&& whence
!= SEEK_CUR
)
389 /* normalize offset to a SEEK_CUR specification */
390 if (whence
== SEEK_SET
)
391 offset
-= state
->x
.pos
;
392 else if (state
->seek
)
393 offset
+= state
->skip
;
396 /* if within raw area while reading, just go there */
397 if (state
->mode
== GZ_READ
&& state
->how
== COPY
&&
398 state
->x
.pos
+ offset
>= 0) {
399 ret
= LSEEK(state
->fd
, offset
- state
->x
.have
, SEEK_CUR
);
406 gz_error(state
, Z_OK
, NULL
);
407 state
->strm
.avail_in
= 0;
408 state
->x
.pos
+= offset
;
412 /* calculate skip amount, rewinding if needed for back seek when reading */
414 if (state
->mode
!= GZ_READ
) /* writing -- can't go backwards */
416 offset
+= state
->x
.pos
;
417 if (offset
< 0) /* before start of file! */
419 if (gzrewind(file
) == -1) /* rewind, then skip to offset */
423 /* if reading, skip what's in output buffer (one less gzgetc() check) */
424 if (state
->mode
== GZ_READ
) {
425 n
= GT_OFF(state
->x
.have
) || (z_off64_t
)state
->x
.have
> offset
?
426 (unsigned)offset
: state
->x
.have
;
433 /* request skip (if not zero) */
436 state
->skip
= offset
;
438 return state
->x
.pos
+ offset
;
441 /* -- see zlib.h -- */
442 z_off_t ZEXPORT
gzseek(file
, offset
, whence
)
449 ret
= gzseek64(file
, (z_off64_t
)offset
, whence
);
450 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
453 /* -- see zlib.h -- */
454 z_off64_t ZEXPORT
gztell64(file
)
459 /* get internal structure and check integrity */
462 state
= (gz_statep
)file
;
463 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
466 /* return position */
467 return state
->x
.pos
+ (state
->seek
? state
->skip
: 0);
470 /* -- see zlib.h -- */
471 z_off_t ZEXPORT
gztell(file
)
476 ret
= gztell64(file
);
477 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
480 /* -- see zlib.h -- */
481 z_off64_t ZEXPORT
gzoffset64(file
)
487 /* get internal structure and check integrity */
490 state
= (gz_statep
)file
;
491 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
494 /* compute and return effective offset in file */
495 offset
= LSEEK(state
->fd
, 0, SEEK_CUR
);
498 if (state
->mode
== GZ_READ
) /* reading */
499 offset
-= state
->strm
.avail_in
; /* don't count buffered input */
503 /* -- see zlib.h -- */
504 z_off_t ZEXPORT
gzoffset(file
)
509 ret
= gzoffset64(file
);
510 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
513 /* -- see zlib.h -- */
514 int ZEXPORT
gzeof(file
)
519 /* get internal structure and check integrity */
522 state
= (gz_statep
)file
;
523 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
526 /* return end-of-file state */
527 return state
->mode
== GZ_READ
? state
->past
: 0;
530 /* -- see zlib.h -- */
531 const char * ZEXPORT
gzerror(file
, errnum
)
537 /* get internal structure and check integrity */
540 state
= (gz_statep
)file
;
541 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
544 /* return error information */
546 *errnum
= state
->err
;
547 return state
->err
== Z_MEM_ERROR
? "out of memory" :
548 (state
->msg
== NULL
? "" : state
->msg
);
551 /* -- see zlib.h -- */
552 void ZEXPORT
gzclearerr(file
)
557 /* get internal structure and check integrity */
560 state
= (gz_statep
)file
;
561 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
564 /* clear error and end-of-file */
565 if (state
->mode
== GZ_READ
) {
569 gz_error(state
, Z_OK
, NULL
);
572 /* Create an error message in allocated memory and set state->err and
573 state->msg accordingly. Free any previous error message already there. Do
574 not try to free or allocate space if the error is Z_MEM_ERROR (out of
575 memory). Simply save the error message as a static string. If there is an
576 allocation failure constructing the error message, then convert the error to
578 void ZLIB_INTERNAL
gz_error(state
, err
, msg
)
583 /* free previously allocated message and clear */
584 if (state
->msg
!= NULL
) {
585 if (state
->err
!= Z_MEM_ERROR
)
590 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
591 if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
)
594 /* set error code, and if no message, then done */
599 /* for an out of memory error, return literal string when requested */
600 if (err
== Z_MEM_ERROR
)
603 /* construct error message with path */
604 if ((state
->msg
= (char *)malloc(strlen(state
->path
) + strlen(msg
) + 3)) ==
606 state
->err
= Z_MEM_ERROR
;
609 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
610 snprintf(state
->msg
, strlen(state
->path
) + strlen(msg
) + 3,
611 "%s%s%s", state
->path
, ": ", msg
);
613 strcpy(state
->msg
, state
->path
);
614 strcat(state
->msg
, ": ");
615 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()