X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fstreams-zlib.cpp;h=9e139e3016c31e41272eaa7d1e0e36683da1711e;hb=c51988f347a9a11ea597fee3d5f56a42f4c0dd7b;hp=73106db17109215088e993a18ca42d91ad5fd1be;hpb=6b15695578f07a3f72c4c9475c1a261a3021472a;p=inkscape.git diff --git a/src/streams-zlib.cpp b/src/streams-zlib.cpp index 73106db17..9e139e301 100644 --- a/src/streams-zlib.cpp +++ b/src/streams-zlib.cpp @@ -9,6 +9,7 @@ * Released under GNU LGPL, read the file 'COPYING.LIB' for more information */ +#include #include "streams-zlib.h" namespace Inkscape { @@ -17,9 +18,9 @@ namespace Inkscape { * ZlibBuffer */ -ZlibBuffer::ZlibBuffer(URIHandle& urih) +ZlibBuffer::ZlibBuffer(URIHandle& urih) : _urihandle(&urih), _putsize(BUFSIZE_STREAM), _getsize(BUFSIZE_STREAM) -{ +{ init_inflation(); } @@ -38,15 +39,15 @@ int ZlibBuffer::allocate_buffers() int ZlibBuffer::reallocate_buffers(int new_getsize, int new_putsize) { char *new_buffer = new char[new_getsize + new_putsize]; - + std::memcpy(new_buffer, eback(), _getsize); std::memcpy(new_buffer, eback() + _getsize, _putsize); - - setg(new_buffer, new_buffer + (gptr() - eback()), + + setg(new_buffer, new_buffer + (gptr() - eback()), new_buffer + new_getsize); new_buffer += new_getsize; setp(new_buffer, new_buffer + new_putsize); - + _getsize = new_getsize; _putsize = new_putsize; @@ -57,10 +58,10 @@ int ZlibBuffer::underflow() { if (eback() == 0 && allocate_buffers() == 0) return EOF; - + if (consume_and_inflate() == EOF) return EOF; - + return *(unsigned char *)gptr(); } @@ -72,13 +73,13 @@ int ZlibBuffer::overflow(int c) if (pbase() == 0 && allocate_buffers() == 0) return EOF; - if (pptr() >= epptr() && + if (pptr() >= epptr() && flush_output() == EOF) return EOF; putchar(c); - - if (pptr() >= epptr() && + + if (pptr() >= epptr() && flush_output() == EOF) return EOF; @@ -93,7 +94,7 @@ int ZlibBuffer::consume(guint8 *buf, int nbytes) int ZlibBuffer::do_consume(guint8 *buf, int nbytes) { nbytes = _urihandle->read(buf, nbytes); - + if (nbytes == EOF) return EOF; else if (nbytes == 0) @@ -104,15 +105,18 @@ int ZlibBuffer::do_consume(guint8 *buf, int nbytes) int ZlibBuffer::do_consume_and_inflate(int nbytes) { - guint8 buf[nbytes]; - if (consume(buf, nbytes) == EOF) - return EOF; - - GByteArray *gba = inflate(buf, nbytes); - copy_to_get(gba->data, gba->len); + std::vector buf(nbytes); + + int ret=consume(&buf[0], nbytes); - g_byte_array_free(gba, TRUE); - return 1; + if ( ret != EOF ) { + ret = 1; + GByteArray *gba = inflate(&buf[0], nbytes); + copy_to_get(gba->data, gba->len); + g_byte_array_free(gba, TRUE); + } + + return ret; } int ZlibBuffer::consume_and_inflate() @@ -136,11 +140,11 @@ int ZlibBuffer::flush_output() void ZlibBuffer::init_inflation() throw(ZlibBufferException) { memset(&_zs, 0, sizeof(z_stream)); - + _zs.zalloc = Z_NULL; _zs.zfree = Z_NULL; _zs.opaque = Z_NULL; - + if(inflateInit2(&_zs, -15) != Z_OK) { throw ZlibBufferException(); } @@ -149,7 +153,7 @@ void ZlibBuffer::init_inflation() throw(ZlibBufferException) void ZlibBuffer::reset_inflation() throw(ZlibBufferException) { - if (inflateReset(&_zs) != Z_OK) + if (inflateReset(&_zs) != Z_OK) throw ZlibBufferException(); } @@ -162,10 +166,10 @@ GByteArray *ZlibBuffer::do_inflate(guint8 *data, int nbytes) { GByteArray *gba = g_byte_array_new(); guint8 out_buffer[BUFSIZE_STREAM]; - + _zs.avail_in = 0; guint32 crc = crc32(0, Z_NULL, 0); - + if (!_zs.avail_in) { _zs.avail_in = nbytes; _zs.next_in = (Bytef *)data; @@ -200,10 +204,10 @@ int ZlibBuffer::copy_to_get(guint8 *data, int nbytes) int ZlibBuffer::do_copy_to_get(guint8 *data, int nbytes) { - if (nbytes + gptr() - eback() > _getsize) + if (nbytes + gptr() - eback() > _getsize) reallocate_buffers(nbytes + gptr() - eback() + BUFSIZE_STREAM, _putsize); - + std::memcpy(gptr(), data, nbytes); setg(eback(), gptr(), gptr() + nbytes); return 1;