Back to index

tetex-bin  3.0
gzio.c
Go to the documentation of this file.
00001 /* gzio.c -- IO on .gz files
00002  * Copyright (C) 1995-2003 Jean-loup Gailly.
00003  * For conditions of distribution and use, see copyright notice in zlib.h
00004  *
00005  * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
00006  */
00007 
00008 /* @(#) $Id$ */
00009 
00010 #include <stdio.h>
00011 
00012 #include "zutil.h"
00013 
00014 #ifdef NO_DEFLATE       /* for compatiblity with old definition */
00015 #  define NO_GZCOMPRESS
00016 #endif
00017 
00018 #ifndef NO_DUMMY_DECL
00019 struct internal_state {int dummy;}; /* for buggy compilers */
00020 #endif
00021 
00022 #ifndef Z_BUFSIZE
00023 #  ifdef MAXSEG_64K
00024 #    define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
00025 #  else
00026 #    define Z_BUFSIZE 16384
00027 #  endif
00028 #endif
00029 #ifndef Z_PRINTF_BUFSIZE
00030 #  define Z_PRINTF_BUFSIZE 4096
00031 #endif
00032 
00033 #ifdef __MVS__
00034 #  pragma map (fdopen , "\174\174FDOPEN")
00035    FILE *fdopen(int, const char *);
00036 #endif
00037 
00038 #ifndef STDC
00039 extern voidp  malloc OF((uInt size));
00040 extern void   free   OF((voidpf ptr));
00041 #endif
00042 
00043 #define ALLOC(size) malloc(size)
00044 #define TRYFREE(p) {if (p) free(p);}
00045 
00046 static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
00047 
00048 /* gzip flag byte */
00049 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
00050 #define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
00051 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
00052 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
00053 #define COMMENT      0x10 /* bit 4 set: file comment present */
00054 #define RESERVED     0xE0 /* bits 5..7: reserved */
00055 
00056 typedef struct gz_stream {
00057     z_stream stream;
00058     int      z_err;   /* error code for last stream operation */
00059     int      z_eof;   /* set if end of input file */
00060     FILE     *file;   /* .gz file */
00061     Byte     *inbuf;  /* input buffer */
00062     Byte     *outbuf; /* output buffer */
00063     uLong    crc;     /* crc32 of uncompressed data */
00064     char     *msg;    /* error message */
00065     char     *path;   /* path name for debugging only */
00066     int      transparent; /* 1 if input file is not a .gz file */
00067     char     mode;    /* 'w' or 'r' */
00068     z_off_t  start;   /* start of compressed data in file (header skipped) */
00069     z_off_t  in;      /* bytes into deflate or inflate */
00070     z_off_t  out;     /* bytes out of deflate or inflate */
00071     int      back;    /* one character push-back */
00072     int      last;    /* true if push-back is last character */
00073 } gz_stream;
00074 
00075 
00076 local gzFile gz_open      OF((const char *path, const char *mode, int  fd));
00077 local int do_flush        OF((gzFile file, int flush));
00078 local int    get_byte     OF((gz_stream *s));
00079 local void   check_header OF((gz_stream *s));
00080 local int    destroy      OF((gz_stream *s));
00081 local void   putLong      OF((FILE *file, uLong x));
00082 local uLong  getLong      OF((gz_stream *s));
00083 
00084 /* ===========================================================================
00085      Opens a gzip (.gz) file for reading or writing. The mode parameter
00086    is as in fopen ("rb" or "wb"). The file is given either by file descriptor
00087    or path name (if fd == -1).
00088      gz_open returns NULL if the file could not be opened or if there was
00089    insufficient memory to allocate the (de)compression state; errno
00090    can be checked to distinguish the two cases (if errno is zero, the
00091    zlib error is Z_MEM_ERROR).
00092 */
00093 local gzFile gz_open (path, mode, fd)
00094     const char *path;
00095     const char *mode;
00096     int  fd;
00097 {
00098     int err;
00099     int level = Z_DEFAULT_COMPRESSION; /* compression level */
00100     int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
00101     char *p = (char*)mode;
00102     gz_stream *s;
00103     char fmode[80]; /* copy of mode, without the compression level */
00104     char *m = fmode;
00105 
00106     if (!path || !mode) return Z_NULL;
00107 
00108     s = (gz_stream *)ALLOC(sizeof(gz_stream));
00109     if (!s) return Z_NULL;
00110 
00111     s->stream.zalloc = (alloc_func)0;
00112     s->stream.zfree = (free_func)0;
00113     s->stream.opaque = (voidpf)0;
00114     s->stream.next_in = s->inbuf = Z_NULL;
00115     s->stream.next_out = s->outbuf = Z_NULL;
00116     s->stream.avail_in = s->stream.avail_out = 0;
00117     s->file = NULL;
00118     s->z_err = Z_OK;
00119     s->z_eof = 0;
00120     s->in = 0;
00121     s->out = 0;
00122     s->back = EOF;
00123     s->crc = crc32(0L, Z_NULL, 0);
00124     s->msg = NULL;
00125     s->transparent = 0;
00126 
00127     s->path = (char*)ALLOC(strlen(path)+1);
00128     if (s->path == NULL) {
00129         return destroy(s), (gzFile)Z_NULL;
00130     }
00131     strcpy(s->path, path); /* do this early for debugging */
00132 
00133     s->mode = '\0';
00134     do {
00135         if (*p == 'r') s->mode = 'r';
00136         if (*p == 'w' || *p == 'a') s->mode = 'w';
00137         if (*p >= '0' && *p <= '9') {
00138             level = *p - '0';
00139         } else if (*p == 'f') {
00140           strategy = Z_FILTERED;
00141         } else if (*p == 'h') {
00142           strategy = Z_HUFFMAN_ONLY;
00143         } else if (*p == 'R') {
00144           strategy = Z_RLE;
00145         } else {
00146             *m++ = *p; /* copy the mode */
00147         }
00148     } while (*p++ && m != fmode + sizeof(fmode));
00149     if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
00150 
00151     if (s->mode == 'w') {
00152 #ifdef NO_GZCOMPRESS
00153         err = Z_STREAM_ERROR;
00154 #else
00155         err = deflateInit2(&(s->stream), level,
00156                            Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
00157         /* windowBits is passed < 0 to suppress zlib header */
00158 
00159         s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
00160 #endif
00161         if (err != Z_OK || s->outbuf == Z_NULL) {
00162             return destroy(s), (gzFile)Z_NULL;
00163         }
00164     } else {
00165         s->stream.next_in  = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
00166 
00167         err = inflateInit2(&(s->stream), -MAX_WBITS);
00168         /* windowBits is passed < 0 to tell that there is no zlib header.
00169          * Note that in this case inflate *requires* an extra "dummy" byte
00170          * after the compressed stream in order to complete decompression and
00171          * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
00172          * present after the compressed stream.
00173          */
00174         if (err != Z_OK || s->inbuf == Z_NULL) {
00175             return destroy(s), (gzFile)Z_NULL;
00176         }
00177     }
00178     s->stream.avail_out = Z_BUFSIZE;
00179 
00180     errno = 0;
00181     s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
00182 
00183     if (s->file == NULL) {
00184         return destroy(s), (gzFile)Z_NULL;
00185     }
00186     if (s->mode == 'w') {
00187         /* Write a very simple .gz header:
00188          */
00189         fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
00190              Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
00191         s->start = 10L;
00192         /* We use 10L instead of ftell(s->file) to because ftell causes an
00193          * fflush on some systems. This version of the library doesn't use
00194          * start anyway in write mode, so this initialization is not
00195          * necessary.
00196          */
00197     } else {
00198         check_header(s); /* skip the .gz header */
00199         s->start = ftell(s->file) - s->stream.avail_in;
00200     }
00201 
00202     return (gzFile)s;
00203 }
00204 
00205 /* ===========================================================================
00206      Opens a gzip (.gz) file for reading or writing.
00207 */
00208 gzFile ZEXPORT gzopen (path, mode)
00209     const char *path;
00210     const char *mode;
00211 {
00212     return gz_open (path, mode, -1);
00213 }
00214 
00215 /* ===========================================================================
00216      Associate a gzFile with the file descriptor fd. fd is not dup'ed here
00217    to mimic the behavio(u)r of fdopen.
00218 */
00219 gzFile ZEXPORT gzdopen (fd, mode)
00220     int fd;
00221     const char *mode;
00222 {
00223     char name[20];
00224 
00225     if (fd < 0) return (gzFile)Z_NULL;
00226     sprintf(name, "<fd:%d>", fd); /* for debugging */
00227 
00228     return gz_open (name, mode, fd);
00229 }
00230 
00231 /* ===========================================================================
00232  * Update the compression level and strategy
00233  */
00234 int ZEXPORT gzsetparams (file, level, strategy)
00235     gzFile file;
00236     int level;
00237     int strategy;
00238 {
00239     gz_stream *s = (gz_stream*)file;
00240 
00241     if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
00242 
00243     /* Make room to allow flushing */
00244     if (s->stream.avail_out == 0) {
00245 
00246         s->stream.next_out = s->outbuf;
00247         if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
00248             s->z_err = Z_ERRNO;
00249         }
00250         s->stream.avail_out = Z_BUFSIZE;
00251     }
00252 
00253     return deflateParams (&(s->stream), level, strategy);
00254 }
00255 
00256 /* ===========================================================================
00257      Read a byte from a gz_stream; update next_in and avail_in. Return EOF
00258    for end of file.
00259    IN assertion: the stream s has been sucessfully opened for reading.
00260 */
00261 local int get_byte(s)
00262     gz_stream *s;
00263 {
00264     if (s->z_eof) return EOF;
00265     if (s->stream.avail_in == 0) {
00266         errno = 0;
00267         s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
00268         if (s->stream.avail_in == 0) {
00269             s->z_eof = 1;
00270             if (ferror(s->file)) s->z_err = Z_ERRNO;
00271             return EOF;
00272         }
00273         s->stream.next_in = s->inbuf;
00274     }
00275     s->stream.avail_in--;
00276     return *(s->stream.next_in)++;
00277 }
00278 
00279 /* ===========================================================================
00280       Check the gzip header of a gz_stream opened for reading. Set the stream
00281     mode to transparent if the gzip magic header is not present; set s->err
00282     to Z_DATA_ERROR if the magic header is present but the rest of the header
00283     is incorrect.
00284     IN assertion: the stream s has already been created sucessfully;
00285        s->stream.avail_in is zero for the first time, but may be non-zero
00286        for concatenated .gz files.
00287 */
00288 local void check_header(s)
00289     gz_stream *s;
00290 {
00291     int method; /* method byte */
00292     int flags;  /* flags byte */
00293     uInt len;
00294     int c;
00295 
00296     /* Assure two bytes in the buffer so we can peek ahead -- handle case
00297        where first byte of header is at the end of the buffer after the last
00298        gzip segment */
00299     len = s->stream.avail_in;
00300     if (len < 2) {
00301         if (len) s->inbuf[0] = s->stream.next_in[0];
00302         errno = 0;
00303         len = fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
00304         if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
00305         s->stream.avail_in += len;
00306         s->stream.next_in = s->inbuf;
00307         if (s->stream.avail_in < 2) {
00308             s->transparent = s->stream.avail_in;
00309             return;
00310         }
00311     }
00312 
00313     /* Peek ahead to check the gzip magic header */
00314     if (s->stream.next_in[0] != gz_magic[0] ||
00315         s->stream.next_in[1] != gz_magic[1]) {
00316         s->transparent = 1;
00317         return;
00318     }
00319     s->stream.avail_in -= 2;
00320     s->stream.next_in += 2;
00321 
00322     /* Check the rest of the gzip header */
00323     method = get_byte(s);
00324     flags = get_byte(s);
00325     if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
00326         s->z_err = Z_DATA_ERROR;
00327         return;
00328     }
00329 
00330     /* Discard time, xflags and OS code: */
00331     for (len = 0; len < 6; len++) (void)get_byte(s);
00332 
00333     if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
00334         len  =  (uInt)get_byte(s);
00335         len += ((uInt)get_byte(s))<<8;
00336         /* len is garbage if EOF but the loop below will quit anyway */
00337         while (len-- != 0 && get_byte(s) != EOF) ;
00338     }
00339     if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
00340         while ((c = get_byte(s)) != 0 && c != EOF) ;
00341     }
00342     if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
00343         while ((c = get_byte(s)) != 0 && c != EOF) ;
00344     }
00345     if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
00346         for (len = 0; len < 2; len++) (void)get_byte(s);
00347     }
00348     s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
00349 }
00350 
00351  /* ===========================================================================
00352  * Cleanup then free the given gz_stream. Return a zlib error code.
00353    Try freeing in the reverse order of allocations.
00354  */
00355 local int destroy (s)
00356     gz_stream *s;
00357 {
00358     int err = Z_OK;
00359 
00360     if (!s) return Z_STREAM_ERROR;
00361 
00362     TRYFREE(s->msg);
00363 
00364     if (s->stream.state != NULL) {
00365         if (s->mode == 'w') {
00366 #ifdef NO_GZCOMPRESS
00367             err = Z_STREAM_ERROR;
00368 #else
00369             err = deflateEnd(&(s->stream));
00370 #endif
00371         } else if (s->mode == 'r') {
00372             err = inflateEnd(&(s->stream));
00373         }
00374     }
00375     if (s->file != NULL && fclose(s->file)) {
00376 #ifdef ESPIPE
00377         if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
00378 #endif
00379             err = Z_ERRNO;
00380     }
00381     if (s->z_err < 0) err = s->z_err;
00382 
00383     TRYFREE(s->inbuf);
00384     TRYFREE(s->outbuf);
00385     TRYFREE(s->path);
00386     TRYFREE(s);
00387     return err;
00388 }
00389 
00390 /* ===========================================================================
00391      Reads the given number of uncompressed bytes from the compressed file.
00392    gzread returns the number of bytes actually read (0 for end of file).
00393 */
00394 int ZEXPORT gzread (file, buf, len)
00395     gzFile file;
00396     voidp buf;
00397     unsigned len;
00398 {
00399     gz_stream *s = (gz_stream*)file;
00400     Bytef *start = (Bytef*)buf; /* starting point for crc computation */
00401     Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
00402 
00403     if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
00404 
00405     if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
00406     if (s->z_err == Z_STREAM_END) return 0;  /* EOF */
00407 
00408     next_out = (Byte*)buf;
00409     s->stream.next_out = (Bytef*)buf;
00410     s->stream.avail_out = len;
00411 
00412     if (s->stream.avail_out && s->back != EOF) {
00413         *next_out++ = s->back;
00414         s->stream.next_out++;
00415         s->stream.avail_out--;
00416         s->back = EOF;
00417         s->out++;
00418         if (s->last) {
00419             s->z_err = Z_STREAM_END;
00420             return 1;
00421         }
00422     }
00423 
00424     while (s->stream.avail_out != 0) {
00425 
00426         if (s->transparent) {
00427             /* Copy first the lookahead bytes: */
00428             uInt n = s->stream.avail_in;
00429             if (n > s->stream.avail_out) n = s->stream.avail_out;
00430             if (n > 0) {
00431                 zmemcpy(s->stream.next_out, s->stream.next_in, n);
00432                 next_out += n;
00433                 s->stream.next_out = next_out;
00434                 s->stream.next_in   += n;
00435                 s->stream.avail_out -= n;
00436                 s->stream.avail_in  -= n;
00437             }
00438             if (s->stream.avail_out > 0) {
00439                 s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out,
00440                                              s->file);
00441             }
00442             len -= s->stream.avail_out;
00443             s->in  += len;
00444             s->out += len;
00445             if (len == 0) s->z_eof = 1;
00446             return (int)len;
00447         }
00448         if (s->stream.avail_in == 0 && !s->z_eof) {
00449 
00450             errno = 0;
00451             s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
00452             if (s->stream.avail_in == 0) {
00453                 s->z_eof = 1;
00454                 if (ferror(s->file)) {
00455                     s->z_err = Z_ERRNO;
00456                     break;
00457                 }
00458             }
00459             s->stream.next_in = s->inbuf;
00460         }
00461         s->in += s->stream.avail_in;
00462         s->out += s->stream.avail_out;
00463         s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
00464         s->in -= s->stream.avail_in;
00465         s->out -= s->stream.avail_out;
00466 
00467         if (s->z_err == Z_STREAM_END) {
00468             /* Check CRC and original size */
00469             s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
00470             start = s->stream.next_out;
00471 
00472             if (getLong(s) != s->crc) {
00473                 s->z_err = Z_DATA_ERROR;
00474             } else {
00475                 (void)getLong(s);
00476                 /* The uncompressed length returned by above getlong() may be
00477                  * different from s->out in case of concatenated .gz files.
00478                  * Check for such files:
00479                  */
00480                 check_header(s);
00481                 if (s->z_err == Z_OK) {
00482                     inflateReset(&(s->stream));
00483                     s->crc = crc32(0L, Z_NULL, 0);
00484                 }
00485             }
00486         }
00487         if (s->z_err != Z_OK || s->z_eof) break;
00488     }
00489     s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
00490 
00491     return (int)(len - s->stream.avail_out);
00492 }
00493 
00494 
00495 /* ===========================================================================
00496       Reads one byte from the compressed file. gzgetc returns this byte
00497    or -1 in case of end of file or error.
00498 */
00499 int ZEXPORT gzgetc(file)
00500     gzFile file;
00501 {
00502     unsigned char c;
00503 
00504     return gzread(file, &c, 1) == 1 ? c : -1;
00505 }
00506 
00507 
00508 /* ===========================================================================
00509       Push one byte back onto the stream.
00510 */
00511 int ZEXPORT gzungetc(c, file)
00512     int c;
00513     gzFile file;
00514 {
00515     gz_stream *s = (gz_stream*)file;
00516 
00517     if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
00518     s->back = c;
00519     s->out--;
00520     s->last = (s->z_err == Z_STREAM_END);
00521     if (s->last) s->z_err = Z_OK;
00522     s->z_eof = 0;
00523     return c;
00524 }
00525 
00526 
00527 /* ===========================================================================
00528       Reads bytes from the compressed file until len-1 characters are
00529    read, or a newline character is read and transferred to buf, or an
00530    end-of-file condition is encountered.  The string is then terminated
00531    with a null character.
00532       gzgets returns buf, or Z_NULL in case of error.
00533 
00534       The current implementation is not optimized at all.
00535 */
00536 char * ZEXPORT gzgets(file, buf, len)
00537     gzFile file;
00538     char *buf;
00539     int len;
00540 {
00541     char *b = buf;
00542     if (buf == Z_NULL || len <= 0) return Z_NULL;
00543 
00544     while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
00545     *buf = '\0';
00546     return b == buf && len > 0 ? Z_NULL : b;
00547 }
00548 
00549 
00550 #ifndef NO_GZCOMPRESS
00551 /* ===========================================================================
00552      Writes the given number of uncompressed bytes into the compressed file.
00553    gzwrite returns the number of bytes actually written (0 in case of error).
00554 */
00555 int ZEXPORT gzwrite (file, buf, len)
00556     gzFile file;
00557     voidpc buf;
00558     unsigned len;
00559 {
00560     gz_stream *s = (gz_stream*)file;
00561 
00562     if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
00563 
00564     s->stream.next_in = (Bytef*)buf;
00565     s->stream.avail_in = len;
00566 
00567     while (s->stream.avail_in != 0) {
00568 
00569         if (s->stream.avail_out == 0) {
00570 
00571             s->stream.next_out = s->outbuf;
00572             if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
00573                 s->z_err = Z_ERRNO;
00574                 break;
00575             }
00576             s->stream.avail_out = Z_BUFSIZE;
00577         }
00578         s->in += s->stream.avail_in;
00579         s->out += s->stream.avail_out;
00580         s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
00581         s->in -= s->stream.avail_in;
00582         s->out -= s->stream.avail_out;
00583         if (s->z_err != Z_OK) break;
00584     }
00585     s->crc = crc32(s->crc, (const Bytef *)buf, len);
00586 
00587     return (int)(len - s->stream.avail_in);
00588 }
00589 
00590 
00591 /* ===========================================================================
00592      Converts, formats, and writes the args to the compressed file under
00593    control of the format string, as in fprintf. gzprintf returns the number of
00594    uncompressed bytes actually written (0 in case of error).
00595 */
00596 #ifdef STDC
00597 #include <stdarg.h>
00598 
00599 int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
00600 {
00601     char buf[Z_PRINTF_BUFSIZE];
00602     va_list va;
00603     int len;
00604 
00605     buf[sizeof(buf) - 1] = 0;
00606     va_start(va, format);
00607 #ifdef NO_vsnprintf
00608 #  ifdef HAS_vsprintf_void
00609     (void)vsprintf(buf, format, va);
00610     va_end(va);
00611     for (len = 0; len < sizeof(buf); len++)
00612         if (buf[len] == 0) break;
00613 #  else
00614     len = vsprintf(buf, format, va);
00615     va_end(va);
00616 #  endif
00617 #else
00618 #  ifdef HAS_vsnprintf_void
00619     (void)vsnprintf(buf, sizeof(buf), format, va);
00620     va_end(va);
00621     len = strlen(buf);
00622 #  else
00623     len = vsnprintf(buf, sizeof(buf), format, va);
00624     va_end(va);
00625 #  endif
00626 #endif
00627     if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
00628         return 0;
00629     return gzwrite(file, buf, (unsigned)len);
00630 }
00631 #else /* not ANSI C */
00632 
00633 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
00634                        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
00635     gzFile file;
00636     const char *format;
00637     int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
00638         a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
00639 {
00640     char buf[Z_PRINTF_BUFSIZE];
00641     int len;
00642 
00643     buf[sizeof(buf) - 1] = 0;
00644 #ifdef NO_snprintf
00645 #  ifdef HAS_sprintf_void
00646     sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
00647             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
00648     for (len = 0; len < sizeof(buf); len++)
00649         if (buf[len] == 0) break;
00650 #  else
00651     len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
00652                 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
00653 #  endif
00654 #else
00655 #  ifdef HAS_snprintf_void
00656     snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
00657              a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
00658     len = strlen(buf);
00659 #  else
00660     len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
00661                  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
00662 #  endif
00663 #endif
00664     if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
00665         return 0;
00666     return gzwrite(file, buf, len);
00667 }
00668 #endif
00669 
00670 /* ===========================================================================
00671       Writes c, converted to an unsigned char, into the compressed file.
00672    gzputc returns the value that was written, or -1 in case of error.
00673 */
00674 int ZEXPORT gzputc(file, c)
00675     gzFile file;
00676     int c;
00677 {
00678     unsigned char cc = (unsigned char) c; /* required for big endian systems */
00679 
00680     return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
00681 }
00682 
00683 
00684 /* ===========================================================================
00685       Writes the given null-terminated string to the compressed file, excluding
00686    the terminating null character.
00687       gzputs returns the number of characters written, or -1 in case of error.
00688 */
00689 int ZEXPORT gzputs(file, s)
00690     gzFile file;
00691     const char *s;
00692 {
00693     return gzwrite(file, (char*)s, (unsigned)strlen(s));
00694 }
00695 
00696 
00697 /* ===========================================================================
00698      Flushes all pending output into the compressed file. The parameter
00699    flush is as in the deflate() function.
00700 */
00701 local int do_flush (file, flush)
00702     gzFile file;
00703     int flush;
00704 {
00705     uInt len;
00706     int done = 0;
00707     gz_stream *s = (gz_stream*)file;
00708 
00709     if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
00710 
00711     s->stream.avail_in = 0; /* should be zero already anyway */
00712 
00713     for (;;) {
00714         len = Z_BUFSIZE - s->stream.avail_out;
00715 
00716         if (len != 0) {
00717             if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
00718                 s->z_err = Z_ERRNO;
00719                 return Z_ERRNO;
00720             }
00721             s->stream.next_out = s->outbuf;
00722             s->stream.avail_out = Z_BUFSIZE;
00723         }
00724         if (done) break;
00725         s->out += s->stream.avail_out;
00726         s->z_err = deflate(&(s->stream), flush);
00727         s->out -= s->stream.avail_out;
00728 
00729         /* Ignore the second of two consecutive flushes: */
00730         if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
00731 
00732         /* deflate has finished flushing only when it hasn't used up
00733          * all the available space in the output buffer:
00734          */
00735         done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
00736 
00737         if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
00738     }
00739     return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
00740 }
00741 
00742 int ZEXPORT gzflush (file, flush)
00743      gzFile file;
00744      int flush;
00745 {
00746     gz_stream *s = (gz_stream*)file;
00747     int err = do_flush (file, flush);
00748 
00749     if (err) return err;
00750     fflush(s->file);
00751     return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
00752 }
00753 #endif /* NO_GZCOMPRESS */
00754 
00755 /* ===========================================================================
00756       Sets the starting position for the next gzread or gzwrite on the given
00757    compressed file. The offset represents a number of bytes in the
00758       gzseek returns the resulting offset location as measured in bytes from
00759    the beginning of the uncompressed stream, or -1 in case of error.
00760       SEEK_END is not implemented, returns error.
00761       In this version of the library, gzseek can be extremely slow.
00762 */
00763 z_off_t ZEXPORT gzseek (file, offset, whence)
00764     gzFile file;
00765     z_off_t offset;
00766     int whence;
00767 {
00768     gz_stream *s = (gz_stream*)file;
00769 
00770     if (s == NULL || whence == SEEK_END ||
00771         s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
00772         return -1L;
00773     }
00774 
00775     if (s->mode == 'w') {
00776 #ifdef NO_GZCOMPRESS
00777         return -1L;
00778 #else
00779         if (whence == SEEK_SET) {
00780             offset -= s->in;
00781         }
00782         if (offset < 0) return -1L;
00783 
00784         /* At this point, offset is the number of zero bytes to write. */
00785         if (s->inbuf == Z_NULL) {
00786             s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
00787             if (s->inbuf == Z_NULL) return -1L;
00788             zmemzero(s->inbuf, Z_BUFSIZE);
00789         }
00790         while (offset > 0)  {
00791             uInt size = Z_BUFSIZE;
00792             if (offset < Z_BUFSIZE) size = (uInt)offset;
00793 
00794             size = gzwrite(file, s->inbuf, size);
00795             if (size == 0) return -1L;
00796 
00797             offset -= size;
00798         }
00799         return s->in;
00800 #endif
00801     }
00802     /* Rest of function is for reading only */
00803 
00804     /* compute absolute position */
00805     if (whence == SEEK_CUR) {
00806         offset += s->out;
00807     }
00808     if (offset < 0) return -1L;
00809 
00810     if (s->transparent) {
00811         /* map to fseek */
00812         s->back = EOF;
00813         s->stream.avail_in = 0;
00814         s->stream.next_in = s->inbuf;
00815         if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
00816 
00817         s->in = s->out = offset;
00818         return offset;
00819     }
00820 
00821     /* For a negative seek, rewind and use positive seek */
00822     if (offset >= s->out) {
00823         offset -= s->out;
00824     } else if (gzrewind(file) < 0) {
00825         return -1L;
00826     }
00827     /* offset is now the number of bytes to skip. */
00828 
00829     if (offset != 0 && s->outbuf == Z_NULL) {
00830         s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
00831         if (s->outbuf == Z_NULL) return -1L;
00832     }
00833     if (offset && s->back != EOF) {
00834         s->back = EOF;
00835         s->out++;
00836         offset--;
00837         if (s->last) s->z_err = Z_STREAM_END;
00838     }
00839     while (offset > 0)  {
00840         int size = Z_BUFSIZE;
00841         if (offset < Z_BUFSIZE) size = (int)offset;
00842 
00843         size = gzread(file, s->outbuf, (uInt)size);
00844         if (size <= 0) return -1L;
00845         offset -= size;
00846     }
00847     return s->out;
00848 }
00849 
00850 /* ===========================================================================
00851      Rewinds input file.
00852 */
00853 int ZEXPORT gzrewind (file)
00854     gzFile file;
00855 {
00856     gz_stream *s = (gz_stream*)file;
00857 
00858     if (s == NULL || s->mode != 'r') return -1;
00859 
00860     s->z_err = Z_OK;
00861     s->z_eof = 0;
00862     s->back = EOF;
00863     s->stream.avail_in = 0;
00864     s->stream.next_in = s->inbuf;
00865     s->crc = crc32(0L, Z_NULL, 0);
00866     if (!s->transparent) (void)inflateReset(&s->stream);
00867     s->in = 0;
00868     s->out = 0;
00869     return fseek(s->file, s->start, SEEK_SET);
00870 }
00871 
00872 /* ===========================================================================
00873      Returns the starting position for the next gzread or gzwrite on the
00874    given compressed file. This position represents a number of bytes in the
00875    uncompressed data stream.
00876 */
00877 z_off_t ZEXPORT gztell (file)
00878     gzFile file;
00879 {
00880     return gzseek(file, 0L, SEEK_CUR);
00881 }
00882 
00883 /* ===========================================================================
00884      Returns 1 when EOF has previously been detected reading the given
00885    input stream, otherwise zero.
00886 */
00887 int ZEXPORT gzeof (file)
00888     gzFile file;
00889 {
00890     gz_stream *s = (gz_stream*)file;
00891 
00892     /* With concatenated compressed files that can have embedded
00893      * crc trailers, z_eof is no longer the only/best indicator of EOF
00894      * on a gz_stream. Handle end-of-stream error explicitly here.
00895      */
00896     if (s == NULL || s->mode != 'r') return 0;
00897     if (s->z_eof) return 1;
00898     return s->z_err == Z_STREAM_END;
00899 }
00900 
00901 /* ===========================================================================
00902    Outputs a long in LSB order to the given file
00903 */
00904 local void putLong (file, x)
00905     FILE *file;
00906     uLong x;
00907 {
00908     int n;
00909     for (n = 0; n < 4; n++) {
00910         fputc((int)(x & 0xff), file);
00911         x >>= 8;
00912     }
00913 }
00914 
00915 /* ===========================================================================
00916    Reads a long in LSB order from the given gz_stream. Sets z_err in case
00917    of error.
00918 */
00919 local uLong getLong (s)
00920     gz_stream *s;
00921 {
00922     uLong x = (uLong)get_byte(s);
00923     int c;
00924 
00925     x += ((uLong)get_byte(s))<<8;
00926     x += ((uLong)get_byte(s))<<16;
00927     c = get_byte(s);
00928     if (c == EOF) s->z_err = Z_DATA_ERROR;
00929     x += ((uLong)c)<<24;
00930     return x;
00931 }
00932 
00933 /* ===========================================================================
00934      Flushes all pending output if necessary, closes the compressed file
00935    and deallocates all the (de)compression state.
00936 */
00937 int ZEXPORT gzclose (file)
00938     gzFile file;
00939 {
00940     int err;
00941     gz_stream *s = (gz_stream*)file;
00942 
00943     if (s == NULL) return Z_STREAM_ERROR;
00944 
00945     if (s->mode == 'w') {
00946 #ifdef NO_GZCOMPRESS
00947         return Z_STREAM_ERROR;
00948 #else
00949         err = do_flush (file, Z_FINISH);
00950         if (err != Z_OK) return destroy((gz_stream*)file);
00951 
00952         putLong (s->file, s->crc);
00953         putLong (s->file, (uLong)(s->in & 0xffffffff));
00954 #endif
00955     }
00956     return destroy((gz_stream*)file);
00957 }
00958 
00959 /* ===========================================================================
00960      Returns the error message for the last error which occured on the
00961    given compressed file. errnum is set to zlib error number. If an
00962    error occured in the file system and not in the compression library,
00963    errnum is set to Z_ERRNO and the application may consult errno
00964    to get the exact error code.
00965 */
00966 const char * ZEXPORT gzerror (file, errnum)
00967     gzFile file;
00968     int *errnum;
00969 {
00970     char *m;
00971     gz_stream *s = (gz_stream*)file;
00972 
00973     if (s == NULL) {
00974         *errnum = Z_STREAM_ERROR;
00975         return (const char*)ERR_MSG(Z_STREAM_ERROR);
00976     }
00977     *errnum = s->z_err;
00978     if (*errnum == Z_OK) return (const char*)"";
00979 
00980     m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
00981 
00982     if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
00983 
00984     TRYFREE(s->msg);
00985     s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
00986     if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
00987     strcpy(s->msg, s->path);
00988     strcat(s->msg, ": ");
00989     strcat(s->msg, m);
00990     return (const char*)s->msg;
00991 }
00992 
00993 /* ===========================================================================
00994      Clear the error and end-of-file flags, and do the same for the real file.
00995 */
00996 void ZEXPORT gzclearerr (file)
00997     gzFile file;
00998 {
00999     gz_stream *s = (gz_stream*)file;
01000 
01001     if (s == NULL) return;
01002     if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
01003     s->z_eof = 0;
01004     clearerr(s->file);
01005 }