Path: blob/master/core/io/file_access_compressed.cpp
10277 views
/**************************************************************************/1/* file_access_compressed.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "file_access_compressed.h"3132void FileAccessCompressed::configure(const String &p_magic, Compression::Mode p_mode, uint32_t p_block_size) {33magic = p_magic.ascii().get_data();34magic = (magic + " ").substr(0, 4);3536cmode = p_mode;37block_size = p_block_size;38}3940Error FileAccessCompressed::open_after_magic(Ref<FileAccess> p_base) {41f = p_base;42cmode = (Compression::Mode)f->get_32();43block_size = f->get_32();44if (block_size == 0) {45f.unref();46ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, vformat("Can't open compressed file '%s' with block size 0, it is corrupted.", p_base->get_path()));47}48read_total = f->get_32();49uint32_t bc = (read_total / block_size) + 1;50uint64_t acc_ofs = f->get_position() + bc * 4;51uint32_t max_bs = 0;52for (uint32_t i = 0; i < bc; i++) {53ReadBlock rb;54rb.offset = acc_ofs;55rb.csize = f->get_32();56acc_ofs += rb.csize;57max_bs = MAX(max_bs, rb.csize);58read_blocks.push_back(rb);59}6061comp_buffer.resize(max_bs);62buffer.resize(block_size);63read_ptr = buffer.ptrw();64f->get_buffer(comp_buffer.ptrw(), read_blocks[0].csize);65at_end = false;66read_eof = false;67read_block_count = bc;68read_block_size = read_blocks.size() == 1 ? read_total : block_size;6970const int64_t ret = Compression::decompress(buffer.ptrw(), read_block_size, comp_buffer.ptr(), read_blocks[0].csize, cmode);71read_block = 0;72read_pos = 0;7374return ret == -1 ? ERR_FILE_CORRUPT : OK;75}7677Error FileAccessCompressed::open_internal(const String &p_path, int p_mode_flags) {78ERR_FAIL_COND_V(p_mode_flags == READ_WRITE, ERR_UNAVAILABLE);79_close();8081Error err;82f = FileAccess::open(p_path, p_mode_flags, &err);83if (err != OK) {84//not openable85f.unref();86return err;87}8889if (p_mode_flags & WRITE) {90buffer.clear();91writing = true;92write_pos = 0;93write_buffer_size = 256;94buffer.resize(256);95write_max = 0;96write_ptr = buffer.ptrw();9798//don't store anything else unless it's done saving!99} else {100char rmagic[5];101f->get_buffer((uint8_t *)rmagic, 4);102rmagic[4] = 0;103err = ERR_FILE_UNRECOGNIZED;104if (magic != rmagic || (err = open_after_magic(f)) != OK) {105f.unref();106return err;107}108}109110return OK;111}112113void FileAccessCompressed::_close() {114if (f.is_null()) {115return;116}117118if (writing) {119//save block table and all compressed blocks120121CharString mgc = magic.utf8();122f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //write header 4123f->store_32(cmode); //write compression mode 4124f->store_32(block_size); //write block size 4125f->store_32(uint32_t(write_max)); //max amount of data written 4126uint32_t bc = (write_max / block_size) + 1;127128for (uint32_t i = 0; i < bc; i++) {129f->store_32(0); //compressed sizes, will update later130}131132Vector<int> block_sizes;133for (uint32_t i = 0; i < bc; i++) {134uint32_t bl = i == (bc - 1) ? write_max % block_size : block_size;135uint8_t *bp = &write_ptr[i * block_size];136137Vector<uint8_t> cblock;138cblock.resize(Compression::get_max_compressed_buffer_size(bl, cmode));139const int64_t compressed_size = Compression::compress(cblock.ptrw(), bp, bl, cmode);140ERR_FAIL_COND_MSG(compressed_size < 0, "FileAccessCompressed: Error compressing data.");141142f->store_buffer(cblock.ptr(), (uint64_t)compressed_size);143block_sizes.push_back(compressed_size);144}145146f->seek(16); //ok write block sizes147for (uint32_t i = 0; i < bc; i++) {148f->store_32(uint32_t(block_sizes[i]));149}150f->seek_end();151f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too152153buffer.clear();154155} else {156comp_buffer.clear();157buffer.clear();158read_blocks.clear();159}160f.unref();161}162163bool FileAccessCompressed::is_open() const {164return f.is_valid();165}166167String FileAccessCompressed::get_path() const {168if (f.is_valid()) {169return f->get_path();170} else {171return "";172}173}174175String FileAccessCompressed::get_path_absolute() const {176if (f.is_valid()) {177return f->get_path_absolute();178} else {179return "";180}181}182183void FileAccessCompressed::seek(uint64_t p_position) {184ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");185186if (writing) {187ERR_FAIL_COND(p_position > write_max);188189write_pos = p_position;190191} else {192ERR_FAIL_COND(p_position > read_total);193if (p_position == read_total) {194at_end = true;195} else {196at_end = false;197read_eof = false;198uint32_t block_idx = p_position / block_size;199if (block_idx != read_block) {200read_block = block_idx;201f->seek(read_blocks[read_block].offset);202f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize);203const int64_t ret = Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode);204ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");205read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size;206}207208read_pos = p_position % block_size;209}210}211}212213void FileAccessCompressed::seek_end(int64_t p_position) {214ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");215if (writing) {216seek(write_max + p_position);217} else {218seek(read_total + p_position);219}220}221222uint64_t FileAccessCompressed::get_position() const {223ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use.");224if (writing) {225return write_pos;226} else {227return (uint64_t)read_block * block_size + read_pos;228}229}230231uint64_t FileAccessCompressed::get_length() const {232ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use.");233if (writing) {234return write_max;235} else {236return read_total;237}238}239240bool FileAccessCompressed::eof_reached() const {241ERR_FAIL_COND_V_MSG(f.is_null(), false, "File must be opened before use.");242if (writing) {243return false;244} else {245return read_eof;246}247}248249uint64_t FileAccessCompressed::get_buffer(uint8_t *p_dst, uint64_t p_length) const {250if (p_length == 0) {251return 0;252}253254ERR_FAIL_NULL_V(p_dst, -1);255ERR_FAIL_COND_V_MSG(f.is_null(), -1, "File must be opened before use.");256ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode.");257258if (at_end) {259read_eof = true;260return 0;261}262263uint64_t dst_idx = 0;264while (true) {265// Copy over as much of our current block as possible.266const uint32_t copied_bytes_count = MIN(p_length - dst_idx, read_block_size - read_pos);267memcpy(p_dst + dst_idx, read_ptr + read_pos, copied_bytes_count);268dst_idx += copied_bytes_count;269read_pos += copied_bytes_count;270271if (dst_idx == p_length) {272// We're done! We read back all that was requested.273return p_length;274}275276// We're not done yet; try reading the next block.277read_block++;278279if (read_block >= read_block_count) {280// We're done! We read back the whole file.281read_block--;282at_end = true;283if (dst_idx + 1 < p_length) {284read_eof = true;285}286return dst_idx;287}288289// Read the next block of compressed data.290f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize);291const int64_t ret = Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode);292ERR_FAIL_COND_V_MSG(ret == -1, -1, "Compressed file is corrupt.");293read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size;294read_pos = 0;295}296297return p_length;298}299300Error FileAccessCompressed::get_error() const {301return read_eof ? ERR_FILE_EOF : OK;302}303304void FileAccessCompressed::flush() {305ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use.");306ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");307308// compressed files keep data in memory till close()309}310311bool FileAccessCompressed::store_buffer(const uint8_t *p_src, uint64_t p_length) {312ERR_FAIL_COND_V_MSG(f.is_null(), false, "File must be opened before use.");313ERR_FAIL_COND_V_MSG(!writing, false, "File has not been opened in write mode.");314315if (write_pos + (p_length) > write_max) {316write_max = write_pos + (p_length);317}318if (write_max > write_buffer_size) {319write_buffer_size = next_power_of_2(write_max);320ERR_FAIL_COND_V(buffer.resize(write_buffer_size) != OK, false);321write_ptr = buffer.ptrw();322}323324if (p_length) {325memcpy(write_ptr + write_pos, p_src, p_length);326}327328write_pos += p_length;329return true;330}331332bool FileAccessCompressed::file_exists(const String &p_name) {333Ref<FileAccess> fa = FileAccess::open(p_name, FileAccess::READ);334if (fa.is_null()) {335return false;336}337return true;338}339340uint64_t FileAccessCompressed::_get_modified_time(const String &p_file) {341if (f.is_valid()) {342return f->get_modified_time(p_file);343} else {344return 0;345}346}347348uint64_t FileAccessCompressed::_get_access_time(const String &p_file) {349if (f.is_valid()) {350return f->get_access_time(p_file);351} else {352return 0;353}354}355356int64_t FileAccessCompressed::_get_size(const String &p_file) {357if (f.is_valid()) {358return f->get_size(p_file);359} else {360return -1;361}362}363364BitField<FileAccess::UnixPermissionFlags> FileAccessCompressed::_get_unix_permissions(const String &p_file) {365if (f.is_valid()) {366return f->_get_unix_permissions(p_file);367}368return 0;369}370371Error FileAccessCompressed::_set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) {372if (f.is_valid()) {373return f->_set_unix_permissions(p_file, p_permissions);374}375return FAILED;376}377378bool FileAccessCompressed::_get_hidden_attribute(const String &p_file) {379if (f.is_valid()) {380return f->_get_hidden_attribute(p_file);381}382return false;383}384385Error FileAccessCompressed::_set_hidden_attribute(const String &p_file, bool p_hidden) {386if (f.is_valid()) {387return f->_set_hidden_attribute(p_file, p_hidden);388}389return FAILED;390}391392bool FileAccessCompressed::_get_read_only_attribute(const String &p_file) {393if (f.is_valid()) {394return f->_get_read_only_attribute(p_file);395}396return false;397}398399Error FileAccessCompressed::_set_read_only_attribute(const String &p_file, bool p_ro) {400if (f.is_valid()) {401return f->_set_read_only_attribute(p_file, p_ro);402}403return FAILED;404}405406void FileAccessCompressed::close() {407_close();408}409410FileAccessCompressed::~FileAccessCompressed() {411_close();412}413414415