file_access_compressed.cpp 12 KB

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