Debian Codegraph @

core/io/zip_io.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* zip_io.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_io.h"
32
33 #include "core/templates/local_vector.h"
34
35 int godot_unzip_get_current_file_info(unzFile p_zip_file, unz_file_info64 &r_file_info, String &r_filepath) {
36 const uLong short_file_path_buffer_size = 16384ul;
37 char short_file_path_buffer[short_file_path_buffer_size];
38
39 int err = unzGetCurrentFileInfo64(p_zip_file, &r_file_info, short_file_path_buffer, short_file_path_buffer_size, nullptr, 0, nullptr, 0);
40 if (unlikely((err != UNZ_OK) || (r_file_info.size_filename > short_file_path_buffer_size))) {
41 LocalVector<char> long_file_path_buffer;
42 long_file_path_buffer.resize(r_file_info.size_filename);
43
44 err = unzGetCurrentFileInfo64(p_zip_file, &r_file_info, long_file_path_buffer.ptr(), long_file_path_buffer.size(), nullptr, 0, nullptr, 0);
45 if (err != UNZ_OK) {
46 return err;
47 }
48 r_filepath = String::utf8(long_file_path_buffer.ptr(), r_file_info.size_filename);
49 } else {
50 r_filepath = String::utf8(short_file_path_buffer, r_file_info.size_filename);
51 }
52
53 return err;
54 }
55
56 int godot_unzip_locate_file(unzFile p_zip_file, const String &p_filepath, bool p_case_sensitive) {
57 int err = unzGoToFirstFile(p_zip_file);
58 while (err == UNZ_OK) {
59 unz_file_info64 current_file_info;
60 String current_filepath;
61 err = godot_unzip_get_current_file_info(p_zip_file, current_file_info, current_filepath);
62 if (err == UNZ_OK) {
63 bool filepaths_are_equal = p_case_sensitive ? (p_filepath == current_filepath) : (p_filepath.nocasecmp_to(current_filepath) == 0);
64 if (filepaths_are_equal) {
65 return UNZ_OK;
66 }
67 err = unzGoToNextFile(p_zip_file);
68 }
69 }
70 return err;
71 }
72
73 //
74
75 void *zipio_open(voidpf opaque, const char *p_fname, int mode) {
76 Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
77 ERR_FAIL_NULL_V(fa, nullptr);
78
79 String fname = String::utf8(p_fname);
80
81 int file_access_mode = 0;
82 if (mode & ZLIB_FILEFUNC_MODE_READ) {
83 file_access_mode |= FileAccess::READ;
84 }
85 if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
86 file_access_mode |= FileAccess::WRITE;
87 }
88 if (mode & ZLIB_FILEFUNC_MODE_CREATE) {
89 file_access_mode |= FileAccess::WRITE_READ;
90 }
91
92 (*fa) = FileAccess::open(fname, file_access_mode);
93 if (fa->is_null()) {
94 return nullptr;
95 }
96
97 return opaque;
98 }
99
100 uLong zipio_read(voidpf opaque, voidpf stream, void *buf, uLong size) {
101 Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
102 ERR_FAIL_NULL_V(fa, 0);
103 ERR_FAIL_COND_V(fa->is_null(), 0);
104
105 return (*fa)->get_buffer((uint8_t *)buf, size);
106 }
107
108 uLong zipio_write(voidpf opaque, voidpf stream, const void *buf, uLong size) {
109 Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
110 ERR_FAIL_NULL_V(fa, 0);
111 ERR_FAIL_COND_V(fa->is_null(), 0);
112
113 (*fa)->store_buffer((uint8_t *)buf, size);
114 return size;
115 }
116
117 long zipio_tell(voidpf opaque, voidpf stream) {
118 Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
119 ERR_FAIL_NULL_V(fa, 0);
120 ERR_FAIL_COND_V(fa->is_null(), 0);
121
122 return (*fa)->get_position();
123 }
124
125 long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
126 Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
127 ERR_FAIL_NULL_V(fa, 0);
128 ERR_FAIL_COND_V(fa->is_null(), 0);
129
130 uint64_t pos = offset;
131 switch (origin) {
132 case ZLIB_FILEFUNC_SEEK_CUR:
133 pos = (*fa)->get_position() + offset;
134 break;
135 case ZLIB_FILEFUNC_SEEK_END:
136 pos = (*fa)->get_length() + offset;
137 break;
138 default:
139 break;
140 }
141
142 (*fa)->seek(pos);
143 return 0;
144 }
145
146 int zipio_close(voidpf opaque, voidpf stream) {
147 Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
148 ERR_FAIL_NULL_V(fa, 0);
149 ERR_FAIL_COND_V(fa->is_null(), 0);
150
151 fa->unref();
152 return 0;
153 }
154
155 int zipio_testerror(voidpf opaque, voidpf stream) {
156 Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
157 ERR_FAIL_NULL_V(fa, 1);
158 ERR_FAIL_COND_V(fa->is_null(), 0);
159
160 return (fa->is_valid() && (*fa)->get_error() != OK) ? 1 : 0;
161 }
162
163 voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) {
164 voidpf ptr = memalloc_zeroed((size_t)items * size);
165 return ptr;
166 }
167
168 void zipio_free(voidpf opaque, voidpf address) {
169 memfree(address);
170 }
171
172 zlib_filefunc_def zipio_create_io(Ref<FileAccess> *p_data) {
173 zlib_filefunc_def io;
174 io.opaque = (void *)p_data;
175 io.zopen_file = zipio_open;
176 io.zread_file = zipio_read;
177 io.zwrite_file = zipio_write;
178 io.ztell_file = zipio_tell;
179 io.zseek_file = zipio_seek;
180 io.zclose_file = zipio_close;
181 io.zerror_file = zipio_testerror;
182 io.alloc_mem = zipio_alloc;
183 io.free_mem = zipio_free;
184 return io;
185 }