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