1 #ifdef MONO_DOES_NOT_NEED_THIS
2 /* gzio.c -- IO on .gz files
3 * Copyright (C) 1995-2006 Jean-loup Gailly.
4 * For conditions of distribution and use, see copyright notice in zlib.h
6 * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
11 #ifdef _LARGEFILE64_SOURCE
12 # ifndef _LARGEFILE_SOURCE
13 # define _LARGEFILE_SOURCE
15 # ifdef _FILE_OFFSET_BITS
16 # undef _FILE_OFFSET_BITS
23 #ifdef NO_DEFLATE /* for compatibility with old definition */
24 # define NO_GZCOMPRESS
28 struct internal_state
{int dummy
;}; /* for buggy compilers */
33 # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
35 # define Z_BUFSIZE 16384
38 #ifndef Z_PRINTF_BUFSIZE
39 # define Z_PRINTF_BUFSIZE 4096
43 # pragma map (fdopen , "\174\174FDOPEN")
44 FILE *fdopen(int, const char *);
48 extern voidp malloc
OF((uInt size
));
49 extern void free
OF((voidpf ptr
));
60 #define ALLOC(size) malloc(size)
61 #define TRYFREE(p) {if (p) free(p);}
63 static int const gz_magic
[2] = {0x1f, 0x8b}; /* gzip magic header */
66 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
67 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
68 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
69 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
70 #define COMMENT 0x10 /* bit 4 set: file comment present */
71 #define RESERVED 0xE0 /* bits 5..7: reserved */
73 typedef struct gz_stream
{
75 int z_err
; /* error code for last stream operation */
76 int z_eof
; /* set if end of input file */
77 FILE *file
; /* .gz file */
78 Byte
*inbuf
; /* input buffer */
79 Byte
*outbuf
; /* output buffer */
80 uLong crc
; /* crc32 of uncompressed data */
81 char *msg
; /* error message */
82 char *path
; /* path name for debugging only */
83 int transparent
; /* 1 if input file is not a .gz file */
84 char mode
; /* 'w' or 'r' */
85 #ifdef _LARGEFILE64_SOURCE
86 off64_t start
; /* start of compressed data in file (header skipped) */
87 off64_t in
; /* bytes into deflate or inflate */
88 off64_t out
; /* bytes out of deflate or inflate */
90 z_off_t start
; /* start of compressed data in file (header skipped) */
91 z_off_t in
; /* bytes into deflate or inflate */
92 z_off_t out
; /* bytes out of deflate or inflate */
94 int back
; /* one character push-back */
95 int last
; /* true if push-back is last character */
99 local gzFile gz_open
OF((const char *path
, const char *mode
, int fd
,
101 #ifdef _LARGEFILE64_SOURCE
102 local off64_t gz_seek
OF((gzFile file
, off64_t offset
, int whence
, int use64
));
104 local z_off_t gz_seek
OF((gzFile file
, z_off_t offset
, int whence
, int use64
));
106 local
int do_flush
OF((gzFile file
, int flush
));
107 local
int get_byte
OF((gz_stream
*s
));
108 local
void check_header
OF((gz_stream
*s
));
109 local
int destroy
OF((gz_stream
*s
));
110 local
void putLong
OF((FILE *file
, uLong x
));
111 local uLong getLong
OF((gz_stream
*s
));
113 /* ===========================================================================
114 Opens a gzip (.gz) file for reading or writing. The mode parameter
115 is as in fopen ("rb" or "wb"). The file is given either by file descriptor
116 or path name (if fd == -1).
117 gz_open returns NULL if the file could not be opened or if there was
118 insufficient memory to allocate the (de)compression state; errno
119 can be checked to distinguish the two cases (if errno is zero, the
120 zlib error is Z_MEM_ERROR).
122 local gzFile
gz_open (path
, mode
, fd
, use64
)
129 int level
= Z_DEFAULT_COMPRESSION
; /* compression level */
130 int strategy
= Z_DEFAULT_STRATEGY
; /* compression strategy */
131 char *p
= (char*)mode
;
133 char fmode
[80]; /* copy of mode, without the compression level */
136 if (!path
|| !mode
) return Z_NULL
;
138 s
= (gz_stream
*)ALLOC(sizeof(gz_stream
));
139 if (!s
) return Z_NULL
;
141 s
->stream
.zalloc
= (alloc_func
)0;
142 s
->stream
.zfree
= (free_func
)0;
143 s
->stream
.opaque
= (voidpf
)0;
144 s
->stream
.next_in
= s
->inbuf
= Z_NULL
;
145 s
->stream
.next_out
= s
->outbuf
= Z_NULL
;
146 s
->stream
.avail_in
= s
->stream
.avail_out
= 0;
153 s
->crc
= crc32(0L, Z_NULL
, 0);
157 s
->path
= (char*)ALLOC(strlen(path
)+1);
158 if (s
->path
== NULL
) {
159 return destroy(s
), (gzFile
)Z_NULL
;
161 strcpy(s
->path
, path
); /* do this early for debugging */
165 if (*p
== 'r') s
->mode
= 'r';
166 if (*p
== 'w' || *p
== 'a') s
->mode
= 'w';
167 if (*p
>= '0' && *p
<= '9') {
169 } else if (*p
== 'f') {
170 strategy
= Z_FILTERED
;
171 } else if (*p
== 'h') {
172 strategy
= Z_HUFFMAN_ONLY
;
173 } else if (*p
== 'R') {
176 *m
++ = *p
; /* copy the mode */
178 } while (*p
++ && m
!= fmode
+ sizeof(fmode
));
179 if (s
->mode
== '\0') return destroy(s
), (gzFile
)Z_NULL
;
181 if (s
->mode
== 'w') {
183 err
= Z_STREAM_ERROR
;
185 err
= deflateInit2(&(s
->stream
), level
,
186 Z_DEFLATED
, -MAX_WBITS
, DEF_MEM_LEVEL
, strategy
);
187 /* windowBits is passed < 0 to suppress zlib header */
189 s
->stream
.next_out
= s
->outbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
191 if (err
!= Z_OK
|| s
->outbuf
== Z_NULL
) {
192 return destroy(s
), (gzFile
)Z_NULL
;
195 s
->stream
.next_in
= s
->inbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
197 err
= inflateInit2(&(s
->stream
), -MAX_WBITS
);
198 /* windowBits is passed < 0 to tell that there is no zlib header */
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 ? (use64
? F_OPEN64(path
, fmode
) : F_OPEN(path
, fmode
)) :
207 (FILE*)fdopen(fd
, fmode
);
209 if (s
->file
== NULL
) {
210 return destroy(s
), (gzFile
)Z_NULL
;
212 if (s
->mode
== 'w') {
213 /* Write a very simple .gz header:
215 fprintf(s
->file
, "%c%c%c%c%c%c%c%c%c%c", gz_magic
[0], gz_magic
[1],
216 Z_DEFLATED
, 0 /*flags*/, 0,0,0,0 /*time*/, level
== 9 ? 2 :
217 (strategy
>= Z_HUFFMAN_ONLY
||
218 (level
!= Z_DEFAULT_COMPRESSION
&& level
< 2) ?
219 4 : 0) /*xflags*/, OS_CODE
);
221 /* We use 10L instead of ftell(s->file) to because ftell causes an
222 * fflush on some systems. This version of the library doesn't use
223 * start anyway in write mode, so this initialization is not
227 check_header(s
); /* skip the .gz header */
228 s
->start
= FTELL(s
->file
) - s
->stream
.avail_in
;
234 /* ===========================================================================
235 Opens a gzip (.gz) file for reading or writing.
237 gzFile ZEXPORT
gzopen (path
, mode
)
241 return gz_open (path
, mode
, -1, 0);
244 /* ===========================================================================
245 Opens a gzip (.gz) file for reading or writing for 64-bit offsets
247 gzFile ZEXPORT
gzopen64 (path
, mode
)
251 return gz_open (path
, mode
, -1, 1);
254 /* ===========================================================================
255 Associate a gzFile with the file descriptor fd. fd is not dup'ed here
256 to mimic the behavio(u)r of fdopen.
258 gzFile ZEXPORT
gzdopen (fd
, mode
)
262 char name
[46]; /* allow for up to 128-bit integers */
264 if (fd
< 0) return (gzFile
)Z_NULL
;
265 sprintf(name
, "<fd:%d>", fd
); /* for debugging */
267 return gz_open (name
, mode
, fd
, 0);
270 /* ===========================================================================
271 * Update the compression level and strategy
273 int ZEXPORT
gzsetparams (file
, level
, strategy
)
278 gz_stream
*s
= (gz_stream
*)file
;
280 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
282 /* Make room to allow flushing */
283 if (s
->stream
.avail_out
== 0) {
285 s
->stream
.next_out
= s
->outbuf
;
286 if (fwrite(s
->outbuf
, 1, Z_BUFSIZE
, s
->file
) != Z_BUFSIZE
) {
289 s
->stream
.avail_out
= Z_BUFSIZE
;
292 return deflateParams (&(s
->stream
), level
, strategy
);
295 /* ===========================================================================
296 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
298 IN assertion: the stream s has been successfully opened for reading.
300 local
int get_byte(s
)
303 if (s
->z_eof
) return EOF
;
304 if (s
->stream
.avail_in
== 0) {
306 s
->stream
.avail_in
= (uInt
)fread(s
->inbuf
, 1, Z_BUFSIZE
, s
->file
);
307 if (s
->stream
.avail_in
== 0) {
309 if (ferror(s
->file
)) s
->z_err
= Z_ERRNO
;
312 s
->stream
.next_in
= s
->inbuf
;
314 s
->stream
.avail_in
--;
315 return *(s
->stream
.next_in
)++;
318 /* ===========================================================================
319 Check the gzip header of a gz_stream opened for reading. Set the stream
320 mode to transparent if the gzip magic header is not present; set s->err
321 to Z_DATA_ERROR if the magic header is present but the rest of the header
323 IN assertion: the stream s has already been created successfully;
324 s->stream.avail_in is zero for the first time, but may be non-zero
325 for concatenated .gz files.
327 local
void check_header(s
)
330 int method
; /* method byte */
331 int flags
; /* flags byte */
335 /* Assure two bytes in the buffer so we can peek ahead -- handle case
336 where first byte of header is at the end of the buffer after the last
338 len
= s
->stream
.avail_in
;
340 if (len
) s
->inbuf
[0] = s
->stream
.next_in
[0];
342 len
= (uInt
)fread(s
->inbuf
+ len
, 1, Z_BUFSIZE
>> len
, s
->file
);
343 if (len
== 0) s
->z_eof
= 1;
344 if (len
== 0 && ferror(s
->file
)) s
->z_err
= Z_ERRNO
;
345 s
->stream
.avail_in
+= len
;
346 s
->stream
.next_in
= s
->inbuf
;
347 if (s
->stream
.avail_in
< 2) {
348 s
->transparent
= s
->stream
.avail_in
;
353 /* Peek ahead to check the gzip magic header */
354 if (s
->stream
.next_in
[0] != gz_magic
[0] ||
355 s
->stream
.next_in
[1] != gz_magic
[1]) {
359 s
->stream
.avail_in
-= 2;
360 s
->stream
.next_in
+= 2;
362 /* Check the rest of the gzip header */
363 method
= get_byte(s
);
365 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0) {
366 s
->z_err
= Z_DATA_ERROR
;
370 /* Discard time, xflags and OS code: */
371 for (len
= 0; len
< 6; len
++) (void)get_byte(s
);
373 if ((flags
& EXTRA_FIELD
) != 0) { /* skip the extra field */
374 len
= (uInt
)get_byte(s
);
375 len
+= ((uInt
)get_byte(s
))<<8;
376 /* len is garbage if EOF but the loop below will quit anyway */
377 while (len
-- != 0 && get_byte(s
) != EOF
) ;
379 if ((flags
& ORIG_NAME
) != 0) { /* skip the original file name */
380 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) ;
382 if ((flags
& COMMENT
) != 0) { /* skip the .gz file comment */
383 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) ;
385 if ((flags
& HEAD_CRC
) != 0) { /* skip the header crc */
386 for (len
= 0; len
< 2; len
++) (void)get_byte(s
);
388 s
->z_err
= s
->z_eof
? Z_DATA_ERROR
: Z_OK
;
391 /* ===========================================================================
392 * Cleanup then free the given gz_stream. Return a zlib error code.
393 Try freeing in the reverse order of allocations.
395 local
int destroy (s
)
400 if (!s
) return Z_STREAM_ERROR
;
404 if (s
->stream
.state
!= NULL
) {
405 if (s
->mode
== 'w') {
407 err
= Z_STREAM_ERROR
;
409 err
= deflateEnd(&(s
->stream
));
411 } else if (s
->mode
== 'r') {
412 err
= inflateEnd(&(s
->stream
));
415 if (s
->file
!= NULL
&& fclose(s
->file
)) {
417 if (errno
!= ESPIPE
) /* fclose is broken for pipes in HP/UX */
421 if (s
->z_err
< 0) err
= s
->z_err
;
430 /* ===========================================================================
431 Reads the given number of uncompressed bytes from the compressed file.
432 gzread returns the number of bytes actually read (0 for end of file).
434 int ZEXPORT
gzread (file
, buf
, len
)
439 gz_stream
*s
= (gz_stream
*)file
;
440 Bytef
*start
= (Bytef
*)buf
; /* starting point for crc computation */
441 Byte
*next_out
; /* == stream.next_out but not forced far (for MSDOS) */
443 if (s
== NULL
|| s
->mode
!= 'r') return Z_STREAM_ERROR
;
445 if (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
) return -1;
446 if (s
->z_err
== Z_STREAM_END
) return 0; /* EOF */
448 next_out
= (Byte
*)buf
;
449 s
->stream
.next_out
= (Bytef
*)buf
;
450 s
->stream
.avail_out
= len
;
452 if (s
->stream
.avail_out
&& s
->back
!= EOF
) {
453 *next_out
++ = s
->back
;
454 s
->stream
.next_out
++;
455 s
->stream
.avail_out
--;
460 s
->z_err
= Z_STREAM_END
;
465 while (s
->stream
.avail_out
!= 0) {
467 if (s
->transparent
) {
468 /* Copy first the lookahead bytes: */
469 uInt n
= s
->stream
.avail_in
;
470 if (n
> s
->stream
.avail_out
) n
= s
->stream
.avail_out
;
472 zmemcpy(s
->stream
.next_out
, s
->stream
.next_in
, n
);
474 s
->stream
.next_out
= next_out
;
475 s
->stream
.next_in
+= n
;
476 s
->stream
.avail_out
-= n
;
477 s
->stream
.avail_in
-= n
;
479 if (s
->stream
.avail_out
> 0 && !feof(s
->file
)) {
480 s
->stream
.avail_out
-=
481 (uInt
)fread(next_out
, 1, s
->stream
.avail_out
, s
->file
);
483 len
-= s
->stream
.avail_out
;
486 if (len
== 0) s
->z_eof
= 1;
489 if (s
->stream
.avail_in
== 0 && !s
->z_eof
) {
492 s
->stream
.avail_in
= (uInt
)fread(s
->inbuf
, 1, Z_BUFSIZE
, s
->file
);
493 if (s
->stream
.avail_in
== 0) {
495 if (ferror(s
->file
)) {
500 s
->stream
.next_in
= s
->inbuf
;
502 s
->in
+= s
->stream
.avail_in
;
503 s
->out
+= s
->stream
.avail_out
;
504 s
->z_err
= inflate(&(s
->stream
), Z_NO_FLUSH
);
505 s
->in
-= s
->stream
.avail_in
;
506 s
->out
-= s
->stream
.avail_out
;
508 if (s
->z_err
== Z_STREAM_END
) {
509 /* Check CRC and original size */
510 s
->crc
= crc32(s
->crc
, start
, (uInt
)(s
->stream
.next_out
- start
));
511 start
= s
->stream
.next_out
;
513 if (getLong(s
) != s
->crc
) {
514 s
->z_err
= Z_DATA_ERROR
;
517 /* The uncompressed length returned by above getlong() may be
518 * different from s->out in case of concatenated .gz files.
519 * Check for such files:
522 if (s
->z_err
== Z_OK
) {
523 inflateReset(&(s
->stream
));
524 s
->crc
= crc32(0L, Z_NULL
, 0);
528 if (s
->z_err
!= Z_OK
|| s
->z_eof
) break;
530 s
->crc
= crc32(s
->crc
, start
, (uInt
)(s
->stream
.next_out
- start
));
532 if (len
== s
->stream
.avail_out
&&
533 (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
))
535 return (int)(len
- s
->stream
.avail_out
);
539 /* ===========================================================================
540 Reads one byte from the compressed file. gzgetc returns this byte
541 or -1 in case of end of file or error.
543 int ZEXPORT
gzgetc(file
)
548 return gzread(file
, &c
, 1) == 1 ? c
: -1;
552 /* ===========================================================================
553 Push one byte back onto the stream.
555 int ZEXPORT
gzungetc(c
, file
)
559 gz_stream
*s
= (gz_stream
*)file
;
561 if (s
== NULL
|| s
->mode
!= 'r' || c
== EOF
|| s
->back
!= EOF
) return EOF
;
564 s
->last
= (s
->z_err
== Z_STREAM_END
);
565 if (s
->last
) s
->z_err
= Z_OK
;
571 /* ===========================================================================
572 Reads bytes from the compressed file until len-1 characters are
573 read, or a newline character is read and transferred to buf, or an
574 end-of-file condition is encountered. The string is then terminated
575 with a null character.
576 gzgets returns buf, or Z_NULL in case of error.
578 The current implementation is not optimized at all.
580 char * ZEXPORT
gzgets(file
, buf
, len
)
586 if (buf
== Z_NULL
|| len
<= 0) return Z_NULL
;
588 while (--len
> 0 && gzread(file
, buf
, 1) == 1 && *buf
++ != '\n') ;
590 return b
== buf
&& len
> 0 ? Z_NULL
: b
;
594 #ifndef NO_GZCOMPRESS
595 /* ===========================================================================
596 Writes the given number of uncompressed bytes into the compressed file.
597 gzwrite returns the number of bytes actually written (0 in case of error).
599 int ZEXPORT
gzwrite (file
, buf
, len
)
604 gz_stream
*s
= (gz_stream
*)file
;
606 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
608 s
->stream
.next_in
= (Bytef
*)buf
;
609 s
->stream
.avail_in
= len
;
611 while (s
->stream
.avail_in
!= 0) {
613 if (s
->stream
.avail_out
== 0) {
615 s
->stream
.next_out
= s
->outbuf
;
616 if (fwrite(s
->outbuf
, 1, Z_BUFSIZE
, s
->file
) != Z_BUFSIZE
) {
620 s
->stream
.avail_out
= Z_BUFSIZE
;
622 s
->in
+= s
->stream
.avail_in
;
623 s
->out
+= s
->stream
.avail_out
;
624 s
->z_err
= deflate(&(s
->stream
), Z_NO_FLUSH
);
625 s
->in
-= s
->stream
.avail_in
;
626 s
->out
-= s
->stream
.avail_out
;
627 if (s
->z_err
!= Z_OK
) break;
629 s
->crc
= crc32(s
->crc
, (const Bytef
*)buf
, len
);
631 return (int)(len
- s
->stream
.avail_in
);
635 /* ===========================================================================
636 Converts, formats, and writes the args to the compressed file under
637 control of the format string, as in fprintf. gzprintf returns the number of
638 uncompressed bytes actually written (0 in case of error).
643 int ZEXPORTVA
gzprintf (gzFile file
, const char *format
, /* args */ ...)
645 char buf
[Z_PRINTF_BUFSIZE
];
649 buf
[sizeof(buf
) - 1] = 0;
650 va_start(va
, format
);
652 # ifdef HAS_vsprintf_void
653 (void)vsprintf(buf
, format
, va
);
655 for (len
= 0; len
< sizeof(buf
); len
++)
656 if (buf
[len
] == 0) break;
658 len
= vsprintf(buf
, format
, va
);
662 # ifdef HAS_vsnprintf_void
663 (void)vsnprintf(buf
, sizeof(buf
), format
, va
);
667 len
= vsnprintf(buf
, sizeof(buf
), format
, va
);
671 if (len
<= 0 || len
>= (int)sizeof(buf
) || buf
[sizeof(buf
) - 1] != 0)
673 return gzwrite(file
, buf
, (unsigned)len
);
675 #else /* not ANSI C */
677 int ZEXPORTVA
gzprintf (file
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
, a10
,
678 a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
)
681 int a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
, a10
,
682 a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
;
684 char buf
[Z_PRINTF_BUFSIZE
];
687 buf
[sizeof(buf
) - 1] = 0;
689 # ifdef HAS_sprintf_void
690 sprintf(buf
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
691 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
692 for (len
= 0; len
< sizeof(buf
); len
++)
693 if (buf
[len
] == 0) break;
695 len
= sprintf(buf
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
696 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
699 # ifdef HAS_snprintf_void
700 snprintf(buf
, sizeof(buf
), format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
701 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
704 len
= snprintf(buf
, sizeof(buf
), format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
705 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
708 if (len
<= 0 || len
>= sizeof(buf
) || buf
[sizeof(buf
) - 1] != 0)
710 return gzwrite(file
, buf
, len
);
714 /* ===========================================================================
715 Writes c, converted to an unsigned char, into the compressed file.
716 gzputc returns the value that was written, or -1 in case of error.
718 int ZEXPORT
gzputc(file
, c
)
722 unsigned char cc
= (unsigned char) c
; /* required for big endian systems */
724 return gzwrite(file
, &cc
, 1) == 1 ? (int)cc
: -1;
728 /* ===========================================================================
729 Writes the given null-terminated string to the compressed file, excluding
730 the terminating null character.
731 gzputs returns the number of characters written, or -1 in case of error.
733 int ZEXPORT
gzputs(file
, s
)
737 return gzwrite(file
, (char*)s
, (unsigned)strlen(s
));
741 /* ===========================================================================
742 Flushes all pending output into the compressed file. The parameter
743 flush is as in the deflate() function.
745 local
int do_flush (file
, flush
)
751 gz_stream
*s
= (gz_stream
*)file
;
753 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
755 s
->stream
.avail_in
= 0; /* should be zero already anyway */
758 len
= Z_BUFSIZE
- s
->stream
.avail_out
;
761 if ((uInt
)fwrite(s
->outbuf
, 1, len
, s
->file
) != len
) {
765 s
->stream
.next_out
= s
->outbuf
;
766 s
->stream
.avail_out
= Z_BUFSIZE
;
769 s
->out
+= s
->stream
.avail_out
;
770 s
->z_err
= deflate(&(s
->stream
), flush
);
771 s
->out
-= s
->stream
.avail_out
;
773 /* Ignore the second of two consecutive flushes: */
774 if (len
== 0 && s
->z_err
== Z_BUF_ERROR
) s
->z_err
= Z_OK
;
776 /* deflate has finished flushing only when it hasn't used up
777 * all the available space in the output buffer:
779 done
= (s
->stream
.avail_out
!= 0 || s
->z_err
== Z_STREAM_END
);
781 if (s
->z_err
!= Z_OK
&& s
->z_err
!= Z_STREAM_END
) break;
783 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
786 int ZEXPORT
gzflush (file
, flush
)
790 gz_stream
*s
= (gz_stream
*)file
;
791 int err
= do_flush (file
, flush
);
795 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
797 #endif /* NO_GZCOMPRESS */
799 /* ===========================================================================
800 Sets the starting position for the next gzread or gzwrite on the given
801 compressed file. The offset represents a number of bytes in the
802 gzseek returns the resulting offset location as measured in bytes from
803 the beginning of the uncompressed stream, or -1 in case of error.
804 SEEK_END is not implemented, returns error.
805 In this version of the library, gzseek can be extremely slow.
807 #ifdef _LARGEFILE64_SOURCE
808 local off64_t
gz_seek (file
, offset
, whence
, use64
)
812 local z_off_t
gz_seek (file
, offset
, whence
, use64
)
819 gz_stream
*s
= (gz_stream
*)file
;
821 if (s
== NULL
|| whence
== SEEK_END
||
822 s
->z_err
== Z_ERRNO
|| s
->z_err
== Z_DATA_ERROR
) {
826 if (s
->mode
== 'w') {
830 if (whence
== SEEK_SET
) {
833 if (offset
< 0) return -1L;
835 /* At this point, offset is the number of zero bytes to write. */
836 if (s
->inbuf
== Z_NULL
) {
837 s
->inbuf
= (Byte
*)ALLOC(Z_BUFSIZE
); /* for seeking */
838 if (s
->inbuf
== Z_NULL
) return -1L;
839 zmemzero(s
->inbuf
, Z_BUFSIZE
);
842 uInt size
= Z_BUFSIZE
;
843 if (offset
< Z_BUFSIZE
) size
= (uInt
)offset
;
845 size
= gzwrite(file
, s
->inbuf
, size
);
846 if (size
== 0) return -1L;
853 /* Rest of function is for reading only */
855 /* compute absolute position */
856 if (whence
== SEEK_CUR
) {
859 if (offset
< 0) return -1L;
861 if (s
->transparent
) {
864 s
->stream
.avail_in
= 0;
865 s
->stream
.next_in
= s
->inbuf
;
866 #ifdef _LARGEFILE64_SOURCE
867 if ((use64
? fseeko64(s
->file
, offset
, SEEK_SET
) :
868 FSEEK(s
->file
, offset
, SEEK_SET
)) < 0)
871 if (FSEEK(s
->file
, offset
, SEEK_SET
) < 0) return -1L;
874 s
->in
= s
->out
= offset
;
878 /* For a negative seek, rewind and use positive seek */
879 if (offset
>= s
->out
) {
881 } else if (gzrewind(file
) < 0) {
884 /* offset is now the number of bytes to skip. */
886 if (offset
!= 0 && s
->outbuf
== Z_NULL
) {
887 s
->outbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
888 if (s
->outbuf
== Z_NULL
) return -1L;
890 if (offset
&& s
->back
!= EOF
) {
894 if (s
->last
) s
->z_err
= Z_STREAM_END
;
897 int size
= Z_BUFSIZE
;
898 if (offset
< Z_BUFSIZE
) size
= (int)offset
;
900 size
= gzread(file
, s
->outbuf
, (uInt
)size
);
901 if (size
<= 0) return -1L;
907 /* ===========================================================================
908 Define external functions gzseek() and gzseek64() using local gz_seek().
910 z_off_t ZEXPORT
gzseek (file
, offset
, whence
)
915 return (z_off_t
)gz_seek(file
, offset
, whence
, 0);
918 #ifdef _LARGEFILE64_SOURCE
919 off64_t ZEXPORT
gzseek64 (file
, offset
, whence
)
924 return gz_seek(file
, offset
, whence
, 1);
927 z_off_t ZEXPORT
gzseek64 (file
, offset
, whence
)
932 return gz_seek(file
, offset
, whence
, 0);
936 /* ===========================================================================
939 int ZEXPORT
gzrewind (file
)
942 gz_stream
*s
= (gz_stream
*)file
;
944 if (s
== NULL
|| s
->mode
!= 'r') return -1;
949 s
->stream
.avail_in
= 0;
950 s
->stream
.next_in
= s
->inbuf
;
951 s
->crc
= crc32(0L, Z_NULL
, 0);
952 if (!s
->transparent
) (void)inflateReset(&s
->stream
);
955 return FSEEK(s
->file
, s
->start
, SEEK_SET
);
958 /* ===========================================================================
959 Returns the starting position for the next gzread or gzwrite on the
960 given compressed file. This position represents a number of bytes in the
961 uncompressed data stream.
963 z_off_t ZEXPORT
gztell (file
)
966 return gzseek(file
, 0L, SEEK_CUR
);
969 /* ===========================================================================
972 #ifdef _LARGEFILE64_SOURCE
973 off64_t ZEXPORT
gztell64 (file
)
975 z_off_t ZEXPORT
gztell64 (file
)
979 return gzseek64(file
, 0L, SEEK_CUR
);
982 /* ===========================================================================
983 Returns 1 when EOF has previously been detected reading the given
984 input stream, otherwise zero.
986 int ZEXPORT
gzeof (file
)
989 gz_stream
*s
= (gz_stream
*)file
;
991 /* With concatenated compressed files that can have embedded
992 * crc trailers, z_eof is no longer the only/best indicator of EOF
993 * on a gz_stream. Handle end-of-stream error explicitly here.
995 if (s
== NULL
|| s
->mode
!= 'r') return 0;
996 if (s
->z_eof
) return 1;
997 return s
->z_err
== Z_STREAM_END
;
1000 /* ===========================================================================
1001 Returns 1 if reading and doing so transparently, otherwise zero.
1003 int ZEXPORT
gzdirect (file
)
1006 gz_stream
*s
= (gz_stream
*)file
;
1008 if (s
== NULL
|| s
->mode
!= 'r') return 0;
1009 return s
->transparent
;
1012 /* ===========================================================================
1013 Outputs a long in LSB order to the given file
1015 local
void putLong (file
, x
)
1020 for (n
= 0; n
< 4; n
++) {
1021 fputc((int)(x
& 0xff), file
);
1026 /* ===========================================================================
1027 Reads a long in LSB order from the given gz_stream. Sets z_err in case
1030 local uLong
getLong (s
)
1033 uLong x
= (uLong
)get_byte(s
);
1036 x
+= ((uLong
)get_byte(s
))<<8;
1037 x
+= ((uLong
)get_byte(s
))<<16;
1039 if (c
== EOF
) s
->z_err
= Z_DATA_ERROR
;
1040 x
+= ((uLong
)c
)<<24;
1044 /* ===========================================================================
1045 Flushes all pending output if necessary, closes the compressed file
1046 and deallocates all the (de)compression state.
1048 int ZEXPORT
gzclose (file
)
1051 gz_stream
*s
= (gz_stream
*)file
;
1053 if (s
== NULL
) return Z_STREAM_ERROR
;
1055 if (s
->mode
== 'w') {
1056 #ifdef NO_GZCOMPRESS
1057 return Z_STREAM_ERROR
;
1059 if (do_flush (file
, Z_FINISH
) != Z_OK
)
1060 return destroy((gz_stream
*)file
);
1062 putLong (s
->file
, s
->crc
);
1063 putLong (s
->file
, (uLong
)(s
->in
& 0xffffffff));
1066 return destroy((gz_stream
*)file
);
1069 #if defined(STDC) && !defined(_WIN32_WCE)
1070 # define zstrerror(errnum) strerror(errnum)
1072 # define zstrerror(errnum) ""
1075 /* ===========================================================================
1076 Returns the error message for the last error which occurred on the
1077 given compressed file. errnum is set to zlib error number. If an
1078 error occurred in the file system and not in the compression library,
1079 errnum is set to Z_ERRNO and the application may consult errno
1080 to get the exact error code.
1082 const char * ZEXPORT
gzerror (file
, errnum
)
1087 gz_stream
*s
= (gz_stream
*)file
;
1090 *errnum
= Z_STREAM_ERROR
;
1091 return (const char*)ERR_MSG(Z_STREAM_ERROR
);
1094 if (*errnum
== Z_OK
) return (const char*)"";
1096 m
= (char*)(*errnum
== Z_ERRNO
? zstrerror(errno
) : s
->stream
.msg
);
1098 if (m
== NULL
|| *m
== '\0') m
= (char*)ERR_MSG(s
->z_err
);
1101 s
->msg
= (char*)ALLOC(strlen(s
->path
) + strlen(m
) + 3);
1102 if (s
->msg
== Z_NULL
) return (const char*)ERR_MSG(Z_MEM_ERROR
);
1103 strcpy(s
->msg
, s
->path
);
1104 strcat(s
->msg
, ": ");
1106 return (const char*)s
->msg
;
1109 /* ===========================================================================
1110 Clear the error and end-of-file flags, and do the same for the real file.
1112 void ZEXPORT
gzclearerr (file
)
1115 gz_stream
*s
= (gz_stream
*)file
;
1117 if (s
== NULL
) return;
1118 if (s
->z_err
!= Z_STREAM_END
) s
->z_err
= Z_OK
;