]> git.saurik.com Git - wxWidgets.git/blame - src/zlib/gzio.c
don't append anything back in DoDeleteOneItem() if the control becomes empty (fixes...
[wxWidgets.git] / src / zlib / gzio.c
CommitLineData
c801d85f 1/* gzio.c -- IO on .gz files
41faf807 2 * Copyright (C) 1995-2005 Jean-loup Gailly.
c801d85f
KB
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 *
51dbdf87 5 * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
c801d85f
KB
6 */
7
8/* @(#) $Id$ */
9
10#include <stdio.h>
11
12#include "zutil.h"
13
41faf807 14#ifdef NO_DEFLATE /* for compatibility with old definition */
51dbdf87
VS
15# define NO_GZCOMPRESS
16#endif
17
18#ifndef NO_DUMMY_DECL
c801d85f 19struct internal_state {int dummy;}; /* for buggy compilers */
51dbdf87 20#endif
c801d85f
KB
21
22#ifndef Z_BUFSIZE
23# ifdef MAXSEG_64K
24# define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
25# else
26# define Z_BUFSIZE 16384
27# endif
28#endif
29#ifndef Z_PRINTF_BUFSIZE
30# define Z_PRINTF_BUFSIZE 4096
31#endif
32
51dbdf87
VS
33#ifdef __MVS__
34# pragma map (fdopen , "\174\174FDOPEN")
35 FILE *fdopen(int, const char *);
36#endif
37
38#ifndef STDC
39extern voidp malloc OF((uInt size));
40extern void free OF((voidpf ptr));
41#endif
42
c801d85f
KB
43#define ALLOC(size) malloc(size)
44#define TRYFREE(p) {if (p) free(p);}
45
b0744f80
VZ
46/* there is no errno under Windows CE, provide a dummy one to avoid modifying
47 too much code in this file */
48#ifdef _WIN32_WCE
49static int errno;
50#endif
51
51dbdf87 52static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
c801d85f
KB
53
54/* gzip flag byte */
55#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
56#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
57#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
58#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
59#define COMMENT 0x10 /* bit 4 set: file comment present */
60#define RESERVED 0xE0 /* bits 5..7: reserved */
61
62typedef struct gz_stream {
63 z_stream stream;
64 int z_err; /* error code for last stream operation */
65 int z_eof; /* set if end of input file */
66 FILE *file; /* .gz file */
67 Byte *inbuf; /* input buffer */
68 Byte *outbuf; /* output buffer */
69 uLong crc; /* crc32 of uncompressed data */
70 char *msg; /* error message */
71 char *path; /* path name for debugging only */
72 int transparent; /* 1 if input file is not a .gz file */
73 char mode; /* 'w' or 'r' */
51dbdf87
VS
74 z_off_t start; /* start of compressed data in file (header skipped) */
75 z_off_t in; /* bytes into deflate or inflate */
76 z_off_t out; /* bytes out of deflate or inflate */
77 int back; /* one character push-back */
78 int last; /* true if push-back is last character */
c801d85f
KB
79} gz_stream;
80
81
82local gzFile gz_open OF((const char *path, const char *mode, int fd));
83local int do_flush OF((gzFile file, int flush));
84local int get_byte OF((gz_stream *s));
85local void check_header OF((gz_stream *s));
86local int destroy OF((gz_stream *s));
87local void putLong OF((FILE *file, uLong x));
88local uLong getLong OF((gz_stream *s));
89
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).
51dbdf87 94 gz_open returns NULL if the file could not be opened or if there was
c801d85f
KB
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).
98*/
99local gzFile gz_open (path, mode, fd)
100 const char *path;
101 const char *mode;
102 int fd;
103{
104 int err;
105 int level = Z_DEFAULT_COMPRESSION; /* compression level */
106 int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
107 char *p = (char*)mode;
108 gz_stream *s;
109 char fmode[80]; /* copy of mode, without the compression level */
110 char *m = fmode;
111
112 if (!path || !mode) return Z_NULL;
113
114 s = (gz_stream *)ALLOC(sizeof(gz_stream));
115 if (!s) return Z_NULL;
116
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;
123 s->file = NULL;
124 s->z_err = Z_OK;
125 s->z_eof = 0;
51dbdf87
VS
126 s->in = 0;
127 s->out = 0;
128 s->back = EOF;
c801d85f
KB
129 s->crc = crc32(0L, Z_NULL, 0);
130 s->msg = NULL;
131 s->transparent = 0;
132
133 s->path = (char*)ALLOC(strlen(path)+1);
134 if (s->path == NULL) {
135 return destroy(s), (gzFile)Z_NULL;
136 }
137 strcpy(s->path, path); /* do this early for debugging */
138
139 s->mode = '\0';
140 do {
141 if (*p == 'r') s->mode = 'r';
142 if (*p == 'w' || *p == 'a') s->mode = 'w';
143 if (*p >= '0' && *p <= '9') {
51dbdf87
VS
144 level = *p - '0';
145 } else if (*p == 'f') {
146 strategy = Z_FILTERED;
147 } else if (*p == 'h') {
148 strategy = Z_HUFFMAN_ONLY;
149 } else if (*p == 'R') {
150 strategy = Z_RLE;
151 } else {
152 *m++ = *p; /* copy the mode */
153 }
c801d85f
KB
154 } while (*p++ && m != fmode + sizeof(fmode));
155 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
e6ebb514 156
c801d85f 157 if (s->mode == 'w') {
51dbdf87 158#ifdef NO_GZCOMPRESS
c801d85f
KB
159 err = Z_STREAM_ERROR;
160#else
161 err = deflateInit2(&(s->stream), level,
162 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
163 /* windowBits is passed < 0 to suppress zlib header */
164
165 s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
166#endif
167 if (err != Z_OK || s->outbuf == Z_NULL) {
168 return destroy(s), (gzFile)Z_NULL;
169 }
170 } else {
171 s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
172
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.
179 */
180 if (err != Z_OK || s->inbuf == Z_NULL) {
181 return destroy(s), (gzFile)Z_NULL;
182 }
183 }
184 s->stream.avail_out = Z_BUFSIZE;
185
186 errno = 0;
187 s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
188
189 if (s->file == NULL) {
190 return destroy(s), (gzFile)Z_NULL;
191 }
192 if (s->mode == 'w') {
193 /* Write a very simple .gz header:
194 */
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);
51dbdf87
VS
197 s->start = 10L;
198 /* We use 10L instead of ftell(s->file) to because ftell causes an
c801d85f 199 * fflush on some systems. This version of the library doesn't use
51dbdf87 200 * start anyway in write mode, so this initialization is not
c801d85f
KB
201 * necessary.
202 */
203 } else {
51dbdf87
VS
204 check_header(s); /* skip the .gz header */
205 s->start = ftell(s->file) - s->stream.avail_in;
c801d85f 206 }
e6ebb514 207
c801d85f
KB
208 return (gzFile)s;
209}
210
211/* ===========================================================================
212 Opens a gzip (.gz) file for reading or writing.
213*/
214gzFile ZEXPORT gzopen (path, mode)
215 const char *path;
216 const char *mode;
217{
218 return gz_open (path, mode, -1);
219}
220
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.
224*/
225gzFile ZEXPORT gzdopen (fd, mode)
226 int fd;
227 const char *mode;
228{
41faf807 229 char name[46]; /* allow for up to 128-bit integers */
c801d85f
KB
230
231 if (fd < 0) return (gzFile)Z_NULL;
232 sprintf(name, "<fd:%d>", fd); /* for debugging */
233
234 return gz_open (name, mode, fd);
235}
236
237/* ===========================================================================
238 * Update the compression level and strategy
239 */
240int ZEXPORT gzsetparams (file, level, strategy)
241 gzFile file;
242 int level;
243 int strategy;
244{
245 gz_stream *s = (gz_stream*)file;
246
247 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
248
249 /* Make room to allow flushing */
250 if (s->stream.avail_out == 0) {
251
51dbdf87
VS
252 s->stream.next_out = s->outbuf;
253 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
254 s->z_err = Z_ERRNO;
255 }
256 s->stream.avail_out = Z_BUFSIZE;
c801d85f
KB
257 }
258
259 return deflateParams (&(s->stream), level, strategy);
260}
261
262/* ===========================================================================
263 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
264 for end of file.
265 IN assertion: the stream s has been sucessfully opened for reading.
266*/
267local int get_byte(s)
268 gz_stream *s;
269{
270 if (s->z_eof) return EOF;
271 if (s->stream.avail_in == 0) {
51dbdf87 272 errno = 0;
41faf807 273 s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
51dbdf87
VS
274 if (s->stream.avail_in == 0) {
275 s->z_eof = 1;
276 if (ferror(s->file)) s->z_err = Z_ERRNO;
277 return EOF;
278 }
279 s->stream.next_in = s->inbuf;
c801d85f
KB
280 }
281 s->stream.avail_in--;
282 return *(s->stream.next_in)++;
283}
284
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
289 is incorrect.
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.
293*/
294local void check_header(s)
295 gz_stream *s;
296{
297 int method; /* method byte */
298 int flags; /* flags byte */
299 uInt len;
300 int c;
301
51dbdf87
VS
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
304 gzip segment */
305 len = s->stream.avail_in;
306 if (len < 2) {
307 if (len) s->inbuf[0] = s->stream.next_in[0];
308 errno = 0;
41faf807 309 len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
51dbdf87
VS
310 if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
311 s->stream.avail_in += len;
312 s->stream.next_in = s->inbuf;
313 if (s->stream.avail_in < 2) {
314 s->transparent = s->stream.avail_in;
315 return;
316 }
317 }
318
319 /* Peek ahead to check the gzip magic header */
320 if (s->stream.next_in[0] != gz_magic[0] ||
321 s->stream.next_in[1] != gz_magic[1]) {
322 s->transparent = 1;
323 return;
c801d85f 324 }
51dbdf87
VS
325 s->stream.avail_in -= 2;
326 s->stream.next_in += 2;
327
328 /* Check the rest of the gzip header */
c801d85f
KB
329 method = get_byte(s);
330 flags = get_byte(s);
331 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
51dbdf87
VS
332 s->z_err = Z_DATA_ERROR;
333 return;
c801d85f
KB
334 }
335
336 /* Discard time, xflags and OS code: */
337 for (len = 0; len < 6; len++) (void)get_byte(s);
338
339 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
51dbdf87
VS
340 len = (uInt)get_byte(s);
341 len += ((uInt)get_byte(s))<<8;
342 /* len is garbage if EOF but the loop below will quit anyway */
343 while (len-- != 0 && get_byte(s) != EOF) ;
c801d85f
KB
344 }
345 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
51dbdf87 346 while ((c = get_byte(s)) != 0 && c != EOF) ;
c801d85f
KB
347 }
348 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
51dbdf87 349 while ((c = get_byte(s)) != 0 && c != EOF) ;
c801d85f
KB
350 }
351 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
51dbdf87 352 for (len = 0; len < 2; len++) (void)get_byte(s);
c801d85f
KB
353 }
354 s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
355}
356
357 /* ===========================================================================
358 * Cleanup then free the given gz_stream. Return a zlib error code.
359 Try freeing in the reverse order of allocations.
360 */
361local int destroy (s)
362 gz_stream *s;
363{
364 int err = Z_OK;
365
366 if (!s) return Z_STREAM_ERROR;
367
368 TRYFREE(s->msg);
369
370 if (s->stream.state != NULL) {
51dbdf87
VS
371 if (s->mode == 'w') {
372#ifdef NO_GZCOMPRESS
373 err = Z_STREAM_ERROR;
c801d85f 374#else
51dbdf87 375 err = deflateEnd(&(s->stream));
c801d85f 376#endif
51dbdf87
VS
377 } else if (s->mode == 'r') {
378 err = inflateEnd(&(s->stream));
379 }
c801d85f
KB
380 }
381 if (s->file != NULL && fclose(s->file)) {
382#ifdef ESPIPE
51dbdf87 383 if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
c801d85f 384#endif
51dbdf87 385 err = Z_ERRNO;
c801d85f
KB
386 }
387 if (s->z_err < 0) err = s->z_err;
388
389 TRYFREE(s->inbuf);
390 TRYFREE(s->outbuf);
391 TRYFREE(s->path);
392 TRYFREE(s);
393 return err;
394}
395
396/* ===========================================================================
397 Reads the given number of uncompressed bytes from the compressed file.
398 gzread returns the number of bytes actually read (0 for end of file).
399*/
400int ZEXPORT gzread (file, buf, len)
401 gzFile file;
402 voidp buf;
403 unsigned len;
404{
405 gz_stream *s = (gz_stream*)file;
406 Bytef *start = (Bytef*)buf; /* starting point for crc computation */
407 Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
408
409 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
410
411 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
412 if (s->z_err == Z_STREAM_END) return 0; /* EOF */
413
414 next_out = (Byte*)buf;
415 s->stream.next_out = (Bytef*)buf;
416 s->stream.avail_out = len;
417
51dbdf87
VS
418 if (s->stream.avail_out && s->back != EOF) {
419 *next_out++ = s->back;
420 s->stream.next_out++;
421 s->stream.avail_out--;
422 s->back = EOF;
423 s->out++;
41faf807 424 start++;
51dbdf87
VS
425 if (s->last) {
426 s->z_err = Z_STREAM_END;
427 return 1;
428 }
429 }
430
c801d85f
KB
431 while (s->stream.avail_out != 0) {
432
51dbdf87
VS
433 if (s->transparent) {
434 /* Copy first the lookahead bytes: */
435 uInt n = s->stream.avail_in;
436 if (n > s->stream.avail_out) n = s->stream.avail_out;
437 if (n > 0) {
438 zmemcpy(s->stream.next_out, s->stream.next_in, n);
439 next_out += n;
440 s->stream.next_out = next_out;
441 s->stream.next_in += n;
442 s->stream.avail_out -= n;
443 s->stream.avail_in -= n;
444 }
445 if (s->stream.avail_out > 0) {
41faf807
MW
446 s->stream.avail_out -=
447 (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
51dbdf87
VS
448 }
449 len -= s->stream.avail_out;
450 s->in += len;
451 s->out += len;
c801d85f 452 if (len == 0) s->z_eof = 1;
51dbdf87
VS
453 return (int)len;
454 }
c801d85f
KB
455 if (s->stream.avail_in == 0 && !s->z_eof) {
456
457 errno = 0;
41faf807 458 s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
c801d85f
KB
459 if (s->stream.avail_in == 0) {
460 s->z_eof = 1;
51dbdf87
VS
461 if (ferror(s->file)) {
462 s->z_err = Z_ERRNO;
463 break;
464 }
c801d85f
KB
465 }
466 s->stream.next_in = s->inbuf;
467 }
51dbdf87
VS
468 s->in += s->stream.avail_in;
469 s->out += s->stream.avail_out;
c801d85f 470 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
51dbdf87
VS
471 s->in -= s->stream.avail_in;
472 s->out -= s->stream.avail_out;
473
474 if (s->z_err == Z_STREAM_END) {
475 /* Check CRC and original size */
476 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
477 start = s->stream.next_out;
478
479 if (getLong(s) != s->crc) {
480 s->z_err = Z_DATA_ERROR;
481 } else {
482 (void)getLong(s);
483 /* The uncompressed length returned by above getlong() may be
484 * different from s->out in case of concatenated .gz files.
485 * Check for such files:
486 */
487 check_header(s);
488 if (s->z_err == Z_OK) {
489 inflateReset(&(s->stream));
490 s->crc = crc32(0L, Z_NULL, 0);
491 }
492 }
493 }
494 if (s->z_err != Z_OK || s->z_eof) break;
c801d85f
KB
495 }
496 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
497
41faf807
MW
498 if (len == s->stream.avail_out &&
499 (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
500 return -1;
c801d85f
KB
501 return (int)(len - s->stream.avail_out);
502}
503
504
505/* ===========================================================================
506 Reads one byte from the compressed file. gzgetc returns this byte
507 or -1 in case of end of file or error.
508*/
509int ZEXPORT gzgetc(file)
510 gzFile file;
511{
512 unsigned char c;
513
514 return gzread(file, &c, 1) == 1 ? c : -1;
515}
516
517
51dbdf87
VS
518/* ===========================================================================
519 Push one byte back onto the stream.
520*/
521int ZEXPORT gzungetc(c, file)
522 int c;
523 gzFile file;
524{
525 gz_stream *s = (gz_stream*)file;
526
527 if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
528 s->back = c;
529 s->out--;
530 s->last = (s->z_err == Z_STREAM_END);
531 if (s->last) s->z_err = Z_OK;
532 s->z_eof = 0;
533 return c;
534}
535
536
c801d85f
KB
537/* ===========================================================================
538 Reads bytes from the compressed file until len-1 characters are
539 read, or a newline character is read and transferred to buf, or an
540 end-of-file condition is encountered. The string is then terminated
541 with a null character.
542 gzgets returns buf, or Z_NULL in case of error.
543
544 The current implementation is not optimized at all.
545*/
546char * ZEXPORT gzgets(file, buf, len)
547 gzFile file;
548 char *buf;
549 int len;
550{
551 char *b = buf;
552 if (buf == Z_NULL || len <= 0) return Z_NULL;
553
554 while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
555 *buf = '\0';
556 return b == buf && len > 0 ? Z_NULL : b;
557}
558
559
51dbdf87 560#ifndef NO_GZCOMPRESS
c801d85f
KB
561/* ===========================================================================
562 Writes the given number of uncompressed bytes into the compressed file.
563 gzwrite returns the number of bytes actually written (0 in case of error).
564*/
565int ZEXPORT gzwrite (file, buf, len)
566 gzFile file;
51dbdf87 567 voidpc buf;
c801d85f
KB
568 unsigned len;
569{
570 gz_stream *s = (gz_stream*)file;
571
572 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
573
574 s->stream.next_in = (Bytef*)buf;
575 s->stream.avail_in = len;
576
577 while (s->stream.avail_in != 0) {
578
579 if (s->stream.avail_out == 0) {
580
581 s->stream.next_out = s->outbuf;
582 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
583 s->z_err = Z_ERRNO;
584 break;
585 }
586 s->stream.avail_out = Z_BUFSIZE;
587 }
51dbdf87
VS
588 s->in += s->stream.avail_in;
589 s->out += s->stream.avail_out;
c801d85f 590 s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
51dbdf87
VS
591 s->in -= s->stream.avail_in;
592 s->out -= s->stream.avail_out;
c801d85f
KB
593 if (s->z_err != Z_OK) break;
594 }
595 s->crc = crc32(s->crc, (const Bytef *)buf, len);
596
597 return (int)(len - s->stream.avail_in);
598}
599
51dbdf87 600
c801d85f
KB
601/* ===========================================================================
602 Converts, formats, and writes the args to the compressed file under
603 control of the format string, as in fprintf. gzprintf returns the number of
604 uncompressed bytes actually written (0 in case of error).
605*/
606#ifdef STDC
607#include <stdarg.h>
608
609int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
610{
611 char buf[Z_PRINTF_BUFSIZE];
612 va_list va;
613 int len;
614
51dbdf87 615 buf[sizeof(buf) - 1] = 0;
c801d85f 616 va_start(va, format);
51dbdf87
VS
617#ifdef NO_vsnprintf
618# ifdef HAS_vsprintf_void
c801d85f 619 (void)vsprintf(buf, format, va);
c801d85f 620 va_end(va);
51dbdf87
VS
621 for (len = 0; len < sizeof(buf); len++)
622 if (buf[len] == 0) break;
623# else
624 len = vsprintf(buf, format, va);
625 va_end(va);
626# endif
627#else
628# ifdef HAS_vsnprintf_void
629 (void)vsnprintf(buf, sizeof(buf), format, va);
630 va_end(va);
631 len = strlen(buf);
632# else
633 len = vsnprintf(buf, sizeof(buf), format, va);
634 va_end(va);
635# endif
636#endif
637 if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
638 return 0;
c801d85f
KB
639 return gzwrite(file, buf, (unsigned)len);
640}
641#else /* not ANSI C */
642
643int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
51dbdf87 644 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
c801d85f
KB
645 gzFile file;
646 const char *format;
647 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
51dbdf87 648 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
c801d85f
KB
649{
650 char buf[Z_PRINTF_BUFSIZE];
651 int len;
652
51dbdf87
VS
653 buf[sizeof(buf) - 1] = 0;
654#ifdef NO_snprintf
655# ifdef HAS_sprintf_void
c801d85f 656 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
51dbdf87
VS
657 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
658 for (len = 0; len < sizeof(buf); len++)
659 if (buf[len] == 0) break;
660# else
661 len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
662 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
663# endif
664#else
665# ifdef HAS_snprintf_void
666 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
667 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
668 len = strlen(buf);
669# else
670 len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
671 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
672# endif
c801d85f 673#endif
51dbdf87
VS
674 if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
675 return 0;
c801d85f
KB
676 return gzwrite(file, buf, len);
677}
678#endif
679
680/* ===========================================================================
681 Writes c, converted to an unsigned char, into the compressed file.
682 gzputc returns the value that was written, or -1 in case of error.
683*/
684int ZEXPORT gzputc(file, c)
685 gzFile file;
686 int c;
687{
688 unsigned char cc = (unsigned char) c; /* required for big endian systems */
689
690 return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
691}
692
693
694/* ===========================================================================
695 Writes the given null-terminated string to the compressed file, excluding
696 the terminating null character.
697 gzputs returns the number of characters written, or -1 in case of error.
698*/
699int ZEXPORT gzputs(file, s)
700 gzFile file;
701 const char *s;
702{
703 return gzwrite(file, (char*)s, (unsigned)strlen(s));
704}
705
706
707/* ===========================================================================
708 Flushes all pending output into the compressed file. The parameter
709 flush is as in the deflate() function.
710*/
711local int do_flush (file, flush)
712 gzFile file;
713 int flush;
714{
715 uInt len;
716 int done = 0;
717 gz_stream *s = (gz_stream*)file;
718
719 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
720
721 s->stream.avail_in = 0; /* should be zero already anyway */
722
723 for (;;) {
724 len = Z_BUFSIZE - s->stream.avail_out;
725
726 if (len != 0) {
727 if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
728 s->z_err = Z_ERRNO;
729 return Z_ERRNO;
730 }
731 s->stream.next_out = s->outbuf;
732 s->stream.avail_out = Z_BUFSIZE;
733 }
734 if (done) break;
51dbdf87 735 s->out += s->stream.avail_out;
c801d85f 736 s->z_err = deflate(&(s->stream), flush);
51dbdf87 737 s->out -= s->stream.avail_out;
c801d85f 738
51dbdf87
VS
739 /* Ignore the second of two consecutive flushes: */
740 if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
c801d85f
KB
741
742 /* deflate has finished flushing only when it hasn't used up
e6ebb514 743 * all the available space in the output buffer:
c801d85f
KB
744 */
745 done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
e6ebb514 746
c801d85f
KB
747 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
748 }
749 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
750}
751
752int ZEXPORT gzflush (file, flush)
753 gzFile file;
754 int flush;
755{
756 gz_stream *s = (gz_stream*)file;
757 int err = do_flush (file, flush);
758
759 if (err) return err;
760 fflush(s->file);
761 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
762}
51dbdf87 763#endif /* NO_GZCOMPRESS */
c801d85f
KB
764
765/* ===========================================================================
766 Sets the starting position for the next gzread or gzwrite on the given
767 compressed file. The offset represents a number of bytes in the
768 gzseek returns the resulting offset location as measured in bytes from
769 the beginning of the uncompressed stream, or -1 in case of error.
770 SEEK_END is not implemented, returns error.
771 In this version of the library, gzseek can be extremely slow.
772*/
773z_off_t ZEXPORT gzseek (file, offset, whence)
774 gzFile file;
775 z_off_t offset;
776 int whence;
777{
778 gz_stream *s = (gz_stream*)file;
779
780 if (s == NULL || whence == SEEK_END ||
51dbdf87
VS
781 s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
782 return -1L;
c801d85f 783 }
e6ebb514 784
c801d85f 785 if (s->mode == 'w') {
51dbdf87
VS
786#ifdef NO_GZCOMPRESS
787 return -1L;
c801d85f 788#else
51dbdf87
VS
789 if (whence == SEEK_SET) {
790 offset -= s->in;
791 }
792 if (offset < 0) return -1L;
793
794 /* At this point, offset is the number of zero bytes to write. */
795 if (s->inbuf == Z_NULL) {
796 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
797 if (s->inbuf == Z_NULL) return -1L;
798 zmemzero(s->inbuf, Z_BUFSIZE);
799 }
800 while (offset > 0) {
801 uInt size = Z_BUFSIZE;
802 if (offset < Z_BUFSIZE) size = (uInt)offset;
803
804 size = gzwrite(file, s->inbuf, size);
805 if (size == 0) return -1L;
806
807 offset -= size;
808 }
809 return s->in;
c801d85f
KB
810#endif
811 }
812 /* Rest of function is for reading only */
813
814 /* compute absolute position */
815 if (whence == SEEK_CUR) {
51dbdf87 816 offset += s->out;
c801d85f
KB
817 }
818 if (offset < 0) return -1L;
819
820 if (s->transparent) {
51dbdf87
VS
821 /* map to fseek */
822 s->back = EOF;
823 s->stream.avail_in = 0;
824 s->stream.next_in = s->inbuf;
c801d85f
KB
825 if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
826
51dbdf87
VS
827 s->in = s->out = offset;
828 return offset;
c801d85f
KB
829 }
830
831 /* For a negative seek, rewind and use positive seek */
51dbdf87
VS
832 if (offset >= s->out) {
833 offset -= s->out;
c801d85f 834 } else if (gzrewind(file) < 0) {
51dbdf87 835 return -1L;
c801d85f
KB
836 }
837 /* offset is now the number of bytes to skip. */
838
839 if (offset != 0 && s->outbuf == Z_NULL) {
51dbdf87
VS
840 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
841 if (s->outbuf == Z_NULL) return -1L;
842 }
843 if (offset && s->back != EOF) {
844 s->back = EOF;
845 s->out++;
846 offset--;
847 if (s->last) s->z_err = Z_STREAM_END;
c801d85f
KB
848 }
849 while (offset > 0) {
51dbdf87
VS
850 int size = Z_BUFSIZE;
851 if (offset < Z_BUFSIZE) size = (int)offset;
c801d85f 852
51dbdf87
VS
853 size = gzread(file, s->outbuf, (uInt)size);
854 if (size <= 0) return -1L;
855 offset -= size;
c801d85f 856 }
51dbdf87 857 return s->out;
c801d85f
KB
858}
859
860/* ===========================================================================
e6ebb514 861 Rewinds input file.
c801d85f
KB
862*/
863int ZEXPORT gzrewind (file)
864 gzFile file;
865{
866 gz_stream *s = (gz_stream*)file;
e6ebb514 867
c801d85f
KB
868 if (s == NULL || s->mode != 'r') return -1;
869
870 s->z_err = Z_OK;
871 s->z_eof = 0;
51dbdf87 872 s->back = EOF;
c801d85f
KB
873 s->stream.avail_in = 0;
874 s->stream.next_in = s->inbuf;
a4019ec2 875 s->crc = crc32(0L, Z_NULL, 0);
51dbdf87
VS
876 if (!s->transparent) (void)inflateReset(&s->stream);
877 s->in = 0;
878 s->out = 0;
879 return fseek(s->file, s->start, SEEK_SET);
c801d85f
KB
880}
881
882/* ===========================================================================
883 Returns the starting position for the next gzread or gzwrite on the
884 given compressed file. This position represents a number of bytes in the
885 uncompressed data stream.
886*/
887z_off_t ZEXPORT gztell (file)
888 gzFile file;
889{
890 return gzseek(file, 0L, SEEK_CUR);
891}
892
893/* ===========================================================================
894 Returns 1 when EOF has previously been detected reading the given
895 input stream, otherwise zero.
896*/
897int ZEXPORT gzeof (file)
898 gzFile file;
899{
900 gz_stream *s = (gz_stream*)file;
e6ebb514 901
51dbdf87
VS
902 /* With concatenated compressed files that can have embedded
903 * crc trailers, z_eof is no longer the only/best indicator of EOF
904 * on a gz_stream. Handle end-of-stream error explicitly here.
905 */
906 if (s == NULL || s->mode != 'r') return 0;
907 if (s->z_eof) return 1;
908 return s->z_err == Z_STREAM_END;
c801d85f
KB
909}
910
41faf807
MW
911/* ===========================================================================
912 Returns 1 if reading and doing so transparently, otherwise zero.
913*/
914int ZEXPORT gzdirect (file)
915 gzFile file;
916{
917 gz_stream *s = (gz_stream*)file;
918
919 if (s == NULL || s->mode != 'r') return 0;
920 return s->transparent;
921}
922
c801d85f
KB
923/* ===========================================================================
924 Outputs a long in LSB order to the given file
925*/
926local void putLong (file, x)
927 FILE *file;
928 uLong x;
929{
930 int n;
931 for (n = 0; n < 4; n++) {
932 fputc((int)(x & 0xff), file);
933 x >>= 8;
934 }
935}
936
937/* ===========================================================================
a4019ec2
SC
938 Reads a long in LSB order from the given gz_stream. Sets z_err in case
939 of error.
c801d85f
KB
940*/
941local uLong getLong (s)
942 gz_stream *s;
943{
944 uLong x = (uLong)get_byte(s);
945 int c;
946
947 x += ((uLong)get_byte(s))<<8;
948 x += ((uLong)get_byte(s))<<16;
949 c = get_byte(s);
950 if (c == EOF) s->z_err = Z_DATA_ERROR;
951 x += ((uLong)c)<<24;
952 return x;
953}
954
955/* ===========================================================================
956 Flushes all pending output if necessary, closes the compressed file
957 and deallocates all the (de)compression state.
958*/
959int ZEXPORT gzclose (file)
960 gzFile file;
961{
c801d85f
KB
962 gz_stream *s = (gz_stream*)file;
963
964 if (s == NULL) return Z_STREAM_ERROR;
965
966 if (s->mode == 'w') {
51dbdf87
VS
967#ifdef NO_GZCOMPRESS
968 return Z_STREAM_ERROR;
c801d85f 969#else
41faf807
MW
970 if (do_flush (file, Z_FINISH) != Z_OK)
971 return destroy((gz_stream*)file);
c801d85f
KB
972
973 putLong (s->file, s->crc);
51dbdf87 974 putLong (s->file, (uLong)(s->in & 0xffffffff));
c801d85f
KB
975#endif
976 }
977 return destroy((gz_stream*)file);
978}
979
41faf807
MW
980#ifdef STDC
981# define zstrerror(errnum) strerror(errnum)
982#else
983# define zstrerror(errnum) ""
984#endif
985
c801d85f 986/* ===========================================================================
41faf807 987 Returns the error message for the last error which occurred on the
c801d85f 988 given compressed file. errnum is set to zlib error number. If an
41faf807 989 error occurred in the file system and not in the compression library,
c801d85f
KB
990 errnum is set to Z_ERRNO and the application may consult errno
991 to get the exact error code.
992*/
51dbdf87 993const char * ZEXPORT gzerror (file, errnum)
c801d85f
KB
994 gzFile file;
995 int *errnum;
996{
997 char *m;
998 gz_stream *s = (gz_stream*)file;
999
1000 if (s == NULL) {
1001 *errnum = Z_STREAM_ERROR;
1002 return (const char*)ERR_MSG(Z_STREAM_ERROR);
1003 }
1004 *errnum = s->z_err;
1005 if (*errnum == Z_OK) return (const char*)"";
1006
51dbdf87 1007 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
c801d85f
KB
1008
1009 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
1010
1011 TRYFREE(s->msg);
1012 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
51dbdf87 1013 if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
c801d85f
KB
1014 strcpy(s->msg, s->path);
1015 strcat(s->msg, ": ");
1016 strcat(s->msg, m);
1017 return (const char*)s->msg;
1018}
51dbdf87
VS
1019
1020/* ===========================================================================
1021 Clear the error and end-of-file flags, and do the same for the real file.
1022*/
1023void ZEXPORT gzclearerr (file)
1024 gzFile file;
1025{
1026 gz_stream *s = (gz_stream*)file;
1027
1028 if (s == NULL) return;
1029 if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
1030 s->z_eof = 0;
1031 clearerr(s->file);
1032}