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