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