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