]> git.saurik.com Git - wxWidgets.git/blame - src/common/zstream.cpp
fix building with WXWIN_COMPATIBILITY_2_8 == 0
[wxWidgets.git] / src / common / zstream.cpp
CommitLineData
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 6// Created: 11/07/98
79c3e0e1 7// Copyright: (c) Guilhem Lavaux
65571936 8// Licence: wxWindows licence
79c3e0e1 9/////////////////////////////////////////////////////////////////////////////
ce4169a4 10
79c3e0e1
GL
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
ac57418f 13
ce4169a4 14#ifdef __BORLANDC__
88a7a4e1 15 #pragma hdrstop
ce4169a4
RR
16#endif
17
ce4169a4 18#if wxUSE_ZLIB && wxUSE_STREAMS
ac57418f 19
ce4169a4 20#include "wx/zstream.h"
ccec9093 21#include "wx/versioninfo.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
d98a58c5 34#if defined(__WINDOWS__) && !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 40enum {
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 46
ccec9093
VZ
47wxVersionInfo wxGetZlibVersionInfo()
48{
d02a916b
VZ
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);
ccec9093
VZ
61}
62
55420742
MW
63/////////////////////////////////////////////////////////////////////////////
64// Zlib Class factory
65
66IMPLEMENT_DYNAMIC_CLASS(wxZlibClassFactory, wxFilterClassFactory)
67
68static wxZlibClassFactory g_wxZlibClassFactory;
69
70wxZlibClassFactory::wxZlibClassFactory()
71{
72 if (this == &g_wxZlibClassFactory)
73 PushFront();
74}
75
76const wxChar * const *
77wxZlibClassFactory::GetProtocols(wxStreamProtocolType type) const
78{
9a83f860
VZ
79 static const wxChar *mimes[] = { wxT("application/x-deflate"), NULL };
80 static const wxChar *encs[] = { wxT("deflate"), NULL };
55420742
MW
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
94IMPLEMENT_DYNAMIC_CLASS(wxGzipClassFactory, wxFilterClassFactory)
95
96static wxGzipClassFactory g_wxGzipClassFactory;
97
98wxGzipClassFactory::wxGzipClassFactory()
99{
100 if (this == &g_wxGzipClassFactory && wxZlibInputStream::CanHandleGZip())
101 PushFront();
102}
103
104const wxChar * const *
105wxGzipClassFactory::GetProtocols(wxStreamProtocolType type) const
106{
03647350 107 static const wxChar *protos[] =
9a83f860 108 { wxT("gzip"), NULL };
03647350 109 static const wxChar *mimes[] =
9a83f860 110 { wxT("application/gzip"), wxT("application/x-gzip"), NULL };
03647350 111 static const wxChar *encs[] =
9a83f860 112 { wxT("gzip"), NULL };
03647350 113 static const wxChar *exts[] =
9a83f860 114 { wxT(".gz"), wxT(".gzip"), NULL };
55420742
MW
115 static const wxChar *empty[] =
116 { NULL };
117
118 switch (type) {
489a164c
MW
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;
55420742
MW
124 }
125}
126
127
79c3e0e1
GL
128//////////////////////
129// wxZlibInputStream
130//////////////////////
131
0915d0b2 132wxZlibInputStream::wxZlibInputStream(wxInputStream& stream, int flags)
79c3e0e1 133 : wxFilterInputStream(stream)
55420742
MW
134{
135 Init(flags);
136}
137
138wxZlibInputStream::wxZlibInputStream(wxInputStream *stream, int flags)
139 : wxFilterInputStream(stream)
140{
141 Init(flags);
142}
143
144void wxZlibInputStream::Init(int flags)
79c3e0e1 145{
0915d0b2
VZ
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;
6d44bf31 150
4c68a102
VS
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
0915d0b2
VZ
164 if (m_z_buffer) {
165 m_inflate = new z_stream_s;
79c3e0e1 166
0915d0b2 167 if (m_inflate) {
5fc01d13 168 memset(m_inflate, 0, sizeof(z_stream_s));
79c3e0e1 169
4c68a102
VS
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 }
6d44bf31 179
4c68a102 180 if (inflateInit2(m_inflate, windowBits) == Z_OK)
0915d0b2
VZ
181 return;
182 }
183 }
184
185 wxLogError(_("Can't initialize zlib inflate stream."));
186 m_lasterror = wxSTREAM_READ_ERROR;
79c3e0e1
GL
187}
188
189wxZlibInputStream::~wxZlibInputStream()
190{
856d2e52
GL
191 inflateEnd(m_inflate);
192 delete m_inflate;
45805ba3
VZ
193
194 delete [] m_z_buffer;
79c3e0e1
GL
195}
196
75ed1d15 197size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size)
79c3e0e1 198{
0915d0b2
VZ
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;
79c3e0e1 205
0915d0b2 206 int err = Z_OK;
856d2e52
GL
207 m_inflate->next_out = (unsigned char *)buffer;
208 m_inflate->avail_out = size;
79c3e0e1 209
0915d0b2 210 while (err == Z_OK && m_inflate->avail_out > 0) {
4c68a102 211 if (m_inflate->avail_in == 0 && m_parent_i_stream->IsOk()) {
0915d0b2 212 m_parent_i_stream->Read(m_z_buffer, m_z_size);
856d2e52
GL
213 m_inflate->next_in = m_z_buffer;
214 m_inflate->avail_in = m_parent_i_stream->LastRead();
4c68a102
VS
215 }
216 err = inflate(m_inflate, Z_SYNC_FLUSH);
217 }
6d44bf31 218
4c68a102
VS
219 switch (err) {
220 case Z_OK:
0915d0b2 221 break;
4c68a102
VS
222
223 case Z_STREAM_END:
8e50d1ad
MW
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) {
abffc1ff 229 m_parent_i_stream->Reset();
8e50d1ad
MW
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;
777fd647 234 }
4c68a102 235 break;
f6bcfd97 236
4c68a102
VS
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())
af588446 243 {
4c68a102 244 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
af588446 245 }
4c68a102
VS
246 break;
247
248 default:
249 wxString msg(m_inflate->msg, *wxConvCurrent);
250 if (!msg)
2a1f999f 251 msg = wxString::Format(_("zlib error %d"), err);
4c68a102
VS
252 wxLogError(_("Can't read from inflate stream: %s"), msg.c_str());
253 m_lasterror = wxSTREAM_READ_ERROR;
79c3e0e1
GL
254 }
255
0915d0b2
VZ
256 size -= m_inflate->avail_out;
257 m_pos += size;
258 return size;
79c3e0e1
GL
259}
260
4c68a102
VS
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
51acf83b
VZ
269bool wxZlibInputStream::SetDictionary(const char *data, const size_t datalen)
270{
271 return (inflateSetDictionary(m_inflate, (Bytef*)data, datalen) == Z_OK);
272}
273
274bool wxZlibInputStream::SetDictionary(const wxMemoryBuffer &buf)
275{
276 return SetDictionary((char*)buf.GetData(), buf.GetDataLen());
277}
278
4c68a102 279
79c3e0e1
GL
280//////////////////////
281// wxZlibOutputStream
282//////////////////////
283
0915d0b2
VZ
284wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream,
285 int level,
286 int flags)
79c3e0e1 287 : wxFilterOutputStream(stream)
55420742
MW
288{
289 Init(level, flags);
290}
291
292wxZlibOutputStream::wxZlibOutputStream(wxOutputStream *stream,
293 int level,
294 int flags)
295 : wxFilterOutputStream(stream)
296{
297 Init(level, flags);
298}
299
300void wxZlibOutputStream::Init(int level, int flags)
79c3e0e1 301{
0915d0b2
VZ
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;
79c3e0e1 306
af850422
VZ
307 if ( level == -1 )
308 {
0915d0b2 309 level = Z_DEFAULT_COMPRESSION;
af850422
VZ
310 }
311 else
312 {
313 wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
314 }
5824f314 315
4c68a102
VS
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 }
6d44bf31 322
4c68a102
VS
323 if (m_z_buffer) {
324 m_deflate = new z_stream_s;
0915d0b2 325
4c68a102
VS
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;
cab1a605 330
4c68a102
VS
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
cab1a605 340 if (deflateInit2(m_deflate, level, Z_DEFLATED, windowBits,
4c68a102
VS
341 8, Z_DEFAULT_STRATEGY) == Z_OK)
342 return;
343 }
0915d0b2
VZ
344 }
345
346 wxLogError(_("Can't initialize zlib deflate stream."));
347 m_lasterror = wxSTREAM_WRITE_ERROR;
79c3e0e1
GL
348}
349
8f0ff178
RN
350bool wxZlibOutputStream::Close()
351 {
352 DoFlush(true);
353 deflateEnd(m_deflate);
5276b0a5
VZ
354 wxDELETE(m_deflate);
355 wxDELETEA(m_z_buffer);
55420742
MW
356
357 return wxFilterOutputStream::Close() && IsOk();
8f0ff178 358 }
79c3e0e1 359
0915d0b2 360void wxZlibOutputStream::DoFlush(bool final)
6d44bf31 361{
0915d0b2
VZ
362 if (!m_deflate || !m_z_buffer)
363 m_lasterror = wxSTREAM_WRITE_ERROR;
364 if (!IsOk())
6d44bf31 365 return;
6d44bf31 366
0915d0b2
VZ
367 int err = Z_OK;
368 bool done = false;
babd4308 369
0915d0b2
VZ
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 }
6d44bf31
GL
387}
388
75ed1d15 389size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1 390{
0915d0b2 391 wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open"));
79c3e0e1 392
0915d0b2 393 if (!m_deflate || !m_z_buffer)
10793ebf 394 {
517cce71 395 // notice that this will make IsOk() test just below return false
0915d0b2 396 m_lasterror = wxSTREAM_WRITE_ERROR;
10793ebf
VZ
397 }
398
0915d0b2
VZ
399 if (!IsOk() || !size)
400 return 0;
401
402 int err = Z_OK;
856d2e52
GL
403 m_deflate->next_in = (unsigned char *)buffer;
404 m_deflate->avail_in = size;
79c3e0e1 405
0915d0b2 406 while (err == Z_OK && m_deflate->avail_in > 0) {
856d2e52 407 if (m_deflate->avail_out == 0) {
6d44bf31 408 m_parent_o_stream->Write(m_z_buffer, m_z_size);
0915d0b2
VZ
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 }
6d44bf31 414
856d2e52
GL
415 m_deflate->next_out = m_z_buffer;
416 m_deflate->avail_out = m_z_size;
79c3e0e1 417 }
6d44bf31 418
856d2e52 419 err = deflate(m_deflate, Z_NO_FLUSH);
79c3e0e1 420 }
0915d0b2
VZ
421
422 if (err != Z_OK) {
423 m_lasterror = wxSTREAM_WRITE_ERROR;
301deecc
RN
424 wxString msg(m_deflate->msg, *wxConvCurrent);
425 if (!msg)
2a1f999f 426 msg = wxString::Format(_("zlib error %d"), err);
4c68a102 427 wxLogError(_("Can't write to deflate stream: %s"), msg.c_str());
0915d0b2
VZ
428 }
429
430 size -= m_deflate->avail_in;
431 m_pos += size;
6d44bf31 432 return size;
79c3e0e1 433}
ac57418f 434
4c68a102 435/* static */ bool wxZlibOutputStream::CanHandleGZip()
cab1a605 436{
4c68a102
VS
437 return wxZlibInputStream::CanHandleGZip();
438}
439
51acf83b
VZ
440bool wxZlibOutputStream::SetDictionary(const char *data, const size_t datalen)
441{
442 return (deflateSetDictionary(m_deflate, (Bytef*)data, datalen) == Z_OK);
443}
444
445bool wxZlibOutputStream::SetDictionary(const wxMemoryBuffer &buf)
446{
447 return SetDictionary((char*)buf.GetData(), buf.GetDataLen());
448}
449
ac57418f 450#endif
ce4169a4 451 // wxUSE_ZLIB && wxUSE_STREAMS