src/utilfuns/zlib/gzio.c File Reference

#include <stdio.h>
#include "zutil.h"
Include dependency graph for gzio.c:

Go to the source code of this file.

Classes

struct  gz_stream
struct  internal_state

Defines

#define ALLOC(size)   malloc(size)
#define ASCII_FLAG   0x01
#define COMMENT   0x10
#define EXTRA_FIELD   0x04
#define HEAD_CRC   0x02
#define ORIG_NAME   0x08
#define RESERVED   0xE0
#define TRYFREE(p)   {if (p) free(p);}
#define Z_BUFSIZE   16384
#define Z_PRINTF_BUFSIZE   4096

Functions

local void check_header (gz_stream *s)
local int destroy (gz_stream *s)
local int do_flush (gzFile file, int flush)
local int get_byte (gz_stream *s)
local uLong getLong (gz_stream *s)
local gzFile gz_open (char *path, const char *mode, int fd) const
int ZEXPORT gzclose (gzFile file)
gzFile ZEXPORT gzdopen (int fd, const char *mode)
int ZEXPORT gzeof (gzFile file)
const char *ZEXPORT gzerror (gzFile file, int *errnum)
int ZEXPORT gzflush (gzFile file, int flush)
int ZEXPORT gzgetc (gzFile file)
char *ZEXPORT gzgets (gzFile file, char *buf, int len)
gzFile ZEXPORT gzopen (char *path, const char *mode) const
int ZEXPORTVA gzprintf (gzFile file, const char *format, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20)
int ZEXPORT gzputc (gzFile file, int c)
int ZEXPORT gzputs (gzFile file, const char *s)
int ZEXPORT gzread (gzFile file, voidp buf, unsigned len)
int ZEXPORT gzrewind (gzFile file)
z_off_t ZEXPORT gzseek (gzFile file, z_off_t offset, int whence)
int ZEXPORT gzsetparams (gzFile file, int level, int strategy)
z_off_t ZEXPORT gztell (gzFile file)
int ZEXPORT gzwrite (gzFile file, const voidp buf, unsigned len)
local void putLong OF ((FILE *file, uLong x))
local int get_byte OF ((gz_stream *s))
local int do_flush OF ((gzFile file, int flush))
local gzFile gz_open OF ((const char *path, const char *mode, intfd))
local void putLong (FILE *file, uLong x)

Variables

static int gz_magic [2] = {0x1f, 0x8b}

Define Documentation

#define ALLOC ( size   )     malloc(size)

Definition at line 27 of file gzio.c.

#define ASCII_FLAG   0x01

Definition at line 33 of file gzio.c.

#define COMMENT   0x10

Definition at line 37 of file gzio.c.

#define EXTRA_FIELD   0x04

Definition at line 35 of file gzio.c.

#define HEAD_CRC   0x02

Definition at line 34 of file gzio.c.

#define ORIG_NAME   0x08

Definition at line 36 of file gzio.c.

#define RESERVED   0xE0

Definition at line 38 of file gzio.c.

#define TRYFREE (  )     {if (p) free(p);}

Definition at line 28 of file gzio.c.

#define Z_BUFSIZE   16384

Definition at line 20 of file gzio.c.

#define Z_PRINTF_BUFSIZE   4096

Definition at line 24 of file gzio.c.


Function Documentation

local void check_header ( gz_stream s  ) 

Definition at line 263 of file gzio.c.

00265 {
00266     int method; /* method byte */
00267     int flags;  /* flags byte */
00268     uInt len;
00269     int c;
00270 
00271     /* Check the gzip magic header */
00272     for (len = 0; len < 2; len++) {
00273     c = get_byte(s);
00274     if (c != gz_magic[len]) {
00275         if (len != 0) s->stream.avail_in++, s->stream.next_in--;
00276         if (c != EOF) {
00277         s->stream.avail_in++, s->stream.next_in--;
00278         s->transparent = 1;
00279         }
00280         s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
00281         return;
00282     }
00283     }
00284     method = get_byte(s);
00285     flags = get_byte(s);
00286     if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
00287     s->z_err = Z_DATA_ERROR;
00288     return;
00289     }
00290 
00291     /* Discard time, xflags and OS code: */
00292     for (len = 0; len < 6; len++) (void)get_byte(s);
00293 
00294     if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
00295     len  =  (uInt)get_byte(s);
00296     len += ((uInt)get_byte(s))<<8;
00297     /* len is garbage if EOF but the loop below will quit anyway */
00298     while (len-- != 0 && get_byte(s) != EOF) ;
00299     }
00300     if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
00301     while ((c = get_byte(s)) != 0 && c != EOF) ;
00302     }
00303     if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
00304     while ((c = get_byte(s)) != 0 && c != EOF) ;
00305     }
00306     if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
00307     for (len = 0; len < 2; len++) (void)get_byte(s);
00308     }
00309     s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
00310 }

