file_access_compressed.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*************************************************************************/
  2. /* file_access_compressed.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "file_access_compressed.h"
  30. #include "print_string.h"
  31. void FileAccessCompressed::configure(const String& p_magic, Compression::Mode p_mode, int p_block_size) {
  32. magic=p_magic.ascii().get_data();
  33. if (magic.length()>4)
  34. magic=magic.substr(0,4);
  35. else {
  36. while(magic.length()<4)
  37. magic+=" ";
  38. }
  39. cmode=p_mode;
  40. block_size=p_block_size;
  41. }
  42. #define WRITE_FIT(m_bytes) \
  43. {\
  44. if (write_pos+(m_bytes) > write_max) {\
  45. write_max=write_pos+(m_bytes);\
  46. }\
  47. if (write_max > write_buffer_size) {\
  48. write_buffer_size = nearest_power_of_2( write_max );\
  49. buffer.resize(write_buffer_size);\
  50. write_ptr=buffer.ptr();\
  51. }\
  52. }
  53. Error FileAccessCompressed::open_after_magic(FileAccess *p_base) {
  54. f=p_base;
  55. cmode=(Compression::Mode)f->get_32();
  56. block_size=f->get_32();
  57. read_total=f->get_32();
  58. int bc = (read_total/block_size)+1;
  59. int acc_ofs=f->get_pos()+bc*4;
  60. int max_bs=0;
  61. for(int 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.ptr();
  72. f->get_buffer(comp_buffer.ptr(),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. Compression::decompress(buffer.ptr(),read_block_size,comp_buffer.ptr(),read_blocks[0].csize,cmode);
  78. read_block=0;
  79. read_pos=0;
  80. return OK;
  81. }
  82. Error FileAccessCompressed::_open(const String& p_path, int p_mode_flags){
  83. ERR_FAIL_COND_V(p_mode_flags==READ_WRITE,ERR_UNAVAILABLE);
  84. if (f)
  85. close();
  86. Error err;
  87. f = FileAccess::open(p_path,p_mode_flags,&err);
  88. if (err!=OK) {
  89. //not openable
  90. f=NULL;
  91. return err;
  92. }
  93. if (p_mode_flags&WRITE) {
  94. buffer.clear();
  95. writing=true;
  96. write_pos=0;
  97. write_buffer_size=256;
  98. buffer.resize(256);
  99. write_max=0;
  100. write_ptr=buffer.ptr();
  101. //don't store anything else unless it's done saving!
  102. } else {
  103. char rmagic[5];
  104. f->get_buffer((uint8_t*)rmagic,4);
  105. rmagic[4]=0;
  106. if (magic!=rmagic) {
  107. memdelete(f);
  108. f=NULL;
  109. return ERR_FILE_UNRECOGNIZED;
  110. }
  111. open_after_magic(f);
  112. }
  113. return OK;
  114. }
  115. void FileAccessCompressed::close(){
  116. if (!f)
  117. return;
  118. if (writing) {
  119. //save block table and all compressed blocks
  120. CharString mgc = magic.utf8();
  121. f->store_buffer((const uint8_t*)mgc.get_data(),mgc.length()); //write header 4
  122. f->store_32(cmode); //write compression mode 4
  123. f->store_32(block_size); //write block size 4
  124. f->store_32(write_max); //max amount of data written 4
  125. int bc=(write_max/block_size)+1;
  126. for(int i=0;i<bc;i++) {
  127. f->store_32(0); //compressed sizes, will update later
  128. }
  129. Vector<int> block_sizes;
  130. for(int i=0;i<bc;i++) {
  131. int bl = i==(bc-1) ? write_max % block_size : block_size;
  132. uint8_t *bp = &write_ptr[i*block_size];
  133. Vector<uint8_t> cblock;
  134. cblock.resize(Compression::get_max_compressed_buffer_size(bl,cmode));
  135. int s = Compression::compress(cblock.ptr(),bp,bl,cmode);
  136. f->store_buffer(cblock.ptr(),s);
  137. block_sizes.push_back(s);
  138. }
  139. f->seek(16); //ok write block sizes
  140. for(int i=0;i<bc;i++)
  141. f->store_32(block_sizes[i]);
  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. memdelete(f);
  151. f=NULL;
  152. }
  153. bool FileAccessCompressed::is_open() const{
  154. return f!=NULL;
  155. }
  156. void FileAccessCompressed::seek(size_t p_position){
  157. ERR_FAIL_COND(!f);
  158. if (writing) {
  159. ERR_FAIL_COND(p_position>write_max);
  160. write_pos=p_position;
  161. } else {
  162. ERR_FAIL_COND(p_position>read_total);
  163. if (p_position==read_total) {
  164. at_end=true;
  165. } else {
  166. int block_idx = p_position/block_size;
  167. if (block_idx!=read_block) {
  168. read_block=block_idx;
  169. f->seek(read_blocks[read_block].offset);
  170. f->get_buffer(comp_buffer.ptr(),read_blocks[read_block].csize);
  171. Compression::decompress(buffer.ptr(),read_blocks.size()==1?read_total:block_size,comp_buffer.ptr(),read_blocks[read_block].csize,cmode);
  172. read_block_size=read_block==read_block_count-1?read_total%block_size:block_size;
  173. }
  174. read_pos=p_position%block_size;
  175. }
  176. }
  177. }
  178. void FileAccessCompressed::seek_end(int64_t p_position){
  179. ERR_FAIL_COND(!f);
  180. if (writing) {
  181. seek(write_max+p_position);
  182. } else {
  183. seek(read_total+p_position);
  184. }
  185. }
  186. size_t FileAccessCompressed::get_pos() const{
  187. ERR_FAIL_COND_V(!f,0);
  188. if (writing) {
  189. return write_pos;
  190. } else {
  191. return read_block*block_size+read_pos;
  192. }
  193. }
  194. size_t FileAccessCompressed::get_len() const{
  195. ERR_FAIL_COND_V(!f,0);
  196. if (writing) {
  197. return write_max;
  198. } else {
  199. return read_total;
  200. }
  201. }
  202. bool FileAccessCompressed::eof_reached() const{
  203. ERR_FAIL_COND_V(!f,false);
  204. if (writing) {
  205. return false;
  206. } else {
  207. return read_eof;
  208. }
  209. }
  210. uint8_t FileAccessCompressed::get_8() const{
  211. ERR_FAIL_COND_V(writing,0);
  212. ERR_FAIL_COND_V(!f,0);
  213. if (at_end) {
  214. read_eof=true;
  215. return 0;
  216. }
  217. uint8_t ret = read_ptr[read_pos];
  218. read_pos++;
  219. if (read_pos>=read_block_size) {
  220. read_block++;
  221. if (read_block<read_block_count) {
  222. //read another block of compressed data
  223. f->get_buffer(comp_buffer.ptr(),read_blocks[read_block].csize);
  224. Compression::decompress(buffer.ptr(),read_blocks.size()==1?read_total:block_size,comp_buffer.ptr(),read_blocks[read_block].csize,cmode);
  225. read_block_size=read_block==read_block_count-1?read_total%block_size:block_size;
  226. read_pos=0;
  227. } else {
  228. read_block--;
  229. at_end=true;
  230. ret =0;
  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.ptr(),read_blocks[read_block].csize);
  250. Compression::decompress(buffer.ptr(),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::store_8(uint8_t p_dest){
  268. ERR_FAIL_COND(!f);
  269. ERR_FAIL_COND(!writing);
  270. WRITE_FIT(1);
  271. write_ptr[write_pos++]=p_dest;
  272. }
  273. bool FileAccessCompressed::file_exists(const String& p_name){
  274. FileAccess *fa = FileAccess::open(p_name,FileAccess::READ);
  275. if (!fa)
  276. return false;
  277. memdelete(fa);
  278. return true;
  279. }
  280. uint64_t FileAccessCompressed::_get_modified_time(const String& p_file) {
  281. if (f)
  282. return f->get_modified_time(p_file);
  283. else
  284. return 0;
  285. }
  286. FileAccessCompressed::FileAccessCompressed() {
  287. f=NULL;
  288. magic="GCMP";
  289. block_size=16384;
  290. cmode=Compression::MODE_DEFLATE;
  291. writing=false;
  292. write_ptr=0;
  293. write_buffer_size=0;
  294. write_max=0;
  295. block_size=0;
  296. read_eof=false;
  297. at_end=false;
  298. read_total=0;
  299. read_ptr=NULL;
  300. read_block=0;
  301. read_block_count=0;
  302. read_block_size=0;
  303. read_pos=0;
  304. }
  305. FileAccessCompressed::~FileAccessCompressed(){
  306. if (f)
  307. close();
  308. }