1 //////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/zstream.cpp
3 // Purpose: Compressed stream classes
4 // Author: Guilhem Lavaux
5 // Modified by: Mike Wetherell
7 // Copyright: (c) Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_ZLIB && wxUSE_STREAMS
20 #include "wx/zstream.h"
21 #include "wx/versioninfo.h"
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(__WINDOWS__) && !defined(__WX_SETUP_H__) && !defined(wxUSE_ZLIB_H_IN_PATH)
35 #include "../zlib/zlib.h"
41 ZSTREAM_BUFFER_SIZE
= 16384,
42 ZSTREAM_GZIP
= 0x10, // gzip header
43 ZSTREAM_AUTO
= 0x20 // auto detect between gzip and zlib
47 wxVersionInfo
wxGetZlibVersionInfo()
53 if ( sscanf(zlibVersion(), "%d.%d.%d", &major
, &minor
, &build
) != 3 )
60 return wxVersionInfo("zlib", major
, minor
, build
);
63 /////////////////////////////////////////////////////////////////////////////
66 IMPLEMENT_DYNAMIC_CLASS(wxZlibClassFactory
, wxFilterClassFactory
)
68 static wxZlibClassFactory g_wxZlibClassFactory
;
70 wxZlibClassFactory::wxZlibClassFactory()
72 if (this == &g_wxZlibClassFactory
)
76 const wxChar
* const *
77 wxZlibClassFactory::GetProtocols(wxStreamProtocolType type
) const
79 static const wxChar
*mimes
[] = { wxT("application/x-deflate"), NULL
};
80 static const wxChar
*encs
[] = { wxT("deflate"), NULL
};
81 static const wxChar
*empty
[] = { NULL
};
84 case wxSTREAM_MIMETYPE
: return mimes
;
85 case wxSTREAM_ENCODING
: return encs
;
86 default: return empty
;
91 /////////////////////////////////////////////////////////////////////////////
94 IMPLEMENT_DYNAMIC_CLASS(wxGzipClassFactory
, wxFilterClassFactory
)
96 static wxGzipClassFactory g_wxGzipClassFactory
;
98 wxGzipClassFactory::wxGzipClassFactory()
100 if (this == &g_wxGzipClassFactory
&& wxZlibInputStream::CanHandleGZip())
104 const wxChar
* const *
105 wxGzipClassFactory::GetProtocols(wxStreamProtocolType type
) const
107 static const wxChar
*protos
[] =
108 { wxT("gzip"), NULL
};
109 static const wxChar
*mimes
[] =
110 { wxT("application/gzip"), wxT("application/x-gzip"), NULL
};
111 static const wxChar
*encs
[] =
112 { wxT("gzip"), NULL
};
113 static const wxChar
*exts
[] =
114 { wxT(".gz"), wxT(".gzip"), NULL
};
115 static const wxChar
*empty
[] =
119 case wxSTREAM_PROTOCOL
: return protos
;
120 case wxSTREAM_MIMETYPE
: return mimes
;
121 case wxSTREAM_ENCODING
: return encs
;
122 case wxSTREAM_FILEEXT
: return exts
;
123 default: return empty
;
128 //////////////////////
130 //////////////////////
132 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
, int flags
)
133 : wxFilterInputStream(stream
)
138 wxZlibInputStream::wxZlibInputStream(wxInputStream
*stream
, int flags
)
139 : wxFilterInputStream(stream
)
144 void wxZlibInputStream::Init(int flags
)
147 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
148 m_z_size
= ZSTREAM_BUFFER_SIZE
;
151 // if gzip is asked for but not supported...
152 if ((flags
== wxZLIB_GZIP
|| flags
== wxZLIB_AUTO
) && !CanHandleGZip()) {
153 if (flags
== wxZLIB_AUTO
) {
154 // an error will come later if the input turns out not to be a zlib
158 wxLogError(_("Gzip not supported by this version of zlib"));
159 m_lasterror
= wxSTREAM_READ_ERROR
;
165 m_inflate
= new z_stream_s
;
168 memset(m_inflate
, 0, sizeof(z_stream_s
));
170 // see zlib.h for documentation on windowBits
171 int windowBits
= MAX_WBITS
;
173 case wxZLIB_NO_HEADER
: windowBits
= -MAX_WBITS
; break;
174 case wxZLIB_ZLIB
: windowBits
= MAX_WBITS
; break;
175 case wxZLIB_GZIP
: windowBits
= MAX_WBITS
| ZSTREAM_GZIP
; break;
176 case wxZLIB_AUTO
: windowBits
= MAX_WBITS
| ZSTREAM_AUTO
; break;
177 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
180 if (inflateInit2(m_inflate
, windowBits
) == Z_OK
)
185 wxLogError(_("Can't initialize zlib inflate stream."));
186 m_lasterror
= wxSTREAM_READ_ERROR
;
189 wxZlibInputStream::~wxZlibInputStream()
191 inflateEnd(m_inflate
);
194 delete [] m_z_buffer
;
197 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
199 wxASSERT_MSG(m_inflate
&& m_z_buffer
, wxT("Inflate stream not open"));
201 if (!m_inflate
|| !m_z_buffer
)
202 m_lasterror
= wxSTREAM_READ_ERROR
;
203 if (!IsOk() || !size
)
207 m_inflate
->next_out
= (unsigned char *)buffer
;
208 m_inflate
->avail_out
= size
;
210 while (err
== Z_OK
&& m_inflate
->avail_out
> 0) {
211 if (m_inflate
->avail_in
== 0 && m_parent_i_stream
->IsOk()) {
212 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
213 m_inflate
->next_in
= m_z_buffer
;
214 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
216 err
= inflate(m_inflate
, Z_SYNC_FLUSH
);
224 if (m_inflate
->avail_out
) {
225 // Unread any data taken from past the end of the deflate stream, so that
226 // any additional data can be read from the underlying stream (the crc
227 // in a gzip for example)
228 if (m_inflate
->avail_in
) {
229 m_parent_i_stream
->Reset();
230 m_parent_i_stream
->Ungetch(m_inflate
->next_in
, m_inflate
->avail_in
);
231 m_inflate
->avail_in
= 0;
233 m_lasterror
= wxSTREAM_EOF
;
238 // Indicates that zlib was expecting more data, but the parent stream
239 // has none. Other than Eof the error will have been already reported
240 // by the parent strean,
241 m_lasterror
= wxSTREAM_READ_ERROR
;
242 if (m_parent_i_stream
->Eof())
244 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
249 wxString
msg(m_inflate
->msg
, *wxConvCurrent
);
251 msg
= wxString::Format(_("zlib error %d"), err
);
252 wxLogError(_("Can't read from inflate stream: %s"), msg
.c_str());
253 m_lasterror
= wxSTREAM_READ_ERROR
;
256 size
-= m_inflate
->avail_out
;
261 /* static */ bool wxZlibInputStream::CanHandleGZip()
263 const char *dot
= strchr(zlibVersion(), '.');
264 int major
= atoi(zlibVersion());
265 int minor
= dot
? atoi(dot
+ 1) : 0;
266 return major
> 1 || (major
== 1 && minor
>= 2);
269 bool wxZlibInputStream::SetDictionary(const char *data
, const size_t datalen
)
271 return (inflateSetDictionary(m_inflate
, (Bytef
*)data
, datalen
) == Z_OK
);
274 bool wxZlibInputStream::SetDictionary(const wxMemoryBuffer
&buf
)
276 return SetDictionary((char*)buf
.GetData(), buf
.GetDataLen());
280 //////////////////////
281 // wxZlibOutputStream
282 //////////////////////
284 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
,
287 : wxFilterOutputStream(stream
)
292 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
*stream
,
295 : wxFilterOutputStream(stream
)
300 void wxZlibOutputStream::Init(int level
, int flags
)
303 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
304 m_z_size
= ZSTREAM_BUFFER_SIZE
;
309 level
= Z_DEFAULT_COMPRESSION
;
313 wxASSERT_MSG(level
>= 0 && level
<= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
316 // if gzip is asked for but not supported...
317 if (flags
== wxZLIB_GZIP
&& !CanHandleGZip()) {
318 wxLogError(_("Gzip not supported by this version of zlib"));
319 m_lasterror
= wxSTREAM_WRITE_ERROR
;
324 m_deflate
= new z_stream_s
;
327 memset(m_deflate
, 0, sizeof(z_stream_s
));
328 m_deflate
->next_out
= m_z_buffer
;
329 m_deflate
->avail_out
= m_z_size
;
331 // see zlib.h for documentation on windowBits
332 int windowBits
= MAX_WBITS
;
334 case wxZLIB_NO_HEADER
: windowBits
= -MAX_WBITS
; break;
335 case wxZLIB_ZLIB
: windowBits
= MAX_WBITS
; break;
336 case wxZLIB_GZIP
: windowBits
= MAX_WBITS
| ZSTREAM_GZIP
; break;
337 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
340 if (deflateInit2(m_deflate
, level
, Z_DEFLATED
, windowBits
,
341 8, Z_DEFAULT_STRATEGY
) == Z_OK
)
346 wxLogError(_("Can't initialize zlib deflate stream."));
347 m_lasterror
= wxSTREAM_WRITE_ERROR
;
350 bool wxZlibOutputStream::Close()
353 deflateEnd(m_deflate
);
355 wxDELETEA(m_z_buffer
);
357 return wxFilterOutputStream::Close() && IsOk();
360 void wxZlibOutputStream::DoFlush(bool final
)
362 if (!m_deflate
|| !m_z_buffer
)
363 m_lasterror
= wxSTREAM_WRITE_ERROR
;
370 while (err
== Z_OK
|| err
== Z_STREAM_END
) {
371 size_t len
= m_z_size
- m_deflate
->avail_out
;
373 if (m_parent_o_stream
->Write(m_z_buffer
, len
).LastWrite() != len
) {
374 m_lasterror
= wxSTREAM_WRITE_ERROR
;
375 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
378 m_deflate
->next_out
= m_z_buffer
;
379 m_deflate
->avail_out
= m_z_size
;
384 err
= deflate(m_deflate
, final
? Z_FINISH
: Z_FULL_FLUSH
);
385 done
= m_deflate
->avail_out
!= 0 || err
== Z_STREAM_END
;
389 size_t wxZlibOutputStream::OnSysWrite(const void *buffer
, size_t size
)
391 wxASSERT_MSG(m_deflate
&& m_z_buffer
, wxT("Deflate stream not open"));
393 if (!m_deflate
|| !m_z_buffer
)
395 // notice that this will make IsOk() test just below return false
396 m_lasterror
= wxSTREAM_WRITE_ERROR
;
399 if (!IsOk() || !size
)
403 m_deflate
->next_in
= (unsigned char *)buffer
;
404 m_deflate
->avail_in
= size
;
406 while (err
== Z_OK
&& m_deflate
->avail_in
> 0) {
407 if (m_deflate
->avail_out
== 0) {
408 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
409 if (m_parent_o_stream
->LastWrite() != m_z_size
) {
410 m_lasterror
= wxSTREAM_WRITE_ERROR
;
411 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
415 m_deflate
->next_out
= m_z_buffer
;
416 m_deflate
->avail_out
= m_z_size
;
419 err
= deflate(m_deflate
, Z_NO_FLUSH
);
423 m_lasterror
= wxSTREAM_WRITE_ERROR
;
424 wxString
msg(m_deflate
->msg
, *wxConvCurrent
);
426 msg
= wxString::Format(_("zlib error %d"), err
);
427 wxLogError(_("Can't write to deflate stream: %s"), msg
.c_str());
430 size
-= m_deflate
->avail_in
;
435 /* static */ bool wxZlibOutputStream::CanHandleGZip()
437 return wxZlibInputStream::CanHandleGZip();
440 bool wxZlibOutputStream::SetDictionary(const char *data
, const size_t datalen
)
442 return (deflateSetDictionary(m_deflate
, (Bytef
*)data
, datalen
) == Z_OK
);
445 bool wxZlibOutputStream::SetDictionary(const wxMemoryBuffer
&buf
)
447 return SetDictionary((char*)buf
.GetData(), buf
.GetDataLen());
451 // wxUSE_ZLIB && wxUSE_STREAMS