forked from snes9xgit/snes9x
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathloadzip.cpp
More file actions
172 lines (142 loc) · 3.67 KB
/
Copy pathloadzip.cpp
File metadata and controls
172 lines (142 loc) · 3.67 KB
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
/*****************************************************************************\
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
This file is licensed under the Snes9x License.
For further information, consult the LICENSE file in the root directory.
\*****************************************************************************/
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include "snes9x.h"
#include "zipfile.h"
#include "memmap.h"
/* Selection rules, unchanged from the minizip implementation: prefer a file
named program.rom or one whose extension is ".1", otherwise take the largest
entry that fits in a cart. A .msu1 pack only ever loads program.rom, since
its other entries are MSU1 companion data rather than the ROM. */
static int find_rom_entry (const struct zip_archive *ar, uint32 *out_size)
{
int best = -1;
uint32 best_size = 0;
int i;
for (i = 0; i < ar->count; i++)
{
const char *name = ar->entries[i].name;
uint32 size = ar->entries[i].uncomp_size;
int len = (int) strlen(name);
if (size > CMemory::MAX_ROM_SIZE + 512)
continue;
/* Skip entries this reader cannot decode rather than selecting one
and failing the load. */
if (ar->entries[i].method != ZIP_METHOD_STORE &&
ar->entries[i].method != ZIP_METHOD_DEFLATE)
continue;
if (size > best_size)
{
best = i;
best_size = size;
}
if (len > 2 && name[len - 2] == '.' && name[len - 1] == '1')
{
best = i;
best_size = size;
break;
}
if (strncasecmp(name, "program.rom", 11) == 0)
{
best = i;
best_size = size;
break;
}
}
*out_size = best_size;
return (best);
}
bool8 LoadZip (const char *zipname, uint32 *TotalFileSize, uint8 *buffer)
{
struct zip_archive ar;
char filename[ZIP_MAX_NAME];
uint32 filesize = 0;
int idx;
int len;
*TotalFileSize = 0;
if (!zip_archive_open(&ar, zipname))
return (FALSE);
idx = find_rom_entry(&ar, &filesize);
if (idx < 0 || filesize == 0)
{
zip_archive_close(&ar);
return (FALSE);
}
strcpy(filename, ar.entries[idx].name);
len = (int) strlen(zipname);
if (len > 5 && strcasecmp(zipname + len - 5, ".msu1") == 0 &&
strcasecmp(filename, "program.rom") != 0)
{
zip_archive_close(&ar);
return (FALSE);
}
// find extension
char tmp[2] = { 0, 0 };
char *ext = strrchr(filename, '.');
if (ext)
ext++;
else
ext = tmp;
uint8 *ptr = buffer;
bool8 more = FALSE;
do
{
uint32 FileSize = ar.entries[idx].uncomp_size;
uint32 got;
assert(FileSize <= CMemory::MAX_ROM_SIZE + 512);
{
struct zip_file zf;
if (!zip_file_open(&zf, &ar, idx))
{
zip_archive_close(&ar);
return (FALSE);
}
got = zip_file_read(&zf, 0, ptr, FileSize);
zip_file_close(&zf);
}
if (got != FileSize)
{
zip_archive_close(&ar);
return (FALSE);
}
FileSize = Memory.HeaderRemove(FileSize, ptr);
ptr += FileSize;
*TotalFileSize += FileSize;
if (ptr - Memory.ROM < CMemory::MAX_ROM_SIZE + 512 && (isdigit(ext[0]) && ext[1] == 0 && ext[0] < '9'))
{
more = TRUE;
ext[0]++;
}
else
if (ptr - Memory.ROM < CMemory::MAX_ROM_SIZE + 512)
{
int nlen;
if (ext == tmp)
nlen = (int) strlen(filename);
else
nlen = (int) (ext - filename - 1);
if ((nlen == 7 || nlen == 8) && strncasecmp(filename, "sf", 2) == 0 &&
isdigit(filename[2]) && isdigit(filename[3]) && isdigit(filename[4]) &&
isdigit(filename[5]) && isalpha(filename[nlen - 1]))
{
more = TRUE;
filename[nlen - 1]++;
}
}
else
more = FALSE;
if (more)
{
idx = zip_find_name(&ar, filename, 0);
if (idx < 0)
break;
}
} while (more);
zip_archive_close(&ar);
return (TRUE);
}