1 /* $NetBSD: gzio.c,v 1.3 2011/05/19 22:23:12 tsutsui Exp $ */
3 /* gzio.c -- IO on .gz files
4 * Copyright (C) 1995-2005 Jean-loup Gailly.
5 * For conditions of distribution and use, see copyright notice in zlib.h
7 * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
16 #ifdef NO_DEFLATE /* for compatibility with old definition */
17 # define NO_GZCOMPRESS
21 struct internal_state
{int dummy
;}; /* for buggy compilers */
26 # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
28 # define Z_BUFSIZE 16384
31 #ifndef Z_PRINTF_BUFSIZE
32 # define Z_PRINTF_BUFSIZE 4096
36 # pragma map (fdopen , "\174\174FDOPEN")
37 FILE *fdopen(int, const char *);
41 extern voidp malloc
OF((uInt size
));
42 extern void free
OF((voidpf ptr
));
45 #define ALLOC(size) malloc(size)
46 #define TRYFREE(p) {if (p) free(p);}
48 static int const gz_magic
[2] = {0x1f, 0x8b}; /* gzip magic header */
51 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
52 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
53 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
54 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
55 #define COMMENT 0x10 /* bit 4 set: file comment present */
56 #define RESERVED 0xE0 /* bits 5..7: reserved */
58 typedef struct gz_stream
{
60 int z_err
; /* error code for last stream operation */
61 int z_eof
; /* set if end of input file */
62 FILE *file
; /* .gz file */
63 Byte
*inbuf
; /* input buffer */
64 Byte
*outbuf
; /* output buffer */
65 uLong crc
; /* crc32 of uncompressed data */
66 char *msg
; /* error message */
67 char *path
; /* path name for debugging only */
68 int transparent
; /* 1 if input file is not a .gz file */
69 char mode
; /* 'w' or 'r' */
70 z_off_t start
; /* start of compressed data in file (header skipped) */
71 z_off_t in
; /* bytes into deflate or inflate */
72 z_off_t out
; /* bytes out of deflate or inflate */
73 int back
; /* one character push-back */
74 int last
; /* true if push-back is last character */
78 local gzFile gz_open
OF((const char *path
, const char *mode
, int fd
));
80 local
int do_flush
OF((gzFile file
, int flush
));
82 local
int get_byte
OF((gz_stream
*s
));
83 local
void check_header
OF((gz_stream
*s
));
84 local
int destroy
OF((gz_stream
*s
));
86 local
void putLong
OF((FILE *file
, uLong x
));
88 local uLong getLong
OF((gz_stream
*s
));
90 /* ===========================================================================
91 Opens a gzip (.gz) file for reading or writing. The mode parameter
92 is as in fopen ("rb" or "wb"). The file is given either by file descriptor
93 or path name (if fd == -1).
94 gz_open returns NULL if the file could not be opened or if there was
95 insufficient memory to allocate the (de)compression state; errno
96 can be checked to distinguish the two cases (if errno is zero, the
97 zlib error is Z_MEM_ERROR).
99 local gzFile
gz_open (path
, mode
, fd
)
105 int level
= Z_DEFAULT_COMPRESSION
; /* compression level */
106 int strategy
= Z_DEFAULT_STRATEGY
; /* compression strategy */
107 const char *p
= mode
;
109 char fmode
[80]; /* copy of mode, without the compression level */
112 if (!path
|| !mode
) return Z_NULL
;
114 s
= (gz_stream
*)ALLOC(sizeof(gz_stream
));
115 if (!s
) return Z_NULL
;
117 s
->stream
.zalloc
= (alloc_func
)0;
118 s
->stream
.zfree
= (free_func
)0;
119 s
->stream
.opaque
= (voidpf
)0;
120 s
->stream
.next_in
= s
->inbuf
= Z_NULL
;
121 s
->stream
.next_out
= s
->outbuf
= Z_NULL
;
122 s
->stream
.avail_in
= s
->stream
.avail_out
= 0;
129 s
->crc
= crc32(0L, Z_NULL
, 0);
133 s
->path
= (char*)ALLOC(strlen(path
)+1);
134 if (s
->path
== NULL
) {
135 return destroy(s
), (gzFile
)Z_NULL
;
137 strcpy(s
->path
, path
); /* do this early for debugging */
141 if (*p
== 'r') s
->mode
= 'r';
142 if (*p
== 'w' || *p
== 'a') s
->mode
= 'w';
143 if (*p
>= '0' && *p
<= '9') {
145 } else if (*p
== 'f') {
146 strategy
= Z_FILTERED
;
147 } else if (*p
== 'h') {
148 strategy
= Z_HUFFMAN_ONLY
;
149 } else if (*p
== 'R') {
152 *m
++ = *p
; /* copy the mode */
154 } while (*p
++ && m
!= fmode
+ sizeof(fmode
));
155 if (s
->mode
== '\0') return destroy(s
), (gzFile
)Z_NULL
;
157 if (s
->mode
== 'w') {
159 err
= Z_STREAM_ERROR
;
161 err
= deflateInit2(&(s
->stream
), level
,
162 Z_DEFLATED
, -MAX_WBITS
, DEF_MEM_LEVEL
, strategy
);
163 /* windowBits is passed < 0 to suppress zlib header */
165 s
->stream
.next_out
= s
->outbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
167 if (err
!= Z_OK
|| s
->outbuf
== Z_NULL
) {
168 return destroy(s
), (gzFile
)Z_NULL
;
171 s
->stream
.next_in
= s
->inbuf
= (Byte
*)ALLOC(Z_BUFSIZE
);
173 err
= inflateInit2(&(s
->stream
), -MAX_WBITS
);
174 /* windowBits is passed < 0 to tell that there is no zlib header.
175 * Note that in this case inflate *requires* an extra "dummy" byte
176 * after the compressed stream in order to complete decompression and
177 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
178 * present after the compressed stream.
180 if (err
!= Z_OK
|| s
->inbuf
== Z_NULL
) {
181 return destroy(s
), (gzFile
)Z_NULL
;
184 s
->stream
.avail_out
= Z_BUFSIZE
;
187 s
->file
= fd
< 0 ? F_OPEN(path
, fmode
) : (FILE*)fdopen(fd
, fmode
);
189 if (s
->file
== NULL
) {
190 return destroy(s
), (gzFile
)Z_NULL
;
192 if (s
->mode
== 'w') {
193 /* Write a very simple .gz header:
195 fprintf(s
->file
, "%c%c%c%c%c%c%c%c%c%c", gz_magic
[0], gz_magic
[1],
196 Z_DEFLATED
, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE
);
198 /* We use 10L instead of ftell(s->file) to because ftell causes an
199 * fflush on some systems. This version of the library doesn't use
200 * start anyway in write mode, so this initialization is not
204 check_header(s
); /* skip the .gz header */
205 s
->start
= ftell(s
->file
) - s
->stream
.avail_in
;
211 /* ===========================================================================
212 Opens a gzip (.gz) file for reading or writing.
214 gzFile ZEXPORT
gzopen (path
, mode
)
218 return gz_open (path
, mode
, -1);
221 /* ===========================================================================
222 Associate a gzFile with the file descriptor fd. fd is not dup'ed here
223 to mimic the behavio(u)r of fdopen.
225 gzFile ZEXPORT
gzdopen (fd
, mode
)
229 char name
[46]; /* allow for up to 128-bit integers */
231 if (fd
< 0) return (gzFile
)Z_NULL
;
232 sprintf(name
, "<fd:%d>", fd
); /* for debugging */
234 return gz_open (name
, mode
, fd
);
237 /* ===========================================================================
238 * Update the compression level and strategy
240 int ZEXPORT
gzsetparams (file
, level
, strategy
)
245 gz_stream
*s
= (gz_stream
*)file
;
247 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
249 /* Make room to allow flushing */
250 if (s
->stream
.avail_out
== 0) {
252 s
->stream
.next_out
= s
->outbuf
;
253 if (fwrite(s
->outbuf
, 1, Z_BUFSIZE
, s
->file
) != Z_BUFSIZE
) {
256 s
->stream
.avail_out
= Z_BUFSIZE
;
259 return deflateParams (&(s
->stream
), level
, strategy
);
262 /* ===========================================================================
263 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
265 IN assertion: the stream s has been sucessfully opened for reading.
267 local
int get_byte(s
)
270 if (s
->z_eof
) return EOF
;
271 if (s
->stream
.avail_in
== 0) {
273 s
->stream
.avail_in
= (uInt
)fread(s
->inbuf
, 1, Z_BUFSIZE
, s
->file
);
274 if (s
->stream
.avail_in
== 0) {
276 if (ferror(s
->file
)) s
->z_err
= Z_ERRNO
;
279 s
->stream
.next_in
= s
->inbuf
;
281 s
->stream
.avail_in
--;
282 return *(s
->stream
.next_in
)++;
285 /* ===========================================================================
286 Check the gzip header of a gz_stream opened for reading. Set the stream
287 mode to transparent if the gzip magic header is not present; set s->err
288 to Z_DATA_ERROR if the magic header is present but the rest of the header
290 IN assertion: the stream s has already been created sucessfully;
291 s->stream.avail_in is zero for the first time, but may be non-zero
292 for concatenated .gz files.
294 local
void check_header(s
)
297 int method
; /* method byte */
298 int flags
; /* flags byte */
302 /* Assure two bytes in the buffer so we can peek ahead -- handle case
303 where first byte of header is at the end of the buffer after the last
305 len
= s
->stream
.avail_in
;
307 if (len
) s
->inbuf
[0] = s
->stream
.next_in
[0];
309 len
= (uInt
)fread(s
->inbuf
+ len
, 1, (size_t)(Z_BUFSIZE
>> len
),
311 if (len
== 0 && ferror(s
->file
)) s
->z_err
= Z_ERRNO
;
312 s
->stream
.avail_in
+= len
;
313 s
->stream
.next_in
= s
->inbuf
;
314 if (s
->stream
.avail_in
< 2) {
315 s
->transparent
= s
->stream
.avail_in
;
320 /* Peek ahead to check the gzip magic header */
321 if (s
->stream
.next_in
[0] != gz_magic
[0] ||
322 s
->stream
.next_in
[1] != gz_magic
[1]) {
326 s
->stream
.avail_in
-= 2;
327 s
->stream
.next_in
+= 2;
329 /* Check the rest of the gzip header */
330 method
= get_byte(s
);
332 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0) {
333 s
->z_err
= Z_DATA_ERROR
;
337 /* Discard time, xflags and OS code: */
338 for (len
= 0; len
< 6; len
++) (void)get_byte(s
);
340 if ((flags
& EXTRA_FIELD
) != 0) { /* skip the extra field */
341 len
= (uInt
)get_byte(s
);
342 len
+= ((uInt
)get_byte(s
))<<8;
343 /* len is garbage if EOF but the loop below will quit anyway */
344 while (len
-- != 0 && get_byte(s
) != EOF
) ;
346 if ((flags
& ORIG_NAME
) != 0) { /* skip the original file name */
347 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) ;
349 if ((flags
& COMMENT
) != 0) { /* skip the .gz file comment */
350 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) ;
352 if ((flags
& HEAD_CRC
) != 0) { /* skip the header crc */
353 for (len
= 0; len
< 2; len
++) (void)get_byte(s
);
355 s
->z_err
= s
->z_eof
? Z_DATA_ERROR
: Z_OK
;
358 /* ===========================================================================
359 * Cleanup then free the given gz_stream. Return a zlib error code.
360 Try freeing in the reverse order of allocations.
362 local
int destroy (s
)
367 if (!s
) return Z_STREAM_ERROR
;
371 if (s
->stream
.state
!= NULL
) {
372 if (s
->mode
== 'w') {
374 err
= Z_STREAM_ERROR
;
376 err
= deflateEnd(&(s
->stream
));
378 } else if (s
->mode
== 'r') {
379 err
= inflateEnd(&(s
->stream
));
382 if (s
->file
!= NULL
&& fclose(s
->file
)) {
384 if (errno
!= ESPIPE
) /* fclose is broken for pipes in HP/UX */
388 if (s
->z_err
< 0) err
= s
->z_err
;
397 /* ===========================================================================
398 Reads the given number of uncompressed bytes from the compressed file.
399 gzread returns the number of bytes actually read (0 for end of file).
401 int ZEXPORT
gzread (file
, buf
, len
)
406 gz_stream
*s
= (gz_stream
*)file
;
407 Bytef
*start
= (Bytef
*)buf
; /* starting point for crc computation */
408 Byte
*next_out
; /* == stream.next_out but not forced far (for MSDOS) */
410 if (s
== NULL
|| s
->mode
!= 'r') return Z_STREAM_ERROR
;
412 if (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
) return -1;
413 if (s
->z_err
== Z_STREAM_END
) return 0; /* EOF */
415 next_out
= (Byte
*)buf
;
416 s
->stream
.next_out
= (Bytef
*)buf
;
417 s
->stream
.avail_out
= len
;
419 if (s
->stream
.avail_out
&& s
->back
!= EOF
) {
420 *next_out
++ = s
->back
;
421 s
->stream
.next_out
++;
422 s
->stream
.avail_out
--;
427 s
->z_err
= Z_STREAM_END
;
432 while (s
->stream
.avail_out
!= 0) {
434 if (s
->transparent
) {
435 /* Copy first the lookahead bytes: */
436 uInt n
= s
->stream
.avail_in
;
437 if (n
> s
->stream
.avail_out
) n
= s
->stream
.avail_out
;
439 zmemcpy(s
->stream
.next_out
, s
->stream
.next_in
, n
);
441 s
->stream
.next_out
= next_out
;
442 s
->stream
.next_in
+= n
;
443 s
->stream
.avail_out
-= n
;
444 s
->stream
.avail_in
-= n
;
446 if (s
->stream
.avail_out
> 0) {
447 s
->stream
.avail_out
-=
448 (uInt
)fread(next_out
, 1, s
->stream
.avail_out
, s
->file
);
450 len
-= s
->stream
.avail_out
;
453 if (len
== 0) s
->z_eof
= 1;
456 if (s
->stream
.avail_in
== 0 && !s
->z_eof
) {
459 s
->stream
.avail_in
= (uInt
)fread(s
->inbuf
, 1, Z_BUFSIZE
, s
->file
);
460 if (s
->stream
.avail_in
== 0) {
462 if (ferror(s
->file
)) {
467 s
->stream
.next_in
= s
->inbuf
;
469 s
->in
+= s
->stream
.avail_in
;
470 s
->out
+= s
->stream
.avail_out
;
471 s
->z_err
= inflate(&(s
->stream
), Z_NO_FLUSH
);
472 s
->in
-= s
->stream
.avail_in
;
473 s
->out
-= s
->stream
.avail_out
;
475 if (s
->z_err
== Z_STREAM_END
) {
476 /* Check CRC and original size */
477 s
->crc
= crc32(s
->crc
, start
, (uInt
)(s
->stream
.next_out
- start
));
478 start
= s
->stream
.next_out
;
480 if (getLong(s
) != s
->crc
) {
481 s
->z_err
= Z_DATA_ERROR
;
484 /* The uncompressed length returned by above getlong() may be
485 * different from s->out in case of concatenated .gz files.
486 * Check for such files:
489 if (s
->z_err
== Z_OK
) {
490 inflateReset(&(s
->stream
));
491 s
->crc
= crc32(0L, Z_NULL
, 0);
495 if (s
->z_err
!= Z_OK
|| s
->z_eof
) break;
497 s
->crc
= crc32(s
->crc
, start
, (uInt
)(s
->stream
.next_out
- start
));
499 if (len
== s
->stream
.avail_out
&&
500 (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
))
502 return (int)(len
- s
->stream
.avail_out
);
506 /* ===========================================================================
507 Reads one byte from the compressed file. gzgetc returns this byte
508 or -1 in case of end of file or error.
510 int ZEXPORT
gzgetc(file
)
515 return gzread(file
, &c
, 1) == 1 ? c
: -1;
519 /* ===========================================================================
520 Push one byte back onto the stream.
522 int ZEXPORT
gzungetc(c
, file
)
526 gz_stream
*s
= (gz_stream
*)file
;
528 if (s
== NULL
|| s
->mode
!= 'r' || c
== EOF
|| s
->back
!= EOF
) return EOF
;
531 s
->last
= (s
->z_err
== Z_STREAM_END
);
532 if (s
->last
) s
->z_err
= Z_OK
;
538 /* ===========================================================================
539 Reads bytes from the compressed file until len-1 characters are
540 read, or a newline character is read and transferred to buf, or an
541 end-of-file condition is encountered. The string is then terminated
542 with a null character.
543 gzgets returns buf, or Z_NULL in case of error.
545 The current implementation is not optimized at all.
547 char * ZEXPORT
gzgets(file
, buf
, len
)
553 if (buf
== Z_NULL
|| len
<= 0) return Z_NULL
;
555 while (--len
> 0 && gzread(file
, buf
, 1) == 1 && *buf
++ != '\n') ;
557 return b
== buf
&& len
> 0 ? Z_NULL
: b
;
561 #ifndef NO_GZCOMPRESS
562 /* ===========================================================================
563 Writes the given number of uncompressed bytes into the compressed file.
564 gzwrite returns the number of bytes actually written (0 in case of error).
566 int ZEXPORT
gzwrite (file
, buf
, len
)
571 gz_stream
*s
= (gz_stream
*)file
;
573 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
575 s
->stream
.next_in
= __UNCONST(buf
);
576 s
->stream
.avail_in
= len
;
578 while (s
->stream
.avail_in
!= 0) {
580 if (s
->stream
.avail_out
== 0) {
582 s
->stream
.next_out
= s
->outbuf
;
583 if (fwrite(s
->outbuf
, 1, Z_BUFSIZE
, s
->file
) != Z_BUFSIZE
) {
587 s
->stream
.avail_out
= Z_BUFSIZE
;
589 s
->in
+= s
->stream
.avail_in
;
590 s
->out
+= s
->stream
.avail_out
;
591 s
->z_err
= deflate(&(s
->stream
), Z_NO_FLUSH
);
592 s
->in
-= s
->stream
.avail_in
;
593 s
->out
-= s
->stream
.avail_out
;
594 if (s
->z_err
!= Z_OK
) break;
596 s
->crc
= crc32(s
->crc
, (const Bytef
*)buf
, len
);
598 return (int)(len
- s
->stream
.avail_in
);
602 /* ===========================================================================
603 Converts, formats, and writes the args to the compressed file under
604 control of the format string, as in fprintf. gzprintf returns the number of
605 uncompressed bytes actually written (0 in case of error).
610 int ZEXPORTVA
gzprintf (gzFile file
, const char *format
, /* args */ ...)
612 char buf
[Z_PRINTF_BUFSIZE
];
616 buf
[sizeof(buf
) - 1] = 0;
617 va_start(va
, format
);
619 # ifdef HAS_vsprintf_void
620 (void)vsprintf(buf
, format
, va
);
622 for (len
= 0; len
< sizeof(buf
); len
++)
623 if (buf
[len
] == 0) break;
625 len
= vsprintf(buf
, format
, va
);
629 # ifdef HAS_vsnprintf_void
630 (void)vsnprintf(buf
, sizeof(buf
), format
, va
);
634 len
= vsnprintf(buf
, sizeof(buf
), format
, va
);
638 if (len
<= 0 || len
>= (int)sizeof(buf
) || buf
[sizeof(buf
) - 1] != 0)
640 return gzwrite(file
, buf
, (unsigned)len
);
642 #else /* not ANSI C */
644 int ZEXPORTVA
gzprintf (file
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
, a10
,
645 a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
)
648 int a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
, a10
,
649 a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
;
651 char buf
[Z_PRINTF_BUFSIZE
];
654 buf
[sizeof(buf
) - 1] = 0;
656 # ifdef HAS_sprintf_void
657 sprintf(buf
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
658 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
659 for (len
= 0; len
< sizeof(buf
); len
++)
660 if (buf
[len
] == 0) break;
662 len
= sprintf(buf
, format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
663 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
666 # ifdef HAS_snprintf_void
667 snprintf(buf
, sizeof(buf
), format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
668 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
671 len
= snprintf(buf
, sizeof(buf
), format
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
,
672 a9
, a10
, a11
, a12
, a13
, a14
, a15
, a16
, a17
, a18
, a19
, a20
);
675 if (len
<= 0 || len
>= sizeof(buf
) || buf
[sizeof(buf
) - 1] != 0)
677 return gzwrite(file
, buf
, len
);
681 /* ===========================================================================
682 Writes c, converted to an unsigned char, into the compressed file.
683 gzputc returns the value that was written, or -1 in case of error.
685 int ZEXPORT
gzputc(file
, c
)
689 unsigned char cc
= (unsigned char) c
; /* required for big endian systems */
691 return gzwrite(file
, &cc
, 1) == 1 ? (int)cc
: -1;
695 /* ===========================================================================
696 Writes the given null-terminated string to the compressed file, excluding
697 the terminating null character.
698 gzputs returns the number of characters written, or -1 in case of error.
700 int ZEXPORT
gzputs(file
, s
)
704 return gzwrite(file
, __UNCONST(s
), (unsigned)strlen(s
));
708 /* ===========================================================================
709 Flushes all pending output into the compressed file. The parameter
710 flush is as in the deflate() function.
712 local
int do_flush (file
, flush
)
718 gz_stream
*s
= (gz_stream
*)file
;
720 if (s
== NULL
|| s
->mode
!= 'w') return Z_STREAM_ERROR
;
722 s
->stream
.avail_in
= 0; /* should be zero already anyway */
725 len
= Z_BUFSIZE
- s
->stream
.avail_out
;
728 if ((uInt
)fwrite(s
->outbuf
, 1, len
, s
->file
) != len
) {
732 s
->stream
.next_out
= s
->outbuf
;
733 s
->stream
.avail_out
= Z_BUFSIZE
;
736 s
->out
+= s
->stream
.avail_out
;
737 s
->z_err
= deflate(&(s
->stream
), flush
);
738 s
->out
-= s
->stream
.avail_out
;
740 /* Ignore the second of two consecutive flushes: */
741 if (len
== 0 && s
->z_err
== Z_BUF_ERROR
) s
->z_err
= Z_OK
;
743 /* deflate has finished flushing only when it hasn't used up
744 * all the available space in the output buffer:
746 done
= (s
->stream
.avail_out
!= 0 || s
->z_err
== Z_STREAM_END
);
748 if (s
->z_err
!= Z_OK
&& s
->z_err
!= Z_STREAM_END
) break;
750 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
753 int ZEXPORT
gzflush (file
, flush
)
757 gz_stream
*s
= (gz_stream
*)file
;
758 int err
= do_flush (file
, flush
);
762 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
764 #endif /* NO_GZCOMPRESS */
766 /* ===========================================================================
767 Sets the starting position for the next gzread or gzwrite on the given
768 compressed file. The offset represents a number of bytes in the
769 gzseek returns the resulting offset location as measured in bytes from
770 the beginning of the uncompressed stream, or -1 in case of error.
771 SEEK_END is not implemented, returns error.
772 In this version of the library, gzseek can be extremely slow.
774 z_off_t ZEXPORT
gzseek (file
, offset
, 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 /* ===========================================================================
864 int ZEXPORT
gzrewind (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
= 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.
888 z_off_t ZEXPORT
gztell (file
)
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.
898 int ZEXPORT
gzeof (file
)
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.
915 int ZEXPORT
gzdirect (file
)
918 gz_stream
*s
= (gz_stream
*)file
;
920 if (s
== NULL
|| s
->mode
!= 'r') return 0;
921 return s
->transparent
;
924 #ifndef NO_GZCOMPRESS
925 /* ===========================================================================
926 Outputs a long in LSB order to the given file
928 local
void putLong (file
, x
)
933 for (n
= 0; n
< 4; n
++) {
934 fputc((int)(x
& 0xff), file
);
940 /* ===========================================================================
941 Reads a long in LSB order from the given gz_stream. Sets z_err in case
944 local uLong
getLong (s
)
947 uLong x
= (uLong
)get_byte(s
);
950 x
+= ((uLong
)get_byte(s
))<<8;
951 x
+= ((uLong
)get_byte(s
))<<16;
953 if (c
== EOF
) s
->z_err
= Z_DATA_ERROR
;
958 /* ===========================================================================
959 Flushes all pending output if necessary, closes the compressed file
960 and deallocates all the (de)compression state.
962 int ZEXPORT
gzclose (file
)
965 gz_stream
*s
= (gz_stream
*)file
;
967 if (s
== NULL
) return Z_STREAM_ERROR
;
969 if (s
->mode
== 'w') {
971 return Z_STREAM_ERROR
;
973 if (do_flush (file
, Z_FINISH
) != Z_OK
)
974 return destroy((gz_stream
*)file
);
976 putLong (s
->file
, s
->crc
);
977 putLong (s
->file
, (uLong
)(s
->in
& 0xffffffff));
980 return destroy((gz_stream
*)file
);
984 # define zstrerror(errnum) strerror(errnum)
986 # define zstrerror(errnum) ""
989 /* ===========================================================================
990 Returns the error message for the last error which occurred on the
991 given compressed file. errnum is set to zlib error number. If an
992 error occurred in the file system and not in the compression library,
993 errnum is set to Z_ERRNO and the application may consult errno
994 to get the exact error code.
996 const char * ZEXPORT
gzerror (file
, errnum
)
1001 gz_stream
*s
= (gz_stream
*)file
;
1004 *errnum
= Z_STREAM_ERROR
;
1005 return (const char*)ERR_MSG(Z_STREAM_ERROR
);
1008 if (*errnum
== Z_OK
) return (const char*)"";
1010 m
= *errnum
== Z_ERRNO
? zstrerror(errno
) : s
->stream
.msg
;
1012 if (m
== NULL
|| *m
== '\0') m
= ERR_MSG(s
->z_err
);
1015 s
->msg
= (char*)ALLOC(strlen(s
->path
) + strlen(m
) + 3);
1016 if (s
->msg
== Z_NULL
) return ERR_MSG(Z_MEM_ERROR
);
1017 strcpy(s
->msg
, s
->path
);
1018 strcat(s
->msg
, ": ");
1020 return (const char*)s
->msg
;
1023 /* ===========================================================================
1024 Clear the error and end-of-file flags, and do the same for the real file.
1026 void ZEXPORT
gzclearerr (file
)
1029 gz_stream
*s
= (gz_stream
*)file
;
1031 if (s
== NULL
) return;
1032 if (s
->z_err
!= Z_STREAM_END
) s
->z_err
= Z_OK
;