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