1/**************************************************************************/
2/* file_access_compressed.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 "file_access_compressed.h"
32
33#include "core/string/print_string.h"
34
35void FileAccessCompressed::configure(const String &p_magic, Compression::Mode p_mode, uint32_t p_block_size) {
36 magic = p_magic.ascii().get_data();
37 magic = (magic + " ").substr(0, 4);
38
39 cmode = p_mode;
40 block_size = p_block_size;
41}
42
43#define WRITE_FIT(m_bytes) \
44 { \
45 if (write_pos + (m_bytes) > write_max) { \
46 write_max = write_pos + (m_bytes); \
47 } \
48 if (write_max > write_buffer_size) { \
49 write_buffer_size = next_power_of_2(write_max); \
50 buffer.resize(write_buffer_size); \
51 write_ptr = buffer.ptrw(); \
52 } \
53 }
54
55Error FileAccessCompressed::open_after_magic(Ref<FileAccess> p_base) {
56 f = p_base;
57 cmode = (Compression::Mode)f->get_32();
58 block_size = f->get_32();
59 if (block_size == 0) {
60 f.unref();
61 ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Can't open compressed file '" + p_base->get_path() + "' with block size 0, it is corrupted.");
62 }
63 read_total = f->get_32();
64 uint32_t bc = (read_total / block_size) + 1;
65 uint64_t acc_ofs = f->get_position() + bc * 4;
66 uint32_t max_bs = 0;
67 for (uint32_t i = 0; i < bc; i++) {
68 ReadBlock rb;
69 rb.offset = acc_ofs;
70 rb.csize = f->get_32();
71 acc_ofs += rb.csize;
72 max_bs = MAX(max_bs, rb.csize);
73 read_blocks.push_back(rb);
74 }
75
76 comp_buffer.resize(max_bs);
77 buffer.resize(block_size);
78 read_ptr = buffer.ptrw();
79 f->get_buffer(comp_buffer.ptrw(), read_blocks[0].csize);
80 at_end = false;
81 read_eof = false;
82 read_block_count = bc;
83 read_block_size = read_blocks.size() == 1 ? read_total : block_size;
84
85 int ret = Compression::decompress(buffer.ptrw(), read_block_size, comp_buffer.ptr(), read_blocks[0].csize, cmode);
86 read_block = 0;
87 read_pos = 0;
88
89 return ret == -1 ? ERR_FILE_CORRUPT : OK;
90}
91
92Error FileAccessCompressed::open_internal(const String &p_path, int p_mode_flags) {
93 ERR_FAIL_COND_V(p_mode_flags == READ_WRITE, ERR_UNAVAILABLE);
94 _close();
95
96 Error err;
97 f = FileAccess::open(p_path, p_mode_flags, &err);
98 if (err != OK) {
99 //not openable
100 f.unref();
101 return err;
102 }
103
104 if (p_mode_flags & WRITE) {
105 buffer.clear();
106 writing = true;
107 write_pos = 0;
108 write_buffer_size = 256;
109 buffer.resize(256);
110 write_max = 0;
111 write_ptr = buffer.ptrw();
112
113 //don't store anything else unless it's done saving!
114 } else {
115 char rmagic[5];
116 f->get_buffer((uint8_t *)rmagic, 4);
117 rmagic[4] = 0;
118 err = ERR_FILE_UNRECOGNIZED;
119 if (magic != rmagic || (err = open_after_magic(f)) != OK) {
120 f.unref();
121 return err;
122 }
123 }
124
125 return OK;
126}
127
128void FileAccessCompressed::_close() {
129 if (f.is_null()) {
130 return;
131 }
132
133 if (writing) {
134 //save block table and all compressed blocks
135
136 CharString mgc = magic.utf8();
137 f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //write header 4
138 f->store_32(cmode); //write compression mode 4
139 f->store_32(block_size); //write block size 4
140 f->store_32(write_max); //max amount of data written 4
141 uint32_t bc = (write_max / block_size) + 1;
142
143 for (uint32_t i = 0; i < bc; i++) {
144 f->store_32(0); //compressed sizes, will update later
145 }
146
147 Vector<int> block_sizes;
148 for (uint32_t i = 0; i < bc; i++) {
149 uint32_t bl = i == (bc - 1) ? write_max % block_size : block_size;
150 uint8_t *bp = &write_ptr[i * block_size];
151
152 Vector<uint8_t> cblock;
153 cblock.resize(Compression::get_max_compressed_buffer_size(bl, cmode));
154 int s = Compression::compress(cblock.ptrw(), bp, bl, cmode);
155
156 f->store_buffer(cblock.ptr(), s);
157 block_sizes.push_back(s);
158 }
159
160 f->seek(16); //ok write block sizes
161 for (uint32_t i = 0; i < bc; i++) {
162 f->store_32(block_sizes[i]);
163 }
164 f->seek_end();
165 f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too
166
167 buffer.clear();
168
169 } else {
170 comp_buffer.clear();
171 buffer.clear();
172 read_blocks.clear();
173 }
174 f.unref();
175}
176
177bool FileAccessCompressed::is_open() const {
178 return f.is_valid();
179}
180
181String FileAccessCompressed::get_path() const {
182 if (f.is_valid()) {
183 return f->get_path();
184 } else {
185 return "";
186 }
187}
188
189String FileAccessCompressed::get_path_absolute() const {
190 if (f.is_valid()) {
191 return f->get_path_absolute();
192 } else {
193 return "";
194 }
195}
196
197void FileAccessCompressed::seek(uint64_t p_position) {
198 ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");
199
200 if (writing) {
201 ERR_FAIL_COND(p_position > write_max);
202
203 write_pos = p_position;
204
205 } else {
206 ERR_FAIL_COND(p_position > read_total);
207 if (p_position == read_total) {
208 at_end = true;
209 } else {
210 at_end = false;
211 read_eof = false;
212 uint32_t block_idx = p_position / block_size;
213 if (block_idx != read_block) {
214 read_block = block_idx;
215 f->seek(read_blocks[read_block].offset);
216 f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize);
217 int ret = Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode);
218 ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
219 read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size;
220 }
221
222 read_pos = p_position % block_size;
223 }
224 }
225}
226
227void FileAccessCompressed::seek_end(int64_t p_position) {
228 ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");
229 if (writing) {
230 seek(write_max + p_position);
231 } else {
232 seek(read_total + p_position);
233 }
234}
235
236uint64_t FileAccessCompressed::get_position() const {
237 ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use.");
238 if (writing) {
239 return write_pos;
240 } else {
241 return (uint64_t)read_block * block_size + read_pos;
242 }
243}
244
245uint64_t FileAccessCompressed::get_length() const {
246 ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use.");
247 if (writing) {
248 return write_max;
249 } else {
250 return read_total;
251 }
252}
253
254bool FileAccessCompressed::eof_reached() const {
255 ERR_FAIL_COND_V_MSG(f.is_null(), false, "File must be opened before use.");
256 if (writing) {
257 return false;
258 } else {
259 return read_eof;
260 }
261}
262
263uint8_t FileAccessCompressed::get_8() const {
264 ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use.");
265 ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode.");
266
267 if (at_end) {
268 read_eof = true;
269 return 0;
270 }
271
272 uint8_t ret = read_ptr[read_pos];
273
274 read_pos++;
275 if (read_pos >= read_block_size) {
276 read_block++;
277
278 if (read_block < read_block_count) {
279 //read another block of compressed data
280 f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize);
281 int total = Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode);
282 ERR_FAIL_COND_V_MSG(total == -1, 0, "Compressed file is corrupt.");
283 read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size;
284 read_pos = 0;
285
286 } else {
287 read_block--;
288 at_end = true;
289 }
290 }
291
292 return ret;
293}
294
295uint64_t FileAccessCompressed::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
296 ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
297 ERR_FAIL_COND_V_MSG(f.is_null(), -1, "File must be opened before use.");
298 ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode.");
299
300 if (at_end) {
301 read_eof = true;
302 return 0;
303 }
304
305 for (uint64_t i = 0; i < p_length; i++) {
306 p_dst[i] = read_ptr[read_pos];
307 read_pos++;
308 if (read_pos >= read_block_size) {
309 read_block++;
310
311 if (read_block < read_block_count) {
312 //read another block of compressed data
313 f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize);
314 int ret = Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode);
315 ERR_FAIL_COND_V_MSG(ret == -1, -1, "Compressed file is corrupt.");
316 read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size;
317 read_pos = 0;
318
319 } else {
320 read_block--;
321 at_end = true;
322 if (i + 1 < p_length) {
323 read_eof = true;
324 }
325 return i + 1;
326 }
327 }
328 }
329
330 return p_length;
331}
332
333Error FileAccessCompressed::get_error() const {
334 return read_eof ? ERR_FILE_EOF : OK;
335}
336
337void FileAccessCompressed::flush() {
338 ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");
339 ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
340
341 // compressed files keep data in memory till close()
342}
343
344void FileAccessCompressed::store_8(uint8_t p_dest) {
345 ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");
346 ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
347
348 WRITE_FIT(1);
349 write_ptr[write_pos++] = p_dest;
350}
351
352bool FileAccessCompressed::file_exists(const String &p_name) {
353 Ref<FileAccess> fa = FileAccess::open(p_name, FileAccess::READ);
354 if (fa.is_null()) {
355 return false;
356 }
357 return true;
358}
359
360uint64_t FileAccessCompressed::_get_modified_time(const String &p_file) {
361 if (f.is_valid()) {
362 return f->get_modified_time(p_file);
363 } else {
364 return 0;
365 }
366}
367
368BitField<FileAccess::UnixPermissionFlags> FileAccessCompressed::_get_unix_permissions(const String &p_file) {
369 if (f.is_valid()) {
370 return f->_get_unix_permissions(p_file);
371 }
372 return 0;
373}
374
375Error FileAccessCompressed::_set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) {
376 if (f.is_valid()) {
377 return f->_set_unix_permissions(p_file, p_permissions);
378 }
379 return FAILED;
380}
381
382bool FileAccessCompressed::_get_hidden_attribute(const String &p_file) {
383 if (f.is_valid()) {
384 return f->_get_hidden_attribute(p_file);
385 }
386 return false;
387}
388
389Error FileAccessCompressed::_set_hidden_attribute(const String &p_file, bool p_hidden) {
390 if (f.is_valid()) {
391 return f->_set_hidden_attribute(p_file, p_hidden);
392 }
393 return FAILED;
394}
395
396bool FileAccessCompressed::_get_read_only_attribute(const String &p_file) {
397 if (f.is_valid()) {
398 return f->_get_read_only_attribute(p_file);
399 }
400 return false;
401}
402
403Error FileAccessCompressed::_set_read_only_attribute(const String &p_file, bool p_ro) {
404 if (f.is_valid()) {
405 return f->_set_read_only_attribute(p_file, p_ro);
406 }
407 return FAILED;
408}
409
410void FileAccessCompressed::close() {
411 _close();
412}
413
414FileAccessCompressed::~FileAccessCompressed() {
415 _close();
416}
417