18 # include <sys/types.h>
19 # include <sys/stat.h>
20 # include <sys/time.h>
30 typedef short int16_t;
32 typedef long long int64_t;
33 typedef unsigned short uint16_t;
34 typedef unsigned int uint32_t;
35 typedef unsigned long long uint64_t;
38 typedef int32_t VIndex
;
39 typedef int32_t Ordinal
;
51 #include "find_files.h"
53 inline void CHECK_ERR(int err
, const char* msg
)
57 MessageBox(NULL
, "One of the compressed files on disk has errors in it. Restore from backup if you are able.", 0, 0);
64 char file_compressorst::def_ibuff
[FILE_IN_BUFF
];
65 char file_compressorst::def_obuff
[FILE_OUT_BUFF
];
67 char file_compressorst::load_posnull_pointer()
76 char file_compressorst::save_posnull_pointer(void *ptr
)
94 char file_compressorst::write_file(string
&str
)
96 short ln
=str
.length();
97 if(ln
>=10000||ln
<0)ln
=0;
99 if(!write_file(ln
))return 0;
103 if(!write_file((void *)str
.c_str(),sizeof(char)*ln
))return 0;
108 char file_compressorst::read_file(string
&str
)
114 if(!read_file(ln
))return 0;
118 char *strar
=new char[ln
+1];
120 if(!read_file(strar
,sizeof(char)*ln
))
133 char file_compressorst::write_file(void *write_var
,long write_size
)
135 if (!f
.is_open())return 0;
139 f
.write((char *)write_var
,write_size
);
143 //WRITE OUT THE VARIABLE CHUNK BY CHUNK
146 //FLUSH THE BUFFER IF NECESSARY
147 if(in_buffer_amount_loaded
>=in_buffersize
)
149 if(!flush_in_buffer())return 0;
152 //SET THE AMOUNT TO COPY
153 long copy_size
=in_buffersize
-in_buffer_amount_loaded
;
154 if(write_size
<copy_size
)copy_size
=write_size
;
156 //COPY THE NEXT CHUNK INTO THE BUFFER
157 memmove(in_buffer
+in_buffer_amount_loaded
,write_var
,copy_size
);
159 write_var
=((char *)write_var
) + copy_size
;
160 write_size
-=copy_size
;
161 in_buffer_amount_loaded
+=copy_size
;
167 char file_compressorst::flush_in_buffer()
169 if (!f
.is_open())return 0;
170 if(!compressed
)return 1;
171 if(in_buffer_amount_loaded
==0)return 1;//EXTRA CALLS TO FLUSH SHOULDN'T KILL YOU
173 //TARN: adapted from zlib example files
175 //SET UP THE COMPRESSOR
179 c_stream
.zalloc
= (alloc_func
)0;
180 c_stream
.zfree
= (free_func
)0;
181 c_stream
.opaque
= (voidpf
)0;
183 err
= deflateInit(&c_stream
, 9);
184 CHECK_ERR(err
, "deflateInit");
186 c_stream
.next_out
= (Bytef
*)out_buffer
;
187 c_stream
.avail_out
= out_buffersize
;
189 //SEND IT TO THE COMPRESSOR
190 c_stream
.next_in
= (Bytef
*)in_buffer
;
191 c_stream
.avail_in
= in_buffer_amount_loaded
;
193 while (c_stream
.total_in
!= in_buffer_amount_loaded
&& c_stream
.total_out
< out_buffersize
)
195 err
= deflate(&c_stream
, Z_NO_FLUSH
);
196 CHECK_ERR(err
, "deflate");
199 //FINISH UP THE STREAM
202 err
= deflate(&c_stream
, Z_FINISH
);
203 if (err
== Z_STREAM_END
) break;
204 CHECK_ERR(err
, "deflate");
207 err
= deflateEnd(&c_stream
);
208 CHECK_ERR(err
, "deflateEnd");
210 //SAVE THE COMPRESSED BIT AS A GIANT BLOCK
211 if(c_stream
.total_out
>0)
213 long compsize
=byteswap((long)(c_stream
.total_out
));
216 f
.write((char*)&compsize
,sizeof(long));
217 f
.write(out_buffer
,c_stream
.total_out
);
220 in_buffer_position
=0;
221 in_buffer_amount_loaded
=0;
226 char file_compressorst::read_file(void *read_var
,long read_size
)
228 if (!f
.is_open())return 0;
232 f
.read((char *)read_var
,read_size
);
236 //NOW LOAD INTO read_var UNTIL DONE
239 //GET A BUFFER IF NECESSARY
240 if(in_buffer_amount_loaded
==0||
241 in_buffer_position
>=in_buffer_amount_loaded
)
243 if(!load_new_in_buffer())return 0;
246 //BAIL IF STILL NO BUFFER LEFT
247 if(in_buffer_amount_loaded
==0)return 0;
249 //SET THE AMOUNT TO COPY
250 long copy_size
=in_buffer_amount_loaded
-in_buffer_position
;
251 if(read_size
<copy_size
)copy_size
=read_size
;
254 memmove(read_var
,in_buffer
+in_buffer_position
,copy_size
);
256 read_var
=((char *)read_var
) + copy_size
;
257 read_size
-=copy_size
;
258 in_buffer_position
+=copy_size
;
264 char file_compressorst::load_new_in_buffer()
266 if (!f
.is_open())return 0;
267 if(!compressed
)return 1;
269 in_buffer_position
=0;
270 in_buffer_amount_loaded
=0;
272 //LOAD THE BLOCK OF COMPRESSED DATA
273 f
.read((char*)&out_buffer_amount_written
,sizeof(long));
274 out_buffer_amount_written
=byteswap(out_buffer_amount_written
);
275 f
.read(out_buffer
,out_buffer_amount_written
);
279 if(out_buffer_amount_written
>0)
281 //TARN: adapted from zlib example files
284 z_stream d_stream
; // decompression stream
286 d_stream
.zalloc
= (alloc_func
)0;
287 d_stream
.zfree
= (free_func
)0;
288 d_stream
.opaque
= (voidpf
)0;
290 d_stream
.next_in
= (Bytef
*)out_buffer
;
291 d_stream
.avail_in
= out_buffer_amount_written
;
293 err
= inflateInit(&d_stream
);
294 CHECK_ERR(err
, "inflateInit");
296 d_stream
.next_out
= (Bytef
*)in_buffer
;
297 d_stream
.avail_out
= in_buffersize
;
299 while (d_stream
.total_out
< in_buffersize
&& d_stream
.total_in
< out_buffer_amount_written
)
301 //d_stream.avail_in = d_stream.avail_out = 1; // force small buffers
302 err
= inflate(&d_stream
, Z_NO_FLUSH
);
304 if (err
== Z_STREAM_END
) break;
305 CHECK_ERR(err
, "inflate");
308 err
= inflateEnd(&d_stream
);
309 CHECK_ERR(err
, "inflateEnd");
311 in_buffer_amount_loaded
=d_stream
.total_out
;
318 void file_compressorst::close_file()
326 char file_compressorst::open_file(const string
&filename
,char existing_only
)
328 if(filename
.empty())return 0;
331 in_buffer_amount_loaded
=0;
332 in_buffer_position
=0;
333 out_buffer_amount_written
=0;
336 //CHECK IF FILE ALREADY EXISTS
337 f.open(filename.c_str(), fstream::in);
338 char file_exists = f.is_open();
339 if (file_exists)f.close();
342 if(!existing_only || file_exists)
343 f.open(filename.c_str(), fstream::in | fstream::out | fstream::binary);
345 f.open(filename.c_str(), fstream::in | fstream::out | fstream::binary);
347 //if(existing_only)h=CreateFile(filename.c_str(),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
348 //else h=CreateFile(filename.c_str(),GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
350 if(f
.is_open())f
.close();
354 f
.open(filename
.c_str(), fstream::in
| fstream::out
| fstream::binary
);
356 f
.open(filename
.c_str(), fstream::in
| fstream::out
| fstream::binary
| fstream::trunc
);
358 if (f
.is_open())return 1;
362 file_compressorst::file_compressorst(char *new_in_buffer
,long new_in_buffersize
,
363 char *new_out_buffer
,long new_out_buffersize
)
367 in_buffer
=new_in_buffer
;
368 in_buffersize
=new_in_buffersize
;
369 in_buffer_amount_loaded
=0;
370 in_buffer_position
=0;
372 out_buffer
=new_out_buffer
;
373 out_buffersize
=new_out_buffersize
;
374 out_buffer_amount_written
=0;
379 file_compressorst::file_compressorst()
384 in_buffersize
=FILE_IN_BUFF
;
385 in_buffer_amount_loaded
=0;
386 in_buffer_position
=0;
388 out_buffer
=def_obuff
;
389 out_buffersize
=FILE_OUT_BUFF
;
390 out_buffer_amount_written
=0;
395 void copy_file(const string
&src
,const string
&dst
)
397 std::ifstream
in_stream(src
.c_str(),std::ios_base::binary
);
398 std::ofstream
out_stream(dst
.c_str(),std::ios_base::binary
);
399 if(in_stream
.is_open()&&out_stream
.is_open())
401 out_stream
<<in_stream
.rdbuf();
407 void replace_file(const string
&src
, const string
&dst
) {
411 rename(src
.c_str(), dst
.c_str());