]>
Commit | Line | Data |
---|---|---|
0915d0b2 | 1 | ////////////////////////////////////////////////////////////////////////////// |
88a7a4e1 | 2 | // Name: src/common/zstream.cpp |
79c3e0e1 GL |
3 | // Purpose: Compressed stream classes |
4 | // Author: Guilhem Lavaux | |
0915d0b2 | 5 | // Modified by: Mike Wetherell |
79c3e0e1 GL |
6 | // Created: 11/07/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
65571936 | 9 | // Licence: wxWindows licence |
79c3e0e1 | 10 | ///////////////////////////////////////////////////////////////////////////// |
ce4169a4 | 11 | |
79c3e0e1 GL |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
ac57418f | 14 | |
ce4169a4 | 15 | #ifdef __BORLANDC__ |
88a7a4e1 | 16 | #pragma hdrstop |
ce4169a4 RR |
17 | #endif |
18 | ||
ce4169a4 | 19 | #if wxUSE_ZLIB && wxUSE_STREAMS |
ac57418f | 20 | |
ce4169a4 | 21 | #include "wx/zstream.h" |
88a7a4e1 WS |
22 | |
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/intl.h" | |
e4db172a | 25 | #include "wx/log.h" |
de6185e2 | 26 | #include "wx/utils.h" |
88a7a4e1 WS |
27 | #endif |
28 | ||
d1af991f | 29 | |
f6bcfd97 BP |
30 | // normally, the compiler options should contain -I../zlib, but it is |
31 | // apparently not the case for all MSW makefiles and so, unless we use | |
32 | // configure (which defines __WX_SETUP_H__) or it is explicitly overridden by | |
33 | // the user (who can define wxUSE_ZLIB_H_IN_PATH), we hardcode the path here | |
34 | #if defined(__WXMSW__) && !defined(__WX_SETUP_H__) && !defined(wxUSE_ZLIB_H_IN_PATH) | |
de6185e2 | 35 | #include "../zlib/zlib.h" |
d1af991f | 36 | #else |
de6185e2 | 37 | #include "zlib.h" |
d1af991f | 38 | #endif |
79c3e0e1 | 39 | |
0915d0b2 | 40 | enum { |
4c68a102 VS |
41 | ZSTREAM_BUFFER_SIZE = 16384, |
42 | ZSTREAM_GZIP = 0x10, // gzip header | |
43 | ZSTREAM_AUTO = 0x20 // auto detect between gzip and zlib | |
0915d0b2 | 44 | }; |
6d44bf31 | 45 | |
55420742 MW |
46 | |
47 | ///////////////////////////////////////////////////////////////////////////// | |
48 | // Zlib Class factory | |
49 | ||
50 | IMPLEMENT_DYNAMIC_CLASS(wxZlibClassFactory, wxFilterClassFactory) | |
51 | ||
52 | static wxZlibClassFactory g_wxZlibClassFactory; | |
53 | ||
54 | wxZlibClassFactory::wxZlibClassFactory() | |
55 | { | |
56 | if (this == &g_wxZlibClassFactory) | |
57 | PushFront(); | |
58 | } | |
59 | ||
60 | const wxChar * const * | |
61 | wxZlibClassFactory::GetProtocols(wxStreamProtocolType type) const | |
62 | { | |
63 | static const wxChar *mimes[] = { _T("application/x-deflate"), NULL }; | |
64 | static const wxChar *encs[] = { _T("deflate"), NULL }; | |
65 | static const wxChar *empty[] = { NULL }; | |
66 | ||
67 | switch (type) { | |
68 | case wxSTREAM_MIMETYPE: return mimes; | |
69 | case wxSTREAM_ENCODING: return encs; | |
70 | default: return empty; | |
71 | } | |
72 | } | |
73 | ||
74 | ||
75 | ///////////////////////////////////////////////////////////////////////////// | |
76 | // Gzip Class factory | |
77 | ||
78 | IMPLEMENT_DYNAMIC_CLASS(wxGzipClassFactory, wxFilterClassFactory) | |
79 | ||
80 | static wxGzipClassFactory g_wxGzipClassFactory; | |
81 | ||
82 | wxGzipClassFactory::wxGzipClassFactory() | |
83 | { | |
84 | if (this == &g_wxGzipClassFactory && wxZlibInputStream::CanHandleGZip()) | |
85 | PushFront(); | |
86 | } | |
87 | ||
88 | const wxChar * const * | |
89 | wxGzipClassFactory::GetProtocols(wxStreamProtocolType type) const | |
90 | { | |
91 | static const wxChar *protos[] = | |
92 | { _T("gzip"), NULL }; | |
93 | static const wxChar *mimes[] = | |
94 | { _T("application/gzip"), _T("application/x-gzip"), NULL }; | |
95 | static const wxChar *encs[] = | |
96 | { _T("gzip"), NULL }; | |
97 | static const wxChar *exts[] = | |
98 | { _T(".gz"), _T(".gzip"), NULL }; | |
99 | static const wxChar *empty[] = | |
100 | { NULL }; | |
101 | ||
102 | switch (type) { | |
489a164c MW |
103 | case wxSTREAM_PROTOCOL: return protos; |
104 | case wxSTREAM_MIMETYPE: return mimes; | |
105 | case wxSTREAM_ENCODING: return encs; | |
106 | case wxSTREAM_FILEEXT: return exts; | |
107 | default: return empty; | |
55420742 MW |
108 | } |
109 | } | |
110 | ||
111 | ||
79c3e0e1 GL |
112 | ////////////////////// |
113 | // wxZlibInputStream | |
114 | ////////////////////// | |
115 | ||
0915d0b2 | 116 | wxZlibInputStream::wxZlibInputStream(wxInputStream& stream, int flags) |
79c3e0e1 | 117 | : wxFilterInputStream(stream) |
55420742 MW |
118 | { |
119 | Init(flags); | |
120 | } | |
121 | ||
122 | wxZlibInputStream::wxZlibInputStream(wxInputStream *stream, int flags) | |
123 | : wxFilterInputStream(stream) | |
124 | { | |
125 | Init(flags); | |
126 | } | |
127 | ||
128 | void wxZlibInputStream::Init(int flags) | |
79c3e0e1 | 129 | { |
0915d0b2 VZ |
130 | m_inflate = NULL; |
131 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
132 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
133 | m_pos = 0; | |
6d44bf31 | 134 | |
4c68a102 VS |
135 | // if gzip is asked for but not supported... |
136 | if ((flags == wxZLIB_GZIP || flags == wxZLIB_AUTO) && !CanHandleGZip()) { | |
137 | if (flags == wxZLIB_AUTO) { | |
138 | // an error will come later if the input turns out not to be a zlib | |
139 | flags = wxZLIB_ZLIB; | |
140 | } | |
141 | else { | |
142 | wxLogError(_("Gzip not supported by this version of zlib")); | |
143 | m_lasterror = wxSTREAM_READ_ERROR; | |
144 | return; | |
145 | } | |
146 | } | |
147 | ||
0915d0b2 VZ |
148 | if (m_z_buffer) { |
149 | m_inflate = new z_stream_s; | |
79c3e0e1 | 150 | |
0915d0b2 | 151 | if (m_inflate) { |
5fc01d13 | 152 | memset(m_inflate, 0, sizeof(z_stream_s)); |
79c3e0e1 | 153 | |
4c68a102 VS |
154 | // see zlib.h for documentation on windowBits |
155 | int windowBits = MAX_WBITS; | |
156 | switch (flags) { | |
157 | case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break; | |
158 | case wxZLIB_ZLIB: windowBits = MAX_WBITS; break; | |
159 | case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break; | |
160 | case wxZLIB_AUTO: windowBits = MAX_WBITS | ZSTREAM_AUTO; break; | |
161 | default: wxFAIL_MSG(wxT("Invalid zlib flag")); | |
162 | } | |
6d44bf31 | 163 | |
4c68a102 | 164 | if (inflateInit2(m_inflate, windowBits) == Z_OK) |
0915d0b2 VZ |
165 | return; |
166 | } | |
167 | } | |
168 | ||
169 | wxLogError(_("Can't initialize zlib inflate stream.")); | |
170 | m_lasterror = wxSTREAM_READ_ERROR; | |
79c3e0e1 GL |
171 | } |
172 | ||
173 | wxZlibInputStream::~wxZlibInputStream() | |
174 | { | |
856d2e52 GL |
175 | inflateEnd(m_inflate); |
176 | delete m_inflate; | |
45805ba3 VZ |
177 | |
178 | delete [] m_z_buffer; | |
79c3e0e1 GL |
179 | } |
180 | ||
75ed1d15 | 181 | size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size) |
79c3e0e1 | 182 | { |
0915d0b2 VZ |
183 | wxASSERT_MSG(m_inflate && m_z_buffer, wxT("Inflate stream not open")); |
184 | ||
185 | if (!m_inflate || !m_z_buffer) | |
186 | m_lasterror = wxSTREAM_READ_ERROR; | |
187 | if (!IsOk() || !size) | |
188 | return 0; | |
79c3e0e1 | 189 | |
0915d0b2 | 190 | int err = Z_OK; |
856d2e52 GL |
191 | m_inflate->next_out = (unsigned char *)buffer; |
192 | m_inflate->avail_out = size; | |
79c3e0e1 | 193 | |
0915d0b2 | 194 | while (err == Z_OK && m_inflate->avail_out > 0) { |
4c68a102 | 195 | if (m_inflate->avail_in == 0 && m_parent_i_stream->IsOk()) { |
0915d0b2 | 196 | m_parent_i_stream->Read(m_z_buffer, m_z_size); |
856d2e52 GL |
197 | m_inflate->next_in = m_z_buffer; |
198 | m_inflate->avail_in = m_parent_i_stream->LastRead(); | |
4c68a102 VS |
199 | } |
200 | err = inflate(m_inflate, Z_SYNC_FLUSH); | |
201 | } | |
6d44bf31 | 202 | |
4c68a102 VS |
203 | switch (err) { |
204 | case Z_OK: | |
0915d0b2 | 205 | break; |
4c68a102 VS |
206 | |
207 | case Z_STREAM_END: | |
8e50d1ad MW |
208 | if (m_inflate->avail_out) { |
209 | // Unread any data taken from past the end of the deflate stream, so that | |
210 | // any additional data can be read from the underlying stream (the crc | |
211 | // in a gzip for example) | |
212 | if (m_inflate->avail_in) { | |
abffc1ff | 213 | m_parent_i_stream->Reset(); |
8e50d1ad MW |
214 | m_parent_i_stream->Ungetch(m_inflate->next_in, m_inflate->avail_in); |
215 | m_inflate->avail_in = 0; | |
216 | } | |
217 | m_lasterror = wxSTREAM_EOF; | |
777fd647 | 218 | } |
4c68a102 | 219 | break; |
f6bcfd97 | 220 | |
4c68a102 VS |
221 | case Z_BUF_ERROR: |
222 | // Indicates that zlib was expecting more data, but the parent stream | |
223 | // has none. Other than Eof the error will have been already reported | |
224 | // by the parent strean, | |
225 | m_lasterror = wxSTREAM_READ_ERROR; | |
226 | if (m_parent_i_stream->Eof()) | |
4c68a102 VS |
227 | wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream.")); |
228 | break; | |
229 | ||
230 | default: | |
231 | wxString msg(m_inflate->msg, *wxConvCurrent); | |
232 | if (!msg) | |
2a1f999f | 233 | msg = wxString::Format(_("zlib error %d"), err); |
4c68a102 VS |
234 | wxLogError(_("Can't read from inflate stream: %s"), msg.c_str()); |
235 | m_lasterror = wxSTREAM_READ_ERROR; | |
79c3e0e1 GL |
236 | } |
237 | ||
0915d0b2 VZ |
238 | size -= m_inflate->avail_out; |
239 | m_pos += size; | |
240 | return size; | |
79c3e0e1 GL |
241 | } |
242 | ||
4c68a102 VS |
243 | /* static */ bool wxZlibInputStream::CanHandleGZip() |
244 | { | |
245 | const char *dot = strchr(zlibVersion(), '.'); | |
246 | int major = atoi(zlibVersion()); | |
247 | int minor = dot ? atoi(dot + 1) : 0; | |
248 | return major > 1 || (major == 1 && minor >= 2); | |
249 | } | |
250 | ||
51acf83b VZ |
251 | bool wxZlibInputStream::SetDictionary(const char *data, const size_t datalen) |
252 | { | |
253 | return (inflateSetDictionary(m_inflate, (Bytef*)data, datalen) == Z_OK); | |
254 | } | |
255 | ||
256 | bool wxZlibInputStream::SetDictionary(const wxMemoryBuffer &buf) | |
257 | { | |
258 | return SetDictionary((char*)buf.GetData(), buf.GetDataLen()); | |
259 | } | |
260 | ||
4c68a102 | 261 | |
79c3e0e1 GL |
262 | ////////////////////// |
263 | // wxZlibOutputStream | |
264 | ////////////////////// | |
265 | ||
0915d0b2 VZ |
266 | wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream, |
267 | int level, | |
268 | int flags) | |
79c3e0e1 | 269 | : wxFilterOutputStream(stream) |
55420742 MW |
270 | { |
271 | Init(level, flags); | |
272 | } | |
273 | ||
274 | wxZlibOutputStream::wxZlibOutputStream(wxOutputStream *stream, | |
275 | int level, | |
276 | int flags) | |
277 | : wxFilterOutputStream(stream) | |
278 | { | |
279 | Init(level, flags); | |
280 | } | |
281 | ||
282 | void wxZlibOutputStream::Init(int level, int flags) | |
79c3e0e1 | 283 | { |
0915d0b2 VZ |
284 | m_deflate = NULL; |
285 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
286 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
287 | m_pos = 0; | |
79c3e0e1 | 288 | |
af850422 VZ |
289 | if ( level == -1 ) |
290 | { | |
0915d0b2 | 291 | level = Z_DEFAULT_COMPRESSION; |
af850422 VZ |
292 | } |
293 | else | |
294 | { | |
295 | wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!")); | |
296 | } | |
5824f314 | 297 | |
4c68a102 VS |
298 | // if gzip is asked for but not supported... |
299 | if (flags == wxZLIB_GZIP && !CanHandleGZip()) { | |
300 | wxLogError(_("Gzip not supported by this version of zlib")); | |
301 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
302 | return; | |
303 | } | |
6d44bf31 | 304 | |
4c68a102 VS |
305 | if (m_z_buffer) { |
306 | m_deflate = new z_stream_s; | |
0915d0b2 | 307 | |
4c68a102 VS |
308 | if (m_deflate) { |
309 | memset(m_deflate, 0, sizeof(z_stream_s)); | |
310 | m_deflate->next_out = m_z_buffer; | |
311 | m_deflate->avail_out = m_z_size; | |
cab1a605 | 312 | |
4c68a102 VS |
313 | // see zlib.h for documentation on windowBits |
314 | int windowBits = MAX_WBITS; | |
315 | switch (flags) { | |
316 | case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break; | |
317 | case wxZLIB_ZLIB: windowBits = MAX_WBITS; break; | |
318 | case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break; | |
319 | default: wxFAIL_MSG(wxT("Invalid zlib flag")); | |
320 | } | |
321 | ||
cab1a605 | 322 | if (deflateInit2(m_deflate, level, Z_DEFLATED, windowBits, |
4c68a102 VS |
323 | 8, Z_DEFAULT_STRATEGY) == Z_OK) |
324 | return; | |
325 | } | |
0915d0b2 VZ |
326 | } |
327 | ||
328 | wxLogError(_("Can't initialize zlib deflate stream.")); | |
329 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
79c3e0e1 GL |
330 | } |
331 | ||
8f0ff178 RN |
332 | bool wxZlibOutputStream::Close() |
333 | { | |
334 | DoFlush(true); | |
335 | deflateEnd(m_deflate); | |
336 | delete m_deflate; | |
6d44bf31 | 337 | |
8f0ff178 RN |
338 | m_deflate = NULL; |
339 | delete[] m_z_buffer; | |
340 | m_z_buffer = NULL; | |
55420742 MW |
341 | |
342 | return wxFilterOutputStream::Close() && IsOk(); | |
8f0ff178 | 343 | } |
79c3e0e1 | 344 | |
0915d0b2 | 345 | void wxZlibOutputStream::DoFlush(bool final) |
6d44bf31 | 346 | { |
0915d0b2 VZ |
347 | if (!m_deflate || !m_z_buffer) |
348 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
349 | if (!IsOk()) | |
6d44bf31 | 350 | return; |
6d44bf31 | 351 | |
0915d0b2 VZ |
352 | int err = Z_OK; |
353 | bool done = false; | |
babd4308 | 354 | |
0915d0b2 VZ |
355 | while (err == Z_OK || err == Z_STREAM_END) { |
356 | size_t len = m_z_size - m_deflate->avail_out; | |
357 | if (len) { | |
358 | if (m_parent_o_stream->Write(m_z_buffer, len).LastWrite() != len) { | |
359 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
360 | wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream")); | |
361 | break; | |
362 | } | |
363 | m_deflate->next_out = m_z_buffer; | |
364 | m_deflate->avail_out = m_z_size; | |
365 | } | |
366 | ||
367 | if (done) | |
368 | break; | |
369 | err = deflate(m_deflate, final ? Z_FINISH : Z_FULL_FLUSH); | |
370 | done = m_deflate->avail_out != 0 || err == Z_STREAM_END; | |
371 | } | |
6d44bf31 GL |
372 | } |
373 | ||
75ed1d15 | 374 | size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size) |
79c3e0e1 | 375 | { |
0915d0b2 | 376 | wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open")); |
79c3e0e1 | 377 | |
0915d0b2 | 378 | if (!m_deflate || !m_z_buffer) |
10793ebf | 379 | { |
517cce71 | 380 | // notice that this will make IsOk() test just below return false |
0915d0b2 | 381 | m_lasterror = wxSTREAM_WRITE_ERROR; |
10793ebf VZ |
382 | } |
383 | ||
0915d0b2 VZ |
384 | if (!IsOk() || !size) |
385 | return 0; | |
386 | ||
387 | int err = Z_OK; | |
856d2e52 GL |
388 | m_deflate->next_in = (unsigned char *)buffer; |
389 | m_deflate->avail_in = size; | |
79c3e0e1 | 390 | |
0915d0b2 | 391 | while (err == Z_OK && m_deflate->avail_in > 0) { |
856d2e52 | 392 | if (m_deflate->avail_out == 0) { |
6d44bf31 | 393 | m_parent_o_stream->Write(m_z_buffer, m_z_size); |
0915d0b2 VZ |
394 | if (m_parent_o_stream->LastWrite() != m_z_size) { |
395 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
396 | wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream")); | |
397 | break; | |
398 | } | |
6d44bf31 | 399 | |
856d2e52 GL |
400 | m_deflate->next_out = m_z_buffer; |
401 | m_deflate->avail_out = m_z_size; | |
79c3e0e1 | 402 | } |
6d44bf31 | 403 | |
856d2e52 | 404 | err = deflate(m_deflate, Z_NO_FLUSH); |
79c3e0e1 | 405 | } |
0915d0b2 VZ |
406 | |
407 | if (err != Z_OK) { | |
408 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
301deecc RN |
409 | wxString msg(m_deflate->msg, *wxConvCurrent); |
410 | if (!msg) | |
2a1f999f | 411 | msg = wxString::Format(_("zlib error %d"), err); |
4c68a102 | 412 | wxLogError(_("Can't write to deflate stream: %s"), msg.c_str()); |
0915d0b2 VZ |
413 | } |
414 | ||
415 | size -= m_deflate->avail_in; | |
416 | m_pos += size; | |
6d44bf31 | 417 | return size; |
79c3e0e1 | 418 | } |
ac57418f | 419 | |
4c68a102 | 420 | /* static */ bool wxZlibOutputStream::CanHandleGZip() |
cab1a605 | 421 | { |
4c68a102 VS |
422 | return wxZlibInputStream::CanHandleGZip(); |
423 | } | |
424 | ||
51acf83b VZ |
425 | bool wxZlibOutputStream::SetDictionary(const char *data, const size_t datalen) |
426 | { | |
427 | return (deflateSetDictionary(m_deflate, (Bytef*)data, datalen) == Z_OK); | |
428 | } | |
429 | ||
430 | bool wxZlibOutputStream::SetDictionary(const wxMemoryBuffer &buf) | |
431 | { | |
432 | return SetDictionary((char*)buf.GetData(), buf.GetDataLen()); | |
433 | } | |
434 | ||
ac57418f | 435 | #endif |
ce4169a4 | 436 | // wxUSE_ZLIB && wxUSE_STREAMS |