local int destroy ( gz_stream s  ) 

Definition at line 316 of file gzio.c.

00318 {
00319     int err = Z_OK;
00320 
00321     if (!s) return Z_STREAM_ERROR;
00322 
00323     TRYFREE(s->msg);
00324 
00325     if (s->stream.state != NULL) {
00326     if (s->mode == 'w') {
00327 #ifdef NO_DEFLATE
00328         err = Z_STREAM_ERROR;
00329 #else
00330         err = deflateEnd(&(s->stream));
00331 #endif
00332     } else if (s->mode == 'r') {
00333         err = inflateEnd(&(s->stream));
00334     }
00335     }
00336     if (s->file != NULL && fclose(s->file)) {
00337 #ifdef ESPIPE
00338     if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
00339 #endif
00340         err = Z_ERRNO;
00341     }
00342     if (s->z_err < 0) err = s->z_err;
00343 
00344     TRYFREE(s->inbuf);
00345     TRYFREE(s->outbuf);
00346     TRYFREE(s->path);
00347     TRYFREE(s);
00348     return err;
00349 }

local int do_flush ( gzFile  file,
int  flush 
)

Definition at line 600 of file gzio.c.

00603 {
00604     uInt len;
00605     int done = 0;
00606     gz_stream *s = (gz_stream*)file;
00607 
00608     if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
00609 
00610     s->stream.avail_in = 0; /* should be zero already anyway */
00611 
00612     for (;;) {
00613         len = Z_BUFSIZE - s->stream.avail_out;
00614 
00615         if (len != 0) {
00616             if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
00617                 s->z_err = Z_ERRNO;
00618                 return Z_ERRNO;
00619             }
00620             s->stream.next_out = s->outbuf;
00621             s->stream.avail_out = Z_BUFSIZE;
00622         }
00623         if (done) break;
00624         s->z_err = deflate(&(s->stream), flush);
00625 
00626     /* Ignore the second of two consecutive flushes: */
00627     if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
00628 
00629         /* deflate has finished flushing only when it hasn't used up
00630          * all the available space in the output buffer: 
00631          */
00632         done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
00633  
00634         if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
00635     }
00636     return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
00637 }

local int get_byte ( gz_stream s  ) 

Definition at line 236 of file gzio.c.

00238 {
00239     if (s->z_eof) return EOF;
00240     if (s->stream.avail_in == 0) {
00241     errno = 0;
00242     s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
00243     if (s->stream.avail_in == 0) {
00244         s->z_eof = 1;
00245         if (ferror(s->file)) s->z_err = Z_ERRNO;
00246         return EOF;
00247     }
00248     s->stream.next_in = s->inbuf;
00249     }
00250     s->stream.avail_in--;
00251     return *(s->stream.next_in)++;
00252 }

local uLong getLong ( gz_stream s  ) 

Definition at line 804 of file gzio.c.

00806 {
00807     uLong x = (uLong)get_byte(s);
00808     int c;
00809 
00810     x += ((uLong)get_byte(s))<<8;
00811     x += ((uLong)get_byte(s))<<16;
00812     c = get_byte(s);
00813     if (c == EOF) s->z_err = Z_DATA_ERROR;
00814     x += ((uLong)c)<<24;
00815     return x;
00816 }

local gzFile gz_open ( char *  path,
const char *  mode,
int  fd 
) const

Definition at line 73 of file gzio.c.

