]> git.saurik.com Git - wxWidgets.git/blame - src/common/zstream.cpp
Moved version number to 2.7.2.0 and rebaked
[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
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 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
MW
46
47/////////////////////////////////////////////////////////////////////////////
48// Zlib Class factory
49
50IMPLEMENT_DYNAMIC_CLASS(wxZlibClassFactory, wxFilterClassFactory)
51
52static wxZlibClassFactory g_wxZlibClassFactory;
53
54wxZlibClassFactory::wxZlibClassFactory()
55{
56 if (this == &g_wxZlibClassFactory)
57 PushFront();
58}
59
60const wxChar * const *
61wxZlibClassFactory::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
78IMPLEMENT_DYNAMIC_CLASS(wxGzipClassFactory, wxFilterClassFactory)
79
80static wxGzipClassFactory g_wxGzipClassFactory;
81
82wxGzipClassFactory::wxGzipClassFactory()
83{
84 if (this == &g_wxGzipClassFactory && wxZlibInputStream::CanHandleGZip())
85 PushFront();
86}
87
88const wxChar * const *
89wxGzipClassFactory::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) {
103 case wxSTREAM_PROTOCOL: return protos;
104 case wxSTREAM_MIMETYPE: return mimes;
105 case wxSTREAM_ENCODING: return encs;
106 case wxSTREAM_FILEEXTENSION: return exts;
107 default: return empty;
108 }
109}
110
111
79c3e0e1
GL
112//////////////////////
113// wxZlibInputStream
114//////////////////////
115
0915d0b2 116wxZlibInputStream::wxZlibInputStream(wxInputStream& stream, int flags)
79c3e0e1 117 : wxFilterInputStream(stream)
55420742
MW
118{
119 Init(flags);
120}
121
122wxZlibInputStream::wxZlibInputStream(wxInputStream *stream, int flags)
123 : wxFilterInputStream(stream)
124{
125 Init(flags);
126}
127
128void 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 135#if WXWIN_COMPATIBILITY_2_4
3103e8a9 136 // treat compatibility mode as auto
4c68a102
VS
137 m_24compatibilty = flags == wxZLIB_24COMPATIBLE;
138 if (m_24compatibilty)
139 flags = wxZLIB_AUTO;
140#endif
141
142 // if gzip is asked for but not supported...
143 if ((flags == wxZLIB_GZIP || flags == wxZLIB_AUTO) && !CanHandleGZip()) {
144 if (flags == wxZLIB_AUTO) {
145 // an error will come later if the input turns out not to be a zlib
146 flags = wxZLIB_ZLIB;
147 }
148 else {
149 wxLogError(_("Gzip not supported by this version of zlib"));
150 m_lasterror = wxSTREAM_READ_ERROR;
151 return;
152 }
153 }
154
0915d0b2
VZ
155 if (m_z_buffer) {
156 m_inflate = new z_stream_s;
79c3e0e1 157
0915d0b2 158 if (m_inflate) {
5fc01d13 159 memset(m_inflate, 0, sizeof(z_stream_s));
79c3e0e1 160
4c68a102
VS
161 // see zlib.h for documentation on windowBits
162 int windowBits = MAX_WBITS;
163 switch (flags) {
164 case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break;
165 case wxZLIB_ZLIB: windowBits = MAX_WBITS; break;
166 case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break;
167 case wxZLIB_AUTO: windowBits = MAX_WBITS | ZSTREAM_AUTO; break;
168 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
169 }
6d44bf31 170
4c68a102 171 if (inflateInit2(m_inflate, windowBits) == Z_OK)
0915d0b2
VZ
172 return;
173 }
174 }
175
176 wxLogError(_("Can't initialize zlib inflate stream."));
177 m_lasterror = wxSTREAM_READ_ERROR;
79c3e0e1
GL
178}
179
180wxZlibInputStream::~wxZlibInputStream()
181{
856d2e52
GL
182 inflateEnd(m_inflate);
183 delete m_inflate;
45805ba3
VZ
184
185 delete [] m_z_buffer;
79c3e0e1
GL
186}
187
75ed1d15 188size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size)
79c3e0e1 189{
0915d0b2
VZ
190 wxASSERT_MSG(m_inflate && m_z_buffer, wxT("Inflate stream not open"));
191
192 if (!m_inflate || !m_z_buffer)
193 m_lasterror = wxSTREAM_READ_ERROR;
194 if (!IsOk() || !size)
195 return 0;
79c3e0e1 196
0915d0b2 197 int err = Z_OK;
856d2e52
GL
198 m_inflate->next_out = (unsigned char *)buffer;
199 m_inflate->avail_out = size;
79c3e0e1 200
0915d0b2 201 while (err == Z_OK && m_inflate->avail_out > 0) {
4c68a102 202 if (m_inflate->avail_in == 0 && m_parent_i_stream->IsOk()) {
0915d0b2 203 m_parent_i_stream->Read(m_z_buffer, m_z_size);
856d2e52
GL
204 m_inflate->next_in = m_z_buffer;
205 m_inflate->avail_in = m_parent_i_stream->LastRead();
4c68a102
VS
206 }
207 err = inflate(m_inflate, Z_SYNC_FLUSH);
208 }
6d44bf31 209
4c68a102
VS
210 switch (err) {
211 case Z_OK:
0915d0b2 212 break;
4c68a102
VS
213
214 case Z_STREAM_END:
8e50d1ad
MW
215 if (m_inflate->avail_out) {
216 // Unread any data taken from past the end of the deflate stream, so that
217 // any additional data can be read from the underlying stream (the crc
218 // in a gzip for example)
219 if (m_inflate->avail_in) {
abffc1ff 220 m_parent_i_stream->Reset();
8e50d1ad
MW
221 m_parent_i_stream->Ungetch(m_inflate->next_in, m_inflate->avail_in);
222 m_inflate->avail_in = 0;
223 }
224 m_lasterror = wxSTREAM_EOF;
777fd647 225 }
4c68a102 226 break;
f6bcfd97 227
4c68a102
VS
228 case Z_BUF_ERROR:
229 // Indicates that zlib was expecting more data, but the parent stream
230 // has none. Other than Eof the error will have been already reported
231 // by the parent strean,
232 m_lasterror = wxSTREAM_READ_ERROR;
233 if (m_parent_i_stream->Eof())
234#if WXWIN_COMPATIBILITY_2_4
235 if (m_24compatibilty)
236 m_lasterror = wxSTREAM_EOF;
237 else
238#endif
239 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
240 break;
241
242 default:
243 wxString msg(m_inflate->msg, *wxConvCurrent);
244 if (!msg)
2a1f999f 245 msg = wxString::Format(_("zlib error %d"), err);
4c68a102
VS
246 wxLogError(_("Can't read from inflate stream: %s"), msg.c_str());
247 m_lasterror = wxSTREAM_READ_ERROR;
79c3e0e1
GL
248 }
249
0915d0b2
VZ
250 size -= m_inflate->avail_out;
251 m_pos += size;
252 return size;
79c3e0e1
GL
253}
254
4c68a102
VS
255/* static */ bool wxZlibInputStream::CanHandleGZip()
256{
257 const char *dot = strchr(zlibVersion(), '.');
258 int major = atoi(zlibVersion());
259 int minor = dot ? atoi(dot + 1) : 0;
260 return major > 1 || (major == 1 && minor >= 2);
261}
262
263
79c3e0e1
GL
264//////////////////////
265// wxZlibOutputStream
266//////////////////////
267
0915d0b2
VZ
268wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream,
269 int level,
270 int flags)
79c3e0e1 271 : wxFilterOutputStream(stream)
55420742
MW
272{
273 Init(level, flags);
274}
275
276wxZlibOutputStream::wxZlibOutputStream(wxOutputStream *stream,
277 int level,
278 int flags)
279 : wxFilterOutputStream(stream)
280{
281 Init(level, flags);
282}
283
284void wxZlibOutputStream::Init(int level, int flags)
79c3e0e1 285{
0915d0b2
VZ
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;
79c3e0e1 290
af850422
VZ
291 if ( level == -1 )
292 {
0915d0b2 293 level = Z_DEFAULT_COMPRESSION;
af850422
VZ
294 }
295 else
296 {
297 wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
298 }
5824f314 299
4c68a102
VS
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 }
6d44bf31 306
4c68a102
VS
307 if (m_z_buffer) {
308 m_deflate = new z_stream_s;
0915d0b2 309
4c68a102
VS
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;
cab1a605 314
4c68a102
VS
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
cab1a605 324 if (deflateInit2(m_deflate, level, Z_DEFLATED, windowBits,
4c68a102
VS
325 8, Z_DEFAULT_STRATEGY) == Z_OK)
326 return;
327 }
0915d0b2
VZ
328 }
329
330 wxLogError(_("Can't initialize zlib deflate stream."));
331 m_lasterror = wxSTREAM_WRITE_ERROR;
79c3e0e1
GL
332}
333
8f0ff178
RN
334bool wxZlibOutputStream::Close()
335 {
336 DoFlush(true);
337 deflateEnd(m_deflate);
338 delete m_deflate;
6d44bf31 339
8f0ff178
RN
340 m_deflate = NULL;
341 delete[] m_z_buffer;
342 m_z_buffer = NULL;
55420742
MW
343
344 return wxFilterOutputStream::Close() && IsOk();
8f0ff178 345 }
79c3e0e1 346
0915d0b2 347void wxZlibOutputStream::DoFlush(bool final)
6d44bf31 348{
0915d0b2
VZ
349 if (!m_deflate || !m_z_buffer)
350 m_lasterror = wxSTREAM_WRITE_ERROR;
351 if (!IsOk())
6d44bf31 352 return;
6d44bf31 353
0915d0b2
VZ
354 int err = Z_OK;
355 bool done = false;
babd4308 356
0915d0b2
VZ
357 while (err == Z_OK || err == Z_STREAM_END) {
358 size_t len = m_z_size - m_deflate->avail_out;
359 if (len) {
360 if (m_parent_o_stream->Write(m_z_buffer, len).LastWrite() != len) {
361 m_lasterror = wxSTREAM_WRITE_ERROR;
362 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
363 break;
364 }
365 m_deflate->next_out = m_z_buffer;
366 m_deflate->avail_out = m_z_size;
367 }
368
369 if (done)
370 break;
371 err = deflate(m_deflate, final ? Z_FINISH : Z_FULL_FLUSH);
372 done = m_deflate->avail_out != 0 || err == Z_STREAM_END;
373 }
6d44bf31
GL
374}
375
75ed1d15 376size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1 377{
0915d0b2 378 wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open"));
79c3e0e1 379
0915d0b2 380 if (!m_deflate || !m_z_buffer)
10793ebf 381 {
517cce71 382 // notice that this will make IsOk() test just below return false
0915d0b2 383 m_lasterror = wxSTREAM_WRITE_ERROR;
10793ebf
VZ
384 }
385
0915d0b2
VZ
386 if (!IsOk() || !size)
387 return 0;
388
389 int err = Z_OK;
856d2e52
GL
390 m_deflate->next_in = (unsigned char *)buffer;
391 m_deflate->avail_in = size;
79c3e0e1 392
0915d0b2 393 while (err == Z_OK && m_deflate->avail_in > 0) {
856d2e52 394 if (m_deflate->avail_out == 0) {
6d44bf31 395 m_parent_o_stream->Write(m_z_buffer, m_z_size);
0915d0b2
VZ
396 if (m_parent_o_stream->LastWrite() != m_z_size) {
397 m_lasterror = wxSTREAM_WRITE_ERROR;
398 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
399 break;
400 }
6d44bf31 401
856d2e52
GL
402 m_deflate->next_out = m_z_buffer;
403 m_deflate->avail_out = m_z_size;
79c3e0e1 404 }
6d44bf31 405
856d2e52 406 err = deflate(m_deflate, Z_NO_FLUSH);
79c3e0e1 407 }
0915d0b2
VZ
408
409 if (err != Z_OK) {
410 m_lasterror = wxSTREAM_WRITE_ERROR;
301deecc
RN
411 wxString msg(m_deflate->msg, *wxConvCurrent);
412 if (!msg)
2a1f999f 413 msg = wxString::Format(_("zlib error %d"), err);
4c68a102 414 wxLogError(_("Can't write to deflate stream: %s"), msg.c_str());
0915d0b2
VZ
415 }
416
417 size -= m_deflate->avail_in;
418 m_pos += size;
6d44bf31 419 return size;
79c3e0e1 420}
ac57418f 421
4c68a102 422/* static */ bool wxZlibOutputStream::CanHandleGZip()
cab1a605 423{
4c68a102
VS
424 return wxZlibInputStream::CanHandleGZip();
425}
426
ac57418f 427#endif
ce4169a4 428 // wxUSE_ZLIB && wxUSE_STREAMS