file_access_compressed.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. read_total = f->get_32();
  59. int bc = (read_total / block_size) + 1;
  60. int acc_ofs = f->get_position() + bc * 4;
  61. int max_bs = 0;
  62. for (int i = 0; i < bc; i++) {
  63. ReadBlock rb;
  64. rb.offset = acc_ofs;
  65. rb.csize = f->get_32();
  66. acc_ofs += rb.csize;
  67. max_bs = MAX(max_bs, rb.csize);
  68. read_blocks.push_back(rb);
  69. }
  70. comp_buffer.resize(max_bs);
  71. buffer.resize(block_size);
  72. read_ptr = buffer.ptrw();
  73. f->get_buffer(comp_buffer.ptrw(), read_blocks[0].csize);
  74. at_end = false;
  75. read_eof = false;
  76. read_block_count = bc;
  77. read_block_size = read_blocks.size() == 1 ? read_total : block_size;
  78. Compression::decompress(buffer.ptrw(), read_block_size, comp_buffer.ptr(), read_blocks[0].csize, cmode);
  79. read_block = 0;
  80. read_pos = 0;
  81. return OK;
  82. }
  83. Error FileAccessCompressed::_open(const String &p_path, int p_mode_flags) {
  84. ERR_FAIL_COND_V(p_mode_flags == READ_WRITE, ERR_UNAVAILABLE);
  85. if (f)
  86. close();
  87. Error err;
  88. f = FileAccess::open(p_path, p_mode_flags, &err);
  89. if (err != OK) {
  90. //not openable
  91. f = NULL;
  92. return err;
  93. }
  94. if (p_mode_flags & WRITE) {
  95. buffer.clear();
  96. writing = true;
  97. write_pos = 0;
  98. write_buffer_size = 256;
  99. buffer.resize(256);
  100. write_max = 0;
  101. write_ptr = buffer.ptrw();
  102. //don't store anything else unless it's done saving!
  103. } else {
  104. char rmagic[5];
  105. f->get_buffer((uint8_t *)rmagic, 4);
  106. rmagic[4] = 0;
  107. if (magic != rmagic) {
  108. memdelete(f);
  109. f = NULL;
  110. return ERR_FILE_UNRECOGNIZED;
  111. }
  112. open_after_magic(f);
  113. }
  114. return OK;
  115. }
  116. void FileAccessCompressed::close() {
  117. if (!f)
  118. return;
  119. if (writing) {
  120. //save block table and all compressed blocks
  121. CharString mgc = magic.utf8();
  122. f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //write header 4
  123. f->store_32(cmode); //write compression mode 4
  124. f->store_32(block_size); //write block size 4
  125. f->store_32(write_max); //max amount of data written 4
  126. int bc = (write_max / block_size) + 1;
  127. for (int i = 0; i < bc; i++) {
  128. f->store_32(0); //compressed sizes, will update later
  129. }
  130. Vector<int> block_sizes;
  131. for (int i = 0; i < bc; i++) {
  132. int bl = i == (bc - 1) ? write_max % block_size : block_size;
  133. uint8_t *bp = &write_ptr[i * block_size];
  134. Vector<uint8_t> cblock;
  135. cblock.resize(Compression::get_max_compressed_buffer_size(bl, cmode));
  136. int s = Compression::compress(cblock.ptrw(), bp, bl, cmode);
  137. f->store_buffer(cblock.ptr(), s);
  138. block_sizes.push_back(s);
  139. }
  140. f->seek(16); //ok write block sizes
  141. for (int i = 0; i < bc; i++)
  142. f->store_32(block_sizes[i]);
  143. f->seek_end();
  144. f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too
  145. buffer.clear();
  146. } else {
  147. comp_buffer.clear();
  148. buffer.clear();
  149. read_blocks.clear();
  150. }
  151. memdelete(f);
  152. f = NULL;
  153. }
  154. bool FileAccessCompressed::is_open() const {
  155. return f != NULL;
  156. }
  157. void FileAccessCompressed::seek(size_t p_position) {
  158. ERR_FAIL_COND(!f);
  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. int block_idx = p_position / block_size;
  168. if (block_idx != read_block) {
  169. read_block = block_idx;
  170. f->seek(read_blocks[read_block].offset);
  171. f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize);
  172. Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode);
  173. read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size;
  174. }
  175. read_pos = p_position % block_size;
  176. }
  177. }
  178. }
  179. void FileAccessCompressed::seek_end(int64_t p_position) {
  180. ERR_FAIL_COND(!f);
  181. if (writing) {
  182. seek(write_max + p_position);
  183. } else {
  184. seek(read_total + p_position);
  185. }
  186. }
  187. size_t FileAccessCompressed::get_position() const {
  188. ERR_FAIL_COND_V(!f, 0);
  189. if (writing) {
  190. return write_pos;
  191. } else {
  192. return read_block * block_size + read_pos;
  193. }
  194. }
  195. size_t FileAccessCompressed::get_len() const {
  196. ERR_FAIL_COND_V(!f, 0);
  197. if (writing) {
  198. return write_max;
  199. } else {
  200. return read_total;
  201. }
  202. }
  203. bool FileAccessCompressed::eof_reached() const {
  204. ERR_FAIL_COND_V(!f, false);
  205. if (writing) {
  206. return false;
  207. } else {
  208. return read_eof;
  209. }
  210. }
  211. uint8_t FileAccessCompressed::get_8() const {
  212. ERR_FAIL_COND_V(writing, 0);
  213. ERR_FAIL_COND_V(!f, 0);
  214. if (at_end) {
  215. read_eof = true;
  216. return 0;
  217. }
  218. uint8_t ret = read_ptr[read_pos];
  219. read_pos++;
  220. if (read_pos >= read_block_size) {
  221. read_block++;
  222. if (read_block < read_block_count) {
  223. //read another block of compressed data
  224. f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize);
  225. Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode);
  226. read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size;
  227. read_pos = 0;
  228. } else {
  229. read_block--;
  230. at_end = true;
  231. }
  232. }
  233. return ret;
  234. }
  235. int FileAccessCompressed::get_buffer(uint8_t *p_dst, int p_length) const {
  236. ERR_FAIL_COND_V(writing, 0);
  237. ERR_FAIL_COND_V(!f, 0);
  238. if (at_end) {
  239. read_eof = true;
  240. return 0;
  241. }
  242. for (int i = 0; i < p_length; i++) {
  243. p_dst[i] = 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. Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode);
  251. read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size;
  252. read_pos = 0;
  253. } else {
  254. read_block--;
  255. at_end = true;
  256. if (i < p_length - 1)
  257. read_eof = true;
  258. return i;
  259. }
  260. }
  261. }
  262. return p_length;
  263. }
  264. Error FileAccessCompressed::get_error() const {
  265. return read_eof ? ERR_FILE_EOF : OK;
  266. }
  267. void FileAccessCompressed::flush() {
  268. ERR_FAIL_COND(!f);
  269. ERR_FAIL_COND(!writing);
  270. // compressed files keep data in memory till close()
  271. }
  272. void FileAccessCompressed::store_8(uint8_t p_dest) {
  273. ERR_FAIL_COND(!f);
  274. ERR_FAIL_COND(!writing);
  275. WRITE_FIT(1);
  276. write_ptr[write_pos++] = p_dest;
  277. }
  278. bool FileAccessCompressed::file_exists(const String &p_name) {
  279. FileAccess *fa = FileAccess::open(p_name, FileAccess::READ);
  280. if (!fa)
  281. return false;
  282. memdelete(fa);
  283. return true;
  284. }
  285. uint64_t FileAccessCompressed::_get_modified_time(const String &p_file) {
  286. if (f)
  287. return f->get_modified_time(p_file);
  288. else
  289. return 0;
  290. }
  291. FileAccessCompressed::FileAccessCompressed() :
  292. cmode(Compression::MODE_ZSTD),
  293. writing(false),
  294. write_ptr(0),
  295. write_buffer_size(0),
  296. write_max(0),
  297. block_size(0),
  298. read_eof(false),
  299. at_end(false),
  300. read_ptr(NULL),
  301. read_block(0),
  302. read_block_count(0),
  303. read_block_size(0),
  304. read_pos(0),
  305. read_total(0),
  306. magic("GCMP"),
  307. f(NULL) {
  308. }
  309. FileAccessCompressed::~FileAccessCompressed() {
  310. if (f)
  311. close();
  312. }