Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/io/file_access_zip.cpp
9973 views
1
/**************************************************************************/
2
/* file_access_zip.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#ifdef MINIZIP_ENABLED
32
33
#include "file_access_zip.h"
34
35
#include "core/io/file_access.h"
36
37
extern "C" {
38
39
struct ZipData {
40
Ref<FileAccess> f;
41
};
42
43
static void *godot_open(voidpf opaque, const char *p_fname, int mode) {
44
if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
45
return nullptr;
46
}
47
48
Ref<FileAccess> f = FileAccess::open(String::utf8(p_fname), FileAccess::READ);
49
ERR_FAIL_COND_V(f.is_null(), nullptr);
50
51
ZipData *zd = memnew(ZipData);
52
zd->f = f;
53
return zd;
54
}
55
56
static uLong godot_read(voidpf opaque, voidpf stream, void *buf, uLong size) {
57
ZipData *zd = (ZipData *)stream;
58
zd->f->get_buffer((uint8_t *)buf, size);
59
return size;
60
}
61
62
static uLong godot_write(voidpf opaque, voidpf stream, const void *buf, uLong size) {
63
return 0;
64
}
65
66
static long godot_tell(voidpf opaque, voidpf stream) {
67
ZipData *zd = (ZipData *)stream;
68
return zd->f->get_position();
69
}
70
71
static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
72
ZipData *zd = (ZipData *)stream;
73
74
uint64_t pos = offset;
75
switch (origin) {
76
case ZLIB_FILEFUNC_SEEK_CUR:
77
pos = zd->f->get_position() + offset;
78
break;
79
case ZLIB_FILEFUNC_SEEK_END:
80
pos = zd->f->get_length() + offset;
81
break;
82
default:
83
break;
84
}
85
86
zd->f->seek(pos);
87
return 0;
88
}
89
90
static int godot_close(voidpf opaque, voidpf stream) {
91
ZipData *zd = (ZipData *)stream;
92
memdelete(zd);
93
return 0;
94
}
95
96
static int godot_testerror(voidpf opaque, voidpf stream) {
97
ZipData *zd = (ZipData *)stream;
98
return zd->f->get_error() != OK ? 1 : 0;
99
}
100
101
static voidpf godot_alloc(voidpf opaque, uInt items, uInt size) {
102
return memalloc((size_t)items * size);
103
}
104
105
static void godot_free(voidpf opaque, voidpf address) {
106
memfree(address);
107
}
108
} // extern "C"
109
110
void ZipArchive::close_handle(unzFile p_file) const {
111
ERR_FAIL_NULL_MSG(p_file, "Cannot close a file if none is open.");
112
unzCloseCurrentFile(p_file);
113
unzClose(p_file);
114
}
115
116
unzFile ZipArchive::get_file_handle(const String &p_file) const {
117
ERR_FAIL_COND_V_MSG(!file_exists(p_file), nullptr, vformat("File '%s' doesn't exist.", p_file));
118
File file = files[p_file];
119
120
zlib_filefunc_def io;
121
memset(&io, 0, sizeof(io));
122
123
io.opaque = nullptr;
124
io.zopen_file = godot_open;
125
io.zread_file = godot_read;
126
io.zwrite_file = godot_write;
127
128
io.ztell_file = godot_tell;
129
io.zseek_file = godot_seek;
130
io.zclose_file = godot_close;
131
io.zerror_file = godot_testerror;
132
133
io.alloc_mem = godot_alloc;
134
io.free_mem = godot_free;
135
136
unzFile pkg = unzOpen2(packages[file.package].filename.utf8().get_data(), &io);
137
ERR_FAIL_NULL_V_MSG(pkg, nullptr, vformat("Cannot open file '%s'.", packages[file.package].filename));
138
int unz_err = unzGoToFilePos(pkg, &file.file_pos);
139
if (unz_err != UNZ_OK || unzOpenCurrentFile(pkg) != UNZ_OK) {
140
unzClose(pkg);
141
ERR_FAIL_V(nullptr);
142
}
143
144
return pkg;
145
}
146
147
bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset = 0) {
148
// load with offset feature only supported for PCK files
149
ERR_FAIL_COND_V_MSG(p_offset != 0, false, "Invalid PCK data. Note that loading files with a non-zero offset isn't supported with ZIP archives.");
150
151
if (p_path.get_extension().nocasecmp_to("zip") != 0 && p_path.get_extension().nocasecmp_to("pcz") != 0) {
152
return false;
153
}
154
155
zlib_filefunc_def io;
156
memset(&io, 0, sizeof(io));
157
158
io.opaque = nullptr;
159
io.zopen_file = godot_open;
160
io.zread_file = godot_read;
161
io.zwrite_file = godot_write;
162
163
io.ztell_file = godot_tell;
164
io.zseek_file = godot_seek;
165
io.zclose_file = godot_close;
166
io.zerror_file = godot_testerror;
167
168
unzFile zfile = unzOpen2(p_path.utf8().get_data(), &io);
169
ERR_FAIL_NULL_V(zfile, false);
170
171
unz_global_info64 gi;
172
int err = unzGetGlobalInfo64(zfile, &gi);
173
ERR_FAIL_COND_V(err != UNZ_OK, false);
174
175
Package pkg;
176
pkg.filename = p_path;
177
packages.push_back(pkg);
178
int pkg_num = packages.size() - 1;
179
180
for (uint64_t i = 0; i < gi.number_entry; i++) {
181
char filename_inzip[256];
182
183
unz_file_info64 file_info;
184
err = unzGetCurrentFileInfo64(zfile, &file_info, filename_inzip, sizeof(filename_inzip), nullptr, 0, nullptr, 0);
185
ERR_CONTINUE(err != UNZ_OK);
186
187
File f;
188
f.package = pkg_num;
189
unzGetFilePos(zfile, &f.file_pos);
190
191
String fname = String("res://") + String::utf8(filename_inzip);
192
files[fname] = f;
193
194
uint8_t md5[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
195
PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files, false);
196
//printf("packed data add path %s, %s\n", p_name.utf8().get_data(), fname.utf8().get_data());
197
198
if ((i + 1) < gi.number_entry) {
199
unzGoToNextFile(zfile);
200
}
201
}
202
203
unzClose(zfile);
204
205
return true;
206
}
207
208
bool ZipArchive::file_exists(const String &p_name) const {
209
return files.has(p_name);
210
}
211
212
Ref<FileAccess> ZipArchive::get_file(const String &p_path, PackedData::PackedFile *p_file) {
213
return memnew(FileAccessZip(p_path, *p_file));
214
}
215
216
ZipArchive *ZipArchive::get_singleton() {
217
if (instance == nullptr) {
218
instance = memnew(ZipArchive);
219
}
220
221
return instance;
222
}
223
224
ZipArchive::ZipArchive() {
225
instance = this;
226
}
227
228
ZipArchive::~ZipArchive() {
229
packages.clear();
230
}
231
232
Error FileAccessZip::open_internal(const String &p_path, int p_mode_flags) {
233
_close();
234
235
ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, FAILED);
236
ZipArchive *arch = ZipArchive::get_singleton();
237
ERR_FAIL_NULL_V(arch, FAILED);
238
zfile = arch->get_file_handle(p_path);
239
ERR_FAIL_NULL_V(zfile, FAILED);
240
241
int err = unzGetCurrentFileInfo64(zfile, &file_info, nullptr, 0, nullptr, 0, nullptr, 0);
242
ERR_FAIL_COND_V(err != UNZ_OK, FAILED);
243
244
return OK;
245
}
246
247
void FileAccessZip::_close() {
248
if (!zfile) {
249
return;
250
}
251
252
ZipArchive *arch = ZipArchive::get_singleton();
253
ERR_FAIL_NULL(arch);
254
arch->close_handle(zfile);
255
zfile = nullptr;
256
}
257
258
bool FileAccessZip::is_open() const {
259
return zfile != nullptr;
260
}
261
262
void FileAccessZip::seek(uint64_t p_position) {
263
ERR_FAIL_NULL(zfile);
264
265
unzSeekCurrentFile(zfile, p_position);
266
}
267
268
void FileAccessZip::seek_end(int64_t p_position) {
269
ERR_FAIL_NULL(zfile);
270
unzSeekCurrentFile(zfile, get_length() + p_position);
271
}
272
273
uint64_t FileAccessZip::get_position() const {
274
ERR_FAIL_NULL_V(zfile, 0);
275
return unztell64(zfile);
276
}
277
278
uint64_t FileAccessZip::get_length() const {
279
ERR_FAIL_NULL_V(zfile, 0);
280
return file_info.uncompressed_size;
281
}
282
283
bool FileAccessZip::eof_reached() const {
284
ERR_FAIL_NULL_V(zfile, true);
285
286
return at_eof;
287
}
288
289
uint64_t FileAccessZip::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
290
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
291
ERR_FAIL_NULL_V(zfile, -1);
292
293
at_eof = unzeof(zfile);
294
if (at_eof) {
295
return 0;
296
}
297
int64_t read = unzReadCurrentFile(zfile, p_dst, p_length);
298
ERR_FAIL_COND_V(read < 0, read);
299
if ((uint64_t)read < p_length) {
300
at_eof = true;
301
}
302
return read;
303
}
304
305
Error FileAccessZip::get_error() const {
306
if (!zfile) {
307
return ERR_UNCONFIGURED;
308
}
309
if (eof_reached()) {
310
return ERR_FILE_EOF;
311
}
312
313
return OK;
314
}
315
316
void FileAccessZip::flush() {
317
ERR_FAIL();
318
}
319
320
bool FileAccessZip::store_buffer(const uint8_t *p_src, uint64_t p_length) {
321
ERR_FAIL_V(false);
322
}
323
324
bool FileAccessZip::file_exists(const String &p_name) {
325
return false;
326
}
327
328
void FileAccessZip::close() {
329
_close();
330
}
331
332
FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) {
333
open_internal(p_path, FileAccess::READ);
334
}
335
336
FileAccessZip::~FileAccessZip() {
337
_close();
338
}
339
340
#endif // MINIZIP_ENABLED
341
342