]> git.saurik.com Git - wxWidgets.git/blob - src/common/zstream.cpp
Set HTML data even in Unicode mode
[wxWidgets.git] / src / common / zstream.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 // Name: zstream.cpp
3 // Purpose: Compressed stream classes
4 // Author: Guilhem Lavaux
5 // Modified by: Mike Wetherell
6 // Created: 11/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_ZLIB && wxUSE_STREAMS
20
21 #include "wx/zstream.h"
22 #include "wx/utils.h"
23 #include "wx/intl.h"
24 #include "wx/log.h"
25
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)
31 #include "../zlib/zlib.h"
32 #else
33 #include "zlib.h"
34 #endif
35
36 enum {
37 ZSTREAM_BUFFER_SIZE = 16384,
38 ZSTREAM_GZIP = 0x10, // gzip header
39 ZSTREAM_AUTO = 0x20 // auto detect between gzip and zlib
40 };
41
42 //////////////////////
43 // wxZlibInputStream
44 //////////////////////
45
46 wxZlibInputStream::wxZlibInputStream(wxInputStream& stream, int flags)
47 : wxFilterInputStream(stream)
48 {
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;
53
54 #if WXWIN_COMPATIBILITY_2_4
55 // treat compatibility mode as auto
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
74 if (m_z_buffer) {
75 m_inflate = new z_stream_s;
76
77 if (m_inflate) {
78 memset(m_inflate, 0, sizeof(z_stream_s));
79
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 }
89
90 if (inflateInit2(m_inflate, windowBits) == Z_OK)
91 return;
92 }
93 }
94
95 wxLogError(_("Can't initialize zlib inflate stream."));
96 m_lasterror = wxSTREAM_READ_ERROR;
97 }
98
99 wxZlibInputStream::~wxZlibInputStream()
100 {
101 inflateEnd(m_inflate);
102 delete m_inflate;
103
104 delete [] m_z_buffer;
105 }
106
107 size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size)
108 {
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;
115
116 int err = Z_OK;
117 m_inflate->next_out = (unsigned char *)buffer;
118 m_inflate->avail_out = size;
119
120 while (err == Z_OK && m_inflate->avail_out > 0) {
121 if (m_inflate->avail_in == 0 && m_parent_i_stream->IsOk()) {
122 m_parent_i_stream->Read(m_z_buffer, m_z_size);
123 m_inflate->next_in = m_z_buffer;
124 m_inflate->avail_in = m_parent_i_stream->LastRead();
125 }
126 err = inflate(m_inflate, Z_SYNC_FLUSH);
127 }
128
129 switch (err) {
130 case Z_OK:
131 break;
132
133 case Z_STREAM_END:
134 // Unread any data taken from past the end of the deflate stream, so that
135 // any additional data can be read from the underlying stream (the crc
136 // in a gzip for example)
137 if (m_inflate->avail_in) {
138 m_parent_i_stream->Ungetch(m_inflate->next_in, m_inflate->avail_in);
139 m_inflate->avail_in = 0;
140 }
141 m_lasterror = wxSTREAM_EOF;
142 break;
143
144 case Z_BUF_ERROR:
145 // Indicates that zlib was expecting more data, but the parent stream
146 // has none. Other than Eof the error will have been already reported
147 // by the parent strean,
148 m_lasterror = wxSTREAM_READ_ERROR;
149 if (m_parent_i_stream->Eof())
150 #if WXWIN_COMPATIBILITY_2_4
151 if (m_24compatibilty)
152 m_lasterror = wxSTREAM_EOF;
153 else
154 #endif
155 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
156 break;
157
158 default:
159 wxString msg(m_inflate->msg, *wxConvCurrent);
160 if (!msg)
161 msg = wxString::Format(_("zlib error %d"), err);
162 wxLogError(_("Can't read from inflate stream: %s"), msg.c_str());
163 m_lasterror = wxSTREAM_READ_ERROR;
164 }
165
166 size -= m_inflate->avail_out;
167 m_pos += size;
168 return size;
169 }
170
171 /* static */ bool wxZlibInputStream::CanHandleGZip()
172 {
173 const char *dot = strchr(zlibVersion(), '.');
174 int major = atoi(zlibVersion());
175 int minor = dot ? atoi(dot + 1) : 0;
176 return major > 1 || (major == 1 && minor >= 2);
177 }
178
179
180 //////////////////////
181 // wxZlibOutputStream
182 //////////////////////
183
184 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream,
185 int level,
186 int flags)
187 : wxFilterOutputStream(stream)
188 {
189 m_deflate = NULL;
190 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
191 m_z_size = ZSTREAM_BUFFER_SIZE;
192 m_pos = 0;
193
194 if ( level == -1 )
195 {
196 level = Z_DEFAULT_COMPRESSION;
197 }
198 else
199 {
200 wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
201 }
202
203 // if gzip is asked for but not supported...
204 if (flags == wxZLIB_GZIP && !CanHandleGZip()) {
205 wxLogError(_("Gzip not supported by this version of zlib"));
206 m_lasterror = wxSTREAM_WRITE_ERROR;
207 return;
208 }
209
210 if (m_z_buffer) {
211 m_deflate = new z_stream_s;
212
213 if (m_deflate) {
214 memset(m_deflate, 0, sizeof(z_stream_s));
215 m_deflate->next_out = m_z_buffer;
216 m_deflate->avail_out = m_z_size;
217
218 // see zlib.h for documentation on windowBits
219 int windowBits = MAX_WBITS;
220 switch (flags) {
221 case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break;
222 case wxZLIB_ZLIB: windowBits = MAX_WBITS; break;
223 case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break;
224 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
225 }
226
227 if (deflateInit2(m_deflate, level, Z_DEFLATED, windowBits,
228 8, Z_DEFAULT_STRATEGY) == Z_OK)
229 return;
230 }
231 }
232
233 wxLogError(_("Can't initialize zlib deflate stream."));
234 m_lasterror = wxSTREAM_WRITE_ERROR;
235 }
236
237 bool wxZlibOutputStream::Close()
238 {
239 DoFlush(true);
240 deflateEnd(m_deflate);
241 delete m_deflate;
242
243 m_deflate = NULL;
244 delete[] m_z_buffer;
245 m_z_buffer = NULL;
246 return IsOk();
247 }
248
249 void wxZlibOutputStream::DoFlush(bool final)
250 {
251 if (!m_deflate || !m_z_buffer)
252 m_lasterror = wxSTREAM_WRITE_ERROR;
253 if (!IsOk())
254 return;
255
256 int err = Z_OK;
257 bool done = false;
258
259 while (err == Z_OK || err == Z_STREAM_END) {
260 size_t len = m_z_size - m_deflate->avail_out;
261 if (len) {
262 if (m_parent_o_stream->Write(m_z_buffer, len).LastWrite() != len) {
263 m_lasterror = wxSTREAM_WRITE_ERROR;
264 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
265 break;
266 }
267 m_deflate->next_out = m_z_buffer;
268 m_deflate->avail_out = m_z_size;
269 }
270
271 if (done)
272 break;
273 err = deflate(m_deflate, final ? Z_FINISH : Z_FULL_FLUSH);
274 done = m_deflate->avail_out != 0 || err == Z_STREAM_END;
275 }
276 }
277
278 size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
279 {
280 wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open"));
281
282 if (!m_deflate || !m_z_buffer)
283 m_lasterror = wxSTREAM_WRITE_ERROR;
284 if (!IsOk() || !size)
285 return 0;
286
287 int err = Z_OK;
288 m_deflate->next_in = (unsigned char *)buffer;
289 m_deflate->avail_in = size;
290
291 while (err == Z_OK && m_deflate->avail_in > 0) {
292 if (m_deflate->avail_out == 0) {
293 m_parent_o_stream->Write(m_z_buffer, m_z_size);
294 if (m_parent_o_stream->LastWrite() != m_z_size) {
295 m_lasterror = wxSTREAM_WRITE_ERROR;
296 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
297 break;
298 }
299
300 m_deflate->next_out = m_z_buffer;
301 m_deflate->avail_out = m_z_size;
302 }
303
304 err = deflate(m_deflate, Z_NO_FLUSH);
305 }
306
307 if (err != Z_OK) {
308 m_lasterror = wxSTREAM_WRITE_ERROR;
309 wxString msg(m_deflate->msg, *wxConvCurrent);
310 if (!msg)
311 msg = wxString::Format(_("zlib error %d"), err);
312 wxLogError(_("Can't write to deflate stream: %s"), msg.c_str());
313 }
314
315 size -= m_deflate->avail_in;
316 m_pos += size;
317 return size;
318 }
319
320 /* static */ bool wxZlibOutputStream::CanHandleGZip()
321 {
322 return wxZlibInputStream::CanHandleGZip();
323 }
324
325 #endif
326 // wxUSE_ZLIB && wxUSE_STREAMS
327