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, 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; |
80 | fname.parse_utf8(p_fname); |
81 | |
82 | int file_access_mode = 0; |
83 | if (mode & ZLIB_FILEFUNC_MODE_READ) { |
84 | file_access_mode |= FileAccess::READ; |
85 | } |
86 | if (mode & ZLIB_FILEFUNC_MODE_WRITE) { |
87 | file_access_mode |= FileAccess::WRITE; |
88 | } |
89 | if (mode & ZLIB_FILEFUNC_MODE_CREATE) { |
90 | file_access_mode |= FileAccess::WRITE_READ; |
91 | } |
92 | |
93 | (*fa) = FileAccess::open(fname, file_access_mode); |
94 | if (fa->is_null()) { |
95 | return nullptr; |
96 | } |
97 | |
98 | return opaque; |
99 | } |
100 | |
101 | uLong zipio_read(voidpf opaque, voidpf stream, void *buf, uLong size) { |
102 | Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque); |
103 | ERR_FAIL_NULL_V(fa, 0); |
104 | ERR_FAIL_COND_V(fa->is_null(), 0); |
105 | |
106 | return (*fa)->get_buffer((uint8_t *)buf, size); |
107 | } |
108 | |
109 | uLong zipio_write(voidpf opaque, voidpf stream, const void *buf, uLong size) { |
110 | Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque); |
111 | ERR_FAIL_NULL_V(fa, 0); |
112 | ERR_FAIL_COND_V(fa->is_null(), 0); |
113 | |
114 | (*fa)->store_buffer((uint8_t *)buf, size); |
115 | return size; |
116 | } |
117 | |
118 | long zipio_tell(voidpf opaque, voidpf stream) { |
119 | Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque); |
120 | ERR_FAIL_NULL_V(fa, 0); |
121 | ERR_FAIL_COND_V(fa->is_null(), 0); |
122 | |
123 | return (*fa)->get_position(); |
124 | } |
125 | |
126 | long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { |
127 | Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque); |
128 | ERR_FAIL_NULL_V(fa, 0); |
129 | ERR_FAIL_COND_V(fa->is_null(), 0); |
130 | |
131 | uint64_t pos = offset; |
132 | switch (origin) { |
133 | case ZLIB_FILEFUNC_SEEK_CUR: |
134 | pos = (*fa)->get_position() + offset; |
135 | break; |
136 | case ZLIB_FILEFUNC_SEEK_END: |
137 | pos = (*fa)->get_length() + offset; |
138 | break; |
139 | default: |
140 | break; |
141 | } |
142 | |
143 | (*fa)->seek(pos); |
144 | return 0; |
145 | } |
146 | |
147 | int zipio_close(voidpf opaque, voidpf stream) { |
148 | Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque); |
149 | ERR_FAIL_NULL_V(fa, 0); |
150 | ERR_FAIL_COND_V(fa->is_null(), 0); |
151 | |
152 | fa->unref(); |
153 | return 0; |
154 | } |
155 | |
156 | int zipio_testerror(voidpf opaque, voidpf stream) { |
157 | Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque); |
158 | ERR_FAIL_NULL_V(fa, 1); |
159 | ERR_FAIL_COND_V(fa->is_null(), 0); |
160 | |
161 | return (fa->is_valid() && (*fa)->get_error() != OK) ? 1 : 0; |
162 | } |
163 | |
164 | voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) { |
165 | voidpf ptr = memalloc((size_t)items * size); |
166 | memset(ptr, 0, items * size); |
167 | return ptr; |
168 | } |
169 | |
170 | void zipio_free(voidpf opaque, voidpf address) { |
171 | memfree(address); |
172 | } |
173 | |
174 | zlib_filefunc_def zipio_create_io(Ref<FileAccess> *p_data) { |
175 | zlib_filefunc_def io; |
176 | io.opaque = (void *)p_data; |
177 | io.zopen_file = zipio_open; |
178 | io.zread_file = zipio_read; |
179 | io.zwrite_file = zipio_write; |
180 | io.ztell_file = zipio_tell; |
181 | io.zseek_file = zipio_seek; |
182 | io.zclose_file = zipio_close; |
183 | io.zerror_file = zipio_testerror; |
184 | io.alloc_mem = zipio_alloc; |
185 | io.free_mem = zipio_free; |
186 | return io; |
187 | } |
188 | |