file_access_compressed.cpp 11 KB

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