adjust famec flags after O2 -> O3 change
[picodrive.git] / unzip / unzip_stream.c
CommitLineData
83bd0b76 1/* seekable zip */
2
3#include "unzip.h"
4
5#include <stdlib.h>
6#include <string.h>
7#include <errno.h>
8
83bd0b76 9#include "zlib/zlib.h"
83bd0b76 10
11
12#define errormsg(str1,def,fname) printf("%s: " #def ": " str1 "\n", fname);
13
14
15/* from gzio.c . Be careful with binary compatibility */
16typedef struct gz_stream {
17 z_stream stream;
18 int z_err; /* error code for last stream operation */
19 int z_eof; /* set if end of input file */
20 FILE *file; /* .gz file */
21 Byte *inbuf; /* input buffer */
22 Byte *outbuf; /* output buffer */
23 uLong crc; /* crc32 of uncompressed data */
24 char *msg; /* error message */
25 char *path; /* path name for debugging only */
26 int transparent; /* 1 if input file is not a .gz file */
27 char mode; /* 'w' or 'r' */
28 z_off_t start; /* start of compressed data in file (header skipped) */
29 z_off_t in; /* bytes into deflate or inflate */
30 z_off_t out; /* bytes out of deflate or inflate */
31 int back; /* one character push-back */
32 int last; /* true if push-back is last character */
33} gz_stream;
34
35#ifndef Z_BUFSIZE
36# ifdef MAXSEG_64K
37# define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
38# else
39# define Z_BUFSIZE 16384
40# endif
41#endif
42#ifndef Z_PRINTF_BUFSIZE
43# define Z_PRINTF_BUFSIZE 4096
44#endif
45
46#define ALLOC(size) malloc(size)
47
48int destroy OF((gz_stream *s));
49
50
51gzFile zip2gz(ZIP* zip, struct zipent* ent)
52{
53 int err;
54 gz_stream *s;
55 const char *path;
56 int transparent = 0;
57 uInt len;
58
59 if (!zip || !ent)
60 return NULL;
61
62 /* zip stuff */
63 if (ent->compression_method == 0x0000)
64 {
65 /* file is not compressed, simply stored */
66
67 /* check if size are equal */
68 if (ent->compressed_size != ent->uncompressed_size) {
69 errormsg("Wrong uncompressed size in store compression", ERROR_CORRUPT,zip->zip);
70 return NULL;
71 }
72
73 transparent = 1;
74 }
75 else if (ent->compression_method == 0x0008)
76 {
77 /* file is compressed using "Deflate" method */
78 if (ent->version_needed_to_extract > 0x14) {
79 errormsg("Version too new", ERROR_UNSUPPORTED,zip->zip);
80 return NULL;
81 }
82
83 if (ent->os_needed_to_extract != 0x00) {
84 errormsg("OS not supported", ERROR_UNSUPPORTED,zip->zip);
85 return NULL;
86 }
87
88 if (ent->disk_number_start != zip->number_of_this_disk) {
89 errormsg("Cannot span disks", ERROR_UNSUPPORTED,zip->zip);
90 return NULL;
91 }
92
93 } else {
94 errormsg("Compression method unsupported", ERROR_UNSUPPORTED, zip->zip);
95 return NULL;
96 }
97
98 /* seek to compressed data */
99 if (seekcompresszip(zip,ent) != 0) {
100 return NULL;
101 }
102
103 path = zip->zip;
104
105 /* normal gzip init for read */
106 s = (gz_stream *)ALLOC(sizeof(gz_stream));
107 if (!s) return Z_NULL;
108
109 s->stream.zalloc = (alloc_func)0;
110 s->stream.zfree = (free_func)0;
111 s->stream.opaque = (voidpf)0;
112 s->stream.next_in = s->inbuf = Z_NULL;
113 s->stream.next_out = s->outbuf = Z_NULL;
114 s->stream.avail_in = s->stream.avail_out = 0;
115 s->file = NULL;
116 s->z_err = Z_OK;
117 s->z_eof = 0;
118 s->in = 0;
119 s->out = 0;
120 s->back = EOF;
121 s->crc = crc32(0L, Z_NULL, 0);
122 s->msg = NULL;
123 s->transparent = transparent;
124 s->mode = 'r';
125
126 s->path = (char*)ALLOC(strlen(path)+1);
127 if (s->path == NULL) {
128 return destroy(s), (gzFile)Z_NULL;
129 }
130 strcpy(s->path, path); /* do this early for debugging */
131
132 s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
133
134 err = inflateInit2(&(s->stream), -MAX_WBITS);
135 /* windowBits is passed < 0 to tell that there is no zlib header.
136 * Note that in this case inflate *requires* an extra "dummy" byte
137 * after the compressed stream in order to complete decompression and
138 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
139 * present after the compressed stream.
140 */
141 if (err != Z_OK || s->inbuf == Z_NULL) {
142 return destroy(s), (gzFile)Z_NULL;
143 }
144 s->stream.avail_out = Z_BUFSIZE;
145
146 errno = 0;
147 s->file = zip->fp;
148 if (s->file == NULL) {
149 return destroy(s), (gzFile)Z_NULL;
150 }
151
152 /* check_header(s); */
153 errno = 0;
154 len = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
155 if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
156 s->stream.avail_in += len;
157 s->stream.next_in = s->inbuf;
158 if (s->stream.avail_in < 2) {
159 return destroy(s), (gzFile)Z_NULL;
160 }
161
162 s->start = ftell(s->file) - s->stream.avail_in;
163
164 return (gzFile)s;
165}
166
167
168int gzerror2(gzFile file)
169{
170 gz_stream *s = (gz_stream*)file;
171
172 if (s == NULL)
173 return Z_STREAM_ERROR;
174
175 return s->z_err;
176}
177
178