support for zipped ISOs
[picodrive.git] / unzip / unzip_stream.c
1 /* seekable zip */
2
3 #include "unzip.h"
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include <errno.h>
8
9 #ifdef __SYMBIAN32__
10 #include <ezlib.h>
11 #else
12 #include "zlib/zlib.h"
13 #endif
14
15
16 #define errormsg(str1,def,fname) printf("%s: " #def ": " str1 "\n", fname);
17
18
19 /* from gzio.c . Be careful with binary compatibility */
20 typedef struct gz_stream {
21     z_stream 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 */
37 } gz_stream;
38
39 #ifndef Z_BUFSIZE
40 #  ifdef MAXSEG_64K
41 #    define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
42 #  else
43 #    define Z_BUFSIZE 16384
44 #  endif
45 #endif
46 #ifndef Z_PRINTF_BUFSIZE
47 #  define Z_PRINTF_BUFSIZE 4096
48 #endif
49
50 #define ALLOC(size) malloc(size)
51
52 int    destroy      OF((gz_stream *s));
53
54
55 gzFile zip2gz(ZIP* zip, struct zipent* ent)
56 {
57     int err;
58     gz_stream *s;
59     const char *path;
60     int transparent = 0;
61     uInt len;
62
63     if (!zip || !ent)
64         return NULL;
65
66     /* zip stuff */
67     if (ent->compression_method == 0x0000)
68     {
69         /* file is not compressed, simply stored */
70
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);
74             return NULL;
75         }
76
77         transparent = 1;
78     }
79     else if (ent->compression_method == 0x0008)
80     {
81         /* file is compressed using "Deflate" method */
82         if (ent->version_needed_to_extract > 0x14) {
83             errormsg("Version too new", ERROR_UNSUPPORTED,zip->zip);
84             return NULL;
85         }
86
87         if (ent->os_needed_to_extract != 0x00) {
88             errormsg("OS not supported", ERROR_UNSUPPORTED,zip->zip);
89             return NULL;
90         }
91
92         if (ent->disk_number_start != zip->number_of_this_disk) {
93             errormsg("Cannot span disks", ERROR_UNSUPPORTED,zip->zip);
94             return NULL;
95         }
96
97     } else {
98         errormsg("Compression method unsupported", ERROR_UNSUPPORTED, zip->zip);
99         return NULL;
100     }
101
102     /* seek to compressed data */
103     if (seekcompresszip(zip,ent) != 0) {
104         return NULL;
105     }
106
107     path = zip->zip;
108
109     /* normal gzip init for read */
110     s = (gz_stream *)ALLOC(sizeof(gz_stream));
111     if (!s) return Z_NULL;
112
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;
119     s->file = NULL;
120     s->z_err = Z_OK;
121     s->z_eof = 0;
122     s->in = 0;
123     s->out = 0;
124     s->back = EOF;
125     s->crc = crc32(0L, Z_NULL, 0);
126     s->msg = NULL;
127     s->transparent = transparent;
128     s->mode = 'r';
129
130     s->path = (char*)ALLOC(strlen(path)+1);
131     if (s->path == NULL) {
132         return destroy(s), (gzFile)Z_NULL;
133     }
134     strcpy(s->path, path); /* do this early for debugging */
135
136     s->stream.next_in  = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
137
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.
144      */
145     if (err != Z_OK || s->inbuf == Z_NULL) {
146         return destroy(s), (gzFile)Z_NULL;
147     }
148     s->stream.avail_out = Z_BUFSIZE;
149
150     errno = 0;
151     s->file = zip->fp;
152     if (s->file == NULL) {
153         return destroy(s), (gzFile)Z_NULL;
154     }
155
156     /* check_header(s); */
157     errno = 0;
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;
164     }
165
166     s->start = ftell(s->file) - s->stream.avail_in;
167
168     return (gzFile)s;
169 }
170
171
172 int gzerror2(gzFile file)
173 {
174     gz_stream *s = (gz_stream*)file;
175
176     if (s == NULL)
177         return Z_STREAM_ERROR;
178
179     return s->z_err;
180 }
181
182