file_access_compressed.cpp 12 KB

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