]>
git.saurik.com Git - apple/xnu.git/blob - libkern/zlib/gzio.c
2 * Copyright (c) 2008 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).
120 local gzFile
gz_open (path
, mode
, fd
)
126 int level
= Z_DEFAULT_COMPRESSION
; /* compression level */
127 int strategy
= Z_DEFAULT_STRATEGY
; /* compression strategy */
128 char *p
= (char*)mode
;
130 char fmode
[80]; /* copy of mode, without the compression level */
133 if (!path
|| !mode
) return Z_NULL
;
135 s
= (gz_stream
*)ALLOC(sizeof(gz_stream
));
136 if (!s
) return Z_NULL
;
138 s
->stream
.zalloc
= (alloc_func
)0;
139 s
->stream
.zfree
= (free_func
)0;
140 s
->stream
.opaque
= (voidpf
)0;
141 s
->stream
.next_in
= s
->inbuf
= Z_NULL
;
142 s
->stream
.next_out
= s
->outbuf
= Z_NULL
;
143 s
->stream
.avail_in
= s
->stream
.avail_out
= 0;
150 s
->crc
= z_crc32(0L, Z_NULL
, 0);
154 s
->path
= (char*)ALLOC(strlen(path
)+1);
155 if (s
->path
== NULL
) {
156 return destroy(s
), (gzFile
)Z_NULL
;
158 strcpy(s
->path
, path
); /* do this early for debugging */
162 if (*p
== 'r') s
->mode
= 'r';
163 if (*p
== 'w' || *p
== 'a') s
->mode
= 'w';
164 if (*p
>= '0' && *p
<= '9') {
166 } else if (*p
== 'f') {
167 strategy
= Z_FILTERED
;
168 } else if (*p
== 'h') {
169 strategy
= Z_HUFFMAN_ONLY
;
170 } else if (*p
== 'R') {
173 *m
++ = *p
; /* copy the mode */
175 } while (*p
++ && m
!= fmode
+ sizeof(fmode
));
176 if (s
->mode
== '\0') return destroy(s
), (gzFile
)Z_NULL
;
178 if (s
->mode
== 'w') {
180 err
= Z_STREAM_ERROR
;
182 err
= deflateInit2(&(s
->stream
), level
,
183 Z_DEFLATED
, -MAX_WBITS
, DEF_MEM_LEVEL
, strategy
);
184 /* windowBits is passed < 0 to suppress zlib header */
186 s
->stream
.next_out
= s
->outbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
188 if (err
!= Z_OK
|| s
->outbuf
== Z_NULL
) {
189 return destroy(s
), (gzFile
)Z_NULL
;
192 s
->stream
.next_in
= s
->inbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
194 err
= inflateInit2(&(s
->stream
), -MAX_WBITS
);
195 /* windowBits is passed < 0 to tell that there is no zlib header.
196 * Note that in this case inflate *requires* an extra "dummy" byte
197 * after the compressed stream in order to complete decompression and
198 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
199 * present after the compressed stream.
201 if (err
!= Z_OK
|| s
->inbuf
== Z_NULL
) {
202 return destroy(s
), (gzFile
)Z_NULL
;
205 s
->stream
.avail_out
= Z_BUFSIZE
;
208 s
->file
= fd
< 0 ? F_OPEN(path
, fmode
) : (FILE*)fdopen(fd
, fmode
);
210 if (s
->file
== NULL
) {
211 return destroy(s
), (gzFile
)Z_NULL
;
213 if (s
->mode
== 'w') {
214 /* Write a very simple .gz header:
216 fprintf(s
->file
, "%c%c%c%c%c%c%c%c%c%c", gz_magic
[0], gz_magic
[1],
217 Z_DEFLATED
, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE
);
219 /* We use 10L instead of ftell(s->file) to because ftell causes an
220 * fflush on some systems. This version of the library doesn't use
221 * start anyway in write mode, so this initialization is not
225 check_header(s
); /* skip the .gz header */
226 s
->start
= ftell(s
->file
) - s
->stream
.avail_in
;
232 /* ===========================================================================
233 Opens a gzip (.gz) file for reading or writing.
235 gzFile ZEXPORT
gzopen (path
, mode
)
239 return gz_open (path
, mode
, -1);
242 /* ===========================================================================
243 Associate a gzFile with the file descriptor fd. fd is not dup'ed here
244 to mimic the behavio(u)r of fdopen.
246 gzFile ZEXPORT
gzdopen (fd
, mode
)
250 char name
[46]; /* allow for up to 128-bit integers */
252 if (fd
< 0) return (gzFile
)Z_NULL
;
253 sprintf(name
, "<fd:%d>", fd
); /* for debugging */
255 return gz_open (name
, mode
, fd
);
258 /* ===========================================================================
259 * Update the compression level and strategy
261 int ZEXPORT
gzsetparams (file
, level
, strategy
)
266 gz_stream
*s
= (gz_stream
*)file
;
268 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
270 /* Make room to allow flushing */
271 if (s
->stream
.avail_out
== 0) {
273 s
->stream
.next_out
= s
->outbuf
;
274 if (fwrite(s
->outbuf
, 1, Z_BUFSIZE
, s
->file
) != Z_BUFSIZE
) {
277 s
->stream
.avail_out
= Z_BUFSIZE
;
280 return deflateParams (&(s
->stream
), level
, strategy
);
283 /* ===========================================================================
284 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
286 IN assertion: the stream s has been sucessfully opened for reading.
288 local
int get_byte(s
)
291 if (s
->z_eof
) return EOF
;
292 if (s
->stream
.avail_in
== 0) {
294 s
->stream
.avail_in
= (uInt
)fread(s
->inbuf
, 1, Z_BUFSIZE
, s
->file
);
295 if (s
->stream
.avail_in
== 0) {
297 if (ferror(s
->file
)) s
->z_err
= Z_ERRNO
;
300 s
->stream
.next_in
= s
->inbuf
;
302 s
->stream
.avail_in
--;
303 return *(s
->stream
.next_in
)++;
306 /* ===========================================================================
307 Check the gzip header of a gz_stream opened for reading. Set the stream
308 mode to transparent if the gzip magic header is not present; set s->err
309 to Z_DATA_ERROR if the magic header is present but the rest of the header
311 IN assertion: the stream s has already been created sucessfully;
312 s->stream.avail_in is zero for the first time, but may be non-zero
313 for concatenated .gz files.
315 local
void check_header(s
)
318 int method
; /* method byte */
319 int flags
; /* flags byte */
323 /* Assure two bytes in the buffer so we can peek ahead -- handle case
324 where first byte of header is at the end of the buffer after the last
326 len
= s
->stream
.avail_in
;
328 if (len
) s
->inbuf
[0] = s
->stream
.next_in
[0];
330 len
= (uInt
)fread(s
->inbuf
+ len
, 1, Z_BUFSIZE
>> len
, s
->file
);
331 if (len
== 0 && ferror(s
->file
)) s
->z_err
= Z_ERRNO
;
332 s
->stream
.avail_in
+= len
;
333 s
->stream
.next_in
= s
->inbuf
;
334 if (s
->stream
.avail_in
< 2) {
335 s
->transparent
= s
->stream
.avail_in
;
340 /* Peek ahead to check the gzip magic header */
341 if (s
->stream
.next_in
[0] != gz_magic
[0] ||
342 s
->stream
.next_in
[1] != gz_magic
[1]) {
346 s
->stream
.avail_in
-= 2;
347 s
->stream
.next_in
+= 2;
349 /* Check the rest of the gzip header */
350 method
= get_byte(s
);
352 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0) {
353 s
->z_err
= Z_DATA_ERROR
;
357 /* Discard time, xflags and OS code: */
358 for (len
= 0; len
< 6; len
++) (void)get_byte(s
);
360 if ((flags
& EXTRA_FIELD
) != 0) { /* skip the extra field */
361 len
= (uInt
)get_byte(s
);
362 len
+= ((uInt
)get_byte(s
))<<8;
363 /* len is garbage if EOF but the loop below will quit anyway */
364 while (len
-- != 0 && get_byte(s
) != EOF
) ;
366 if ((flags
& ORIG_NAME
) != 0) { /* skip the original file name */
367 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) ;
369 if ((flags
& COMMENT
) != 0) { /* skip the .gz file comment */
370 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) ;
372 if ((flags
& HEAD_CRC
) != 0) { /* skip the header crc */
373 for (len
= 0; len
< 2; len
++) (void)get_byte(s
);
375 s
->z_err
= s
->z_eof
? Z_DATA_ERROR
: Z_OK
;
378 /* ===========================================================================
379 * Cleanup then free the given gz_stream. Return a zlib error code.
380 Try freeing in the reverse order of allocations.
382 local
int destroy (s
)
387 if (!s
) return Z_STREAM_ERROR
;
391 if (s
->stream
.state
!= NULL
) {
392 if (s
->mode
== 'w') {
394 err
= Z_STREAM_ERROR
;
396 err
= deflateEnd(&(s
->stream
));
398 } else if (s
->mode
== 'r') {
399 err
= inflateEnd(&(s
->stream
));
402 if (s
->file
!= NULL
&& fclose(s
->file
)) {
404 if (errno
!= ESPIPE
) /* fclose is broken for pipes in HP/UX */
408 if (s
->z_err
< 0) err
= s
->z_err
;
417 /* ===========================================================================
418 Reads the given number of uncompressed bytes from the compressed file.
419 gzread returns the number of bytes actually read (0 for end of file).
421 int ZEXPORT
gzread (file
, buf
, len
)
426 gz_stream
*s
= (gz_stream
*)file
;
427 Bytef
*start
= (Bytef
*)buf
; /* starting point for crc computation */
428 Byte
*next_out
; /* == stream.next_out but not forced far (for MSDOS) */
430 if (s
== NULL
|| s
->mode
!= 'r') return Z_STREAM_ERROR
;
432 if (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
) return -1;
433 if (s
->z_err
== Z_STREAM_END
) return 0; /* EOF */
435 next_out
= (Byte
*)buf
;
436 s
->stream
.next_out
= (Bytef
*)buf
;
437 s
->stream
.avail_out
= len
;
439 if (s
->stream
.avail_out
&& s
->back
!= EOF
) {
440 *next_out
++ = s
->back
;
441 s
->stream
.next_out
++;
442 s
->stream
.avail_out
--;
447 s
->z_err
= Z_STREAM_END
;
452 while (s
->stream
.avail_out
!= 0) {
454 if (s
->transparent
) {
455 /* Copy first the lookahead bytes: */
456 uInt n
= s
->stream
.avail_in
;
457 if (n
> s
->stream
.avail_out
) n
= s
->stream
.avail_out
;
459 zmemcpy(s
->stream
.next_out
, s
->stream
.next_in
, n
);
461 s
->stream
.next_out
= next_out
;
462 s
->stream
.next_in
+= n
;
463 s
->stream
.avail_out
-= n
;
464 s
->stream
.avail_in
-= n
;
466 if (s
->stream
.avail_out
> 0) {
467 s
->stream
.avail_out
-=
468 (uInt
)fread(next_out
, 1, s
->stream
.avail_out
, s
->file
);
470 len
-= s
->stream
.avail_out
;
473 if (len
== 0) s
->z_eof
= 1;
476 if (s
->stream
.avail_in
== 0 && !s
->z_eof
) {
479 s
->stream
.avail_in
= (uInt
)fread(s
->inbuf
, 1, Z_BUFSIZE
, s
->file
);
480 if (s
->stream
.avail_in
== 0) {
482 if (ferror(s
->file
)) {
487 s
->stream
.next_in
= s
->inbuf
;
489 s
->in
+= s
->stream
.avail_in
;
490 s
->out
+= s
->stream
.avail_out
;
491 s
->z_err
= inflate(&(s
->stream
), Z_NO_FLUSH
);
492 s
->in
-= s
->stream
.avail_in
;
493 s
->out
-= s
->stream
.avail_out
;
495 if (s
->z_err
== Z_STREAM_END
) {
496 /* Check CRC and original size */
497 s
->crc
= z_crc32(s
->crc
, start
, (uInt
)(s
->stream
.next_out
- start
));
498 start
= s
->stream
.next_out
;
500 if (getLong(s
) != s
->crc
) {
501 s
->z_err
= Z_DATA_ERROR
;
504 /* The uncompressed length returned by above getlong() may be
505 * different from s->out in case of concatenated .gz files.
506 * Check for such files:
509 if (s
->z_err
== Z_OK
) {
510 inflateReset(&(s
->stream
));
511 s
->crc
= z_crc32(0L, Z_NULL
, 0);
515 if (s
->z_err
!= Z_OK
|| s
->z_eof
) break;
517 s
->crc
= z_crc32(s
->crc
, start
, (uInt
)(s
->stream
.next_out
- start
));
519 if (len
== s
->stream
.avail_out
&&
520 (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
))
522 return (int)(len
- s
->stream
.avail_out
);
526 /* ===========================================================================
527 Reads one byte from the compressed file. gzgetc returns this byte
528 or -1 in case of end of file or error.
530 int ZEXPORT
gzgetc(file
)
535 return gzread(file
, &c
, 1) == 1 ? c
: -1;
539 /* ===========================================================================
540 Push one byte back onto the stream.
542 int ZEXPORT
gzungetc(c
, file
)
546 gz_stream
*s
= (gz_stream
*)file
;
548 if (s
== NULL
|| s
->mode
!= 'r' || c
== EOF
|| s
->back
!= EOF
) return EOF
;
551 s
->last
= (s
->z_err
== Z_STREAM_END
);
552 if (s
->last
) s
->z_err
= Z_OK
;
558 /* ===========================================================================
559 Reads bytes from the compressed file until len-1 characters are
560 read, or a newline character is read and transferred to buf, or an
561 end-of-file condition is encountered. The string is then terminated
562 with a null character.
563 gzgets returns buf, or Z_NULL in case of error.
565 The current implementation is not optimized at all.
567 char * ZEXPORT
gzgets(file
, buf
, len
)
573 if (buf
== Z_NULL
|| len
<= 0) return Z_NULL
;
575 while (--len
> 0 && gzread(file
, buf
, 1) == 1 && *buf
++ != '\n') ;
577 return b
== buf
&& len
> 0 ? Z_NULL
: b
;
581 #ifndef NO_GZCOMPRESS
582 /* ===========================================================================
583 Writes the given number of uncompressed bytes into the compressed file.
584 gzwrite returns the number of bytes actually written (0 in case of error).
586 int ZEXPORT
gzwrite (file
, buf
, len
)
591 gz_stream
*s
= (gz_stream
*)file
;
593 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
595 s
->stream
.next_in
= (Bytef
*)buf
;
596 s
->stream
.avail_in
= len
;
598 while (s
->stream
.avail_in
!= 0) {
600 if (s
->stream
.avail_out
== 0) {
602 s
->stream
.next_out
= s
->outbuf
;
603 if (fwrite(s
->outbuf
, 1, Z_BUFSIZE
, s
->file
) != Z_BUFSIZE
) {
607 s
->stream
.avail_out
= Z_BUFSIZE
;
609 s
->in
+= s
->stream
.avail_in
;
610 s
->out
+= s
->stream
.avail_out
;
611 s
->z_err
= deflate(&(s
->stream
), Z_NO_FLUSH
);
612 s
->in
-= s
->stream
.avail_in
;
613 s
->out
-= s
->stream
.avail_out
;
614 if (s
->z_err
!= Z_OK
) break;
616 s
->crc
= z_crc32(s
->crc
, (const Bytef
*)buf
, len
);
618 return (int)(len
- s
->stream
.avail_in
);
622 /* ===========================================================================
623 Converts, formats, and writes the args to the compressed file under
624 control of the format string, as in fprintf. gzprintf returns the number of
625 uncompressed bytes actually written (0 in case of error).
630 int ZEXPORTVA
gzprintf (gzFile file
, const char *format
, /* args */ ...)
632 char buf
[Z_PRINTF_BUFSIZE
];
636 buf
[sizeof(buf
) - 1] = 0;
637 va_start(va
, format
);
639 # ifdef HAS_vsprintf_void
640 (void)vsprintf(buf
, format
, va
);
642 for (len
= 0; len
< sizeof(buf
); len
++)
643 if (buf
[len
] == 0) break;
645 len
= vsprintf(buf
, format
, va
);
649 # ifdef HAS_vsnprintf_void
650 (void)vsnprintf(buf
, sizeof(buf
), format
, va
);
654 len
= vsnprintf(buf
, sizeof(buf
), format
, va
);
658 if (len
<= 0 || len
>= (int)sizeof(buf
) || buf
[sizeof(buf
) - 1] != 0)
660 return gzwrite(file
, buf
, (unsigned)len
);
662 #else /* not ANSI C */
664 int ZEXPORTVA
gzprintf (file
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
, a10
,
665 a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
)
668 int a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
, a10
,
669 a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
;
671 char buf
[Z_PRINTF_BUFSIZE
];
674 buf
[sizeof(buf
) - 1] = 0;
676 # ifdef HAS_sprintf_void
677 sprintf(buf
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
678 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
679 for (len
= 0; len
< sizeof(buf
); len
++)
680 if (buf
[len
] == 0) break;
682 len
= sprintf(buf
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
683 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
686 # ifdef HAS_snprintf_void
687 snprintf(buf
, sizeof(buf
), format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
688 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
691 len
= snprintf(buf
, sizeof(buf
), format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
692 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
695 if (len
<= 0 || len
>= sizeof(buf
) || buf
[sizeof(buf
) - 1] != 0)
697 return gzwrite(file
, buf
, len
);
701 /* ===========================================================================
702 Writes c, converted to an unsigned char, into the compressed file.
703 gzputc returns the value that was written, or -1 in case of error.
705 int ZEXPORT
gzputc(file
, c
)
709 unsigned char cc
= (unsigned char) c
; /* required for big endian systems */
711 return gzwrite(file
, &cc
, 1) == 1 ? (int)cc
: -1;
715 /* ===========================================================================
716 Writes the given null-terminated string to the compressed file, excluding
717 the terminating null character.
718 gzputs returns the number of characters written, or -1 in case of error.
720 int ZEXPORT
gzputs(file
, s
)
724 return gzwrite(file
, (char*)s
, (unsigned)strlen(s
));
728 /* ===========================================================================
729 Flushes all pending output into the compressed file. The parameter
730 flush is as in the deflate() function.
732 local
int do_flush (file
, flush
)
738 gz_stream
*s
= (gz_stream
*)file
;
740 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
742 s
->stream
.avail_in
= 0; /* should be zero already anyway */
745 len
= Z_BUFSIZE
- s
->stream
.avail_out
;
748 if ((uInt
)fwrite(s
->outbuf
, 1, len
, s
->file
) != len
) {
752 s
->stream
.next_out
= s
->outbuf
;
753 s
->stream
.avail_out
= Z_BUFSIZE
;
756 s
->out
+= s
->stream
.avail_out
;
757 s
->z_err
= deflate(&(s
->stream
), flush
);
758 s
->out
-= s
->stream
.avail_out
;
760 /* Ignore the second of two consecutive flushes: */
761 if (len
== 0 && s
->z_err
== Z_BUF_ERROR
) s
->z_err
= Z_OK
;
763 /* deflate has finished flushing only when it hasn't used up
764 * all the available space in the output buffer:
766 done
= (s
->stream
.avail_out
!= 0 || s
->z_err
== Z_STREAM_END
);
768 if (s
->z_err
!= Z_OK
&& s
->z_err
!= Z_STREAM_END
) break;
770 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
773 int ZEXPORT
gzflush (file
, flush
)
777 gz_stream
*s
= (gz_stream
*)file
;
778 int err
= do_flush (file
, flush
);
782 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
784 #endif /* NO_GZCOMPRESS */
786 /* ===========================================================================
787 Sets the starting position for the next gzread or gzwrite on the given
788 compressed file. The offset represents a number of bytes in the
789 gzseek returns the resulting offset location as measured in bytes from
790 the beginning of the uncompressed stream, or -1 in case of error.
791 SEEK_END is not implemented, returns error.
792 In this version of the library, gzseek can be extremely slow.
794 z_off_t ZEXPORT
gzseek (file
, offset
, whence
)
799 gz_stream
*s
= (gz_stream
*)file
;
801 if (s
== NULL
|| whence
== SEEK_END
||
802 s
->z_err
== Z_ERRNO
|| s
->z_err
== Z_DATA_ERROR
) {
806 if (s
->mode
== 'w') {
810 if (whence
== SEEK_SET
) {
813 if (offset
< 0) return -1L;
815 /* At this point, offset is the number of zero bytes to write. */
816 if (s
->inbuf
== Z_NULL
) {
817 s
->inbuf
= (Byte
*)ALLOC(Z_BUFSIZE
); /* for seeking */
818 if (s
->inbuf
== Z_NULL
) return -1L;
819 zmemzero(s
->inbuf
, Z_BUFSIZE
);
822 uInt size
= Z_BUFSIZE
;
823 if (offset
< Z_BUFSIZE
) size
= (uInt
)offset
;
825 size
= gzwrite(file
, s
->inbuf
, size
);
826 if (size
== 0) return -1L;
833 /* Rest of function is for reading only */
835 /* compute absolute position */
836 if (whence
== SEEK_CUR
) {
839 if (offset
< 0) return -1L;
841 if (s
->transparent
) {
844 s
->stream
.avail_in
= 0;
845 s
->stream
.next_in
= s
->inbuf
;
846 if (fseek(s
->file
, offset
, SEEK_SET
) < 0) return -1L;
848 s
->in
= s
->out
= offset
;
852 /* For a negative seek, rewind and use positive seek */
853 if (offset
>= s
->out
) {
855 } else if (gzrewind(file
) < 0) {
858 /* offset is now the number of bytes to skip. */
860 if (offset
!= 0 && s
->outbuf
== Z_NULL
) {
861 s
->outbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
862 if (s
->outbuf
== Z_NULL
) return -1L;
864 if (offset
&& s
->back
!= EOF
) {
868 if (s
->last
) s
->z_err
= Z_STREAM_END
;
871 int size
= Z_BUFSIZE
;
872 if (offset
< Z_BUFSIZE
) size
= (int)offset
;
874 size
= gzread(file
, s
->outbuf
, (uInt
)size
);
875 if (size
<= 0) return -1L;
881 /* ===========================================================================
884 int ZEXPORT
gzrewind (file
)
887 gz_stream
*s
= (gz_stream
*)file
;
889 if (s
== NULL
|| s
->mode
!= 'r') return -1;
894 s
->stream
.avail_in
= 0;
895 s
->stream
.next_in
= s
->inbuf
;
896 s
->crc
= z_crc32(0L, Z_NULL
, 0);
897 if (!s
->transparent
) (void)inflateReset(&s
->stream
);
900 return fseek(s
->file
, s
->start
, SEEK_SET
);
903 /* ===========================================================================
904 Returns the starting position for the next gzread or gzwrite on the
905 given compressed file. This position represents a number of bytes in the
906 uncompressed data stream.
908 z_off_t ZEXPORT
gztell (file
)
911 return gzseek(file
, 0L, SEEK_CUR
);
914 /* ===========================================================================
915 Returns 1 when EOF has previously been detected reading the given
916 input stream, otherwise zero.
918 int ZEXPORT
gzeof (file
)
921 gz_stream
*s
= (gz_stream
*)file
;
923 /* With concatenated compressed files that can have embedded
924 * crc trailers, z_eof is no longer the only/best indicator of EOF
925 * on a gz_stream. Handle end-of-stream error explicitly here.
927 if (s
== NULL
|| s
->mode
!= 'r') return 0;
928 if (s
->z_eof
) return 1;
929 return s
->z_err
== Z_STREAM_END
;
932 /* ===========================================================================
933 Returns 1 if reading and doing so transparently, otherwise zero.
935 int ZEXPORT
gzdirect (file
)
938 gz_stream
*s
= (gz_stream
*)file
;
940 if (s
== NULL
|| s
->mode
!= 'r') return 0;
941 return s
->transparent
;
944 /* ===========================================================================
945 Outputs a long in LSB order to the given file
947 local
void putLong (file
, x
)
952 for (n
= 0; n
< 4; n
++) {
953 fputc((int)(x
& 0xff), file
);
958 /* ===========================================================================
959 Reads a long in LSB order from the given gz_stream. Sets z_err in case
962 local uLong
getLong (s
)
965 uLong x
= (uLong
)get_byte(s
);
968 x
+= ((uLong
)get_byte(s
))<<8;
969 x
+= ((uLong
)get_byte(s
))<<16;
971 if (c
== EOF
) s
->z_err
= Z_DATA_ERROR
;
976 /* ===========================================================================
977 Flushes all pending output if necessary, closes the compressed file
978 and deallocates all the (de)compression state.
980 int ZEXPORT
gzclose (file
)
983 gz_stream
*s
= (gz_stream
*)file
;
985 if (s
== NULL
) return Z_STREAM_ERROR
;
987 if (s
->mode
== 'w') {
989 return Z_STREAM_ERROR
;
991 if (do_flush (file
, Z_FINISH
) != Z_OK
)
992 return destroy((gz_stream
*)file
);
994 putLong (s
->file
, s
->crc
);
995 putLong (s
->file
, (uLong
)(s
->in
& 0xffffffff));
998 return destroy((gz_stream
*)file
);
1002 # define zstrerror(errnum) strerror(errnum)
1004 # define zstrerror(errnum) ""
1007 /* ===========================================================================
1008 Returns the error message for the last error which occurred on the
1009 given compressed file. errnum is set to zlib error number. If an
1010 error occurred in the file system and not in the compression library,
1011 errnum is set to Z_ERRNO and the application may consult errno
1012 to get the exact error code.
1014 const char * ZEXPORT
gzerror (file
, errnum
)
1019 gz_stream
*s
= (gz_stream
*)file
;
1022 *errnum
= Z_STREAM_ERROR
;
1023 return (const char*)ERR_MSG(Z_STREAM_ERROR
);
1026 if (*errnum
== Z_OK
) return (const char*)"";
1028 m
= (char*)(*errnum
== Z_ERRNO
? zstrerror(errno
) : s
->stream
.msg
);
1030 if (m
== NULL
|| *m
== '\0') m
= (char*)ERR_MSG(s
->z_err
);
1033 s
->msg
= (char*)ALLOC(strlen(s
->path
) + strlen(m
) + 3);
1034 if (s
->msg
== Z_NULL
) return (const char*)ERR_MSG(Z_MEM_ERROR
);
1035 strcpy(s
->msg
, s
->path
);
1036 strcat(s
->msg
, ": ");
1038 return (const char*)s
->msg
;
1041 /* ===========================================================================
1042 Clear the error and end-of-file flags, and do the same for the real file.
1044 void ZEXPORT
gzclearerr (file
)
1047 gz_stream
*s
= (gz_stream
*)file
;
1049 if (s
== NULL
) return;
1050 if (s
->z_err
!= Z_STREAM_END
) s
->z_err
= Z_OK
;