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