file_access_compressed.cpp 12 KB

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