12 #include "zlib/zlib.h"
16 #define errormsg(str1,def,fname) printf("%s: " #def ": " str1 "\n", fname);
19 /* from gzio.c . Be careful with binary compatibility */
20 typedef struct gz_stream {
22 int z_err; /* error code for last stream operation */
23 int z_eof; /* set if end of input file */
24 FILE *file; /* .gz file */
25 Byte *inbuf; /* input buffer */
26 Byte *outbuf; /* output buffer */
27 uLong crc; /* crc32 of uncompressed data */
28 char *msg; /* error message */
29 char *path; /* path name for debugging only */
30 int transparent; /* 1 if input file is not a .gz file */
31 char mode; /* 'w' or 'r' */
32 z_off_t start; /* start of compressed data in file (header skipped) */
33 z_off_t in; /* bytes into deflate or inflate */
34 z_off_t out; /* bytes out of deflate or inflate */
35 int back; /* one character push-back */
36 int last; /* true if push-back is last character */
41 # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
43 # define Z_BUFSIZE 16384
46 #ifndef Z_PRINTF_BUFSIZE
47 # define Z_PRINTF_BUFSIZE 4096
50 #define ALLOC(size) malloc(size)
52 int destroy OF((gz_stream *s));
55 gzFile zip2gz(ZIP* zip, struct zipent* ent)
67 if (ent->compression_method == 0x0000)
69 /* file is not compressed, simply stored */
71 /* check if size are equal */
72 if (ent->compressed_size != ent->uncompressed_size) {
73 errormsg("Wrong uncompressed size in store compression", ERROR_CORRUPT,zip->zip);
79 else if (ent->compression_method == 0x0008)
81 /* file is compressed using "Deflate" method */
82 if (ent->version_needed_to_extract > 0x14) {
83 errormsg("Version too new", ERROR_UNSUPPORTED,zip->zip);
87 if (ent->os_needed_to_extract != 0x00) {
88 errormsg("OS not supported", ERROR_UNSUPPORTED,zip->zip);
92 if (ent->disk_number_start != zip->number_of_this_disk) {
93 errormsg("Cannot span disks", ERROR_UNSUPPORTED,zip->zip);
98 errormsg("Compression method unsupported", ERROR_UNSUPPORTED, zip->zip);
102 /* seek to compressed data */
103 if (seekcompresszip(zip,ent) != 0) {
109 /* normal gzip init for read */
110 s = (gz_stream *)ALLOC(sizeof(gz_stream));
111 if (!s) return Z_NULL;
113 s->stream.zalloc = (alloc_func)0;
114 s->stream.zfree = (free_func)0;
115 s->stream.opaque = (voidpf)0;
116 s->stream.next_in = s->inbuf = Z_NULL;
117 s->stream.next_out = s->outbuf = Z_NULL;
118 s->stream.avail_in = s->stream.avail_out = 0;
125 s->crc = crc32(0L, Z_NULL, 0);
127 s->transparent = transparent;
130 s->path = (char*)ALLOC(strlen(path)+1);
131 if (s->path == NULL) {
132 return destroy(s), (gzFile)Z_NULL;
134 strcpy(s->path, path); /* do this early for debugging */
136 s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
138 err = inflateInit2(&(s->stream), -MAX_WBITS);
139 /* windowBits is passed < 0 to tell that there is no zlib header.
140 * Note that in this case inflate *requires* an extra "dummy" byte
141 * after the compressed stream in order to complete decompression and
142 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
143 * present after the compressed stream.
145 if (err != Z_OK || s->inbuf == Z_NULL) {
146 return destroy(s), (gzFile)Z_NULL;
148 s->stream.avail_out = Z_BUFSIZE;
152 if (s->file == NULL) {
153 return destroy(s), (gzFile)Z_NULL;
156 /* check_header(s); */
158 len = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
159 if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
160 s->stream.avail_in += len;
161 s->stream.next_in = s->inbuf;
162 if (s->stream.avail_in < 2) {
163 return destroy(s), (gzFile)Z_NULL;
166 s->start = ftell(s->file) - s->stream.avail_in;
172 int gzerror2(gzFile file)
174 gz_stream *s = (gz_stream*)file;
177 return Z_STREAM_ERROR;