]>
git.saurik.com Git - wxWidgets.git/blob - src/zlib/gzio.c
1 /* gzio.c -- IO on .gz files
2 * Copyright (C) 1995-1998 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
5 * Compile this file with -DNO_DEFLATE to avoid the compression code.
14 struct internal_state
{int dummy
;}; /* for buggy compilers */
18 # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
20 # define Z_BUFSIZE 16384
23 #ifndef Z_PRINTF_BUFSIZE
24 # define Z_PRINTF_BUFSIZE 4096
27 #define ALLOC(size) malloc(size)
28 #define TRYFREE(p) {if (p) free(p);}
30 static int gz_magic
[2] = {0x1f, 0x8b}; /* gzip magic header */
33 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
34 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
35 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
36 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
37 #define COMMENT 0x10 /* bit 4 set: file comment present */
38 #define RESERVED 0xE0 /* bits 5..7: reserved */
40 typedef struct gz_stream
{
42 int z_err
; /* error code for last stream operation */
43 int z_eof
; /* set if end of input file */
44 FILE *file
; /* .gz file */
45 Byte
*inbuf
; /* input buffer */
46 Byte
*outbuf
; /* output buffer */
47 uLong crc
; /* crc32 of uncompressed data */
48 char *msg
; /* error message */
49 char *path
; /* path name for debugging only */
50 int transparent
; /* 1 if input file is not a .gz file */
51 char mode
; /* 'w' or 'r' */
52 long startpos
; /* start of compressed data in file (header skipped) */
56 local gzFile gz_open
OF((const char *path
, const char *mode
, int fd
));
57 local
int do_flush
OF((gzFile file
, int flush
));
58 local
int get_byte
OF((gz_stream
*s
));
59 local
void check_header
OF((gz_stream
*s
));
60 local
int destroy
OF((gz_stream
*s
));
61 local
void putLong
OF((FILE *file
, uLong x
));
62 local uLong getLong
OF((gz_stream
*s
));
64 /* ===========================================================================
65 Opens a gzip (.gz) file for reading or writing. The mode parameter
66 is as in fopen ("rb" or "wb"). The file is given either by file descriptor
67 or path name (if fd == -1).
68 gz_open return NULL if the file could not be opened or if there was
69 insufficient memory to allocate the (de)compression state; errno
70 can be checked to distinguish the two cases (if errno is zero, the
71 zlib error is Z_MEM_ERROR).
73 local gzFile
gz_open (path
, mode
, fd
)
79 int level
= Z_DEFAULT_COMPRESSION
; /* compression level */
80 int strategy
= Z_DEFAULT_STRATEGY
; /* compression strategy */
81 char *p
= (char*)mode
;
83 char fmode
[80]; /* copy of mode, without the compression level */
86 if (!path
|| !mode
) return Z_NULL
;
88 s
= (gz_stream
*)ALLOC(sizeof(gz_stream
));
89 if (!s
) return Z_NULL
;
91 s
->stream
.zalloc
= (alloc_func
)0;
92 s
->stream
.zfree
= (free_func
)0;
93 s
->stream
.opaque
= (voidpf
)0;
94 s
->stream
.next_in
= s
->inbuf
= Z_NULL
;
95 s
->stream
.next_out
= s
->outbuf
= Z_NULL
;
96 s
->stream
.avail_in
= s
->stream
.avail_out
= 0;
100 s
->crc
= crc32(0L, Z_NULL
, 0);
104 s
->path
= (char*)ALLOC(strlen(path
)+1);
105 if (s
->path
== NULL
) {
106 return destroy(s
), (gzFile
)Z_NULL
;
108 strcpy(s
->path
, path
); /* do this early for debugging */
112 if (*p
== 'r') s
->mode
= 'r';
113 if (*p
== 'w' || *p
== 'a') s
->mode
= 'w';
114 if (*p
>= '0' && *p
<= '9') {
116 } else if (*p
== 'f') {
117 strategy
= Z_FILTERED
;
118 } else if (*p
== 'h') {
119 strategy
= Z_HUFFMAN_ONLY
;
121 *m
++ = *p
; /* copy the mode */
123 } while (*p
++ && m
!= fmode
+ sizeof(fmode
));
124 if (s
->mode
== '\0') return destroy(s
), (gzFile
)Z_NULL
;
126 if (s
->mode
== 'w') {
128 err
= Z_STREAM_ERROR
;
130 err
= deflateInit2(&(s
->stream
), level
,
131 Z_DEFLATED
, -MAX_WBITS
, DEF_MEM_LEVEL
, strategy
);
132 /* windowBits is passed < 0 to suppress zlib header */
134 s
->stream
.next_out
= s
->outbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
136 if (err
!= Z_OK
|| s
->outbuf
== Z_NULL
) {
137 return destroy(s
), (gzFile
)Z_NULL
;
140 s
->stream
.next_in
= s
->inbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
142 err
= inflateInit2(&(s
->stream
), -MAX_WBITS
);
143 /* windowBits is passed < 0 to tell that there is no zlib header.
144 * Note that in this case inflate *requires* an extra "dummy" byte
145 * after the compressed stream in order to complete decompression and
146 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
147 * present after the compressed stream.
149 if (err
!= Z_OK
|| s
->inbuf
== Z_NULL
) {
150 return destroy(s
), (gzFile
)Z_NULL
;
153 s
->stream
.avail_out
= Z_BUFSIZE
;
156 s
->file
= fd
< 0 ? F_OPEN(path
, fmode
) : (FILE*)fdopen(fd
, fmode
);
158 if (s
->file
== NULL
) {
159 return destroy(s
), (gzFile
)Z_NULL
;
161 if (s
->mode
== 'w') {
162 /* Write a very simple .gz header:
164 fprintf(s
->file
, "%c%c%c%c%c%c%c%c%c%c", gz_magic
[0], gz_magic
[1],
165 Z_DEFLATED
, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE
);
167 /* We use 10L instead of ftell(s->file) to because ftell causes an
168 * fflush on some systems. This version of the library doesn't use
169 * startpos anyway in write mode, so this initialization is not
173 check_header(s
); /* skip the .gz header */
174 s
->startpos
= (ftell(s
->file
) - s
->stream
.avail_in
);
180 /* ===========================================================================
181 Opens a gzip (.gz) file for reading or writing.
183 gzFile ZEXPORT
gzopen (path
, mode
)
187 return gz_open (path
, mode
, -1);
190 /* ===========================================================================
191 Associate a gzFile with the file descriptor fd. fd is not dup'ed here
192 to mimic the behavio(u)r of fdopen.
194 gzFile ZEXPORT
gzdopen (fd
, mode
)
200 if (fd
< 0) return (gzFile
)Z_NULL
;
201 sprintf(name
, "<fd:%d>", fd
); /* for debugging */
203 return gz_open (name
, mode
, fd
);
206 /* ===========================================================================
207 * Update the compression level and strategy
209 int ZEXPORT
gzsetparams (file
, level
, strategy
)
214 gz_stream
*s
= (gz_stream
*)file
;
216 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
218 /* Make room to allow flushing */
219 if (s
->stream
.avail_out
== 0) {
221 s
->stream
.next_out
= s
->outbuf
;
222 if (fwrite(s
->outbuf
, 1, Z_BUFSIZE
, s
->file
) != Z_BUFSIZE
) {
225 s
->stream
.avail_out
= Z_BUFSIZE
;
228 return deflateParams (&(s
->stream
), level
, strategy
);
231 /* ===========================================================================
232 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
234 IN assertion: the stream s has been sucessfully opened for reading.
236 local
int get_byte(s
)
239 if (s
->z_eof
) return EOF
;
240 if (s
->stream
.avail_in
== 0) {
242 s
->stream
.avail_in
= fread(s
->inbuf
, 1, Z_BUFSIZE
, s
->file
);
243 if (s
->stream
.avail_in
== 0) {
245 if (ferror(s
->file
)) s
->z_err
= Z_ERRNO
;
248 s
->stream
.next_in
= s
->inbuf
;
250 s
->stream
.avail_in
--;
251 return *(s
->stream
.next_in
)++;
254 /* ===========================================================================
255 Check the gzip header of a gz_stream opened for reading. Set the stream
256 mode to transparent if the gzip magic header is not present; set s->err
257 to Z_DATA_ERROR if the magic header is present but the rest of the header
259 IN assertion: the stream s has already been created sucessfully;
260 s->stream.avail_in is zero for the first time, but may be non-zero
261 for concatenated .gz files.
263 local
void check_header(s
)
266 int method
; /* method byte */
267 int flags
; /* flags byte */
271 /* Check the gzip magic header */
272 for (len
= 0; len
< 2; len
++) {
274 if (c
!= gz_magic
[len
]) {
275 if (len
!= 0) s
->stream
.avail_in
++, s
->stream
.next_in
--;
277 s
->stream
.avail_in
++, s
->stream
.next_in
--;
280 s
->z_err
= s
->stream
.avail_in
!= 0 ? Z_OK
: Z_STREAM_END
;
284 method
= get_byte(s
);
286 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0) {
287 s
->z_err
= Z_DATA_ERROR
;
291 /* Discard time, xflags and OS code: */
292 for (len
= 0; len
< 6; len
++) (void)get_byte(s
);
294 if ((flags
& EXTRA_FIELD
) != 0) { /* skip the extra field */
295 len
= (uInt
)get_byte(s
);
296 len
+= ((uInt
)get_byte(s
))<<8;
297 /* len is garbage if EOF but the loop below will quit anyway */
298 while (len
-- != 0 && get_byte(s
) != EOF
) ;
300 if ((flags
& ORIG_NAME
) != 0) { /* skip the original file name */
301 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) ;
303 if ((flags
& COMMENT
) != 0) { /* skip the .gz file comment */
304 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) ;
306 if ((flags
& HEAD_CRC
) != 0) { /* skip the header crc */
307 for (len
= 0; len
< 2; len
++) (void)get_byte(s
);
309 s
->z_err
= s
->z_eof
? Z_DATA_ERROR
: Z_OK
;
312 /* ===========================================================================
313 * Cleanup then free the given gz_stream. Return a zlib error code.
314 Try freeing in the reverse order of allocations.
316 local
int destroy (s
)
321 if (!s
) return Z_STREAM_ERROR
;
325 if (s
->stream
.state
!= NULL
) {
326 if (s
->mode
== 'w') {
328 err
= Z_STREAM_ERROR
;
330 err
= deflateEnd(&(s
->stream
));
332 } else if (s
->mode
== 'r') {
333 err
= inflateEnd(&(s
->stream
));
336 if (s
->file
!= NULL
&& fclose(s
->file
)) {
338 if (errno
!= ESPIPE
) /* fclose is broken for pipes in HP/UX */
342 if (s
->z_err
< 0) err
= s
->z_err
;
351 /* ===========================================================================
352 Reads the given number of uncompressed bytes from the compressed file.
353 gzread returns the number of bytes actually read (0 for end of file).
355 int ZEXPORT
gzread (file
, buf
, len
)
360 gz_stream
*s
= (gz_stream
*)file
;
361 Bytef
*start
= (Bytef
*)buf
; /* starting point for crc computation */
362 Byte
*next_out
; /* == stream.next_out but not forced far (for MSDOS) */
364 if (s
== NULL
|| s
->mode
!= 'r') return Z_STREAM_ERROR
;
366 if (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
) return -1;
367 if (s
->z_err
== Z_STREAM_END
) return 0; /* EOF */
369 next_out
= (Byte
*)buf
;
370 s
->stream
.next_out
= (Bytef
*)buf
;
371 s
->stream
.avail_out
= len
;
373 while (s
->stream
.avail_out
!= 0) {
375 if (s
->transparent
) {
376 /* Copy first the lookahead bytes: */
377 uInt n
= s
->stream
.avail_in
;
378 if (n
> s
->stream
.avail_out
) n
= s
->stream
.avail_out
;
380 zmemcpy(s
->stream
.next_out
, s
->stream
.next_in
, n
);
382 s
->stream
.next_out
= next_out
;
383 s
->stream
.next_in
+= n
;
384 s
->stream
.avail_out
-= n
;
385 s
->stream
.avail_in
-= n
;
387 if (s
->stream
.avail_out
> 0) {
388 s
->stream
.avail_out
-= fread(next_out
, 1, s
->stream
.avail_out
,
391 len
-= s
->stream
.avail_out
;
392 s
->stream
.total_in
+= (uLong
)len
;
393 s
->stream
.total_out
+= (uLong
)len
;
394 if (len
== 0) s
->z_eof
= 1;
397 if (s
->stream
.avail_in
== 0 && !s
->z_eof
) {
400 s
->stream
.avail_in
= fread(s
->inbuf
, 1, Z_BUFSIZE
, s
->file
);
401 if (s
->stream
.avail_in
== 0) {
403 if (ferror(s
->file
)) {
408 s
->stream
.next_in
= s
->inbuf
;
410 s
->z_err
= inflate(&(s
->stream
), Z_NO_FLUSH
);
412 if (s
->z_err
== Z_STREAM_END
) {
413 /* Check CRC and original size */
414 s
->crc
= crc32(s
->crc
, start
, (uInt
)(s
->stream
.next_out
- start
));
415 start
= s
->stream
.next_out
;
417 if (getLong(s
) != s
->crc
|| getLong(s
) != s
->stream
.total_out
) {
418 s
->z_err
= Z_DATA_ERROR
;
420 /* Check for concatenated .gz files: */
422 if (s
->z_err
== Z_OK
) {
423 uLong total_in
= s
->stream
.total_in
;
424 uLong total_out
= s
->stream
.total_out
;
426 inflateReset(&(s
->stream
));
427 s
->stream
.total_in
= total_in
;
428 s
->stream
.total_out
= total_out
;
429 s
->crc
= crc32(0L, Z_NULL
, 0);
433 if (s
->z_err
!= Z_OK
|| s
->z_eof
) break;
435 s
->crc
= crc32(s
->crc
, start
, (uInt
)(s
->stream
.next_out
- start
));
437 return (int)(len
- s
->stream
.avail_out
);
441 /* ===========================================================================
442 Reads one byte from the compressed file. gzgetc returns this byte
443 or -1 in case of end of file or error.
445 int ZEXPORT
gzgetc(file
)
450 return gzread(file
, &c
, 1) == 1 ? c
: -1;
454 /* ===========================================================================
455 Reads bytes from the compressed file until len-1 characters are
456 read, or a newline character is read and transferred to buf, or an
457 end-of-file condition is encountered. The string is then terminated
458 with a null character.
459 gzgets returns buf, or Z_NULL in case of error.
461 The current implementation is not optimized at all.
463 char * ZEXPORT
gzgets(file
, buf
, len
)
469 if (buf
== Z_NULL
|| len
<= 0) return Z_NULL
;
471 while (--len
> 0 && gzread(file
, buf
, 1) == 1 && *buf
++ != '\n') ;
473 return b
== buf
&& len
> 0 ? Z_NULL
: b
;
478 /* ===========================================================================
479 Writes the given number of uncompressed bytes into the compressed file.
480 gzwrite returns the number of bytes actually written (0 in case of error).
482 int ZEXPORT
gzwrite (file
, buf
, len
)
487 gz_stream
*s
= (gz_stream
*)file
;
489 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
491 s
->stream
.next_in
= (Bytef
*)buf
;
492 s
->stream
.avail_in
= len
;
494 while (s
->stream
.avail_in
!= 0) {
496 if (s
->stream
.avail_out
== 0) {
498 s
->stream
.next_out
= s
->outbuf
;
499 if (fwrite(s
->outbuf
, 1, Z_BUFSIZE
, s
->file
) != Z_BUFSIZE
) {
503 s
->stream
.avail_out
= Z_BUFSIZE
;
505 s
->z_err
= deflate(&(s
->stream
), Z_NO_FLUSH
);
506 if (s
->z_err
!= Z_OK
) break;
508 s
->crc
= crc32(s
->crc
, (const Bytef
*)buf
, len
);
510 return (int)(len
- s
->stream
.avail_in
);
513 /* ===========================================================================
514 Converts, formats, and writes the args to the compressed file under
515 control of the format string, as in fprintf. gzprintf returns the number of
516 uncompressed bytes actually written (0 in case of error).
521 int ZEXPORTVA
gzprintf (gzFile file
, const char *format
, /* args */ ...)
523 char buf
[Z_PRINTF_BUFSIZE
];
527 va_start(va
, format
);
529 (void)vsnprintf(buf
, sizeof(buf
), format
, va
);
531 (void)vsprintf(buf
, format
, va
);
534 len
= strlen(buf
); /* some *sprintf don't return the nb of bytes written */
535 if (len
<= 0) return 0;
537 return gzwrite(file
, buf
, (unsigned)len
);
539 #else /* not ANSI C */
541 int ZEXPORTVA
gzprintf (file
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
, a10
,
542 a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
)
545 int a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
, a10
,
546 a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
;
548 char buf
[Z_PRINTF_BUFSIZE
];
552 snprintf(buf
, sizeof(buf
), format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
553 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
555 sprintf(buf
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
556 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
558 len
= strlen(buf
); /* old sprintf doesn't return the nb of bytes written */
559 if (len
<= 0) return 0;
561 return gzwrite(file
, buf
, len
);
565 /* ===========================================================================
566 Writes c, converted to an unsigned char, into the compressed file.
567 gzputc returns the value that was written, or -1 in case of error.
569 int ZEXPORT
gzputc(file
, c
)
573 unsigned char cc
= (unsigned char) c
; /* required for big endian systems */
575 return gzwrite(file
, &cc
, 1) == 1 ? (int)cc
: -1;
579 /* ===========================================================================
580 Writes the given null-terminated string to the compressed file, excluding
581 the terminating null character.
582 gzputs returns the number of characters written, or -1 in case of error.
584 int ZEXPORT
gzputs(file
, s
)
588 return gzwrite(file
, (char*)s
, (unsigned)strlen(s
));
592 /* ===========================================================================
593 Flushes all pending output into the compressed file. The parameter
594 flush is as in the deflate() function.
596 local
int do_flush (file
, flush
)
602 gz_stream
*s
= (gz_stream
*)file
;
604 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
606 s
->stream
.avail_in
= 0; /* should be zero already anyway */
609 len
= Z_BUFSIZE
- s
->stream
.avail_out
;
612 if ((uInt
)fwrite(s
->outbuf
, 1, len
, s
->file
) != len
) {
616 s
->stream
.next_out
= s
->outbuf
;
617 s
->stream
.avail_out
= Z_BUFSIZE
;
620 s
->z_err
= deflate(&(s
->stream
), flush
);
622 /* Ignore the second of two consecutive flushes: */
623 if (len
== 0 && s
->z_err
== Z_BUF_ERROR
) s
->z_err
= Z_OK
;
625 /* deflate has finished flushing only when it hasn't used up
626 * all the available space in the output buffer:
628 done
= (s
->stream
.avail_out
!= 0 || s
->z_err
== Z_STREAM_END
);
630 if (s
->z_err
!= Z_OK
&& s
->z_err
!= Z_STREAM_END
) break;
632 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
635 int ZEXPORT
gzflush (file
, flush
)
639 gz_stream
*s
= (gz_stream
*)file
;
640 int err
= do_flush (file
, flush
);
644 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
646 #endif /* NO_DEFLATE */
648 /* ===========================================================================
649 Sets the starting position for the next gzread or gzwrite on the given
650 compressed file. The offset represents a number of bytes in the
651 gzseek returns the resulting offset location as measured in bytes from
652 the beginning of the uncompressed stream, or -1 in case of error.
653 SEEK_END is not implemented, returns error.
654 In this version of the library, gzseek can be extremely slow.
656 z_off_t ZEXPORT
gzseek (file
, offset
, whence
)
661 gz_stream
*s
= (gz_stream
*)file
;
663 if (s
== NULL
|| whence
== SEEK_END
||
664 s
->z_err
== Z_ERRNO
|| s
->z_err
== Z_DATA_ERROR
) {
668 if (s
->mode
== 'w') {
672 if (whence
== SEEK_SET
) {
673 offset
-= s
->stream
.total_out
;
675 if (offset
< 0) return -1L;
677 /* At this point, offset is the number of zero bytes to write. */
678 if (s
->inbuf
== Z_NULL
) {
679 s
->inbuf
= (Byte
*)ALLOC(Z_BUFSIZE
); /* for seeking */
680 zmemzero(s
->inbuf
, Z_BUFSIZE
);
683 uInt size
= Z_BUFSIZE
;
684 if (offset
< Z_BUFSIZE
) size
= (uInt
)offset
;
686 size
= gzwrite(file
, s
->inbuf
, size
);
687 if (size
== 0) return -1L;
691 return (z_off_t
)s
->stream
.total_in
;
694 /* Rest of function is for reading only */
696 /* compute absolute position */
697 if (whence
== SEEK_CUR
) {
698 offset
+= s
->stream
.total_out
;
700 if (offset
< 0) return -1L;
702 if (s
->transparent
) {
704 s
->stream
.avail_in
= 0;
705 s
->stream
.next_in
= s
->inbuf
;
706 if (fseek(s
->file
, offset
, SEEK_SET
) < 0) return -1L;
708 s
->stream
.total_in
= s
->stream
.total_out
= (uLong
)offset
;
712 /* For a negative seek, rewind and use positive seek */
713 if ((uLong
)offset
>= s
->stream
.total_out
) {
714 offset
-= s
->stream
.total_out
;
715 } else if (gzrewind(file
) < 0) {
718 /* offset is now the number of bytes to skip. */
720 if (offset
!= 0 && s
->outbuf
== Z_NULL
) {
721 s
->outbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
724 int size
= Z_BUFSIZE
;
725 if (offset
< Z_BUFSIZE
) size
= (int)offset
;
727 size
= gzread(file
, s
->outbuf
, (uInt
)size
);
728 if (size
<= 0) return -1L;
731 return (z_off_t
)s
->stream
.total_out
;
734 /* ===========================================================================
737 int ZEXPORT
gzrewind (file
)
740 gz_stream
*s
= (gz_stream
*)file
;
742 if (s
== NULL
|| s
->mode
!= 'r') return -1;
746 s
->stream
.avail_in
= 0;
747 s
->stream
.next_in
= s
->inbuf
;
749 if (s
->startpos
== 0) { /* not a compressed file */
754 (void) inflateReset(&s
->stream
);
755 return fseek(s
->file
, s
->startpos
, SEEK_SET
);
758 /* ===========================================================================
759 Returns the starting position for the next gzread or gzwrite on the
760 given compressed file. This position represents a number of bytes in the
761 uncompressed data stream.
763 z_off_t ZEXPORT
gztell (file
)
766 return gzseek(file
, 0L, SEEK_CUR
);
769 /* ===========================================================================
770 Returns 1 when EOF has previously been detected reading the given
771 input stream, otherwise zero.
773 int ZEXPORT
gzeof (file
)
776 gz_stream
*s
= (gz_stream
*)file
;
778 return (s
== NULL
|| s
->mode
!= 'r') ? 0 : s
->z_eof
;
781 /* ===========================================================================
782 Outputs a long in LSB order to the given file
784 local
void putLong (file
, x
)
789 for (n
= 0; n
< 4; n
++) {
790 fputc((int)(x
& 0xff), file
);
795 /* ===========================================================================
796 Reads a long in LSB order from the given gz_stream. Sets
798 local uLong
getLong (s
)
801 uLong x
= (uLong
)get_byte(s
);
804 x
+= ((uLong
)get_byte(s
))<<8;
805 x
+= ((uLong
)get_byte(s
))<<16;
807 if (c
== EOF
) s
->z_err
= Z_DATA_ERROR
;
812 /* ===========================================================================
813 Flushes all pending output if necessary, closes the compressed file
814 and deallocates all the (de)compression state.
816 int ZEXPORT
gzclose (file
)
820 gz_stream
*s
= (gz_stream
*)file
;
822 if (s
== NULL
) return Z_STREAM_ERROR
;
824 if (s
->mode
== 'w') {
826 return Z_STREAM_ERROR
;
828 err
= do_flush (file
, Z_FINISH
);
829 if (err
!= Z_OK
) return destroy((gz_stream
*)file
);
831 putLong (s
->file
, s
->crc
);
832 putLong (s
->file
, s
->stream
.total_in
);
835 return destroy((gz_stream
*)file
);
838 /* ===========================================================================
839 Returns the error message for the last error which occured on the
840 given compressed file. errnum is set to zlib error number. If an
841 error occured in the file system and not in the compression library,
842 errnum is set to Z_ERRNO and the application may consult errno
843 to get the exact error code.
845 const char* ZEXPORT
gzerror (file
, errnum
)
850 gz_stream
*s
= (gz_stream
*)file
;
853 *errnum
= Z_STREAM_ERROR
;
854 return (const char*)ERR_MSG(Z_STREAM_ERROR
);
857 if (*errnum
== Z_OK
) return (const char*)"";
859 m
= (char*)(*errnum
== Z_ERRNO
? zstrerror(errno
) : s
->stream
.msg
);
861 if (m
== NULL
|| *m
== '\0') m
= (char*)ERR_MSG(s
->z_err
);
864 s
->msg
= (char*)ALLOC(strlen(s
->path
) + strlen(m
) + 3);
865 strcpy(s
->msg
, s
->path
);
866 strcat(s
->msg
, ": ");
868 return (const char*)s
->msg
;