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 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
11 # define LSEEK lseek64
17 local
void gz_reset
OF((gz_statep
));
18 local gzFile gz_open
OF((const char *, int, const char *));
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 ZLIB_INTERNAL
*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
);
72 /* Reset gzip file state */
73 local
void gz_reset(state
)
76 if (state
->mode
== GZ_READ
) { /* for reading ... */
77 state
->have
= 0; /* no output data available */
78 state
->eof
= 0; /* not at end of file */
79 state
->how
= LOOK
; /* look for gzip header */
80 state
->direct
= 1; /* default for empty file */
82 state
->seek
= 0; /* no seek request pending */
83 gz_error(state
, Z_OK
, NULL
); /* clear error */
84 state
->pos
= 0; /* no uncompressed data yet */
85 state
->strm
.avail_in
= 0; /* no input data yet */
88 /* Open a gzip file either by name or file descriptor. */
89 local gzFile
gz_open(path
, fd
, mode
)
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 /* save the path name for error messages */
153 state
->path
= malloc(strlen(path
) + 1);
154 if (state
->path
== NULL
) {
158 strcpy(state
->path
, path
);
160 /* open the file with the appropriate mode (or just use fd) */
161 state
->fd
= fd
!= -1 ? fd
:
169 (state
->mode
== GZ_READ
?
171 (O_WRONLY
| O_CREAT
| (
172 state
->mode
== GZ_WRITE
?
176 if (state
->fd
== -1) {
181 if (state
->mode
== GZ_APPEND
)
182 state
->mode
= GZ_WRITE
; /* simplify later checks */
184 /* save the current position for rewinding (only if reading) */
185 if (state
->mode
== GZ_READ
) {
186 state
->start
= LSEEK(state
->fd
, 0, SEEK_CUR
);
187 if (state
->start
== -1) state
->start
= 0;
190 /* initialize stream */
194 return (gzFile
)state
;
197 /* -- see zlib.h -- */
198 gzFile ZEXPORT
gzopen(path
, mode
)
202 return gz_open(path
, -1, mode
);
205 /* -- see zlib.h -- */
206 gzFile ZEXPORT
gzopen64(path
, mode
)
210 return gz_open(path
, -1, mode
);
213 /* -- see zlib.h -- */
214 gzFile ZEXPORT
gzdopen(fd
, mode
)
218 char *path
; /* identifier for error messages */
221 if (fd
== -1 || (path
= malloc(7 + 3 * sizeof(int))) == NULL
)
223 sprintf(path
, "<fd:%d>", fd
); /* for debugging */
224 gz
= gz_open(path
, fd
, mode
);
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
;
304 else if (state
->seek
)
305 offset
+= state
->skip
;
308 /* if within raw area while reading, just go there */
309 if (state
->mode
== GZ_READ
&& state
->how
== COPY
&&
310 state
->pos
+ offset
>= state
->raw
) {
311 ret
= LSEEK(state
->fd
, offset
- state
->have
, SEEK_CUR
);
317 gz_error(state
, Z_OK
, NULL
);
318 state
->strm
.avail_in
= 0;
319 state
->pos
+= offset
;
323 /* calculate skip amount, rewinding if needed for back seek when reading */
325 if (state
->mode
!= GZ_READ
) /* writing -- can't go backwards */
327 offset
+= state
->pos
;
328 if (offset
< 0) /* before start of file! */
330 if (gzrewind(file
) == -1) /* rewind, then skip to offset */
334 /* if reading, skip what's in output buffer (one less gzgetc() check) */
335 if (state
->mode
== GZ_READ
) {
336 n
= GT_OFF(state
->have
) || (z_off64_t
)state
->have
> offset
?
337 (unsigned)offset
: state
->have
;
344 /* request skip (if not zero) */
347 state
->skip
= offset
;
349 return state
->pos
+ offset
;
352 /* -- see zlib.h -- */
353 z_off_t ZEXPORT
gzseek(file
, offset
, whence
)
360 ret
= gzseek64(file
, (z_off64_t
)offset
, whence
);
361 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
364 /* -- see zlib.h -- */
365 z_off64_t ZEXPORT
gztell64(file
)
370 /* get internal structure and check integrity */
373 state
= (gz_statep
)file
;
374 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
377 /* return position */
378 return state
->pos
+ (state
->seek
? state
->skip
: 0);
381 /* -- see zlib.h -- */
382 z_off_t ZEXPORT
gztell(file
)
387 ret
= gztell64(file
);
388 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
391 /* -- see zlib.h -- */
392 z_off64_t ZEXPORT
gzoffset64(file
)
398 /* get internal structure and check integrity */
401 state
= (gz_statep
)file
;
402 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
405 /* compute and return effective offset in file */
406 offset
= LSEEK(state
->fd
, 0, SEEK_CUR
);
409 if (state
->mode
== GZ_READ
) /* reading */
410 offset
-= state
->strm
.avail_in
; /* don't count buffered input */
414 /* -- see zlib.h -- */
415 z_off_t ZEXPORT
gzoffset(file
)
420 ret
= gzoffset64(file
);
421 return ret
== (z_off_t
)ret
? (z_off_t
)ret
: -1;
424 /* -- see zlib.h -- */
425 int ZEXPORT
gzeof(file
)
430 /* get internal structure and check integrity */
433 state
= (gz_statep
)file
;
434 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
437 /* return end-of-file state */
438 return state
->mode
== GZ_READ
?
439 (state
->eof
&& state
->strm
.avail_in
== 0 && state
->have
== 0) : 0;
442 /* -- see zlib.h -- */
443 const char * ZEXPORT
gzerror(file
, errnum
)
449 /* get internal structure and check integrity */
452 state
= (gz_statep
)file
;
453 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
456 /* return error information */
458 *errnum
= state
->err
;
459 return state
->msg
== NULL
? "" : state
->msg
;
462 /* -- see zlib.h -- */
463 void ZEXPORT
gzclearerr(file
)
468 /* get internal structure and check integrity */
471 state
= (gz_statep
)file
;
472 if (state
->mode
!= GZ_READ
&& state
->mode
!= GZ_WRITE
)
475 /* clear error and end-of-file */
476 if (state
->mode
== GZ_READ
)
478 gz_error(state
, Z_OK
, NULL
);
481 /* Create an error message in allocated memory and set state->err and
482 state->msg accordingly. Free any previous error message already there. Do
483 not try to free or allocate space if the error is Z_MEM_ERROR (out of
484 memory). Simply save the error message as a static string. If there is an
485 allocation failure constructing the error message, then convert the error to
487 void ZLIB_INTERNAL
gz_error(state
, err
, msg
)
492 /* free previously allocated message and clear */
493 if (state
->msg
!= NULL
) {
494 if (state
->err
!= Z_MEM_ERROR
)
499 /* set error code, and if no message, then done */
504 /* for an out of memory error, save as static string */
505 if (err
== Z_MEM_ERROR
) {
506 state
->msg
= (char *)msg
;
510 /* construct error message with path */
511 if ((state
->msg
= malloc(strlen(state
->path
) + strlen(msg
) + 3)) == NULL
) {
512 state
->err
= Z_MEM_ERROR
;
513 state
->msg
= (char *)"out of memory";
516 strcpy(state
->msg
, state
->path
);
517 strcat(state
->msg
, ": ");
518 strcat(state
->msg
, msg
);
523 /* portably return maximum value for an int (when limits.h presumed not
524 available) -- we need to do this to cover cases where 2's complement not
525 used, since C standard permits 1's complement and sign-bit representations,
526 otherwise we could just use ((unsigned)-1) >> 1 */
527 unsigned ZLIB_INTERNAL
gz_intmax()