diff --git a/src/streams-zlib.cpp b/src/streams-zlib.cpp
index 73106db17109215088e993a18ca42d91ad5fd1be..9e139e3016c31e41272eaa7d1e0e36683da1711e 100644 (file)
--- a/src/streams-zlib.cpp
+++ b/src/streams-zlib.cpp
* Released under GNU LGPL, read the file 'COPYING.LIB' for more information
*/
+#include <vector>
#include "streams-zlib.h"
namespace Inkscape {
* ZlibBuffer
*/
-ZlibBuffer::ZlibBuffer(URIHandle& urih)
+ZlibBuffer::ZlibBuffer(URIHandle& urih)
: _urihandle(&urih), _putsize(BUFSIZE_STREAM), _getsize(BUFSIZE_STREAM)
-{
+{
init_inflation();
}
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;
{
if (eback() == 0 && allocate_buffers() == 0)
return EOF;
-
+
if (consume_and_inflate() == EOF)
return EOF;
-
+
return *(unsigned char *)gptr();
}
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;
int ZlibBuffer::do_consume(guint8 *buf, int nbytes)
{
nbytes = _urihandle->read(buf, nbytes);
-
+
if (nbytes == EOF)
return EOF;
else if (nbytes == 0)
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<guint8> 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()
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();
}
void ZlibBuffer::reset_inflation() throw(ZlibBufferException)
{
- if (inflateReset(&_zs) != Z_OK)
+ if (inflateReset(&_zs) != Z_OK)
throw ZlibBufferException();
}
{
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;
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;