Debian Codegraph @

modules/zip/zip_reader.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* zip_reader.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 #include "zip_reader.h"
32
33 #include "core/error/error_macros.h"
34 #include "core/io/zip_io.h"
35
36 Error ZIPReader::open(const String &p_path) {
37 if (fa.is_valid()) {
38 close();
39 }
40
41 zlib_filefunc_def io = zipio_create_io(&fa);
42 uzf = unzOpen2(p_path.utf8().get_data(), &io);
43 return uzf != nullptr ? OK : FAILED;
44 }
45
46 Error ZIPReader::close() {
47 ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPReader cannot be closed because it is not open.");
48
49 Error err = unzClose(uzf) == UNZ_OK ? OK : FAILED;
50 if (err == OK) {
51 DEV_ASSERT(fa.is_null());
52 uzf = nullptr;
53 }
54
55 return err;
56 }
57
58 PackedStringArray ZIPReader::get_files() {
59 ERR_FAIL_COND_V_MSG(fa.is_null(), PackedStringArray(), "ZIPReader must be opened before use.");
60
61 unz_global_info gi;
62 int err = unzGetGlobalInfo(uzf, &gi);
63 ERR_FAIL_COND_V(err != UNZ_OK, PackedStringArray());
64 if (gi.number_entry == 0) {
65 return PackedStringArray();
66 }
67
68 err = unzGoToFirstFile(uzf);
69 ERR_FAIL_COND_V(err != UNZ_OK, PackedStringArray());
70
71 List<String> s;
72 do {
73 unz_file_info64 file_info;
74 String filepath;
75
76 err = godot_unzip_get_current_file_info(uzf, file_info, filepath);
77 if (err == UNZ_OK) {
78 s.push_back(filepath);
79 }
80 } while (unzGoToNextFile(uzf) == UNZ_OK);
81
82 PackedStringArray arr;
83 arr.resize(s.size());
84 int idx = 0;
85 for (const List<String>::Element *E = s.front(); E; E = E->next()) {
86 arr.set(idx++, E->get());
87 }
88 return arr;
89 }
90
91 PackedByteArray ZIPReader::read_file(const String &p_path, bool p_case_sensitive) {
92 ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "ZIPReader must be opened before use.");
93
94 int err = UNZ_OK;
95
96 // Locate and open the file.
97 err = godot_unzip_locate_file(uzf, p_path, p_case_sensitive);
98 ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "File does not exist in zip archive: " + p_path);
99 err = unzOpenCurrentFile(uzf);
100 ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Could not open file within zip archive.");
101
102 // Read the file info.
103 unz_file_info info;
104 err = unzGetCurrentFileInfo(uzf, &info, nullptr, 0, nullptr, 0, nullptr, 0);
105 ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Unable to read file information from zip archive.");
106 ERR_FAIL_COND_V_MSG(info.uncompressed_size > INT_MAX, PackedByteArray(), "File contents too large to read from zip archive (>2 GB).");
107
108 // Read the file data.
109 PackedByteArray data;
110 data.resize(info.uncompressed_size);
111 uint8_t *buffer = data.ptrw();
112 int to_read = data.size();
113 while (to_read > 0) {
114 int bytes_read = unzReadCurrentFile(uzf, buffer, to_read);
115 ERR_FAIL_COND_V_MSG(bytes_read < 0, PackedByteArray(), "IO/zlib error reading file from zip archive.");
116 ERR_FAIL_COND_V_MSG(bytes_read == UNZ_EOF && to_read != 0, PackedByteArray(), "Incomplete file read from zip archive.");
117 DEV_ASSERT(bytes_read <= to_read);
118 buffer += bytes_read;
119 to_read -= bytes_read;
120 }
121
122 // Verify the data and return.
123 err = unzCloseCurrentFile(uzf);
124 ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "CRC error reading file from zip archive.");
125 return data;
126 }
127
128 bool ZIPReader::file_exists(const String &p_path, bool p_case_sensitive) {
129 ERR_FAIL_COND_V_MSG(fa.is_null(), false, "ZIPReader must be opened before use.");
130
131 int cs = p_case_sensitive ? 1 : 2;
132 if (unzLocateFile(uzf, p_path.utf8().get_data(), cs) != UNZ_OK) {
133 return false;
134 }
135 if (unzOpenCurrentFile(uzf) != UNZ_OK) {
136 return false;
137 }
138
139 unzCloseCurrentFile(uzf);
140 return true;
141 }
142
143 int ZIPReader::get_compression_level(const String &p_path, bool p_case_sensitive) {
144 ERR_FAIL_COND_V_MSG(fa.is_null(), -1, "ZIPReader must be opened before use.");
145
146 int cs = p_case_sensitive ? 1 : 2;
147 if (unzLocateFile(uzf, p_path.utf8().get_data(), cs) != UNZ_OK) {
148 return -1;
149 }
150
151 int method;
152 int level;
153 if (unzOpenCurrentFile2(uzf, &method, &level, 1) != UNZ_OK) {
154 return -1;
155 }
156
157 unzCloseCurrentFile(uzf);
158
159 return level;
160 }
161
162 ZIPReader::ZIPReader() {}
163
164 ZIPReader::~ZIPReader() {
165 if (fa.is_valid()) {
166 close();
167 }
168 }
169
170 void ZIPReader::_bind_methods() {
171 ClassDB::bind_method(D_METHOD("open", "path"), &ZIPReader::open);
172 ClassDB::bind_method(D_METHOD("close"), &ZIPReader::close);
173 ClassDB::bind_method(D_METHOD("get_files"), &ZIPReader::get_files);
174 ClassDB::bind_method(D_METHOD("read_file", "path", "case_sensitive"), &ZIPReader::read_file, DEFVAL(Variant(true)));
175 ClassDB::bind_method(D_METHOD("file_exists", "path", "case_sensitive"), &ZIPReader::file_exists, DEFVAL(Variant(true)));
176 ClassDB::bind_method(D_METHOD("get_compression_level", "path", "case_sensitive"), &ZIPReader::get_compression_level, DEFVAL(Variant(true)));
177 }