]>
git.saurik.com Git - apple/xnu.git/blob - libkern/zlib/gzio.c
2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* gzio.c -- IO on .gz files
29 * Copyright (C) 1995-2005 Jean-loup Gailly.
30 * For conditions of distribution and use, see copyright notice in zlib.h
32 * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
41 #ifdef NO_DEFLATE /* for compatibility with old definition */
42 # define NO_GZCOMPRESS
46 struct internal_state
{int dummy
;}; /* for buggy compilers */
51 # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
53 # define Z_BUFSIZE 16384
56 #ifndef Z_PRINTF_BUFSIZE
57 # define Z_PRINTF_BUFSIZE 4096
61 # pragma map (fdopen , "\174\174FDOPEN")
62 FILE *fdopen(int, const char *);
66 extern voidp malloc
OF((uInt size
));
67 extern void free
OF((voidpf ptr
));
70 #define ALLOC(size) malloc(size)
71 #define TRYFREE(p) {if (p) free(p);}
73 static int const gz_magic
[2] = {0x1f, 0x8b}; /* gzip magic header */
76 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
77 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
78 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
79 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
80 #define COMMENT 0x10 /* bit 4 set: file comment present */
81 #define RESERVED 0xE0 /* bits 5..7: reserved */
83 typedef struct gz_stream
{
85 int z_err
; /* error code for last stream operation */
86 int z_eof
; /* set if end of input file */
87 FILE *file
; /* .gz file */
88 Byte
*inbuf
; /* input buffer */
89 Byte
*outbuf
; /* output buffer */
90 uLong crc
; /* crc32 of uncompressed data */
91 char *msg
; /* error message */
92 char *path
; /* path name for debugging only */
93 int transparent
; /* 1 if input file is not a .gz file */
94 char mode
; /* 'w' or 'r' */
95 z_off_t start
; /* start of compressed data in file (header skipped) */
96 z_off_t in
; /* bytes into deflate or inflate */
97 z_off_t out
; /* bytes out of deflate or inflate */
98 int back
; /* one character push-back */
99 int last
; /* true if push-back is last character */
103 local gzFile gz_open
OF((const char *path
, const char *mode
, int fd
));
104 local
int do_flush
OF((gzFile file
, int flush
));
105 local
int get_byte
OF((gz_stream
*s
));
106 local
void check_header
OF((gz_stream
*s
));
107 local
int destroy
OF((gz_stream
*s
));
108 local
void putLong
OF((FILE *file
, uLong x
));
109 local uLong getLong
OF((gz_stream
*s
));
111 /* ===========================================================================
112 Opens a gzip (.gz) file for reading or writing. The mode parameter
113 is as in fopen ("rb" or "wb"). The file is given either by file descriptor
114 or path name (if fd == -1).
115 gz_open returns NULL if the file could not be opened or if there was
116 insufficient memory to allocate the (de)compression state; errno
117 can be checked to distinguish the two cases (if errno is zero, the
118 zlib error is Z_MEM_ERROR).
121 gz_open(const char *path
, const char *mode
, int fd
)
124 int level
= Z_DEFAULT_COMPRESSION
; /* compression level */
125 int strategy
= Z_DEFAULT_STRATEGY
; /* compression strategy */
126 char *p
= (char*)mode
;
128 char fmode
[80]; /* copy of mode, without the compression level */
131 if (!path
|| !mode
) return Z_NULL
;
133 s
= (gz_stream
*)ALLOC(sizeof(gz_stream
));
134 if (!s
) return Z_NULL
;
136 s
->stream
.zalloc
= (alloc_func
)0;
137 s
->stream
.zfree
= (free_func
)0;
138 s
->stream
.opaque
= (voidpf
)0;
139 s
->stream
.next_in
= s
->inbuf
= Z_NULL
;
140 s
->stream
.next_out
= s
->outbuf
= Z_NULL
;
141 s
->stream
.avail_in
= s
->stream
.avail_out
= 0;
148 s
->crc
= z_crc32(0L, Z_NULL
, 0);
152 s
->path
= (char*)ALLOC(strlen(path
)+1);
153 if (s
->path
== NULL
) {
154 return destroy(s
), (gzFile
)Z_NULL
;
156 strcpy(s
->path
, path
); /* do this early for debugging */
160 if (*p
== 'r') s
->mode
= 'r';
161 if (*p
== 'w' || *p
== 'a') s
->mode
= 'w';
162 if (*p
>= '0' && *p
<= '9') {
164 } else if (*p
== 'f') {
165 strategy
= Z_FILTERED
;
166 } else if (*p
== 'h') {
167 strategy
= Z_HUFFMAN_ONLY
;
168 } else if (*p
== 'R') {
171 *m
++ = *p
; /* copy the mode */
173 } while (*p
++ && m
!= fmode
+ sizeof(fmode
));
174 if (s
->mode
== '\0') return destroy(s
), (gzFile
)Z_NULL
;
176 if (s
->mode
== 'w') {
178 err
= Z_STREAM_ERROR
;
180 err
= deflateInit2(&(s
->stream
), level
,
181 Z_DEFLATED
, -MAX_WBITS
, DEF_MEM_LEVEL
, strategy
);
182 /* windowBits is passed < 0 to suppress zlib header */
184 s
->stream
.next_out
= s
->outbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
186 if (err
!= Z_OK
|| s
->outbuf
== Z_NULL
) {
187 return destroy(s
), (gzFile
)Z_NULL
;
190 s
->stream
.next_in
= s
->inbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
192 err
= inflateInit2(&(s
->stream
), -MAX_WBITS
);
193 /* windowBits is passed < 0 to tell that there is no zlib header.
194 * Note that in this case inflate *requires* an extra "dummy" byte
195 * after the compressed stream in order to complete decompression and
196 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
197 * present after the compressed stream.
199 if (err
!= Z_OK
|| s
->inbuf
== Z_NULL
) {
200 return destroy(s
), (gzFile
)Z_NULL
;
203 s
->stream
.avail_out
= Z_BUFSIZE
;
206 s
->file
= fd
< 0 ? F_OPEN(path
, fmode
) : (FILE*)fdopen(fd
, fmode
);
208 if (s
->file
== NULL
) {
209 return destroy(s
), (gzFile
)Z_NULL
;
211 if (s
->mode
== 'w') {
212 /* Write a very simple .gz header:
214 fprintf(s
->file
, "%c%c%c%c%c%c%c%c%c%c", gz_magic
[0], gz_magic
[1],
215 Z_DEFLATED
, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE
);
217 /* We use 10L instead of ftell(s->file) to because ftell causes an
218 * fflush on some systems. This version of the library doesn't use
219 * start anyway in write mode, so this initialization is not
223 check_header(s
); /* skip the .gz header */
224 s
->start
= ftell(s
->file
) - s
->stream
.avail_in
;
230 /* ===========================================================================
231 Opens a gzip (.gz) file for reading or writing.
234 gzopen(const char *path
, const char *mode
)
236 return gz_open (path
, mode
, -1);
239 /* ===========================================================================
240 Associate a gzFile with the file descriptor fd. fd is not dup'ed here
241 to mimic the behavio(u)r of fdopen.
244 gzdopen(int fd
, const char *mode
)
246 char name
[46]; /* allow for up to 128-bit integers */
248 if (fd
< 0) return (gzFile
)Z_NULL
;
249 sprintf(name
, "<fd:%d>", fd
); /* for debugging */
251 return gz_open (name
, mode
, fd
);
254 /* ===========================================================================
255 * Update the compression level and strategy
258 gzsetparams(gzFile file
, int level
, int strategy
)
260 gz_stream
*s
= (gz_stream
*)file
;
262 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
264 /* Make room to allow flushing */
265 if (s
->stream
.avail_out
== 0) {
267 s
->stream
.next_out
= s
->outbuf
;
268 if (fwrite(s
->outbuf
, 1, Z_BUFSIZE
, s
->file
) != Z_BUFSIZE
) {
271 s
->stream
.avail_out
= Z_BUFSIZE
;
274 return deflateParams (&(s
->stream
), level
, strategy
);
277 /* ===========================================================================
278 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
280 IN assertion: the stream s has been sucessfully opened for reading.
283 get_byte(gz_stream
*s
)
285 if (s
->z_eof
) return EOF
;
286 if (s
->stream
.avail_in
== 0) {
288 s
->stream
.avail_in
= (uInt
)fread(s
->inbuf
, 1, Z_BUFSIZE
, s
->file
);
289 if (s
->stream
.avail_in
== 0) {
291 if (ferror(s
->file
)) s
->z_err
= Z_ERRNO
;
294 s
->stream
.next_in
= s
->inbuf
;
296 s
->stream
.avail_in
--;
297 return *(s
->stream
.next_in
)++;
300 /* ===========================================================================
301 Check the gzip header of a gz_stream opened for reading. Set the stream
302 mode to transparent if the gzip magic header is not present; set s->err
303 to Z_DATA_ERROR if the magic header is present but the rest of the header
305 IN assertion: the stream s has already been created sucessfully;
306 s->stream.avail_in is zero for the first time, but may be non-zero
307 for concatenated .gz files.
310 check_header(gz_stream
*s
)
312 int method
; /* method byte */
313 int flags
; /* flags byte */
317 /* Assure two bytes in the buffer so we can peek ahead -- handle case
318 where first byte of header is at the end of the buffer after the last
320 len
= s
->stream
.avail_in
;
322 if (len
) s
->inbuf
[0] = s
->stream
.next_in
[0];
324 len
= (uInt
)fread(s
->inbuf
+ len
, 1, Z_BUFSIZE
>> len
, s
->file
);
325 if (len
== 0 && ferror(s
->file
)) s
->z_err
= Z_ERRNO
;
326 s
->stream
.avail_in
+= len
;
327 s
->stream
.next_in
= s
->inbuf
;
328 if (s
->stream
.avail_in
< 2) {
329 s
->transparent
= s
->stream
.avail_in
;
334 /* Peek ahead to check the gzip magic header */
335 if (s
->stream
.next_in
[0] != gz_magic
[0] ||
336 s
->stream
.next_in
[1] != gz_magic
[1]) {
340 s
->stream
.avail_in
-= 2;
341 s
->stream
.next_in
+= 2;
343 /* Check the rest of the gzip header */
344 method
= get_byte(s
);
346 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0) {
347 s
->z_err
= Z_DATA_ERROR
;
351 /* Discard time, xflags and OS code: */
352 for (len
= 0; len
< 6; len
++) (void)get_byte(s
);
354 if ((flags
& EXTRA_FIELD
) != 0) { /* skip the extra field */
355 len
= (uInt
)get_byte(s
);
356 len
+= ((uInt
)get_byte(s
))<<8;
357 /* len is garbage if EOF but the loop below will quit anyway */
358 while (len
-- != 0 && get_byte(s
) != EOF
) ;
360 if ((flags
& ORIG_NAME
) != 0) { /* skip the original file name */
361 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) ;
363 if ((flags
& COMMENT
) != 0) { /* skip the .gz file comment */
364 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) ;
366 if ((flags
& HEAD_CRC
) != 0) { /* skip the header crc */
367 for (len
= 0; len
< 2; len
++) (void)get_byte(s
);
369 s
->z_err
= s
->z_eof
? Z_DATA_ERROR
: Z_OK
;
372 /* ===========================================================================
373 * Cleanup then free the given gz_stream. Return a zlib error code.
374 Try freeing in the reverse order of allocations.
377 destroy(gz_stream
*s
)
381 if (!s
) return Z_STREAM_ERROR
;
385 if (s
->stream
.state
!= NULL
) {
386 if (s
->mode
== 'w') {
388 err
= Z_STREAM_ERROR
;
390 err
= deflateEnd(&(s
->stream
));
392 } else if (s
->mode
== 'r') {
393 err
= inflateEnd(&(s
->stream
));
396 if (s
->file
!= NULL
&& fclose(s
->file
)) {
398 if (errno
!= ESPIPE
) /* fclose is broken for pipes in HP/UX */
402 if (s
->z_err
< 0) err
= s
->z_err
;
411 /* ===========================================================================
412 Reads the given number of uncompressed bytes from the compressed file.
413 gzread returns the number of bytes actually read (0 for end of file).
416 gzread(gzFile file
, voidp buf
, unsigned len
)
418 gz_stream
*s
= (gz_stream
*)file
;
419 Bytef
*start
= (Bytef
*)buf
; /* starting point for crc computation */
420 Byte
*next_out
; /* == stream.next_out but not forced far (for MSDOS) */
422 if (s
== NULL
|| s
->mode
!= 'r') return Z_STREAM_ERROR
;
424 if (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
) return -1;
425 if (s
->z_err
== Z_STREAM_END
) return 0; /* EOF */
427 next_out
= (Byte
*)buf
;
428 s
->stream
.next_out
= (Bytef
*)buf
;
429 s
->stream
.avail_out
= len
;
431 if (s
->stream
.avail_out
&& s
->back
!= EOF
) {
432 *next_out
++ = s
->back
;
433 s
->stream
.next_out
++;
434 s
->stream
.avail_out
--;
439 s
->z_err
= Z_STREAM_END
;
444 while (s
->stream
.avail_out
!= 0) {
446 if (s
->transparent
) {
447 /* Copy first the lookahead bytes: */
448 uInt n
= s
->stream
.avail_in
;
449 if (n
> s
->stream
.avail_out
) n
= s
->stream
.avail_out
;
451 zmemcpy(s
->stream
.next_out
, s
->stream
.next_in
, n
);
453 s
->stream
.next_out
= next_out
;
454 s
->stream
.next_in
+= n
;
455 s
->stream
.avail_out
-= n
;
456 s
->stream
.avail_in
-= n
;
458 if (s
->stream
.avail_out
> 0) {
459 s
->stream
.avail_out
-=
460 (uInt
)fread(next_out
, 1, s
->stream
.avail_out
, s
->file
);
462 len
-= s
->stream
.avail_out
;
465 if (len
== 0) s
->z_eof
= 1;
468 if (s
->stream
.avail_in
== 0 && !s
->z_eof
) {
471 s
->stream
.avail_in
= (uInt
)fread(s
->inbuf
, 1, Z_BUFSIZE
, s
->file
);
472 if (s
->stream
.avail_in
== 0) {
474 if (ferror(s
->file
)) {
479 s
->stream
.next_in
= s
->inbuf
;
481 s
->in
+= s
->stream
.avail_in
;
482 s
->out
+= s
->stream
.avail_out
;
483 s
->z_err
= inflate(&(s
->stream
), Z_NO_FLUSH
);
484 s
->in
-= s
->stream
.avail_in
;
485 s
->out
-= s
->stream
.avail_out
;
487 if (s
->z_err
== Z_STREAM_END
) {
488 /* Check CRC and original size */
489 s
->crc
= z_crc32(s
->crc
, start
, (uInt
)(s
->stream
.next_out
- start
));
490 start
= s
->stream
.next_out
;
492 if (getLong(s
) != s
->crc
) {
493 s
->z_err
= Z_DATA_ERROR
;
496 /* The uncompressed length returned by above getlong() may be
497 * different from s->out in case of concatenated .gz files.
498 * Check for such files:
501 if (s
->z_err
== Z_OK
) {
502 inflateReset(&(s
->stream
));
503 s
->crc
= z_crc32(0L, Z_NULL
, 0);
507 if (s
->z_err
!= Z_OK
|| s
->z_eof
) break;
509 s
->crc
= z_crc32(s
->crc
, start
, (uInt
)(s
->stream
.next_out
- start
));
511 if (len
== s
->stream
.avail_out
&&
512 (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
))
514 return (int)(len
- s
->stream
.avail_out
);
518 /* ===========================================================================
519 Reads one byte from the compressed file. gzgetc returns this byte
520 or -1 in case of end of file or error.
527 return gzread(file
, &c
, 1) == 1 ? c
: -1;
531 /* ===========================================================================
532 Push one byte back onto the stream.
535 gzungetc(int c
, gzFile file
)
537 gz_stream
*s
= (gz_stream
*)file
;
539 if (s
== NULL
|| s
->mode
!= 'r' || c
== EOF
|| s
->back
!= EOF
) return EOF
;
542 s
->last
= (s
->z_err
== Z_STREAM_END
);
543 if (s
->last
) s
->z_err
= Z_OK
;
549 /* ===========================================================================
550 Reads bytes from the compressed file until len-1 characters are
551 read, or a newline character is read and transferred to buf, or an
552 end-of-file condition is encountered. The string is then terminated
553 with a null character.
554 gzgets returns buf, or Z_NULL in case of error.
556 The current implementation is not optimized at all.
559 gzgets(gzFile file
, char *buf
, int len
)
562 if (buf
== Z_NULL
|| len
<= 0) return Z_NULL
;
564 while (--len
> 0 && gzread(file
, buf
, 1) == 1 && *buf
++ != '\n') ;
566 return b
== buf
&& len
> 0 ? Z_NULL
: b
;
570 #ifndef NO_GZCOMPRESS
571 /* ===========================================================================
572 Writes the given number of uncompressed bytes into the compressed file.
573 gzwrite returns the number of bytes actually written (0 in case of error).
576 gzwrite(gzFile file
, voidpc buf
, unsigned len
)
578 gz_stream
*s
= (gz_stream
*)file
;
580 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
582 s
->stream
.next_in
= (Bytef
*)buf
;
583 s
->stream
.avail_in
= len
;
585 while (s
->stream
.avail_in
!= 0) {
587 if (s
->stream
.avail_out
== 0) {
589 s
->stream
.next_out
= s
->outbuf
;
590 if (fwrite(s
->outbuf
, 1, Z_BUFSIZE
, s
->file
) != Z_BUFSIZE
) {
594 s
->stream
.avail_out
= Z_BUFSIZE
;
596 s
->in
+= s
->stream
.avail_in
;
597 s
->out
+= s
->stream
.avail_out
;
598 s
->z_err
= deflate(&(s
->stream
), Z_NO_FLUSH
);
599 s
->in
-= s
->stream
.avail_in
;
600 s
->out
-= s
->stream
.avail_out
;
601 if (s
->z_err
!= Z_OK
) break;
603 s
->crc
= z_crc32(s
->crc
, (const Bytef
*)buf
, len
);
605 return (int)(len
- s
->stream
.avail_in
);
609 /* ===========================================================================
610 Converts, formats, and writes the args to the compressed file under
611 control of the format string, as in fprintf. gzprintf returns the number of
612 uncompressed bytes actually written (0 in case of error).
618 gzprintf(gzFile file
, const char *format
, /* args */ ...)
620 char buf
[Z_PRINTF_BUFSIZE
];
624 buf
[sizeof(buf
) - 1] = 0;
625 va_start(va
, format
);
627 # ifdef HAS_vsprintf_void
628 (void)vsprintf(buf
, format
, va
);
630 for (len
= 0; len
< sizeof(buf
); len
++)
631 if (buf
[len
] == 0) break;
633 len
= vsprintf(buf
, format
, va
);
637 # ifdef HAS_vsnprintf_void
638 (void)vsnprintf(buf
, sizeof(buf
), format
, va
);
642 len
= vsnprintf(buf
, sizeof(buf
), format
, va
);
646 if (len
<= 0 || len
>= (int)sizeof(buf
) || buf
[sizeof(buf
) - 1] != 0)
648 return gzwrite(file
, buf
, (unsigned)len
);
650 #else /* not ANSI C */
653 gzprintf(gzFile file
, const char *format
, int a1
, int a2
, int a3
, int a4
,
654 int a5
, int a6
, int a7
, int a8
, int a9
, int a10
, int a11
, int a12
,
655 int a13
, int a14
, int a15
, int a16
, int a17
, int a18
, int a19
, int a20
)
657 char buf
[Z_PRINTF_BUFSIZE
];
660 buf
[sizeof(buf
) - 1] = 0;
662 # ifdef HAS_sprintf_void
663 sprintf(buf
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
664 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
665 for (len
= 0; len
< sizeof(buf
); len
++)
666 if (buf
[len
] == 0) break;
668 len
= sprintf(buf
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
669 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
672 # ifdef HAS_snprintf_void
673 snprintf(buf
, sizeof(buf
), format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
674 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
677 len
= snprintf(buf
, sizeof(buf
), format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
678 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
681 if (len
<= 0 || len
>= sizeof(buf
) || buf
[sizeof(buf
) - 1] != 0)
683 return gzwrite(file
, buf
, len
);
687 /* ===========================================================================
688 Writes c, converted to an unsigned char, into the compressed file.
689 gzputc returns the value that was written, or -1 in case of error.
692 gzputc(gzFile file
, int c
)
694 unsigned char cc
= (unsigned char) c
; /* required for big endian systems */
696 return gzwrite(file
, &cc
, 1) == 1 ? (int)cc
: -1;
700 /* ===========================================================================
701 Writes the given null-terminated string to the compressed file, excluding
702 the terminating null character.
703 gzputs returns the number of characters written, or -1 in case of error.
706 gzputs(gzFile file
, const char *s
)
708 return gzwrite(file
, (char*)s
, (unsigned)strlen(s
));
712 /* ===========================================================================
713 Flushes all pending output into the compressed file. The parameter
714 flush is as in the deflate() function.
717 do_flush(gzFile file
, int flush
)
721 gz_stream
*s
= (gz_stream
*)file
;
723 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
725 s
->stream
.avail_in
= 0; /* should be zero already anyway */
728 len
= Z_BUFSIZE
- s
->stream
.avail_out
;
731 if ((uInt
)fwrite(s
->outbuf
, 1, len
, s
->file
) != len
) {
735 s
->stream
.next_out
= s
->outbuf
;
736 s
->stream
.avail_out
= Z_BUFSIZE
;
739 s
->out
+= s
->stream
.avail_out
;
740 s
->z_err
= deflate(&(s
->stream
), flush
);
741 s
->out
-= s
->stream
.avail_out
;
743 /* Ignore the second of two consecutive flushes: */
744 if (len
== 0 && s
->z_err
== Z_BUF_ERROR
) s
->z_err
= Z_OK
;
746 /* deflate has finished flushing only when it hasn't used up
747 * all the available space in the output buffer:
749 done
= (s
->stream
.avail_out
!= 0 || s
->z_err
== Z_STREAM_END
);
751 if (s
->z_err
!= Z_OK
&& s
->z_err
!= Z_STREAM_END
) break;
753 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
757 gzflush(gzFile file
, int flush
)
759 gz_stream
*s
= (gz_stream
*)file
;
760 int err
= do_flush (file
, flush
);
764 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
766 #endif /* NO_GZCOMPRESS */
768 /* ===========================================================================
769 Sets the starting position for the next gzread or gzwrite on the given
770 compressed file. The offset represents a number of bytes in the
771 gzseek returns the resulting offset location as measured in bytes from
772 the beginning of the uncompressed stream, or -1 in case of error.
773 SEEK_END is not implemented, returns error.
774 In this version of the library, gzseek can be extremely slow.
777 gzseek(gzFile file
, z_off_t offset
, int whence
)
779 gz_stream
*s
= (gz_stream
*)file
;
781 if (s
== NULL
|| whence
== SEEK_END
||
782 s
->z_err
== Z_ERRNO
|| s
->z_err
== Z_DATA_ERROR
) {
786 if (s
->mode
== 'w') {
790 if (whence
== SEEK_SET
) {
793 if (offset
< 0) return -1L;
795 /* At this point, offset is the number of zero bytes to write. */
796 if (s
->inbuf
== Z_NULL
) {
797 s
->inbuf
= (Byte
*)ALLOC(Z_BUFSIZE
); /* for seeking */
798 if (s
->inbuf
== Z_NULL
) return -1L;
799 zmemzero(s
->inbuf
, Z_BUFSIZE
);
802 uInt size
= Z_BUFSIZE
;
803 if (offset
< Z_BUFSIZE
) size
= (uInt
)offset
;
805 size
= gzwrite(file
, s
->inbuf
, size
);
806 if (size
== 0) return -1L;
813 /* Rest of function is for reading only */
815 /* compute absolute position */
816 if (whence
== SEEK_CUR
) {
819 if (offset
< 0) return -1L;
821 if (s
->transparent
) {
824 s
->stream
.avail_in
= 0;
825 s
->stream
.next_in
= s
->inbuf
;
826 if (fseek(s
->file
, offset
, SEEK_SET
) < 0) return -1L;
828 s
->in
= s
->out
= offset
;
832 /* For a negative seek, rewind and use positive seek */
833 if (offset
>= s
->out
) {
835 } else if (gzrewind(file
) < 0) {
838 /* offset is now the number of bytes to skip. */
840 if (offset
!= 0 && s
->outbuf
== Z_NULL
) {
841 s
->outbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
842 if (s
->outbuf
== Z_NULL
) return -1L;
844 if (offset
&& s
->back
!= EOF
) {
848 if (s
->last
) s
->z_err
= Z_STREAM_END
;
851 int size
= Z_BUFSIZE
;
852 if (offset
< Z_BUFSIZE
) size
= (int)offset
;
854 size
= gzread(file
, s
->outbuf
, (uInt
)size
);
855 if (size
<= 0) return -1L;
861 /* ===========================================================================
865 gzrewind(gzFile file
)
867 gz_stream
*s
= (gz_stream
*)file
;
869 if (s
== NULL
|| s
->mode
!= 'r') return -1;
874 s
->stream
.avail_in
= 0;
875 s
->stream
.next_in
= s
->inbuf
;
876 s
->crc
= z_crc32(0L, Z_NULL
, 0);
877 if (!s
->transparent
) (void)inflateReset(&s
->stream
);
880 return fseek(s
->file
, s
->start
, SEEK_SET
);
883 /* ===========================================================================
884 Returns the starting position for the next gzread or gzwrite on the
885 given compressed file. This position represents a number of bytes in the
886 uncompressed data stream.
891 return gzseek(file
, 0L, SEEK_CUR
);
894 /* ===========================================================================
895 Returns 1 when EOF has previously been detected reading the given
896 input stream, otherwise zero.
901 gz_stream
*s
= (gz_stream
*)file
;
903 /* With concatenated compressed files that can have embedded
904 * crc trailers, z_eof is no longer the only/best indicator of EOF
905 * on a gz_stream. Handle end-of-stream error explicitly here.
907 if (s
== NULL
|| s
->mode
!= 'r') return 0;
908 if (s
->z_eof
) return 1;
909 return s
->z_err
== Z_STREAM_END
;
912 /* ===========================================================================
913 Returns 1 if reading and doing so transparently, otherwise zero.
916 gzdirect(gzFile file
)
918 gz_stream
*s
= (gz_stream
*)file
;
920 if (s
== NULL
|| s
->mode
!= 'r') return 0;
921 return s
->transparent
;
924 /* ===========================================================================
925 Outputs a long in LSB order to the given file
928 putLong(FILE *file
, uLong x
)
931 for (n
= 0; n
< 4; n
++) {
932 fputc((int)(x
& 0xff), file
);
937 /* ===========================================================================
938 Reads a long in LSB order from the given gz_stream. Sets z_err in case
942 getLong(gz_stream
*s
)
944 uLong x
= (uLong
)get_byte(s
);
947 x
+= ((uLong
)get_byte(s
))<<8;
948 x
+= ((uLong
)get_byte(s
))<<16;
950 if (c
== EOF
) s
->z_err
= Z_DATA_ERROR
;
955 /* ===========================================================================
956 Flushes all pending output if necessary, closes the compressed file
957 and deallocates all the (de)compression state.
962 gz_stream
*s
= (gz_stream
*)file
;
964 if (s
== NULL
) return Z_STREAM_ERROR
;
966 if (s
->mode
== 'w') {
968 return Z_STREAM_ERROR
;
970 if (do_flush (file
, Z_FINISH
) != Z_OK
)
971 return destroy((gz_stream
*)file
);
973 putLong (s
->file
, s
->crc
);
974 putLong (s
->file
, (uLong
)(s
->in
& 0xffffffff));
977 return destroy((gz_stream
*)file
);
981 # define zstrerror(errnum) strerror(errnum)
983 # define zstrerror(errnum) ""
986 /* ===========================================================================
987 Returns the error message for the last error which occurred on the
988 given compressed file. errnum is set to zlib error number. If an
989 error occurred in the file system and not in the compression library,
990 errnum is set to Z_ERRNO and the application may consult errno
991 to get the exact error code.
994 gzerror(gzFile file
, int *errnum
)
997 gz_stream
*s
= (gz_stream
*)file
;
1000 *errnum
= Z_STREAM_ERROR
;
1001 return (const char*)ERR_MSG(Z_STREAM_ERROR
);
1004 if (*errnum
== Z_OK
) return (const char*)"";
1006 m
= (char*)(*errnum
== Z_ERRNO
? zstrerror(errno
) : s
->stream
.msg
);
1008 if (m
== NULL
|| *m
== '\0') m
= (char*)ERR_MSG(s
->z_err
);
1011 s
->msg
= (char*)ALLOC(strlen(s
->path
) + strlen(m
) + 3);
1012 if (s
->msg
== Z_NULL
) return (const char*)ERR_MSG(Z_MEM_ERROR
);
1013 strcpy(s
->msg
, s
->path
);
1014 strcat(s
->msg
, ": ");
1016 return (const char*)s
->msg
;
1019 /* ===========================================================================
1020 Clear the error and end-of-file flags, and do the same for the real file.
1023 gzclearerr(gzFile file
)
1025 gz_stream
*s
= (gz_stream
*)file
;
1027 if (s
== NULL
) return;
1028 if (s
->z_err
!= Z_STREAM_END
) s
->z_err
= Z_OK
;