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