]> git.saurik.com Git - wxWidgets.git/blame - src/common/zstream.cpp
Warning fixes found under hardest mode of OpenWatcom. Seems clean in Borland, MinGW...
[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
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
79c3e0e1
GL
13#pragma implementation "zstream.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
ac57418f 18
ce4169a4
RR
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
ce4169a4 23#if wxUSE_ZLIB && wxUSE_STREAMS
ac57418f 24
ce4169a4 25#include "wx/zstream.h"
ac57418f
RR
26#include "wx/utils.h"
27#include "wx/intl.h"
3069ac4e 28#include "wx/log.h"
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)
31e78e0c 35 #include "../zlib/zlib.h"
d1af991f 36#else
ac2834ab 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
VS
58#if WXWIN_COMPATIBILITY_2_4
59 // treat compatibilty mode as auto
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:
138 // Unread any data taken from past the end of the deflate stream, so that
139 // any additional data can be read from the underlying stream (the crc
140 // in a gzip for example)
141 if (m_inflate->avail_in) {
142 m_parent_i_stream->Ungetch(m_inflate->next_in, m_inflate->avail_in);
143 m_inflate->avail_in = 0;
777fd647 144 }
4c68a102
VS
145 m_lasterror = wxSTREAM_EOF;
146 break;
f6bcfd97 147
4c68a102
VS
148 case Z_BUF_ERROR:
149 // Indicates that zlib was expecting more data, but the parent stream
150 // has none. Other than Eof the error will have been already reported
151 // by the parent strean,
152 m_lasterror = wxSTREAM_READ_ERROR;
153 if (m_parent_i_stream->Eof())
154#if WXWIN_COMPATIBILITY_2_4
155 if (m_24compatibilty)
156 m_lasterror = wxSTREAM_EOF;
157 else
158#endif
159 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
160 break;
161
162 default:
163 wxString msg(m_inflate->msg, *wxConvCurrent);
164 if (!msg)
165 msg.Format(_("zlib error %d"), err);
166 wxLogError(_("Can't read from inflate stream: %s"), msg.c_str());
167 m_lasterror = wxSTREAM_READ_ERROR;
79c3e0e1
GL
168 }
169
0915d0b2
VZ
170 size -= m_inflate->avail_out;
171 m_pos += size;
172 return size;
79c3e0e1
GL
173}
174
4c68a102
VS
175/* static */ bool wxZlibInputStream::CanHandleGZip()
176{
177 const char *dot = strchr(zlibVersion(), '.');
178 int major = atoi(zlibVersion());
179 int minor = dot ? atoi(dot + 1) : 0;
180 return major > 1 || (major == 1 && minor >= 2);
181}
182
183
79c3e0e1
GL
184//////////////////////
185// wxZlibOutputStream
186//////////////////////
187
0915d0b2
VZ
188wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream,
189 int level,
190 int flags)
79c3e0e1
GL
191 : wxFilterOutputStream(stream)
192{
0915d0b2
VZ
193 m_deflate = NULL;
194 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
195 m_z_size = ZSTREAM_BUFFER_SIZE;
196 m_pos = 0;
79c3e0e1 197
af850422
VZ
198 if ( level == -1 )
199 {
0915d0b2 200 level = Z_DEFAULT_COMPRESSION;
af850422
VZ
201 }
202 else
203 {
204 wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
205 }
5824f314 206
4c68a102
VS
207 // if gzip is asked for but not supported...
208 if (flags == wxZLIB_GZIP && !CanHandleGZip()) {
209 wxLogError(_("Gzip not supported by this version of zlib"));
210 m_lasterror = wxSTREAM_WRITE_ERROR;
211 return;
212 }
6d44bf31 213
4c68a102
VS
214 if (m_z_buffer) {
215 m_deflate = new z_stream_s;
0915d0b2 216
4c68a102
VS
217 if (m_deflate) {
218 memset(m_deflate, 0, sizeof(z_stream_s));
219 m_deflate->next_out = m_z_buffer;
220 m_deflate->avail_out = m_z_size;
cab1a605 221
4c68a102
VS
222 // see zlib.h for documentation on windowBits
223 int windowBits = MAX_WBITS;
224 switch (flags) {
225 case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break;
226 case wxZLIB_ZLIB: windowBits = MAX_WBITS; break;
227 case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break;
228 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
229 }
230
cab1a605 231 if (deflateInit2(m_deflate, level, Z_DEFLATED, windowBits,
4c68a102
VS
232 8, Z_DEFAULT_STRATEGY) == Z_OK)
233 return;
234 }
0915d0b2
VZ
235 }
236
237 wxLogError(_("Can't initialize zlib deflate stream."));
238 m_lasterror = wxSTREAM_WRITE_ERROR;
79c3e0e1
GL
239}
240
241wxZlibOutputStream::~wxZlibOutputStream()
242{
0915d0b2
VZ
243 if (m_deflate && m_z_buffer)
244 DoFlush(true);
856d2e52 245 deflateEnd(m_deflate);
778d618a 246 delete m_deflate;
6d44bf31
GL
247
248 delete[] m_z_buffer;
79c3e0e1
GL
249}
250
0915d0b2 251void wxZlibOutputStream::DoFlush(bool final)
6d44bf31 252{
0915d0b2 253 wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open"));
6d44bf31 254
0915d0b2
VZ
255 if (!m_deflate || !m_z_buffer)
256 m_lasterror = wxSTREAM_WRITE_ERROR;
257 if (!IsOk())
6d44bf31 258 return;
6d44bf31 259
0915d0b2
VZ
260 int err = Z_OK;
261 bool done = false;
babd4308 262
0915d0b2
VZ
263 while (err == Z_OK || err == Z_STREAM_END) {
264 size_t len = m_z_size - m_deflate->avail_out;
265 if (len) {
266 if (m_parent_o_stream->Write(m_z_buffer, len).LastWrite() != len) {
267 m_lasterror = wxSTREAM_WRITE_ERROR;
268 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
269 break;
270 }
271 m_deflate->next_out = m_z_buffer;
272 m_deflate->avail_out = m_z_size;
273 }
274
275 if (done)
276 break;
277 err = deflate(m_deflate, final ? Z_FINISH : Z_FULL_FLUSH);
278 done = m_deflate->avail_out != 0 || err == Z_STREAM_END;
279 }
6d44bf31
GL
280}
281
75ed1d15 282size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1 283{
0915d0b2 284 wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open"));
79c3e0e1 285
0915d0b2
VZ
286 if (!m_deflate || !m_z_buffer)
287 m_lasterror = wxSTREAM_WRITE_ERROR;
288 if (!IsOk() || !size)
289 return 0;
290
291 int err = Z_OK;
856d2e52
GL
292 m_deflate->next_in = (unsigned char *)buffer;
293 m_deflate->avail_in = size;
79c3e0e1 294
0915d0b2 295 while (err == Z_OK && m_deflate->avail_in > 0) {
856d2e52 296 if (m_deflate->avail_out == 0) {
6d44bf31 297 m_parent_o_stream->Write(m_z_buffer, m_z_size);
0915d0b2
VZ
298 if (m_parent_o_stream->LastWrite() != m_z_size) {
299 m_lasterror = wxSTREAM_WRITE_ERROR;
300 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
301 break;
302 }
6d44bf31 303
856d2e52
GL
304 m_deflate->next_out = m_z_buffer;
305 m_deflate->avail_out = m_z_size;
79c3e0e1 306 }
6d44bf31 307
856d2e52 308 err = deflate(m_deflate, Z_NO_FLUSH);
79c3e0e1 309 }
0915d0b2
VZ
310
311 if (err != Z_OK) {
312 m_lasterror = wxSTREAM_WRITE_ERROR;
301deecc
RN
313 wxString msg(m_deflate->msg, *wxConvCurrent);
314 if (!msg)
315 msg.Format(_("zlib error %d"), err);
4c68a102 316 wxLogError(_("Can't write to deflate stream: %s"), msg.c_str());
0915d0b2
VZ
317 }
318
319 size -= m_deflate->avail_in;
320 m_pos += size;
6d44bf31 321 return size;
79c3e0e1 322}
ac57418f 323
4c68a102 324/* static */ bool wxZlibOutputStream::CanHandleGZip()
cab1a605 325{
4c68a102
VS
326 return wxZlibInputStream::CanHandleGZip();
327}
328
ac57418f 329#endif
ce4169a4 330 // wxUSE_ZLIB && wxUSE_STREAMS
cab1a605 331