15 #include "zStreamBuf.h"
20 #include "config_express.h"
22 #if !defined(USE_MEMORY_NOWRAPPERS) && !defined(CPPPARSER)
25 do_zlib_alloc(voidpf opaque, uInt items, uInt size) {
26 return PANDA_MALLOC_ARRAY(items * size);
29 do_zlib_free(voidpf opaque, voidpf address) {
30 PANDA_FREE_ARRAY(address);
32 #endif // !USE_MEMORY_NOWRAPPERS
41 _source = (istream *)NULL;
43 _dest = (ostream *)NULL;
47 _buffer = (
char *)PANDA_MALLOC_ARRAY(4096);
48 char *ebuf = _buffer + 4096;
49 setg(_buffer, ebuf, ebuf);
54 setg(base(), ebuf(), ebuf());
69 PANDA_FREE_ARRAY(_buffer);
79 open_read(istream *source,
bool owns_source) {
81 _owns_source = owns_source;
83 _z_source.next_in = Z_NULL;
84 _z_source.avail_in = 0;
85 _z_source.next_out = Z_NULL;
86 _z_source.avail_out = 0;
87 #ifdef USE_MEMORY_NOWRAPPERS
88 _z_source.zalloc = Z_NULL;
89 _z_source.zfree = Z_NULL;
91 _z_source.zalloc = (alloc_func)&do_zlib_alloc;
92 _z_source.zfree = (free_func)&do_zlib_free;
94 _z_source.opaque = Z_NULL;
95 _z_source.msg = (
char *)
"no error message";
97 int result = inflateInit(&_z_source);
99 show_zlib_error(
"inflateInit", result, _z_source);
102 thread_consider_yield();
112 if (_source != (istream *)NULL) {
114 int result = inflateEnd(&_z_source);
116 show_zlib_error(
"inflateEnd", result, _z_source);
118 thread_consider_yield();
122 _owns_source =
false;
124 _source = (istream *)NULL;
134 open_write(ostream *dest,
bool owns_dest,
int compression_level) {
136 _owns_dest = owns_dest;
138 _z_dest.next_in = Z_NULL;
139 _z_dest.avail_in = 0;
140 _z_dest.next_out = Z_NULL;
141 _z_dest.avail_out = 0;
142 #ifdef USE_MEMORY_NOWRAPPERS
143 _z_dest.zalloc = Z_NULL;
144 _z_dest.zfree = Z_NULL;
146 _z_dest.zalloc = (alloc_func)&do_zlib_alloc;
147 _z_dest.zfree = (free_func)&do_zlib_free;
149 _z_dest.opaque = Z_NULL;
150 _z_dest.msg = (
char *)
"no error message";
152 int result = deflateInit(&_z_dest, compression_level);
154 show_zlib_error(
"deflateInit", result, _z_dest);
157 thread_consider_yield();
167 if (_dest != (ostream *)NULL) {
168 size_t n = pptr() - pbase();
169 write_chars(pbase(), n, Z_FINISH);
172 int result = deflateEnd(&_z_dest);
174 show_zlib_error(
"deflateEnd", result, _z_dest);
176 thread_consider_yield();
182 _dest = (ostream *)NULL;
194 size_t n = pptr() - pbase();
196 write_chars(pbase(), n, 0);
203 write_chars(&c, 1, 0);
217 if (_source != (istream *)NULL) {
218 size_t n = egptr() - gptr();
222 if (_dest != (ostream *)NULL) {
223 size_t n = pptr() - pbase();
224 write_chars(pbase(), n, Z_SYNC_FLUSH);
241 if (gptr() >= egptr()) {
242 size_t buffer_size = egptr() - eback();
243 gbump(-(
int)buffer_size);
245 size_t num_bytes = buffer_size;
246 size_t read_count = read_chars(gptr(), buffer_size);
248 if (read_count != num_bytes) {
250 if (read_count == 0) {
256 nassertr(read_count < num_bytes, EOF);
257 size_t delta = num_bytes - read_count;
258 memmove(gptr() + delta, gptr(), read_count);
263 return (
unsigned char)*gptr();
273 read_chars(
char *start,
size_t length) {
274 _z_source.next_out = (Bytef *)start;
275 _z_source.avail_out = length;
277 bool eof = (_source->eof() || _source->fail());
280 while (_z_source.avail_out > 0) {
281 if (_z_source.avail_in == 0 && !eof) {
282 _source->read(decompress_buffer, decompress_buffer_size);
283 size_t read_count = _source->gcount();
284 eof = (read_count == 0 || _source->eof() || _source->fail());
286 _z_source.next_in = (Bytef *)decompress_buffer;
287 _z_source.avail_in = read_count;
289 int result = inflate(&_z_source, flush);
290 thread_consider_yield();
291 size_t bytes_read = length - _z_source.avail_out;
293 if (result == Z_STREAM_END) {
297 }
else if (result == Z_BUF_ERROR && flush == 0) {
303 }
else if (result < 0) {
304 show_zlib_error(
"inflate", result, _z_source);
319 write_chars(
const char *start,
size_t length,
int flush) {
320 static const size_t compress_buffer_size = 4096;
321 char compress_buffer[compress_buffer_size];
323 _z_dest.next_in = (Bytef *)(
char *)start;
324 _z_dest.avail_in = length;
326 _z_dest.next_out = (Bytef *)compress_buffer;
327 _z_dest.avail_out = compress_buffer_size;
329 int result = deflate(&_z_dest, flush);
330 if (result < 0 && result != Z_BUF_ERROR) {
331 show_zlib_error(
"deflate", result, _z_dest);
333 thread_consider_yield();
335 while (_z_dest.avail_in != 0) {
336 if (_z_dest.avail_out != compress_buffer_size) {
337 _dest->write(compress_buffer, compress_buffer_size - _z_dest.avail_out);
338 _z_dest.next_out = (Bytef *)compress_buffer;
339 _z_dest.avail_out = compress_buffer_size;
341 result = deflate(&_z_dest, flush);
343 show_zlib_error(
"deflate", result, _z_dest);
345 thread_consider_yield();
348 while (_z_dest.avail_out != compress_buffer_size) {
349 _dest->write(compress_buffer, compress_buffer_size - _z_dest.avail_out);
350 _z_dest.next_out = (Bytef *)compress_buffer;
351 _z_dest.avail_out = compress_buffer_size;
352 result = deflate(&_z_dest, flush);
353 if (result < 0 && result != Z_BUF_ERROR) {
354 show_zlib_error(
"deflate", result, _z_dest);
356 thread_consider_yield();
366 show_zlib_error(
const char *
function,
int error_code, z_stream &z) {
370 <<
"zlib error in " <<
function <<
": ";
371 switch (error_code) {
373 error_line <<
"Z_OK";
376 error_line <<
"Z_STREAM_END";
379 error_line <<
"Z_NEED_DICT";
382 error_line <<
"Z_ERRNO";
385 error_line <<
"Z_STREAM_ERROR";
388 error_line <<
"Z_DATA_ERROR";
391 error_line <<
"Z_MEM_ERROR";
394 error_line <<
"Z_BUF_ERROR";
396 case Z_VERSION_ERROR:
397 error_line <<
"Z_VERSION_ERROR";
400 error_line << error_code;
402 if (z.msg != (
char *)NULL) {
407 express_cat.warning() << error_line.str() <<
"\n";