00077 {
00078     int err;
00079     int level = Z_DEFAULT_COMPRESSION; /* compression level */
00080     int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
00081     char *p = (char*)mode;
00082     gz_stream *s;
00083     char fmode[80]; /* copy of mode, without the compression level */
00084     char *m = fmode;
00085 
00086     if (!path || !mode) return Z_NULL;
00087 
00088     s = (gz_stream *)ALLOC(sizeof(gz_stream));
00089     if (!s) return Z_NULL;
00090 
00091     s->stream.zalloc = (alloc_func)0;
00092     s->stream.zfree = (free_func)0;
00093     s->stream.opaque = (voidpf)0;
00094     s->stream.next_in = s->inbuf = Z_NULL;
00095     s->stream.next_out = s->outbuf = Z_NULL;
00096     s->stream.avail_in = s->stream.avail_out = 0;
00097     s->file = NULL;
00098     s->z_err = Z_OK;
00099     s->z_eof = 0;
00100     s->crc = crc32(0L, Z_NULL, 0);
00101     s->msg = NULL;
00102     s->transparent = 0;
00103 
00104     s->path = (char*)ALLOC(strlen(path)+1);
00105     if (s->path == NULL) {
00106         return destroy(s), (gzFile)Z_NULL;
00107     }
00108     strcpy(s->path, path); /* do this early for debugging */
00109 
00110     s->mode = '\0';
00111     do {
00112         if (*p == 'r') s->mode = 'r';
00113         if (*p == 'w' || *p == 'a') s->mode = 'w';
00114         if (*p >= '0' && *p <= '9') {
00115         level = *p - '0';
00116     } else if (*p == 'f') {
00117       strategy = Z_FILTERED;
00118     } else if (*p == 'h') {
00119       strategy = Z_HUFFMAN_ONLY;
00120     } else {
00121         *m++ = *p; /* copy the mode */
00122     }
00123     } while (*p++ && m != fmode + sizeof(fmode));
00124     if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
00125     
00126     if (s->mode == 'w') {
00127 #ifdef NO_DEFLATE
00128         err = Z_STREAM_ERROR;
00129 #else
00130         err = deflateInit2(&(s->stream), level,
00131                            Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
00132         /* windowBits is passed < 0 to suppress zlib header */
00133 
00134         s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
00135 #endif
00136         if (err != Z_OK || s->outbuf == Z_NULL) {
00137             return destroy(s), (gzFile)Z_NULL;
00138         }
00139     } else {
00140         s->stream.next_in  = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
00141 
00142         err = inflateInit2(&(s->stream), -MAX_WBITS);
00143         /* windowBits is passed < 0 to tell that there is no zlib header.
00144          * Note that in this case inflate *requires* an extra "dummy" byte
00145          * after the compressed stream in order to complete decompression and
00146          * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
00147          * present after the compressed stream.
00148          */
00149         if (err != Z_OK || s->inbuf == Z_NULL) {
00150             return destroy(s), (gzFile)Z_NULL;
00151         }
00152     }
00153     s->stream.avail_out = Z_BUFSIZE;
00154 
00155     errno = 0;
00156     s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
00157 
00158     if (s->file == NULL) {
00159         return destroy(s), (gzFile)Z_NULL;
00160     }
00161     if (s->mode == 'w') {
00162         /* Write a very simple .gz header:
00163          */
00164         fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
00165              Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
00166     s->startpos = 10L;
00167     /* We use 10L instead of ftell(s->file) to because ftell causes an
00168          * fflush on some systems. This version of the library doesn't use
00169          * startpos anyway in write mode, so this initialization is not
00170          * necessary.
00171          */
00172     } else {
00173     check_header(s); /* skip the .gz header */
00174     s->startpos = (ftell(s->file) - s->stream.avail_in);
00175     }
00176     
00177     return (gzFile)s;
00178 }

int ZEXPORT gzclose ( gzFile  file  ) 

Definition at line 822 of file gzio.c.

00824 {
00825     int err;
00826     gz_stream *s = (gz_stream*)file;
00827 
00828     if (s == NULL) return Z_STREAM_ERROR;
00829 
00830     if (s->mode == 'w') {
00831 #ifdef NO_DEFLATE
00832     return Z_STREAM_ERROR;
00833 #else
00834         err = do_flush (file, Z_FINISH);
00835         if (err != Z_OK) return destroy((gz_stream*)file);
00836 
00837         putLong (s->file, s->crc);
00838         putLong (s->file, s->stream.total_in);
00839 #endif
00840     }
00841     return destroy((gz_stream*)file);
00842 }

gzFile ZEXPORT gzdopen ( int  fd,
const char *  mode 
)

Definition at line 194 of file gzio.c.

00197 {
00198     char name[20];
00199 
00200     if (fd < 0) return (gzFile)Z_NULL;
00201     sprintf(name, "<fd:%d>", fd); /* for debugging */
00202 
00203     return gz_open (name, mode, fd);
00204 }

int ZEXPORT gzeof ( gzFile  file  ) 

Definition at line 778 of file gzio.c.

00780 {
00781     gz_stream *s = (gz_stream*)file;
00782     
00783     return (s == NULL || s->mode != 'r') ? 0 : s->z_eof;
00784 }

const char* ZEXPORT gzerror ( gzFile  file,
int *  errnum 
)

Definition at line 851 of file gzio.c.

00854 {
00855     char *m;
00856     gz_stream *s = (gz_stream*)file;
00857 
00858     if (s == NULL) {
00859         *errnum = Z_STREAM_ERROR;
00860         return (const char*)ERR_MSG(Z_STREAM_ERROR);
00861     }
00862     *errnum = s->z_err;
00863     if (*errnum == Z_OK) return (const char*)"";
00864 
00865     m =  (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
00866 
00867     if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
00868 
00869     TRYFREE(s->msg);
00870     s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
00871     strcpy(s->msg, s->path);
00872     strcat(s->msg, ": ");
00873     strcat(s->msg, m);
00874     return (const char*)s->msg;
00875 }

int ZEXPORT gzflush ( gzFile  file,
int  flush 
)

Definition at line 639 of file gzio.c.

00642 {
00643     gz_stream *s = (gz_stream*)file;
00644     int err = do_flush (file, flush);
00645 
00646     if (err) return err;
00647     fflush(s->file);
00648     return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
00649 }

int ZEXPORT gzgetc ( gzFile  file  ) 

Definition at line 449 of file gzio.c.

00451 {
00452     unsigned char c;
00453 
00454     return gzread(file, &c, 1) == 1 ? c : -1;
00455 }

char* ZEXPORT gzgets ( gzFile  file,
char *  buf,
int  len 
)

Definition at line 467 of file gzio.c.

00471 {
00472     char *b = buf;
00473     if (buf == Z_NULL || len <= 0) return Z_NULL;
00474 
00475     while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
00476     *buf = '\0';
00477     return b == buf && len > 0 ? Z_NULL : b;
00478 }

gzFile ZEXPORT gzopen ( char *  path,
const char *  mode 
) const

Definition at line 183 of file gzio.c.

00186 {
00187     return gz_open (path, mode, -1);
00188 }

int ZEXPORTVA gzprintf ( gzFile  file,
const char *  format,
int  a1,
int  a2,
int  a3,
int  a4,
int  a5,
int  a6,
int  a7,
int  a8,
int  a9,
int  a10,
int  a11,
int  a12,
int  a13,
int  a14,
int  a15,
int  a16,
int  a17,
int  a18,
int  a19,
int  a20 
)

Definition at line 545 of file gzio.c.

00551 {
00552     char buf[Z_PRINTF_BUFSIZE];
00553     int len;
00554 
00555 #ifdef HAS_snprintf
00556     snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
00557          a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
00558 #else
00559     sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
00560         a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
00561 #endif
00562     len = strlen(buf); /* old sprintf doesn't return the nb of bytes written */
00563     if (len <= 0) return 0;
00564 
00565     return gzwrite(file, buf, len);
00566 }

int ZEXPORT gzputc ( gzFile  file,
int  c 
)

Definition at line 573 of file gzio.c.

00576 {
00577     unsigned char cc = (unsigned char) c; /* required for big endian systems */
00578 
00579     return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
00580 }

int ZEXPORT gzputs ( gzFile  file,
const char *  s 
)

Definition at line 588 of file gzio.c.

00591 {
00592     return gzwrite(file, (char*)s, (unsigned)strlen(s));
00593 }

int ZEXPORT gzread ( gzFile  file,
voidp  buf,
unsigned  len 
)

Definition at line 355 of file gzio.c.

00359 {
00360     gz_stream *s = (gz_stream*)file;
00361     Bytef *start = (Bytef*)buf; /* starting point for crc computation */
00362     Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
00363 
00364     if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
00365 
00366     if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
00367     if (s->z_err == Z_STREAM_END) return 0;  /* EOF */
00368 
00369     next_out = (Byte*)buf;
00370     s->stream.next_out = (Bytef*)buf;
00371     s->stream.avail_out = len;
00372 
00373     while (s->stream.avail_out != 0) {
00374 
00375     if (s->transparent) {
00376         /* Copy first the lookahead bytes: */
00377         uInt n = s->stream.avail_in;
00378         if (n > s->stream.avail_out) n = s->stream.avail_out;
00379         if (n > 0) {
00380         zmemcpy(s->stream.next_out, s->stream.next_in, n);
00381         next_out += n;
00382         s->stream.next_out = next_out;
00383         s->stream.next_in   += n;
00384         s->stream.avail_out -= n;
00385         s->stream.avail_in  -= n;
00386         }
00387         if (s->stream.avail_out > 0) {
00388         s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out,
00389                          s->file);
00390         }
00391         len -= s->stream.avail_out;
00392         s->stream.total_in  += (uLong)len;
00393         s->stream.total_out += (uLong)len;
00394             if (len == 0) s->z_eof = 1;
00395         return (int)len;
00396     }
00397         if (s->stream.avail_in == 0 && !s->z_eof) {
00398 
00399             errno = 0;
00400             s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
00401             if (s->stream.avail_in == 0) {
00402                 s->z_eof = 1;
00403         if (ferror(s->file)) {
00404             s->z_err = Z_ERRNO;
00405             break;
00406         }
00407             }
00408             s->stream.next_in = s->inbuf;
00409         }
00410         s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
00411 
00412     if (s->z_err == Z_STREAM_END) {
00413         /* Check CRC and original size */
00414         s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
00415         start = s->stream.next_out;
00416 
00417         if (getLong(s) != s->crc) {
00418         s->z_err = Z_DATA_ERROR;
00419         } else {
00420             (void)getLong(s);
00421                 /* The uncompressed length returned by above getlong() may
00422                  * be different from s->stream.total_out) in case of
00423          * concatenated .gz files. Check for such files:
00424          */
00425         check_header(s);
00426         if (s->z_err == Z_OK) {
00427             uLong total_in = s->stream.total_in;
00428             uLong total_out = s->stream.total_out;
00429 
00430             inflateReset(&(s->stream));
00431             s->stream.total_in = total_in;
00432             s->stream.total_out = total_out;
00433             s->crc = crc32(0L, Z_NULL, 0);
00434         }
00435         }
00436     }
00437     if (s->z_err != Z_OK || s->z_eof) break;
00438     }
00439     s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
00440 
00441     return (int)(len - s->stream.avail_out);
00442 }

int ZEXPORT gzrewind ( gzFile  file  ) 

Definition at line 741 of file gzio.c.

00743 {
00744     gz_stream *s = (gz_stream*)file;
00745     
00746     if (s == NULL || s->mode != 'r') return -1;
00747 
00748     s->z_err = Z_OK;
00749     s->z_eof = 0;
00750     s->stream.avail_in = 0;
00751     s->stream.next_in = s->inbuf;
00752     s->crc = crc32(0L, Z_NULL, 0);
00753     
00754     if (s->startpos == 0) { /* not a compressed file */
00755     rewind(s->file);
00756     return 0;
00757     }
00758 
00759     (void) inflateReset(&s->stream);
00760     return fseek(s->file, s->startpos, SEEK_SET);
00761 }

z_off_t ZEXPORT gzseek ( gzFile  file,
z_off_t  offset,
int  whence 
)

Definition at line 660 of file gzio.c.

00664 {
00665     gz_stream *s = (gz_stream*)file;
00666 
00667     if (s == NULL || whence == SEEK_END ||
00668     s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
00669     return -1L;
00670     }
00671     
00672     if (s->mode == 'w') {
00673 #ifdef NO_DEFLATE
00674     return -1L;
00675 #else
00676     if (whence == SEEK_SET) {
00677         offset -= s->stream.total_in;
00678     }
00679     if (offset < 0) return -1L;
00680 
00681     /* At this point, offset is the number of zero bytes to write. */
00682     if (s->inbuf == Z_NULL) {
00683         s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
00684         zmemzero(s->inbuf, Z_BUFSIZE);
00685     }
00686     while (offset > 0)  {
00687         uInt size = Z_BUFSIZE;
00688         if (offset < Z_BUFSIZE) size = (uInt)offset;
00689 
00690         size = gzwrite(file, s->inbuf, size);
00691         if (size == 0) return -1L;
00692 
00693         offset -= size;
00694     }
00695     return (z_off_t)s->stream.total_in;
00696 #endif
00697     }
00698     /* Rest of function is for reading only */
00699 
00700     /* compute absolute position */
00701     if (whence == SEEK_CUR) {
00702     offset += s->stream.total_out;
00703     }
00704     if (offset < 0) return -1L;
00705 
00706     if (s->transparent) {
00707     /* map to fseek */
00708     s->stream.avail_in = 0;
00709     s->stream.next_in = s->inbuf;
00710         if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
00711 
00712     s->stream.total_in = s->stream.total_out = (uLong)offset;
00713     return offset;
00714     }
00715 
00716     /* For a negative seek, rewind and use positive seek */
00717     if ((uLong)offset >= s->stream.total_out) {
00718     offset -= s->stream.total_out;
00719     } else if (gzrewind(file) < 0) {
00720     return -1L;
00721     }
00722     /* offset is now the number of bytes to skip. */
00723 
00724     if (offset != 0 && s->outbuf == Z_NULL) {
00725     s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
00726     }
00727     while (offset > 0)  {
00728     int size = Z_BUFSIZE;
00729     if (offset < Z_BUFSIZE) size = (int)offset;
00730 
00731     size = gzread(file, s->outbuf, (uInt)size);
00732     if (size <= 0) return -1L;
00733     offset -= size;
00734     }
00735     return (z_off_t)s->stream.total_out;
00736 }

int ZEXPORT gzsetparams ( gzFile  file,
int  level,
int  strategy 
)

Definition at line 209 of file gzio.c.

00213 {
00214     gz_stream *s = (gz_stream*)file;
00215 
00216     if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
00217 
00218     /* Make room to allow flushing */
00219     if (s->stream.avail_out == 0) {
00220 
00221     s->stream.next_out = s->outbuf;
00222     if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
00223         s->z_err = Z_ERRNO;
00224     }
00225     s->stream.avail_out = Z_BUFSIZE;
00226     }
00227 
00228     return deflateParams (&(s->stream), level, strategy);
00229 }

