Use wxDELETE() and wxDELETEA() when possible.
[wxWidgets.git] / src / common / zstream.cpp
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 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_ZLIB && wxUSE_STREAMS
20
21 #include "wx/zstream.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(__WXMSW__) && !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 /////////////////////////////////////////////////////////////////////////////
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[] = { wxT("application/x-deflate"), NULL };
64 static const wxChar *encs[] = { wxT("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 { wxT("gzip"), NULL };
93 static const wxChar *mimes[] =
94 { wxT("application/gzip"), wxT("application/x-gzip"), NULL };
95 static const wxChar *encs[] =
96 { wxT("gzip"), NULL };
97 static const wxChar *exts[] =
98 { wxT(".gz"), wxT(".gzip"), NULL };
99 static const wxChar *empty[] =
100 { NULL };
101
102 switch (type) {
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;
108 }
109 }
110
111
112 //////////////////////
113 // wxZlibInputStream
114 //////////////////////
115
116 wxZlibInputStream::wxZlibInputStream(wxInputStream& stream, int flags)
117 : wxFilterInputStream(stream)
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)
129 {
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;
134
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
148 if (m_z_buffer) {
149 m_inflate = new z_stream_s;
150
151 if (m_inflate) {
152 memset(m_inflate, 0, sizeof(z_stream_s));
153
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 }
163
164 if (inflateInit2(m_inflate, windowBits) == Z_OK)
165 return;
166 }
167 }
168
169 wxLogError(_("Can't initialize zlib inflate stream."));
170 m_lasterror = wxSTREAM_READ_ERROR;
171 }
172
173 wxZlibInputStream::~wxZlibInputStream()
174 {
175 inflateEnd(m_inflate);
176 delete m_inflate;
177
178 delete [] m_z_buffer;
179 }
180
181 size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size)
182 {
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;
189
190 int err = Z_OK;
191 m_inflate->next_out = (unsigned char *)buffer;
192 m_inflate->avail_out = size;
193
194 while (err == Z_OK && m_inflate->avail_out > 0) {
195 if (m_inflate->avail_in == 0 && m_parent_i_stream->IsOk()) {
196 m_parent_i_stream->Read(m_z_buffer, m_z_size);
197 m_inflate->next_in = m_z_buffer;
198 m_inflate->avail_in = m_parent_i_stream->LastRead();
199 }
200 err = inflate(m_inflate, Z_SYNC_FLUSH);
201 }
202
203 switch (err) {
204 case Z_OK:
205 break;
206
207 case Z_STREAM_END:
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) {
213 m_parent_i_stream->Reset();
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;
218 }
219 break;
220
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())
227 {
228 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
229 }
230 break;
231
232 default:
233 wxString msg(m_inflate->msg, *wxConvCurrent);
234 if (!msg)
235 msg = wxString::Format(_("zlib error %d"), err);
236 wxLogError(_("Can't read from inflate stream: %s"), msg.c_str());
237 m_lasterror = wxSTREAM_READ_ERROR;
238 }
239
240 size -= m_inflate->avail_out;
241 m_pos += size;
242 return size;
243 }
244
245 /* static */ bool wxZlibInputStream::CanHandleGZip()
246 {
247 const char *dot = strchr(zlibVersion(), '.');
248 int major = atoi(zlibVersion());
249 int minor = dot ? atoi(dot + 1) : 0;
250 return major > 1 || (major == 1 && minor >= 2);
251 }
252
253 bool wxZlibInputStream::SetDictionary(const char *data, const size_t datalen)
254 {
255 return (inflateSetDictionary(m_inflate, (Bytef*)data, datalen) == Z_OK);
256 }
257
258 bool wxZlibInputStream::SetDictionary(const wxMemoryBuffer &buf)
259 {
260 return SetDictionary((char*)buf.GetData(), buf.GetDataLen());
261 }
262
263
264 //////////////////////
265 // wxZlibOutputStream
266 //////////////////////
267
268 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream,
269 int level,
270 int flags)
271 : wxFilterOutputStream(stream)
272 {
273 Init(level, flags);
274 }
275
276 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream *stream,
277 int level,
278 int flags)
279 : wxFilterOutputStream(stream)
280 {
281 Init(level, flags);
282 }
283
284 void wxZlibOutputStream::Init(int level, int flags)
285 {
286 m_deflate = NULL;
287 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
288 m_z_size = ZSTREAM_BUFFER_SIZE;
289 m_pos = 0;
290
291 if ( level == -1 )
292 {
293 level = Z_DEFAULT_COMPRESSION;
294 }
295 else
296 {
297 wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
298 }
299
300 // if gzip is asked for but not supported...
301 if (flags == wxZLIB_GZIP && !CanHandleGZip()) {
302 wxLogError(_("Gzip not supported by this version of zlib"));
303 m_lasterror = wxSTREAM_WRITE_ERROR;
304 return;
305 }
306
307 if (m_z_buffer) {
308 m_deflate = new z_stream_s;
309
310 if (m_deflate) {
311 memset(m_deflate, 0, sizeof(z_stream_s));
312 m_deflate->next_out = m_z_buffer;
313 m_deflate->avail_out = m_z_size;
314
315 // see zlib.h for documentation on windowBits
316 int windowBits = MAX_WBITS;
317 switch (flags) {
318 case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break;
319 case wxZLIB_ZLIB: windowBits = MAX_WBITS; break;
320 case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break;
321 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
322 }
323
324 if (deflateInit2(m_deflate, level, Z_DEFLATED, windowBits,
325 8, Z_DEFAULT_STRATEGY) == Z_OK)
326 return;
327 }
328 }
329
330 wxLogError(_("Can't initialize zlib deflate stream."));
331 m_lasterror = wxSTREAM_WRITE_ERROR;
332 }
333
334 bool wxZlibOutputStream::Close()
335 {
336 DoFlush(true);
337 deflateEnd(m_deflate);
338 wxDELETE(m_deflate);
339 wxDELETEA(m_z_buffer);
340
341 return wxFilterOutputStream::Close() && IsOk();
342 }
343
344 void wxZlibOutputStream::DoFlush(bool final)
345 {
346 if (!m_deflate || !m_z_buffer)
347 m_lasterror = wxSTREAM_WRITE_ERROR;
348 if (!IsOk())
349 return;
350
351 int err = Z_OK;
352 bool done = false;
353
354 while (err == Z_OK || err == Z_STREAM_END) {
355 size_t len = m_z_size - m_deflate->avail_out;
356 if (len) {
357 if (m_parent_o_stream->Write(m_z_buffer, len).LastWrite() != len) {
358 m_lasterror = wxSTREAM_WRITE_ERROR;
359 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
360 break;
361 }
362 m_deflate->next_out = m_z_buffer;
363 m_deflate->avail_out = m_z_size;
364 }
365
366 if (done)
367 break;
368 err = deflate(m_deflate, final ? Z_FINISH : Z_FULL_FLUSH);
369 done = m_deflate->avail_out != 0 || err == Z_STREAM_END;
370 }
371 }
372
373 size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
374 {
375 wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open"));
376
377 if (!m_deflate || !m_z_buffer)
378 {
379 // notice that this will make IsOk() test just below return false
380 m_lasterror = wxSTREAM_WRITE_ERROR;
381 }
382
383 if (!IsOk() || !size)
384 return 0;
385
386 int err = Z_OK;
387 m_deflate->next_in = (unsigned char *)buffer;
388 m_deflate->avail_in = size;
389
390 while (err == Z_OK && m_deflate->avail_in > 0) {
391 if (m_deflate->avail_out == 0) {
392 m_parent_o_stream->Write(m_z_buffer, m_z_size);
393 if (m_parent_o_stream->LastWrite() != m_z_size) {
394 m_lasterror = wxSTREAM_WRITE_ERROR;
395 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
396 break;
397 }
398
399 m_deflate->next_out = m_z_buffer;
400 m_deflate->avail_out = m_z_size;
401 }
402
403 err = deflate(m_deflate, Z_NO_FLUSH);
404 }
405
406 if (err != Z_OK) {
407 m_lasterror = wxSTREAM_WRITE_ERROR;
408 wxString msg(m_deflate->msg, *wxConvCurrent);
409 if (!msg)
410 msg = wxString::Format(_("zlib error %d"), err);
411 wxLogError(_("Can't write to deflate stream: %s"), msg.c_str());
412 }
413
414 size -= m_deflate->avail_in;
415 m_pos += size;
416 return size;
417 }
418
419 /* static */ bool wxZlibOutputStream::CanHandleGZip()
420 {
421 return wxZlibInputStream::CanHandleGZip();
422 }
423
424 bool wxZlibOutputStream::SetDictionary(const char *data, const size_t datalen)
425 {
426 return (deflateSetDictionary(m_deflate, (Bytef*)data, datalen) == Z_OK);
427 }
428
429 bool wxZlibOutputStream::SetDictionary(const wxMemoryBuffer &buf)
430 {
431 return SetDictionary((char*)buf.GetData(), buf.GetDataLen());
432 }
433
434 #endif
435 // wxUSE_ZLIB && wxUSE_STREAMS