file_access_compressed.cpp 11 KB

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