Cross Reference: /inkscape/src/streams-zlib.cpp
streams-zlib.cpp revision 5db8a66d7863bc0af7dfdc71621201a1120a37b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* IO layer : zlib streambuf
*
* Authors:
* Johan Ceuppens <jceuppen at easynet dot be>
*
* Copyright (C) 2004 Johan Ceuppens
*
* 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)
: _urihandle(&urih), _putsize(BUFSIZE_STREAM), _getsize(BUFSIZE_STREAM)
{
init_inflation();
}
int ZlibBuffer::allocate_buffers()
{
if (!eback()) {
char *buf = new char[_getsize + _putsize];
setg(buf, buf , buf);
buf += _getsize;
setp(buf, buf + _putsize);
return 1;
}
return 0;
}
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()),
new_buffer + new_getsize);
new_buffer += new_getsize;
setp(new_buffer, new_buffer + new_putsize);
_getsize = new_getsize;
_putsize = new_putsize;
return 1;
}
int ZlibBuffer::underflow()
{
if (eback() == 0 && allocate_buffers() == 0)
return EOF;
if (consume_and_inflate() == EOF)
return EOF;
return *(unsigned char *)gptr();
}
int ZlibBuffer::overflow(int c)
{
if (c == EOF)
return flush_output();
if (pbase() == 0 && allocate_buffers() == 0)
return EOF;
if (pptr() >= epptr() &&
flush_output() == EOF)
return EOF;
putchar(c);
if (pptr() >= epptr() &&
flush_output() == EOF)
return EOF;
return c;
}
int ZlibBuffer::consume(guint8 *buf, int nbytes)
{
return do_consume(buf, nbytes);
}
int ZlibBuffer::do_consume(guint8 *buf, int nbytes)
{
nbytes = _urihandle->read(buf, nbytes);
if (nbytes == EOF)
return EOF;
else if (nbytes == 0)
return EOF;
return nbytes;
}
int ZlibBuffer::do_consume_and_inflate(int nbytes)
{
std::vector<guint8> buf(nbytes);
int ret=consume(&buf[0], nbytes);
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()
{
return do_consume_and_inflate(BUFSIZE_STREAM);
}
int ZlibBuffer::flush_output()
{
if (pptr() <= pbase())
return 0;
int len = pptr() - pbase();
int nbytes = _urihandle->write(pbase(), len);
setp(pbase(), pbase() + BUFSIZE_STREAM);
if (len == nbytes)
return 0;
else
return EOF;
}
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)
throw ZlibBufferException();
}
GByteArray *ZlibBuffer::inflate(guint8 *in_buffer, int nbytes)
{
return do_inflate(in_buffer, nbytes);
}
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;
crc = crc32(crc, (Bytef *)data, _zs.avail_in);
}
do {
_zs.next_out = out_buffer;
_zs.avail_out = BUFSIZE_STREAM;
int ret = ::inflate(&_zs, Z_NO_FLUSH);
if (BUFSIZE_STREAM != _zs.avail_out) {
unsigned int tmp_len = BUFSIZE_STREAM - _zs.avail_out;
g_byte_array_append(gba, out_buffer, tmp_len);
}
if (ret == Z_STREAM_END) {
break;
}
if (ret != Z_OK) {
std::fprintf(stderr, "decompression error %d\n", ret);
break;
}
} while (_zs.avail_in);
return gba;
}
int ZlibBuffer::copy_to_get(guint8 *data, int nbytes)
{
return do_copy_to_get(data, nbytes);
}
int ZlibBuffer::do_copy_to_get(guint8 *data, int nbytes)
{
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;
}
} // namespace Inkscape
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :