summaryrefslogtreecommitdiff
path: root/gzio.c
diff options
context:
space:
mode:
Diffstat (limited to 'gzio.c')
-rw-r--r--gzio.c107
1 files changed, 82 insertions, 25 deletions
diff --git a/gzio.c b/gzio.c
index 09e0a20..3337353 100644
--- a/gzio.c
+++ b/gzio.c
@@ -1,5 +1,5 @@
1/* gzio.c -- IO on .gz files 1/* gzio.c -- IO on .gz files
2 * Copyright (C) 1995-2002 Jean-loup Gailly. 2 * Copyright (C) 1995-2003 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 * 4 *
5 * Compile this file with -DNO_DEFLATE to avoid the compression code. 5 * Compile this file with -DNO_DEFLATE to avoid the compression code.
@@ -24,10 +24,15 @@ struct internal_state {int dummy;}; /* for buggy compilers */
24# define Z_PRINTF_BUFSIZE 4096 24# define Z_PRINTF_BUFSIZE 4096
25#endif 25#endif
26 26
27#ifndef STDC
28extern voidp malloc OF((uInt size));
29extern void free OF((voidpf ptr));
30#endif
31
27#define ALLOC(size) malloc(size) 32#define ALLOC(size) malloc(size)
28#define TRYFREE(p) {if (p) free(p);} 33#define TRYFREE(p) {if (p) free(p);}
29 34
30static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ 35static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
31 36
32/* gzip flag byte */ 37/* gzip flag byte */
33#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ 38#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
@@ -268,19 +273,33 @@ local void check_header(s)
268 uInt len; 273 uInt len;
269 int c; 274 int c;
270 275
271 /* Check the gzip magic header */ 276 /* Assure two bytes in the buffer so we can peek ahead -- handle case
272 for (len = 0; len < 2; len++) { 277 where first byte of header is at the end of the buffer after the last
273 c = get_byte(s); 278 gzip segment */
274 if (c != gz_magic[len]) { 279 len = s->stream.avail_in;
275 if (len != 0) s->stream.avail_in++, s->stream.next_in--; 280 if (len < 2) {
276 if (c != EOF) { 281 if (len) s->inbuf[0] = s->stream.next_in[0];
277 s->stream.avail_in++, s->stream.next_in--; 282 errno = 0;
278 s->transparent = 1; 283 len = fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
279 } 284 if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
280 s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END; 285 s->stream.avail_in += len;
281 return; 286 s->stream.next_in = s->inbuf;
282 } 287 if (s->stream.avail_in < 2) {
288 s->transparent = s->stream.avail_in;
289 return;
290 }
283 } 291 }
292
293 /* Peek ahead to check the gzip magic header */
294 if (s->stream.next_in[0] != gz_magic[0] ||
295 s->stream.next_in[1] != gz_magic[1]) {
296 s->transparent = 1;
297 return;
298 }
299 s->stream.avail_in -= 2;
300 s->stream.next_in += 2;
301
302 /* Check the rest of the gzip header */
284 method = get_byte(s); 303 method = get_byte(s);
285 flags = get_byte(s); 304 flags = get_byte(s);
286 if (method != Z_DEFLATED || (flags & RESERVED) != 0) { 305 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
@@ -485,7 +504,7 @@ char * ZEXPORT gzgets(file, buf, len)
485*/ 504*/
486int ZEXPORT gzwrite (file, buf, len) 505int ZEXPORT gzwrite (file, buf, len)
487 gzFile file; 506 gzFile file;
488 const voidp buf; 507 voidpc buf;
489 unsigned len; 508 unsigned len;
490{ 509{
491 gz_stream *s = (gz_stream*)file; 510 gz_stream *s = (gz_stream*)file;
@@ -529,14 +548,32 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
529 int len; 548 int len;
530 549
531 va_start(va, format); 550 va_start(va, format);
532#ifdef HAS_vsnprintf 551#ifdef NO_vsnprintf
533 (void)vsnprintf(buf, sizeof(buf), format, va); 552# ifdef HAS_vsprintf_void
534#else
535 (void)vsprintf(buf, format, va); 553 (void)vsprintf(buf, format, va);
536#endif
537 va_end(va); 554 va_end(va);
538 len = strlen(buf); /* some *sprintf don't return the nb of bytes written */ 555 len = strlen(buf); /* some *sprintf don't return the nb of bytes written */
539 if (len <= 0) return 0; 556 if (len <= 0) return 0;
557# else
558 len = vsprintf(buf, format, va);
559 va_end(va);
560 if (len <= 0 || len >= sizeof(buf))
561 return 0;
562# endif
563#else
564# ifdef HAS_vsnprintf_void
565 (void)vsnprintf(buf, sizeof(buf), format, va);
566 va_end(va);
567 len = strlen(buf);
568 if (len <= 0)
569 return 0;
570# else
571 len = vsnprintf(buf, sizeof(buf), format, va);
572 va_end(va);
573 if (len <= 0 || len >= sizeof(buf))
574 return 0;
575# endif
576#endif
540 577
541 return gzwrite(file, buf, (unsigned)len); 578 return gzwrite(file, buf, (unsigned)len);
542} 579}
@@ -552,15 +589,32 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
552 char buf[Z_PRINTF_BUFSIZE]; 589 char buf[Z_PRINTF_BUFSIZE];
553 int len; 590 int len;
554 591
555#ifdef HAS_snprintf 592#ifdef NO_snprintf
556 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8, 593# ifdef HAS_sprintf_void
557 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
558#else
559 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8, 594 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
560 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); 595 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
561#endif
562 len = strlen(buf); /* old sprintf doesn't return the nb of bytes written */ 596 len = strlen(buf); /* old sprintf doesn't return the nb of bytes written */
563 if (len <= 0) return 0; 597 if (len <= 0) return 0;
598# else
599 len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
600 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
601 if (len <= 0 || len >= sizeof(buf))
602 return 0;
603# endif
604#else
605# ifdef HAS_snprintf_void
606 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
607 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
608 len = strlen(buf);
609 if (len <= 0)
610 return 0;
611# else
612 len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
613 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
614 if (len <= 0 || len >= sizeof(buf))
615 return 0;
616# endif
617#endif
564 618
565 return gzwrite(file, buf, len); 619 return gzwrite(file, buf, len);
566} 620}
@@ -681,6 +735,7 @@ z_off_t ZEXPORT gzseek (file, offset, whence)
681 /* At this point, offset is the number of zero bytes to write. */ 735 /* At this point, offset is the number of zero bytes to write. */
682 if (s->inbuf == Z_NULL) { 736 if (s->inbuf == Z_NULL) {
683 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */ 737 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
738 if (s->inbuf == Z_NULL) return -1L;
684 zmemzero(s->inbuf, Z_BUFSIZE); 739 zmemzero(s->inbuf, Z_BUFSIZE);
685 } 740 }
686 while (offset > 0) { 741 while (offset > 0) {
@@ -723,6 +778,7 @@ z_off_t ZEXPORT gzseek (file, offset, whence)
723 778
724 if (offset != 0 && s->outbuf == Z_NULL) { 779 if (offset != 0 && s->outbuf == Z_NULL) {
725 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE); 780 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
781 if (s->outbuf == Z_NULL) return -1L;
726 } 782 }
727 while (offset > 0) { 783 while (offset > 0) {
728 int size = Z_BUFSIZE; 784 int size = Z_BUFSIZE;
@@ -862,12 +918,13 @@ const char* ZEXPORT gzerror (file, errnum)
862 *errnum = s->z_err; 918 *errnum = s->z_err;
863 if (*errnum == Z_OK) return (const char*)""; 919 if (*errnum == Z_OK) return (const char*)"";
864 920
865 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg); 921 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
866 922
867 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err); 923 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
868 924
869 TRYFREE(s->msg); 925 TRYFREE(s->msg);
870 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3); 926 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
927 if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
871 strcpy(s->msg, s->path); 928 strcpy(s->msg, s->path);
872 strcat(s->msg, ": "); 929 strcat(s->msg, ": ");
873 strcat(s->msg, m); 930 strcat(s->msg, m);