summaryrefslogtreecommitdiff
path: root/gzio.c
diff options
context:
space:
mode:
Diffstat (limited to 'gzio.c')
-rw-r--r--gzio.c40
1 files changed, 21 insertions, 19 deletions
diff --git a/gzio.c b/gzio.c
index d323fbc..1f0f9fc 100644
--- a/gzio.c
+++ b/gzio.c
@@ -13,8 +13,8 @@ struct internal_state {int dummy;}; /* for buggy compilers */
13 13
14#define Z_BUFSIZE 4096 14#define Z_BUFSIZE 4096
15 15
16#define ALLOC(size) zcalloc((voidp)0, 1, size) 16#define ALLOC(size) malloc(size)
17#define TRYFREE(p) {if (p) zcfree((voidp)0, p);} 17#define TRYFREE(p) {if (p) free(p);}
18 18
19#define GZ_MAGIC_1 0x1f 19#define GZ_MAGIC_1 0x1f
20#define GZ_MAGIC_2 0x8b 20#define GZ_MAGIC_2 0x8b
@@ -46,10 +46,10 @@ typedef struct gz_stream {
46} gz_stream; 46} gz_stream;
47 47
48 48
49local int destroy __P((gz_stream *s)); 49local int destroy OF((gz_stream *s));
50local gzFile gz_open __P((char *path, char *mode, int fd)); 50local gzFile gz_open OF((char *path, char *mode, int fd));
51local void putLong __P((FILE *file, uLong x)); 51local void putLong OF((FILE *file, uLong x));
52local uLong getLong __P((Byte *buf)); 52local uLong getLong OF((Bytef *buf));
53 53
54 /* =========================================================================== 54 /* ===========================================================================
55 * Cleanup then free the given gz_stream. Return a zlib error code. 55 * Cleanup then free the given gz_stream. Return a zlib error code.
@@ -77,7 +77,7 @@ local int destroy (s)
77 err = Z_ERRNO; 77 err = Z_ERRNO;
78 } 78 }
79 if (s->z_err < 0) err = s->z_err; 79 if (s->z_err < 0) err = s->z_err;
80 zcfree((voidp)0, s); 80 TRYFREE(s);
81 return err; 81 return err;
82} 82}
83 83
@@ -96,6 +96,7 @@ local gzFile gz_open (path, mode, fd)
96 int fd; 96 int fd;
97{ 97{
98 int err; 98 int err;
99 int level = Z_DEFAULT_COMPRESSION; /* compression level */
99 char *p = mode; 100 char *p = mode;
100 gz_stream *s = (gz_stream *)ALLOC(sizeof(gz_stream)); 101 gz_stream *s = (gz_stream *)ALLOC(sizeof(gz_stream));
101 102
@@ -123,22 +124,23 @@ local gzFile gz_open (path, mode, fd)
123 do { 124 do {
124 if (*p == 'r') s->mode = 'r'; 125 if (*p == 'r') s->mode = 'r';
125 if (*p == 'w') s->mode = 'w'; 126 if (*p == 'w') s->mode = 'w';
127 if (*p >= '1' && *p <= '9') level = *p - '0';
126 } while (*p++); 128 } while (*p++);
127 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL; 129 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
128 130
129 if (s->mode == 'w') { 131 if (s->mode == 'w') {
130 err = deflateInit2(&(s->stream), Z_DEFAULT_COMPRESSION, 132 err = deflateInit2(&(s->stream), level,
131 DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0); 133 DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0);
132 /* windowBits is passed < 0 to suppress zlib header */ 134 /* windowBits is passed < 0 to suppress zlib header */
133 135
134 s->stream.next_out = s->outbuf = ALLOC(Z_BUFSIZE); 136 s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
135 137
136 if (err != Z_OK || s->outbuf == Z_NULL) { 138 if (err != Z_OK || s->outbuf == Z_NULL) {
137 return destroy(s), (gzFile)Z_NULL; 139 return destroy(s), (gzFile)Z_NULL;
138 } 140 }
139 } else { 141 } else {
140 err = inflateInit2(&(s->stream), -MAX_WBITS); 142 err = inflateInit2(&(s->stream), -MAX_WBITS);
141 s->stream.next_in = s->inbuf = ALLOC(Z_BUFSIZE); 143 s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
142 144
143 if (err != Z_OK || s->inbuf == Z_NULL) { 145 if (err != Z_OK || s->inbuf == Z_NULL) {
144 return destroy(s), (gzFile)Z_NULL; 146 return destroy(s), (gzFile)Z_NULL;
@@ -232,7 +234,7 @@ gzFile gzdopen (fd, mode)
232*/ 234*/
233int gzread (file, buf, len) 235int gzread (file, buf, len)
234 gzFile file; 236 gzFile file;
235 voidp buf; 237 voidnp buf;
236 unsigned len; 238 unsigned len;
237{ 239{
238 gz_stream *s = (gz_stream*)file; 240 gz_stream *s = (gz_stream*)file;
@@ -240,7 +242,7 @@ int gzread (file, buf, len)
240 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR; 242 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
241 243
242 if (s->transparent) { 244 if (s->transparent) {
243 unsigned n = 0; 245 int n = 0;
244 Byte *b = (Byte*)buf; 246 Byte *b = (Byte*)buf;
245 /* Copy the first two (non-magic) bytes if not done already */ 247 /* Copy the first two (non-magic) bytes if not done already */
246 while (s->stream.avail_in > 0 && len > 0) { 248 while (s->stream.avail_in > 0 && len > 0) {
@@ -281,7 +283,7 @@ int gzread (file, buf, len)
281 } 283 }
282 len -= s->stream.avail_out; 284 len -= s->stream.avail_out;
283 s->crc = crc32(s->crc, buf, len); 285 s->crc = crc32(s->crc, buf, len);
284 return len; 286 return (int)len;
285} 287}
286 288
287/* =========================================================================== 289/* ===========================================================================
@@ -290,7 +292,7 @@ int gzread (file, buf, len)
290*/ 292*/
291int gzwrite (file, buf, len) 293int gzwrite (file, buf, len)
292 gzFile file; 294 gzFile file;
293 voidp buf; 295 voidnp buf;
294 unsigned len; 296 unsigned len;
295{ 297{
296 gz_stream *s = (gz_stream*)file; 298 gz_stream *s = (gz_stream*)file;
@@ -312,12 +314,11 @@ int gzwrite (file, buf, len)
312 s->stream.avail_out = Z_BUFSIZE; 314 s->stream.avail_out = Z_BUFSIZE;
313 } 315 }
314 s->z_err = deflate(&(s->stream), Z_NO_FLUSH); 316 s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
315
316 if (s->z_err != Z_OK) break; 317 if (s->z_err != Z_OK) break;
317 } 318 }
318 s->crc = crc32(s->crc, buf, len); 319 s->crc = crc32(s->crc, buf, len);
319 320
320 return len - s->stream.avail_in; 321 return (int)(len - s->stream.avail_in);
321} 322}
322 323
323/* =========================================================================== 324/* ===========================================================================
@@ -359,6 +360,7 @@ int gzflush (file, flush)
359 360
360 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break; 361 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
361 } 362 }
363 fflush(s->file);
362 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; 364 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
363} 365}
364 366
@@ -380,10 +382,10 @@ local void putLong (file, x)
380 Reads a long in LSB order from the given buffer 382 Reads a long in LSB order from the given buffer
381*/ 383*/
382local uLong getLong (buf) 384local uLong getLong (buf)
383 Byte *buf; 385 Bytef *buf;
384{ 386{
385 uLong x = 0; 387 uLong x = 0;
386 Byte *p = buf+4; 388 Bytef *p = buf+4;
387 389
388 do { 390 do {
389 x <<= 8; 391 x <<= 8;
@@ -417,7 +419,7 @@ int gzclose (file)
417 /* slide CRC and original size if they are at the end of inbuf */ 419 /* slide CRC and original size if they are at the end of inbuf */
418 if ((n = s->stream.avail_in) < 8 && !s->z_eof) { 420 if ((n = s->stream.avail_in) < 8 && !s->z_eof) {
419 Byte *p = s->inbuf; 421 Byte *p = s->inbuf;
420 Byte *q = s->stream.next_in; 422 Bytef *q = s->stream.next_in;
421 while (n--) { *p++ = *q++; }; 423 while (n--) { *p++ = *q++; };
422 424
423 n = s->stream.avail_in; 425 n = s->stream.avail_in;