5784a17cf44683e098a6847c9acc096a3b20b0a3
1 /*
2 * IO layer : zlib streambuf
3 *
4 * Authors:
5 * Johan Ceuppens <jceuppen at easynet dot be>
6 *
7 * Copyright (C) 2004 Johan Ceuppens
8 *
9 * Released under GNU LGPL, read the file 'COPYING.LIB' for more information
10 */
12 #include "streams-zlib.h"
14 namespace Inkscape {
16 /**
17 * ZlibBuffer
18 */
20 ZlibBuffer::ZlibBuffer(URIHandle& urih)
21 : _urihandle(&urih), _putsize(BUFSIZE_STREAM), _getsize(BUFSIZE_STREAM)
22 {
23 init_inflation();
24 }
26 int ZlibBuffer::allocate_buffers()
27 {
28 if (!eback()) {
29 char *buf = new char[_getsize + _putsize];
30 setg(buf, buf , buf);
31 buf += _getsize;
32 setp(buf, buf + _putsize);
33 return 1;
34 }
35 return 0;
36 }
38 int ZlibBuffer::reallocate_buffers(int new_getsize, int new_putsize)
39 {
40 char *new_buffer = new char[new_getsize + new_putsize];
42 std::memcpy(new_buffer, eback(), _getsize);
43 std::memcpy(new_buffer, eback() + _getsize, _putsize);
45 setg(new_buffer, new_buffer + (gptr() - eback()),
46 new_buffer + new_getsize);
47 new_buffer += new_getsize;
48 setp(new_buffer, new_buffer + new_putsize);
50 _getsize = new_getsize;
51 _putsize = new_putsize;
53 return 1;
54 }
56 int ZlibBuffer::underflow()
57 {
58 if (eback() == 0 && allocate_buffers() == 0)
59 return EOF;
61 if (consume_and_inflate() == EOF)
62 return EOF;
64 return *(unsigned char *)gptr();
65 }
67 int ZlibBuffer::overflow(int c)
68 {
69 if (c == EOF)
70 return flush_output();
72 if (pbase() == 0 && allocate_buffers() == 0)
73 return EOF;
75 if (pptr() >= epptr() &&
76 flush_output() == EOF)
77 return EOF;
79 putchar(c);
81 if (pptr() >= epptr() &&
82 flush_output() == EOF)
83 return EOF;
85 return c;
86 }
88 int ZlibBuffer::consume(guint8 *buf, int nbytes)
89 {
90 return do_consume(buf, nbytes);
91 }
93 int ZlibBuffer::do_consume(guint8 *buf, int nbytes)
94 {
95 nbytes = _urihandle->read(buf, nbytes);
97 if (nbytes == EOF)
98 return EOF;
99 else if (nbytes == 0)
100 return EOF;
102 return nbytes;
103 }
105 int ZlibBuffer::do_consume_and_inflate(int nbytes)
106 {
107 guint8 *buf=new guint8[nbytes];
109 int ret=consume(buf, nbytes);
111 if ( ret != EOF ) {
112 ret = 1;
113 GByteArray *gba = inflate(buf, nbytes);
114 copy_to_get(gba->data, gba->len);
115 g_byte_array_free(gba, TRUE);
116 }
118 delete [] buf;
119 return ret;
120 }
122 int ZlibBuffer::consume_and_inflate()
123 {
124 return do_consume_and_inflate(BUFSIZE_STREAM);
125 }
127 int ZlibBuffer::flush_output()
128 {
129 if (pptr() <= pbase())
130 return 0;
131 int len = pptr() - pbase();
132 int nbytes = _urihandle->write(pbase(), len);
133 setp(pbase(), pbase() + BUFSIZE_STREAM);
134 if (len == nbytes)
135 return 0;
136 else
137 return EOF;
138 }
140 void ZlibBuffer::init_inflation() throw(ZlibBufferException)
141 {
142 memset(&_zs, 0, sizeof(z_stream));
144 _zs.zalloc = Z_NULL;
145 _zs.zfree = Z_NULL;
146 _zs.opaque = Z_NULL;
148 if(inflateInit2(&_zs, -15) != Z_OK) {
149 throw ZlibBufferException();
150 }
152 }
154 void ZlibBuffer::reset_inflation() throw(ZlibBufferException)
155 {
156 if (inflateReset(&_zs) != Z_OK)
157 throw ZlibBufferException();
158 }
160 GByteArray *ZlibBuffer::inflate(guint8 *in_buffer, int nbytes)
161 {
162 return do_inflate(in_buffer, nbytes);
163 }
165 GByteArray *ZlibBuffer::do_inflate(guint8 *data, int nbytes)
166 {
167 GByteArray *gba = g_byte_array_new();
168 guint8 out_buffer[BUFSIZE_STREAM];
170 _zs.avail_in = 0;
171 guint32 crc = crc32(0, Z_NULL, 0);
173 if (!_zs.avail_in) {
174 _zs.avail_in = nbytes;
175 _zs.next_in = (Bytef *)data;
176 crc = crc32(crc, (Bytef *)data, _zs.avail_in);
177 }
178 do {
179 _zs.next_out = out_buffer;
180 _zs.avail_out = BUFSIZE_STREAM;
182 int ret = ::inflate(&_zs, Z_NO_FLUSH);
183 if (BUFSIZE_STREAM != _zs.avail_out) {
184 unsigned int tmp_len = BUFSIZE_STREAM - _zs.avail_out;
185 g_byte_array_append(gba, out_buffer, tmp_len);
186 }
188 if (ret == Z_STREAM_END) {
189 break;
190 }
191 if (ret != Z_OK) {
192 std::fprintf(stderr, "decompression error %d\n", ret);
193 break;
194 }
195 } while (_zs.avail_in);
197 return gba;
198 }
200 int ZlibBuffer::copy_to_get(guint8 *data, int nbytes)
201 {
202 return do_copy_to_get(data, nbytes);
203 }
205 int ZlibBuffer::do_copy_to_get(guint8 *data, int nbytes)
206 {
207 if (nbytes + gptr() - eback() > _getsize)
208 reallocate_buffers(nbytes + gptr() - eback() + BUFSIZE_STREAM,
209 _putsize);
211 std::memcpy(gptr(), data, nbytes);
212 setg(eback(), gptr(), gptr() + nbytes);
213 return 1;
214 }
216 } // namespace Inkscape
218 /*
219 Local Variables:
220 mode:c++
221 c-file-style:"stroustrup"
222 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
223 indent-tabs-mode:nil
224 fill-column:99
225 End:
226 */
227 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :