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