-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiunz.c
273 lines (229 loc) · 6.6 KB
/
miniunz.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
# include <limits.h>
#include "utils.h"
#include "unzip.h"
#define CASESENSITIVITY (0)
#define WRITEBUFFERSIZE (8192)
#define MAXFILENAME (256)
static char possible_filename_in_zip[PATH_MAX];
/*!
* find_possible_filename_in_zip : Look whether the given archive contains a rom
* @param zipfilename Name of the archive file in zip format in which to search for a rom
* @return NULL in case of error or no rom found else a pointer to a statically allocated
* storage containing the filename found inside
*/
char *
find_possible_filename_in_zip (char *zipfilename)
{
uLong i;
unz_global_info gi;
unzFile uf;
int err;
#if !defined(FINAL_RELEASE)
fprintf (stderr,
"entering function to find a possible rom name in the archive %s\n",
zipfilename);
#endif
if (zipfilename == NULL)
return NULL;
uf = unzOpen (zipfilename);
if (uf == NULL)
{
strcat (zipfilename, ".zip");
uf = unzOpen (zipfilename);
}
if (uf == NULL)
return NULL;
#if !defined(FINAL_RELEASE)
fprintf (stderr, "Opened the archive");
#endif
err = unzGetGlobalInfo (uf, &gi);
if (err != UNZ_OK)
{
return NULL;
}
for (i = 0; i < gi.number_entry; i++)
{
char filename_inzip[256];
unz_file_info file_info;
#if !defined(FINAL_RELEASE)
fprintf (stderr, "Testing entry #%ld\n", i);
#endif
err = unzGetCurrentFileInfo (uf, &file_info, filename_inzip,
sizeof (filename_inzip), NULL, 0, NULL, 0);
if (err != UNZ_OK)
{
return NULL;
}
#if !defined(FINAL_RELEASE)
fprintf (stderr, "Filename for this entry is %s\n", filename_inzip);
#endif
if (strcasestr (filename_inzip, ".PCE"))
{
strncpy (possible_filename_in_zip, filename_inzip, PATH_MAX);
return possible_filename_in_zip;
}
if ((i + 1) < gi.number_entry)
{
err = unzGoToNextFile (uf);
if (err != UNZ_OK)
{
return NULL;
}
}
}
return NULL;
}
/*!
* do_extract_currentfile_in_memory : Extract the current pointed file in the zip archive into a buffer
* @param uf the archive file handle
* @param unzipped_size a pointer to the size of the unzipped data, it is filled by this function for caller
* @return uncompressed data as pointer, to be freed by caller or NULL in case of problem
*/
static char *
do_extract_currentfile_in_memory (unzFile uf, size_t * unzipped_size)
{
char filename_inzip[256];
char *filename_withoutpath;
char *p;
int err = UNZ_OK;
FILE *fout = NULL;
void *buf;
uInt size_buf;
unz_file_info file_info;
char *return_value = NULL;
err = unzGetCurrentFileInfo (uf, &file_info, filename_inzip,
sizeof (filename_inzip), NULL, 0, NULL, 0);
if (err != UNZ_OK)
{
Log ("error %d with zipfile in unzGetCurrentFileInfo\n", err);
return NULL;
}
size_buf = WRITEBUFFERSIZE;
buf = (void *) malloc (size_buf);
if (buf == NULL)
{
Log ("Error allocating memory\n");
return UNZ_INTERNALERROR;
}
p = filename_withoutpath = filename_inzip;
while ((*p) != '\0')
{
if (((*p) == '/') || ((*p) == '\\'))
filename_withoutpath = p + 1;
p++;
}
if ((*filename_withoutpath) != '\0')
{
const char *write_filename;
int skip = 0;
write_filename = filename_withoutpath;
err = unzOpenCurrentFile (uf);
if (err != UNZ_OK)
{
Log ("error %d with zipfile in unzOpenCurrentFile\n", err);
return NULL;
}
*unzipped_size = 0;
do
{
err = unzReadCurrentFile (uf, buf, size_buf);
if (err < 0)
{
Log ("error %d with zipfile in unzReadCurrentFile\n", err);
break;
}
if (err > 0)
{
*unzipped_size += err;
return_value = realloc (return_value, *unzipped_size);
if (return_value == NULL)
{
err = UNZ_ERRNO;
break;
}
memcpy (return_value + *unzipped_size - err, buf, err);
}
}
while (err > 0);
if (err != UNZ_OK)
{
Log ("Error %s with zipfile in uncompressing\n", strerror (errno));
}
err = unzCloseCurrentFile (uf);
if (err != UNZ_OK)
{
Log ("error %d with zipfile in unzCloseCurrentFile\n", err);
}
}
free (buf);
return return_value;
}
/*!
* do_extract_onefile_in_memory : Extract an archived file into a buffer
* @param uf Handle to the archive in ip format in which to read data
* @param filename Name of the file archived in the zip file the data of which we want
* @param unzipped_size Pointer to the size of the extracted data
* @return NULL in case of problem or a pointer to the archived file content. It is allocated
*/
static char *
do_extract_onefile_in_memory (unzFile uf, const char *filename,
size_t * unzipped_size)
{
if (unzLocateFile (uf, filename, CASESENSITIVITY) != UNZ_OK)
{
Log ("file %s not found in the zipfile\n", filename);
return NULL;
}
return do_extract_currentfile_in_memory (uf, unzipped_size);
}
/*!
* extract_file_in_memory : Extract an archived file into a buffer
* @param zipfilename Name of the archive in zip format in which to read data
* @param archivedfile Name of the file archived in the zip file the data of which we want
* @param unzipped_size Pointer to the size of the extracted data
* @return NULL in case of problem or a pointer to the archived file content. It is allocated
* dynamically and needs to be explicitely freed
*/
extern char *
extract_file_in_memory (char *zipfilename, char *archivedfile,
size_t * unzipped_size)
{
unzFile uf;
int err;
char *return_value;
if (zipfilename == NULL)
return NULL;
uf = unzOpen (zipfilename);
if (uf == NULL)
{
strcat (zipfilename, ".zip");
uf = unzOpen (zipfilename);
}
if (uf == NULL)
return NULL;
return_value =
do_extract_onefile_in_memory (uf, archivedfile, unzipped_size);
unzCloseCurrentFile (uf);
return return_value;
}