z_off_t ZEXPORT gztell ( gzFile  file  ) 

Definition at line 768 of file gzio.c.

00770 {
00771     return gzseek(file, 0L, SEEK_CUR);
00772 }

int ZEXPORT gzwrite ( gzFile  file,
const voidp  buf,
unsigned  len 
)

Definition at line 486 of file gzio.c.

00490 {
00491     gz_stream *s = (gz_stream*)file;
00492 
00493     if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
00494 
00495     s->stream.next_in = (Bytef*)buf;
00496     s->stream.avail_in = len;
00497 
00498     while (s->stream.avail_in != 0) {
00499 
00500         if (s->stream.avail_out == 0) {
00501 
00502             s->stream.next_out = s->outbuf;
00503             if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
00504                 s->z_err = Z_ERRNO;
00505                 break;
00506             }
00507             s->stream.avail_out = Z_BUFSIZE;
00508         }
00509         s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
00510         if (s->z_err != Z_OK) break;
00511     }
00512     s->crc = crc32(s->crc, (const Bytef *)buf, len);
00513 
00514     return (int)(len - s->stream.avail_in);
00515 }

local void putLong OF ( (FILE *file, uLong x)   ) 
local int get_byte OF ( (gz_stream *s)   ) 
local int do_flush OF ( (gzFile file, int flush)   ) 
local gzFile gz_open OF ( (const char *path, const char *mode, intfd)   ) 
local void putLong ( FILE *  file,
uLong  x 
)

Definition at line 789 of file gzio.c.

00792 {
00793     int n;
00794     for (n = 0; n < 4; n++) {
00795         fputc((int)(x & 0xff), file);
00796         x >>= 8;
00797     }
00798 }


Variable Documentation

int gz_magic[2] = {0x1f, 0x8b} [static]

Definition at line 30 of file gzio.c.


Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1