/
file_type_zip.cpp
241 lines (223 loc) · 5.93 KB
/
file_type_zip.cpp
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
/*
Portable ZX-Spectrum emulator.
Copyright (C) 2001-2012 SMT, Dexus, Alone Coder, deathsoft, djdron, scor
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 3 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "platform/platform.h"
#ifdef USE_ZIP
#include <unzip.h>
#include "platform/io.h"
#include "options_common.h"
#include "tools/stream_memory.h"
#include "file_type.h"
namespace xIo
{
eFileSelect* FileSelectZIP(const char* contain_path, const char* contain_name);
}
//namespace xIo
namespace xPlatform
{
static struct eFileTypeZIP : public eFileType
{
virtual bool Open(const void* data, size_t data_size) const;
virtual bool Open(const char* name) const;
virtual bool Contain(const char* name, char* contain_path, char* contain_name) const;
virtual const char* Type() const { return "zip"; }
virtual xIo::eFileSelect* FileSelect(const char* path) const
{
char contain_path[xIo::MAX_PATH_LEN];
char contain_name[xIo::MAX_PATH_LEN];
if(Contain(path, contain_path, contain_name))
return xIo::FileSelectZIP(contain_path, contain_name);
return NULL;
}
private:
bool Open(unzFile h, char* name = NULL) const;
enum eOpenResult { OR_FAIL, OR_OK, OR_OK_TRYNEXT };
eOpenResult OpenCurrent(unzFile h, char* name = NULL) const;
} ft_zip;
static voidpf ZOpen(voidpf opaque, const void* filename, int mode)
{
xIo::eStreamMemory* f = (xIo::eStreamMemory*)filename;
f->Open();
return f;
}
static uLong ZRead(voidpf opaque, voidpf stream, void* buf, uLong size)
{
xIo::eStreamMemory* f = (xIo::eStreamMemory*)stream;
return f->Read(buf, size);
}
static ZPOS64_T ZTell(voidpf opaque, voidpf stream)
{
xIo::eStreamMemory* f = (xIo::eStreamMemory*)stream;
return f->Pos();
}
static long ZSeek(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
{
xIo::eStreamMemory* f = (xIo::eStreamMemory*)stream;
xIo::eStreamMemory::eSeekMode sm = xIo::eStreamMemory::S_CUR;
switch(origin)
{
case ZLIB_FILEFUNC_SEEK_CUR: sm = xIo::eStreamMemory::S_CUR; break;
case ZLIB_FILEFUNC_SEEK_END: sm = xIo::eStreamMemory::S_END; break;
case ZLIB_FILEFUNC_SEEK_SET: sm = xIo::eStreamMemory::S_SET; break;
default: return -1;
}
return f->Seek(offset, sm);
}
static int ZClose(voidpf opaque, voidpf stream)
{
xIo::eStreamMemory* f = (xIo::eStreamMemory*)stream;
return f->Close();
}
bool eFileTypeZIP::Open(const char* name) const
{
char contain_path[xIo::MAX_PATH_LEN];
char contain_name[xIo::MAX_PATH_LEN];
if(Contain(name, contain_path, contain_name))
{
unzFile h = unzOpen64(contain_path);
if(!h)
return false;
bool ok = false;
if(unzLocateFile(h, contain_name, 0) == UNZ_OK)
{
ok = (OpenCurrent(h) != OR_FAIL);
}
unzClose(h);
return ok;
}
char opened_name[xIo::MAX_PATH_LEN];
bool ok = Open(unzOpen64(name), opened_name);
if(ok)
{
char full_name[xIo::MAX_PATH_LEN];
strcpy(full_name, name);
strcat(full_name, "/");
strcat(full_name, opened_name);
OpLastFile(full_name);
}
return ok;
}
bool eFileTypeZIP::Open(const void* data, size_t data_size) const
{
xIo::eStreamMemory mf(data, data_size);
zlib_filefunc64_def zfuncs;
zfuncs.zopen64_file = ZOpen;
zfuncs.zread_file = ZRead;
zfuncs.ztell64_file = ZTell;
zfuncs.zseek64_file = ZSeek;
zfuncs.zclose_file = ZClose;
return Open(unzOpen2_64(&mf, &zfuncs));
}
bool eFileTypeZIP::Open(unzFile h, char* name) const
{
if(!h)
return false;
bool ok = false;
bool stored_auto_play = OpAutoPlayImage();
eDrive stored_drive = OpDrive();
OpDrive(D_A);
if(unzGoToFirstFile(h) == UNZ_OK)
{
bool try_next = true;
do
{
switch(OpenCurrent(h, name))
{
case OR_FAIL:
try_next = true;
break;
case OR_OK:
ok = true;
try_next = false;
break;
case OR_OK_TRYNEXT:
ok = true;
try_next = true;
name = NULL; // do not return next files names
OpAutoPlayImage(false); // do not try to auto play next images
switch(OpDrive())
{
case D_A: OpDrive(D_B); break;
case D_B: OpDrive(D_C); break;
case D_C: OpDrive(D_D); break;
case D_D: OpDrive(D_A); break;
default: break;
}
break;
}
}
while(try_next && unzGoToNextFile(h) != UNZ_END_OF_LIST_OF_FILE);
}
unzClose(h);
OpAutoPlayImage(stored_auto_play);
OpDrive(stored_drive);
return ok;
}
eFileTypeZIP::eOpenResult eFileTypeZIP::OpenCurrent(unzFile h, char* name) const
{
unz_file_info fi;
char n[xIo::MAX_PATH_LEN];
if(unzGetCurrentFileInfo(h, &fi, n, xIo::MAX_PATH_LEN, NULL, 0, NULL, 0) != UNZ_OK)
return OR_FAIL;
const eFileType* t = eFileType::FindByName(n);
if(!t)
return OR_FAIL;
if(unzOpenCurrentFile(h) != UNZ_OK)
return OR_FAIL;
eOpenResult res = OR_FAIL;
byte* buf = new byte[fi.uncompressed_size];
if(unzReadCurrentFile(h, buf, fi.uncompressed_size) == int(fi.uncompressed_size))
{
if(t->Open(buf, fi.uncompressed_size))
{
if(name)
strcpy(name, n);
res = t->IsDisk() ? OR_OK_TRYNEXT : OR_OK;
}
}
SAFE_DELETE_ARRAY(buf);
unzCloseCurrentFile(h);
return res;
}
bool eFileTypeZIP::Contain(const char* name, char* contain_path, char* contain_name) const
{
char n[xIo::MAX_PATH_LEN];
strcpy(n, name);
int l = strlen(n);
for(int i = 0; i < l; ++i)
{
if(n[i] == '/' || n[i] == '\\')
{
char c = n[i];
n[i] = 0;
const eFileType* t = FindByName(n);
if(t == this) // zip
{
FILE* f = fopen(n, "rb");
if(f)
{
fclose(f);
strcpy(contain_path, n);
strcpy(contain_name, n + i + 1);
return true;
}
}
n[i] = c;
}
}
return false;
}
}
//namespace xPlatform
#endif//USE_ZIP