]> git.saurik.com Git - wxWidgets.git/blame - src/common/zstream.cpp
Don't crash on malformed HTML in wxHTML font tag handler.
[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"
ccec9093 22#include "wx/versioninfo.h"
88a7a4e1
WS
23
24#ifndef WX_PRECOMP
25 #include "wx/intl.h"
e4db172a 26 #include "wx/log.h"
de6185e2 27 #include "wx/utils.h"
88a7a4e1
WS
28#endif
29
d1af991f 30
f6bcfd97
BP
31// normally, the compiler options should contain -I../zlib, but it is
32// apparently not the case for all MSW makefiles and so, unless we use
33// configure (which defines __WX_SETUP_H__) or it is explicitly overridden by
34// the user (who can define wxUSE_ZLIB_H_IN_PATH), we hardcode the path here
35#if defined(__WXMSW__) && !defined(__WX_SETUP_H__) && !defined(wxUSE_ZLIB_H_IN_PATH)
de6185e2 36 #include "../zlib/zlib.h"
d1af991f 37#else
de6185e2 38 #include "zlib.h"
d1af991f 39#endif
79c3e0e1 40
0915d0b2 41enum {
4c68a102
VS
42 ZSTREAM_BUFFER_SIZE = 16384,
43 ZSTREAM_GZIP = 0x10, // gzip header
44 ZSTREAM_AUTO = 0x20 // auto detect between gzip and zlib
0915d0b2 45};
6d44bf31 46
55420742 47
ccec9093
VZ
48wxVersionInfo wxGetZlibVersionInfo()
49{
50 return wxVersionInfo("zlib",
51 ZLIB_VERNUM >> 12,
52 (ZLIB_VERNUM >> 8) & 0x0F,
53 (ZLIB_VERNUM & 0xFF) / 0x10);
54}
55
55420742
MW
56/////////////////////////////////////////////////////////////////////////////
57// Zlib Class factory
58
59IMPLEMENT_DYNAMIC_CLASS(wxZlibClassFactory, wxFilterClassFactory)
60
61static wxZlibClassFactory g_wxZlibClassFactory;
62
63wxZlibClassFactory::wxZlibClassFactory()
64{
65 if (this == &g_wxZlibClassFactory)
66 PushFront();
67}
68
69const wxChar * const *
70wxZlibClassFactory::GetProtocols(wxStreamProtocolType type) const
71{
9a83f860
VZ
72 static const wxChar *mimes[] = { wxT("application/x-deflate"), NULL };
73 static const wxChar *encs[] = { wxT("deflate"), NULL };
55420742
MW
74 static const wxChar *empty[] = { NULL };
75
76 switch (type) {
77 case wxSTREAM_MIMETYPE: return mimes;
78 case wxSTREAM_ENCODING: return encs;
79 default: return empty;
80 }
81}
82
83
84/////////////////////////////////////////////////////////////////////////////
85// Gzip Class factory
86
87IMPLEMENT_DYNAMIC_CLASS(wxGzipClassFactory, wxFilterClassFactory)
88
89static wxGzipClassFactory g_wxGzipClassFactory;
90
91wxGzipClassFactory::wxGzipClassFactory()
92{
93 if (this == &g_wxGzipClassFactory && wxZlibInputStream::CanHandleGZip())
94 PushFront();
95}
96
97const wxChar * const *
98wxGzipClassFactory::GetProtocols(wxStreamProtocolType type) const
99{
03647350 100 static const wxChar *protos[] =
9a83f860 101 { wxT("gzip"), NULL };
03647350 102 static const wxChar *mimes[] =
9a83f860 103 { wxT("application/gzip"), wxT("application/x-gzip"), NULL };
03647350 104 static const wxChar *encs[] =
9a83f860 105 { wxT("gzip"), NULL };
03647350 106 static const wxChar *exts[] =
9a83f860 107 { wxT(".gz"), wxT(".gzip"), NULL };
55420742
MW
108 static const wxChar *empty[] =
109 { NULL };
110
111 switch (type) {
489a164c
MW
112 case wxSTREAM_PROTOCOL: return protos;
113 case wxSTREAM_MIMETYPE: return mimes;
114 case wxSTREAM_ENCODING: return encs;
115 case wxSTREAM_FILEEXT: return exts;
116 default: return empty;
55420742
MW
117 }
118}
119
120
79c3e0e1
GL
121//////////////////////
122// wxZlibInputStream
123//////////////////////
124
0915d0b2 125wxZlibInputStream::wxZlibInputStream(wxInputStream& stream, int flags)
79c3e0e1 126 : wxFilterInputStream(stream)
55420742
MW
127{
128 Init(flags);
129}
130
131wxZlibInputStream::wxZlibInputStream(wxInputStream *stream, int flags)
132 : wxFilterInputStream(stream)
133{
134 Init(flags);
135}
136
137void wxZlibInputStream::Init(int flags)
79c3e0e1 138{
0915d0b2
VZ
139 m_inflate = NULL;
140 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
141 m_z_size = ZSTREAM_BUFFER_SIZE;
142 m_pos = 0;
6d44bf31 143
4c68a102
VS
144 // if gzip is asked for but not supported...
145 if ((flags == wxZLIB_GZIP || flags == wxZLIB_AUTO) && !CanHandleGZip()) {
146 if (flags == wxZLIB_AUTO) {
147 // an error will come later if the input turns out not to be a zlib
148 flags = wxZLIB_ZLIB;
149 }
150 else {
151 wxLogError(_("Gzip not supported by this version of zlib"));
152 m_lasterror = wxSTREAM_READ_ERROR;
153 return;
154 }
155 }
156
0915d0b2
VZ
157 if (m_z_buffer) {
158 m_inflate = new z_stream_s;
79c3e0e1 159
0915d0b2 160 if (m_inflate) {
5fc01d13 161 memset(m_inflate, 0, sizeof(z_stream_s));
79c3e0e1 162
4c68a102
VS
163 // see zlib.h for documentation on windowBits
164 int windowBits = MAX_WBITS;
165 switch (flags) {
166 case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break;
167 case wxZLIB_ZLIB: windowBits = MAX_WBITS; break;
168 case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break;
169 case wxZLIB_AUTO: windowBits = MAX_WBITS | ZSTREAM_AUTO; break;
170 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
171 }
6d44bf31 172
4c68a102 173 if (inflateInit2(m_inflate, windowBits) == Z_OK)
0915d0b2
VZ
174 return;
175 }
176 }
177
178 wxLogError(_("Can't initialize zlib inflate stream."));
179 m_lasterror = wxSTREAM_READ_ERROR;
79c3e0e1
GL
180}
181
182wxZlibInputStream::~wxZlibInputStream()
183{
856d2e52
GL
184 inflateEnd(m_inflate);
185 delete m_inflate;
45805ba3
VZ
186
187 delete [] m_z_buffer;
79c3e0e1
GL
188}
189
75ed1d15 190size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size)
79c3e0e1 191{
0915d0b2
VZ
192 wxASSERT_MSG(m_inflate && m_z_buffer, wxT("Inflate stream not open"));
193
194 if (!m_inflate || !m_z_buffer)
195 m_lasterror = wxSTREAM_READ_ERROR;
196 if (!IsOk() || !size)
197 return 0;
79c3e0e1 198
0915d0b2 199 int err = Z_OK;
856d2e52
GL
200 m_inflate->next_out = (unsigned char *)buffer;
201 m_inflate->avail_out = size;
79c3e0e1 202
0915d0b2 203 while (err == Z_OK && m_inflate->avail_out > 0) {
4c68a102 204 if (m_inflate->avail_in == 0 && m_parent_i_stream->IsOk()) {
0915d0b2 205 m_parent_i_stream->Read(m_z_buffer, m_z_size);
856d2e52
GL
206 m_inflate->next_in = m_z_buffer;
207 m_inflate->avail_in = m_parent_i_stream->LastRead();
4c68a102
VS
208 }
209 err = inflate(m_inflate, Z_SYNC_FLUSH);
210 }
6d44bf31 211
4c68a102
VS
212 switch (err) {
213 case Z_OK:
0915d0b2 214 break;
4c68a102
VS
215
216 case Z_STREAM_END:
8e50d1ad
MW
217 if (m_inflate->avail_out) {
218 // Unread any data taken from past the end of the deflate stream, so that
219 // any additional data can be read from the underlying stream (the crc
220 // in a gzip for example)
221 if (m_inflate->avail_in) {
abffc1ff 222 m_parent_i_stream->Reset();
8e50d1ad
MW
223 m_parent_i_stream->Ungetch(m_inflate->next_in, m_inflate->avail_in);
224 m_inflate->avail_in = 0;
225 }
226 m_lasterror = wxSTREAM_EOF;
777fd647 227 }
4c68a102 228 break;
f6bcfd97 229
4c68a102
VS
230 case Z_BUF_ERROR:
231 // Indicates that zlib was expecting more data, but the parent stream
232 // has none. Other than Eof the error will have been already reported
233 // by the parent strean,
234 m_lasterror = wxSTREAM_READ_ERROR;
235 if (m_parent_i_stream->Eof())
af588446 236 {
4c68a102 237 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
af588446 238 }
4c68a102
VS
239 break;
240
241 default:
242 wxString msg(m_inflate->msg, *wxConvCurrent);
243 if (!msg)
2a1f999f 244 msg = wxString::Format(_("zlib error %d"), err);
4c68a102
VS
245 wxLogError(_("Can't read from inflate stream: %s"), msg.c_str());
246 m_lasterror = wxSTREAM_READ_ERROR;
79c3e0e1
GL
247 }
248
0915d0b2
VZ
249 size -= m_inflate->avail_out;
250 m_pos += size;
251 return size;
79c3e0e1
GL
252}
253
4c68a102
VS
254/* static */ bool wxZlibInputStream::CanHandleGZip()
255{
256 const char *dot = strchr(zlibVersion(), '.');
257 int major = atoi(zlibVersion());
258 int minor = dot ? atoi(dot + 1) : 0;
259 return major > 1 || (major == 1 && minor >= 2);
260}
261
51acf83b
VZ
262bool wxZlibInputStream::SetDictionary(const char *data, const size_t datalen)
263{
264 return (inflateSetDictionary(m_inflate, (Bytef*)data, datalen) == Z_OK);
265}
266
267bool wxZlibInputStream::SetDictionary(const wxMemoryBuffer &buf)
268{
269 return SetDictionary((char*)buf.GetData(), buf.GetDataLen());
270}
271
4c68a102 272
79c3e0e1
GL
273//////////////////////
274// wxZlibOutputStream
275//////////////////////
276
0915d0b2
VZ
277wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream,
278 int level,
279 int flags)
79c3e0e1 280 : wxFilterOutputStream(stream)
55420742
MW
281{
282 Init(level, flags);
283}
284
285wxZlibOutputStream::wxZlibOutputStream(wxOutputStream *stream,
286 int level,
287 int flags)
288 : wxFilterOutputStream(stream)
289{
290 Init(level, flags);
291}
292
293void wxZlibOutputStream::Init(int level, int flags)
79c3e0e1 294{
0915d0b2
VZ
295 m_deflate = NULL;
296 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
297 m_z_size = ZSTREAM_BUFFER_SIZE;
298 m_pos = 0;
79c3e0e1 299
af850422
VZ
300 if ( level == -1 )
301 {
0915d0b2 302 level = Z_DEFAULT_COMPRESSION;
af850422
VZ
303 }
304 else
305 {
306 wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
307 }
5824f314 308
4c68a102
VS
309 // if gzip is asked for but not supported...
310 if (flags == wxZLIB_GZIP && !CanHandleGZip()) {
311 wxLogError(_("Gzip not supported by this version of zlib"));
312 m_lasterror = wxSTREAM_WRITE_ERROR;
313 return;
314 }
6d44bf31 315
4c68a102
VS
316 if (m_z_buffer) {
317 m_deflate = new z_stream_s;
0915d0b2 318
4c68a102
VS
319 if (m_deflate) {
320 memset(m_deflate, 0, sizeof(z_stream_s));
321 m_deflate->next_out = m_z_buffer;
322 m_deflate->avail_out = m_z_size;
cab1a605 323
4c68a102
VS
324 // see zlib.h for documentation on windowBits
325 int windowBits = MAX_WBITS;
326 switch (flags) {
327 case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break;
328 case wxZLIB_ZLIB: windowBits = MAX_WBITS; break;
329 case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break;
330 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
331 }
332
cab1a605 333 if (deflateInit2(m_deflate, level, Z_DEFLATED, windowBits,
4c68a102
VS
334 8, Z_DEFAULT_STRATEGY) == Z_OK)
335 return;
336 }
0915d0b2
VZ
337 }
338
339 wxLogError(_("Can't initialize zlib deflate stream."));
340 m_lasterror = wxSTREAM_WRITE_ERROR;
79c3e0e1
GL
341}
342
8f0ff178
RN
343bool wxZlibOutputStream::Close()
344 {
345 DoFlush(true);
346 deflateEnd(m_deflate);
5276b0a5
VZ
347 wxDELETE(m_deflate);
348 wxDELETEA(m_z_buffer);
55420742
MW
349
350 return wxFilterOutputStream::Close() && IsOk();
8f0ff178 351 }
79c3e0e1 352
0915d0b2 353void wxZlibOutputStream::DoFlush(bool final)
6d44bf31 354{
0915d0b2
VZ
355 if (!m_deflate || !m_z_buffer)
356 m_lasterror = wxSTREAM_WRITE_ERROR;
357 if (!IsOk())
6d44bf31 358 return;
6d44bf31 359
0915d0b2
VZ
360 int err = Z_OK;
361 bool done = false;
babd4308 362
0915d0b2
VZ
363 while (err == Z_OK || err == Z_STREAM_END) {
364 size_t len = m_z_size - m_deflate->avail_out;
365 if (len) {
366 if (m_parent_o_stream->Write(m_z_buffer, len).LastWrite() != len) {
367 m_lasterror = wxSTREAM_WRITE_ERROR;
368 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
369 break;
370 }
371 m_deflate->next_out = m_z_buffer;
372 m_deflate->avail_out = m_z_size;
373 }
374
375 if (done)
376 break;
377 err = deflate(m_deflate, final ? Z_FINISH : Z_FULL_FLUSH);
378 done = m_deflate->avail_out != 0 || err == Z_STREAM_END;
379 }
6d44bf31
GL
380}
381
75ed1d15 382size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1 383{
0915d0b2 384 wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open"));
79c3e0e1 385
0915d0b2 386 if (!m_deflate || !m_z_buffer)
10793ebf 387 {
517cce71 388 // notice that this will make IsOk() test just below return false
0915d0b2 389 m_lasterror = wxSTREAM_WRITE_ERROR;
10793ebf
VZ
390 }
391
0915d0b2
VZ
392 if (!IsOk() || !size)
393 return 0;
394
395 int err = Z_OK;
856d2e52
GL
396 m_deflate->next_in = (unsigned char *)buffer;
397 m_deflate->avail_in = size;
79c3e0e1 398
0915d0b2 399 while (err == Z_OK && m_deflate->avail_in > 0) {
856d2e52 400 if (m_deflate->avail_out == 0) {
6d44bf31 401 m_parent_o_stream->Write(m_z_buffer, m_z_size);
0915d0b2
VZ
402 if (m_parent_o_stream->LastWrite() != m_z_size) {
403 m_lasterror = wxSTREAM_WRITE_ERROR;
404 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
405 break;
406 }
6d44bf31 407
856d2e52
GL
408 m_deflate->next_out = m_z_buffer;
409 m_deflate->avail_out = m_z_size;
79c3e0e1 410 }
6d44bf31 411
856d2e52 412 err = deflate(m_deflate, Z_NO_FLUSH);
79c3e0e1 413 }
0915d0b2
VZ
414
415 if (err != Z_OK) {
416 m_lasterror = wxSTREAM_WRITE_ERROR;
301deecc
RN
417 wxString msg(m_deflate->msg, *wxConvCurrent);
418 if (!msg)
2a1f999f 419 msg = wxString::Format(_("zlib error %d"), err);
4c68a102 420 wxLogError(_("Can't write to deflate stream: %s"), msg.c_str());
0915d0b2
VZ
421 }
422
423 size -= m_deflate->avail_in;
424 m_pos += size;
6d44bf31 425 return size;
79c3e0e1 426}
ac57418f 427
4c68a102 428/* static */ bool wxZlibOutputStream::CanHandleGZip()
cab1a605 429{
4c68a102
VS
430 return wxZlibInputStream::CanHandleGZip();
431}
432
51acf83b
VZ
433bool wxZlibOutputStream::SetDictionary(const char *data, const size_t datalen)
434{
435 return (deflateSetDictionary(m_deflate, (Bytef*)data, datalen) == Z_OK);
436}
437
438bool wxZlibOutputStream::SetDictionary(const wxMemoryBuffer &buf)
439{
440 return SetDictionary((char*)buf.GetData(), buf.GetDataLen());
441}
442
ac57418f 443#endif
ce4169a4 444 // wxUSE_ZLIB && wxUSE_STREAMS