| 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 | uint32_t last_block_size = write_max % block_size; |
| 134 | |
| 135 | // Temporary buffer for compressed data blocks. |
| 136 | LocalVector<uint8_t> temp_cblock; |
| 137 | temp_cblock.resize(Compression::get_max_compressed_buffer_size(bc == 1 ? last_block_size : block_size, cmode)); |
| 138 | uint8_t *temp_cblock_ptr = temp_cblock.ptr(); |
| 139 | |
| 140 | // Compress and store the blocks. |
| 141 | LocalVector<uint32_t> block_sizes; |
| 142 | for (uint32_t i = 0; i < bc; i++) { |
| 143 | uint32_t bl = i == (bc - 1) ? last_block_size : block_size; |
| 144 | uint8_t *bp = &write_ptr[i * block_size]; |
| 145 | |
| 146 | const int64_t compressed_size = Compression::compress(temp_cblock_ptr, bp, bl, cmode); |
| 147 | ERR_FAIL_COND_MSG(compressed_size < 0, "FileAccessCompressed: Error compressing data."); |
| 148 | |
| 149 | f->store_buffer(temp_cblock_ptr, (uint64_t)compressed_size); |
| 150 | block_sizes.push_back(compressed_size); |
| 151 | } |
| 152 | |
| 153 | f->seek(16); //ok write block sizes |
| 154 | for (uint32_t i = 0; i < bc; i++) { |
| 155 | f->store_32(block_sizes[i]); |
| 156 | } |
| 157 | f->seek_end(); |
| 158 | f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too |
| 159 | } else { |
| 160 | comp_buffer.clear(); |
| 161 | read_blocks.clear(); |
| 162 | } |
| 163 | buffer.clear(); |
| 164 | f.unref(); |
| 165 | } |
| 166 | |
| 167 | bool FileAccessCompressed::is_open() const { |
| 168 | return f.is_valid(); |
| 169 | } |
| 170 | |
| 171 | String FileAccessCompressed::get_path() const { |
| 172 | if (f.is_valid()) { |
| 173 | return f->get_path(); |
| 174 | } else { |
| 175 | return ""; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | String FileAccessCompressed::get_path_absolute() const { |
| 180 | if (f.is_valid()) { |
| 181 | return f->get_path_absolute(); |
| 182 | } else { |
| 183 | return ""; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void FileAccessCompressed::seek(uint64_t p_position) { |
| 188 | ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use."); |
| 189 | |
| 190 | if (writing) { |
| 191 | ERR_FAIL_COND(p_position > write_max); |
| 192 | |
| 193 | write_pos = p_position; |
| 194 | |
| 195 | } else { |
| 196 | ERR_FAIL_COND(p_position > read_total); |
| 197 | if (p_position == read_total) { |
| 198 | at_end = true; |
| 199 | } else { |
| 200 | at_end = false; |
| 201 | read_eof = false; |
| 202 | uint32_t block_idx = p_position / block_size; |
| 203 | if (block_idx != read_block) { |
| 204 | read_block = block_idx; |
| 205 | f->seek(read_blocks[read_block].offset); |
| 206 | f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize); |
| 207 | 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); |
| 208 | ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt."); |
| 209 | read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size; |
| 210 | } |
| 211 | |
| 212 | read_pos = p_position % block_size; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void FileAccessCompressed::seek_end(int64_t p_position) { |
| 218 | ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use."); |
| 219 | if (writing) { |
| 220 | seek(write_max + p_position); |
| 221 | } else { |
| 222 | seek(read_total + p_position); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | uint64_t FileAccessCompressed::get_position() const { |
| 227 | ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use."); |
| 228 | if (writing) { |
| 229 | return write_pos; |
| 230 | } else { |
| 231 | return (uint64_t)read_block * block_size + read_pos; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | uint64_t FileAccessCompressed::get_length() const { |
| 236 | ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use."); |
| 237 | if (writing) { |
| 238 | return write_max; |
| 239 | } else { |
| 240 | return read_total; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | bool FileAccessCompressed::eof_reached() const { |
| 245 | ERR_FAIL_COND_V_MSG(f.is_null(), false, "File must be opened before use."); |
| 246 | if (writing) { |
| 247 | return false; |
| 248 | } else { |
| 249 | return read_eof; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | uint64_t FileAccessCompressed::get_buffer(uint8_t *p_dst, uint64_t p_length) const { |
| 254 | if (p_length == 0) { |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | ERR_FAIL_NULL_V(p_dst, -1); |
| 259 | ERR_FAIL_COND_V_MSG(f.is_null(), -1, "File must be opened before use."); |
| 260 | ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode."); |
| 261 | |
| 262 | if (at_end) { |
| 263 | read_eof = true; |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | uint64_t dst_idx = 0; |
| 268 | while (true) { |
| 269 | // Copy over as much of our current block as possible. |
| 270 | const uint32_t copied_bytes_count = MIN(p_length - dst_idx, read_block_size - read_pos); |
| 271 | memcpy(p_dst + dst_idx, read_ptr + read_pos, copied_bytes_count); |
| 272 | dst_idx += copied_bytes_count; |
| 273 | read_pos += copied_bytes_count; |
| 274 | |
| 275 | if (dst_idx == p_length) { |
| 276 | // We're done! We read back all that was requested. |
| 277 | return p_length; |
| 278 | } |
| 279 | |
| 280 | // We're not done yet; try reading the next block. |
| 281 | read_block++; |
| 282 | |
| 283 | if (read_block >= read_block_count) { |
| 284 | // We're done! We read back the whole file. |
| 285 | read_block--; |
| 286 | at_end = true; |
| 287 | if (dst_idx + 1 < p_length) { |
| 288 | read_eof = true; |
| 289 | } |
| 290 | return dst_idx; |
| 291 | } |
| 292 | |
| 293 | // Read the next block of compressed data. |
| 294 | f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize); |
| 295 | 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); |
| 296 | ERR_FAIL_COND_V_MSG(ret == -1, -1, "Compressed file is corrupt."); |
| 297 | read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size; |
| 298 | read_pos = 0; |
| 299 | } |
| 300 | |
| 301 | return p_length; |
| 302 | } |
| 303 | |
| 304 | Error FileAccessCompressed::get_error() const { |
| 305 | return read_eof ? ERR_FILE_EOF : OK; |
| 306 | } |
| 307 | |
| 308 | void FileAccessCompressed::flush() { |
| 309 | ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use."); |
| 310 | ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); |
| 311 | |
| 312 | // compressed files keep data in memory till close() |
| 313 | } |
| 314 | |
| 315 | bool FileAccessCompressed::store_buffer(const uint8_t *p_src, uint64_t p_length) { |
| 316 | ERR_FAIL_COND_V_MSG(f.is_null(), false, "File must be opened before use."); |
| 317 | ERR_FAIL_COND_V_MSG(!writing, false, "File has not been opened in write mode."); |
| 318 | |
| 319 | if (write_pos + (p_length) > write_max) { |
| 320 | write_max = write_pos + (p_length); |
| 321 | } |
| 322 | if (write_max > write_buffer_size) { |
| 323 | write_buffer_size = next_power_of_2(write_max); |
| 324 | ERR_FAIL_COND_V(buffer.resize(write_buffer_size) != OK, false); |
| 325 | write_ptr = buffer.ptrw(); |
| 326 | } |
| 327 | |
| 328 | if (p_length) { |
| 329 | memcpy(write_ptr + write_pos, p_src, p_length); |
| 330 | } |
| 331 | |
| 332 | write_pos += p_length; |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | bool FileAccessCompressed::file_exists(const String &p_name) { |
| 337 | Ref<FileAccess> fa = FileAccess::open(p_name, FileAccess::READ); |
| 338 | if (fa.is_null()) { |
| 339 | return false; |
| 340 | } |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | uint64_t FileAccessCompressed::_get_modified_time(const String &p_file) { |
| 345 | if (f.is_valid()) { |
| 346 | return f->get_modified_time(p_file); |
| 347 | } else { |
| 348 | return 0; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | uint64_t FileAccessCompressed::_get_access_time(const String &p_file) { |
| 353 | if (f.is_valid()) { |
| 354 | return f->get_access_time(p_file); |
| 355 | } else { |
| 356 | return 0; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | int64_t FileAccessCompressed::_get_size(const String &p_file) { |
| 361 | if (f.is_valid()) { |
| 362 | return f->get_size(p_file); |
| 363 | } else { |
| 364 | return -1; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | BitField<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 | |
| 375 | Error 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 | |
| 382 | bool 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 | |
| 389 | Error 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 | |
| 396 | bool 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 | |
| 403 | Error 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 | |
| 410 | void FileAccessCompressed::close() { |
| 411 | _close(); |
| 412 | } |
| 413 | |
| 414 | FileAccessCompressed::~FileAccessCompressed() { |
| 415 | _close(); |
| 416 | } |