]> git.saurik.com Git - wxWidgets.git/blame - src/common/zstream.cpp
fixed crash in case of not subclasses wxWizardPage in the resources (coverity checker...
[wxWidgets.git] / src / common / zstream.cpp
CommitLineData
0915d0b2 1//////////////////////////////////////////////////////////////////////////////
79c3e0e1
GL
2// Name: zstream.cpp
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
RR
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
ce4169a4 19#if wxUSE_ZLIB && wxUSE_STREAMS
ac57418f 20
ce4169a4 21#include "wx/zstream.h"
ac57418f
RR
22#include "wx/utils.h"
23#include "wx/intl.h"
3069ac4e 24#include "wx/log.h"
d1af991f 25
f6bcfd97
BP
26// normally, the compiler options should contain -I../zlib, but it is
27// apparently not the case for all MSW makefiles and so, unless we use
28// configure (which defines __WX_SETUP_H__) or it is explicitly overridden by
29// the user (who can define wxUSE_ZLIB_H_IN_PATH), we hardcode the path here
30#if defined(__WXMSW__) && !defined(__WX_SETUP_H__) && !defined(wxUSE_ZLIB_H_IN_PATH)
31e78e0c 31 #include "../zlib/zlib.h"
d1af991f 32#else
ac2834ab 33 #include "zlib.h"
d1af991f 34#endif
79c3e0e1 35
0915d0b2 36enum {
4c68a102
VS
37 ZSTREAM_BUFFER_SIZE = 16384,
38 ZSTREAM_GZIP = 0x10, // gzip header
39 ZSTREAM_AUTO = 0x20 // auto detect between gzip and zlib
0915d0b2 40};
6d44bf31 41
79c3e0e1
GL
42//////////////////////
43// wxZlibInputStream
44//////////////////////
45
0915d0b2 46wxZlibInputStream::wxZlibInputStream(wxInputStream& stream, int flags)
79c3e0e1
GL
47 : wxFilterInputStream(stream)
48{
0915d0b2
VZ
49 m_inflate = NULL;
50 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
51 m_z_size = ZSTREAM_BUFFER_SIZE;
52 m_pos = 0;
6d44bf31 53
4c68a102 54#if WXWIN_COMPATIBILITY_2_4
3103e8a9 55 // treat compatibility mode as auto
4c68a102
VS
56 m_24compatibilty = flags == wxZLIB_24COMPATIBLE;
57 if (m_24compatibilty)
58 flags = wxZLIB_AUTO;
59#endif
60
61 // if gzip is asked for but not supported...
62 if ((flags == wxZLIB_GZIP || flags == wxZLIB_AUTO) && !CanHandleGZip()) {
63 if (flags == wxZLIB_AUTO) {
64 // an error will come later if the input turns out not to be a zlib
65 flags = wxZLIB_ZLIB;
66 }
67 else {
68 wxLogError(_("Gzip not supported by this version of zlib"));
69 m_lasterror = wxSTREAM_READ_ERROR;
70 return;
71 }
72 }
73
0915d0b2
VZ
74 if (m_z_buffer) {
75 m_inflate = new z_stream_s;
79c3e0e1 76
0915d0b2 77 if (m_inflate) {
5fc01d13 78 memset(m_inflate, 0, sizeof(z_stream_s));
79c3e0e1 79
4c68a102
VS
80 // see zlib.h for documentation on windowBits
81 int windowBits = MAX_WBITS;
82 switch (flags) {
83 case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break;
84 case wxZLIB_ZLIB: windowBits = MAX_WBITS; break;
85 case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break;
86 case wxZLIB_AUTO: windowBits = MAX_WBITS | ZSTREAM_AUTO; break;
87 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
88 }
6d44bf31 89
4c68a102 90 if (inflateInit2(m_inflate, windowBits) == Z_OK)
0915d0b2
VZ
91 return;
92 }
93 }
94
95 wxLogError(_("Can't initialize zlib inflate stream."));
96 m_lasterror = wxSTREAM_READ_ERROR;
79c3e0e1
GL
97}
98
99wxZlibInputStream::~wxZlibInputStream()
100{
856d2e52
GL
101 inflateEnd(m_inflate);
102 delete m_inflate;
45805ba3
VZ
103
104 delete [] m_z_buffer;
79c3e0e1
GL
105}
106
75ed1d15 107size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size)
79c3e0e1 108{
0915d0b2
VZ
109 wxASSERT_MSG(m_inflate && m_z_buffer, wxT("Inflate stream not open"));
110
111 if (!m_inflate || !m_z_buffer)
112 m_lasterror = wxSTREAM_READ_ERROR;
113 if (!IsOk() || !size)
114 return 0;
79c3e0e1 115
0915d0b2 116 int err = Z_OK;
856d2e52
GL
117 m_inflate->next_out = (unsigned char *)buffer;
118 m_inflate->avail_out = size;
79c3e0e1 119
0915d0b2 120 while (err == Z_OK && m_inflate->avail_out > 0) {
4c68a102 121 if (m_inflate->avail_in == 0 && m_parent_i_stream->IsOk()) {
0915d0b2 122 m_parent_i_stream->Read(m_z_buffer, m_z_size);
856d2e52
GL
123 m_inflate->next_in = m_z_buffer;
124 m_inflate->avail_in = m_parent_i_stream->LastRead();
4c68a102
VS
125 }
126 err = inflate(m_inflate, Z_SYNC_FLUSH);
127 }
6d44bf31 128
4c68a102
VS
129 switch (err) {
130 case Z_OK:
0915d0b2 131 break;
4c68a102
VS
132
133 case Z_STREAM_END:
8e50d1ad
MW
134 if (m_inflate->avail_out) {
135 // Unread any data taken from past the end of the deflate stream, so that
136 // any additional data can be read from the underlying stream (the crc
137 // in a gzip for example)
138 if (m_inflate->avail_in) {
abffc1ff 139 m_parent_i_stream->Reset();
8e50d1ad
MW
140 m_parent_i_stream->Ungetch(m_inflate->next_in, m_inflate->avail_in);
141 m_inflate->avail_in = 0;
142 }
143 m_lasterror = wxSTREAM_EOF;
777fd647 144 }
4c68a102 145 break;
f6bcfd97 146
4c68a102
VS
147 case Z_BUF_ERROR:
148 // Indicates that zlib was expecting more data, but the parent stream
149 // has none. Other than Eof the error will have been already reported
150 // by the parent strean,
151 m_lasterror = wxSTREAM_READ_ERROR;
152 if (m_parent_i_stream->Eof())
153#if WXWIN_COMPATIBILITY_2_4
154 if (m_24compatibilty)
155 m_lasterror = wxSTREAM_EOF;
156 else
157#endif
158 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
159 break;
160
161 default:
162 wxString msg(m_inflate->msg, *wxConvCurrent);
163 if (!msg)
2a1f999f 164 msg = wxString::Format(_("zlib error %d"), err);
4c68a102
VS
165 wxLogError(_("Can't read from inflate stream: %s"), msg.c_str());
166 m_lasterror = wxSTREAM_READ_ERROR;
79c3e0e1
GL
167 }
168
0915d0b2
VZ
169 size -= m_inflate->avail_out;
170 m_pos += size;
171 return size;
79c3e0e1
GL
172}
173
4c68a102
VS
174/* static */ bool wxZlibInputStream::CanHandleGZip()
175{
176 const char *dot = strchr(zlibVersion(), '.');
177 int major = atoi(zlibVersion());
178 int minor = dot ? atoi(dot + 1) : 0;
179 return major > 1 || (major == 1 && minor >= 2);
180}
181
182
79c3e0e1
GL
183//////////////////////
184// wxZlibOutputStream
185//////////////////////
186
0915d0b2
VZ
187wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream,
188 int level,
189 int flags)
79c3e0e1
GL
190 : wxFilterOutputStream(stream)
191{
0915d0b2
VZ
192 m_deflate = NULL;
193 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
194 m_z_size = ZSTREAM_BUFFER_SIZE;
195 m_pos = 0;
79c3e0e1 196
af850422
VZ
197 if ( level == -1 )
198 {
0915d0b2 199 level = Z_DEFAULT_COMPRESSION;
af850422
VZ
200 }
201 else
202 {
203 wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
204 }
5824f314 205
4c68a102
VS
206 // if gzip is asked for but not supported...
207 if (flags == wxZLIB_GZIP && !CanHandleGZip()) {
208 wxLogError(_("Gzip not supported by this version of zlib"));
209 m_lasterror = wxSTREAM_WRITE_ERROR;
210 return;
211 }
6d44bf31 212
4c68a102
VS
213 if (m_z_buffer) {
214 m_deflate = new z_stream_s;
0915d0b2 215
4c68a102
VS
216 if (m_deflate) {
217 memset(m_deflate, 0, sizeof(z_stream_s));
218 m_deflate->next_out = m_z_buffer;
219 m_deflate->avail_out = m_z_size;
cab1a605 220
4c68a102
VS
221 // see zlib.h for documentation on windowBits
222 int windowBits = MAX_WBITS;
223 switch (flags) {
224 case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break;
225 case wxZLIB_ZLIB: windowBits = MAX_WBITS; break;
226 case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break;
227 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
228 }
229
cab1a605 230 if (deflateInit2(m_deflate, level, Z_DEFLATED, windowBits,
4c68a102
VS
231 8, Z_DEFAULT_STRATEGY) == Z_OK)
232 return;
233 }
0915d0b2
VZ
234 }
235
236 wxLogError(_("Can't initialize zlib deflate stream."));
237 m_lasterror = wxSTREAM_WRITE_ERROR;
79c3e0e1
GL
238}
239
8f0ff178
RN
240bool wxZlibOutputStream::Close()
241 {
242 DoFlush(true);
243 deflateEnd(m_deflate);
244 delete m_deflate;
6d44bf31 245
8f0ff178
RN
246 m_deflate = NULL;
247 delete[] m_z_buffer;
248 m_z_buffer = NULL;
249 return IsOk();
250 }
79c3e0e1 251
0915d0b2 252void wxZlibOutputStream::DoFlush(bool final)
6d44bf31 253{
0915d0b2
VZ
254 if (!m_deflate || !m_z_buffer)
255 m_lasterror = wxSTREAM_WRITE_ERROR;
256 if (!IsOk())
6d44bf31 257 return;
6d44bf31 258
0915d0b2
VZ
259 int err = Z_OK;
260 bool done = false;
babd4308 261
0915d0b2
VZ
262 while (err == Z_OK || err == Z_STREAM_END) {
263 size_t len = m_z_size - m_deflate->avail_out;
264 if (len) {
265 if (m_parent_o_stream->Write(m_z_buffer, len).LastWrite() != len) {
266 m_lasterror = wxSTREAM_WRITE_ERROR;
267 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
268 break;
269 }
270 m_deflate->next_out = m_z_buffer;
271 m_deflate->avail_out = m_z_size;
272 }
273
274 if (done)
275 break;
276 err = deflate(m_deflate, final ? Z_FINISH : Z_FULL_FLUSH);
277 done = m_deflate->avail_out != 0 || err == Z_STREAM_END;
278 }
6d44bf31
GL
279}
280
75ed1d15 281size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1 282{
0915d0b2 283 wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open"));
79c3e0e1 284
0915d0b2
VZ
285 if (!m_deflate || !m_z_buffer)
286 m_lasterror = wxSTREAM_WRITE_ERROR;
287 if (!IsOk() || !size)
288 return 0;
289
290 int err = Z_OK;
856d2e52
GL
291 m_deflate->next_in = (unsigned char *)buffer;
292 m_deflate->avail_in = size;
79c3e0e1 293
0915d0b2 294 while (err == Z_OK && m_deflate->avail_in > 0) {
856d2e52 295 if (m_deflate->avail_out == 0) {
6d44bf31 296 m_parent_o_stream->Write(m_z_buffer, m_z_size);
0915d0b2
VZ
297 if (m_parent_o_stream->LastWrite() != m_z_size) {
298 m_lasterror = wxSTREAM_WRITE_ERROR;
299 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
300 break;
301 }
6d44bf31 302
856d2e52
GL
303 m_deflate->next_out = m_z_buffer;
304 m_deflate->avail_out = m_z_size;
79c3e0e1 305 }
6d44bf31 306
856d2e52 307 err = deflate(m_deflate, Z_NO_FLUSH);
79c3e0e1 308 }
0915d0b2
VZ
309
310 if (err != Z_OK) {
311 m_lasterror = wxSTREAM_WRITE_ERROR;
301deecc
RN
312 wxString msg(m_deflate->msg, *wxConvCurrent);
313 if (!msg)
2a1f999f 314 msg = wxString::Format(_("zlib error %d"), err);
4c68a102 315 wxLogError(_("Can't write to deflate stream: %s"), msg.c_str());
0915d0b2
VZ
316 }
317
318 size -= m_deflate->avail_in;
319 m_pos += size;
6d44bf31 320 return size;
79c3e0e1 321}
ac57418f 322
4c68a102 323/* static */ bool wxZlibOutputStream::CanHandleGZip()
cab1a605 324{
4c68a102
VS
325 return wxZlibInputStream::CanHandleGZip();
326}
327
ac57418f 328#endif
ce4169a4 329 // wxUSE_ZLIB && wxUSE_STREAMS
cab1a605 330