1 //////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/zstream.cpp
3 // Purpose: Compressed stream classes
4 // Author: Guilhem Lavaux
5 // Modified by: Mike Wetherell
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_ZLIB && wxUSE_STREAMS
21 #include "wx/zstream.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(__WXMSW__) && !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 /////////////////////////////////////////////////////////////////////////////
50 IMPLEMENT_DYNAMIC_CLASS(wxZlibClassFactory
, wxFilterClassFactory
)
52 static wxZlibClassFactory g_wxZlibClassFactory
;
54 wxZlibClassFactory::wxZlibClassFactory()
56 if (this == &g_wxZlibClassFactory
)
60 const wxChar
* const *
61 wxZlibClassFactory::GetProtocols(wxStreamProtocolType type
) const
63 static const wxChar
*mimes
[] = { _T("application/x-deflate"), NULL
};
64 static const wxChar
*encs
[] = { _T("deflate"), NULL
};
65 static const wxChar
*empty
[] = { NULL
};
68 case wxSTREAM_MIMETYPE
: return mimes
;
69 case wxSTREAM_ENCODING
: return encs
;
70 default: return empty
;
75 /////////////////////////////////////////////////////////////////////////////
78 IMPLEMENT_DYNAMIC_CLASS(wxGzipClassFactory
, wxFilterClassFactory
)
80 static wxGzipClassFactory g_wxGzipClassFactory
;
82 wxGzipClassFactory::wxGzipClassFactory()
84 if (this == &g_wxGzipClassFactory
&& wxZlibInputStream::CanHandleGZip())
88 const wxChar
* const *
89 wxGzipClassFactory::GetProtocols(wxStreamProtocolType type
) const
91 static const wxChar
*protos
[] =
93 static const wxChar
*mimes
[] =
94 { _T("application/gzip"), _T("application/x-gzip"), NULL
};
95 static const wxChar
*encs
[] =
97 static const wxChar
*exts
[] =
98 { _T(".gz"), _T(".gzip"), NULL
};
99 static const wxChar
*empty
[] =
103 case wxSTREAM_PROTOCOL
: return protos
;
104 case wxSTREAM_MIMETYPE
: return mimes
;
105 case wxSTREAM_ENCODING
: return encs
;
106 case wxSTREAM_FILEEXT
: return exts
;
107 default: return empty
;
112 //////////////////////
114 //////////////////////
116 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
, int flags
)
117 : wxFilterInputStream(stream
)
122 wxZlibInputStream::wxZlibInputStream(wxInputStream
*stream
, int flags
)
123 : wxFilterInputStream(stream
)
128 void wxZlibInputStream::Init(int flags
)
131 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
132 m_z_size
= ZSTREAM_BUFFER_SIZE
;
135 // if gzip is asked for but not supported...
136 if ((flags
== wxZLIB_GZIP
|| flags
== wxZLIB_AUTO
) && !CanHandleGZip()) {
137 if (flags
== wxZLIB_AUTO
) {
138 // an error will come later if the input turns out not to be a zlib
142 wxLogError(_("Gzip not supported by this version of zlib"));
143 m_lasterror
= wxSTREAM_READ_ERROR
;
149 m_inflate
= new z_stream_s
;
152 memset(m_inflate
, 0, sizeof(z_stream_s
));
154 // see zlib.h for documentation on windowBits
155 int windowBits
= MAX_WBITS
;
157 case wxZLIB_NO_HEADER
: windowBits
= -MAX_WBITS
; break;
158 case wxZLIB_ZLIB
: windowBits
= MAX_WBITS
; break;
159 case wxZLIB_GZIP
: windowBits
= MAX_WBITS
| ZSTREAM_GZIP
; break;
160 case wxZLIB_AUTO
: windowBits
= MAX_WBITS
| ZSTREAM_AUTO
; break;
161 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
164 if (inflateInit2(m_inflate
, windowBits
) == Z_OK
)
169 wxLogError(_("Can't initialize zlib inflate stream."));
170 m_lasterror
= wxSTREAM_READ_ERROR
;
173 wxZlibInputStream::~wxZlibInputStream()
175 inflateEnd(m_inflate
);
178 delete [] m_z_buffer
;
181 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
183 wxASSERT_MSG(m_inflate
&& m_z_buffer
, wxT("Inflate stream not open"));
185 if (!m_inflate
|| !m_z_buffer
)
186 m_lasterror
= wxSTREAM_READ_ERROR
;
187 if (!IsOk() || !size
)
191 m_inflate
->next_out
= (unsigned char *)buffer
;
192 m_inflate
->avail_out
= size
;
194 while (err
== Z_OK
&& m_inflate
->avail_out
> 0) {
195 if (m_inflate
->avail_in
== 0 && m_parent_i_stream
->IsOk()) {
196 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
197 m_inflate
->next_in
= m_z_buffer
;
198 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
200 err
= inflate(m_inflate
, Z_SYNC_FLUSH
);
208 if (m_inflate
->avail_out
) {
209 // Unread any data taken from past the end of the deflate stream, so that
210 // any additional data can be read from the underlying stream (the crc
211 // in a gzip for example)
212 if (m_inflate
->avail_in
) {
213 m_parent_i_stream
->Reset();
214 m_parent_i_stream
->Ungetch(m_inflate
->next_in
, m_inflate
->avail_in
);
215 m_inflate
->avail_in
= 0;
217 m_lasterror
= wxSTREAM_EOF
;
222 // Indicates that zlib was expecting more data, but the parent stream
223 // has none. Other than Eof the error will have been already reported
224 // by the parent strean,
225 m_lasterror
= wxSTREAM_READ_ERROR
;
226 if (m_parent_i_stream
->Eof())
227 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
231 wxString
msg(m_inflate
->msg
, *wxConvCurrent
);
233 msg
= wxString::Format(_("zlib error %d"), err
);
234 wxLogError(_("Can't read from inflate stream: %s"), msg
.c_str());
235 m_lasterror
= wxSTREAM_READ_ERROR
;
238 size
-= m_inflate
->avail_out
;
243 /* static */ bool wxZlibInputStream::CanHandleGZip()
245 const char *dot
= strchr(zlibVersion(), '.');
246 int major
= atoi(zlibVersion());
247 int minor
= dot
? atoi(dot
+ 1) : 0;
248 return major
> 1 || (major
== 1 && minor
>= 2);
251 bool wxZlibInputStream::SetDictionary(const char *data
, const size_t datalen
)
253 return (inflateSetDictionary(m_inflate
, (Bytef
*)data
, datalen
) == Z_OK
);
256 bool wxZlibInputStream::SetDictionary(const wxMemoryBuffer
&buf
)
258 return SetDictionary((char*)buf
.GetData(), buf
.GetDataLen());
262 //////////////////////
263 // wxZlibOutputStream
264 //////////////////////
266 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
,
269 : wxFilterOutputStream(stream
)
274 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
*stream
,
277 : wxFilterOutputStream(stream
)
282 void wxZlibOutputStream::Init(int level
, int flags
)
285 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
286 m_z_size
= ZSTREAM_BUFFER_SIZE
;
291 level
= Z_DEFAULT_COMPRESSION
;
295 wxASSERT_MSG(level
>= 0 && level
<= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
298 // if gzip is asked for but not supported...
299 if (flags
== wxZLIB_GZIP
&& !CanHandleGZip()) {
300 wxLogError(_("Gzip not supported by this version of zlib"));
301 m_lasterror
= wxSTREAM_WRITE_ERROR
;
306 m_deflate
= new z_stream_s
;
309 memset(m_deflate
, 0, sizeof(z_stream_s
));
310 m_deflate
->next_out
= m_z_buffer
;
311 m_deflate
->avail_out
= m_z_size
;
313 // see zlib.h for documentation on windowBits
314 int windowBits
= MAX_WBITS
;
316 case wxZLIB_NO_HEADER
: windowBits
= -MAX_WBITS
; break;
317 case wxZLIB_ZLIB
: windowBits
= MAX_WBITS
; break;
318 case wxZLIB_GZIP
: windowBits
= MAX_WBITS
| ZSTREAM_GZIP
; break;
319 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
322 if (deflateInit2(m_deflate
, level
, Z_DEFLATED
, windowBits
,
323 8, Z_DEFAULT_STRATEGY
) == Z_OK
)
328 wxLogError(_("Can't initialize zlib deflate stream."));
329 m_lasterror
= wxSTREAM_WRITE_ERROR
;
332 bool wxZlibOutputStream::Close()
335 deflateEnd(m_deflate
);
342 return wxFilterOutputStream::Close() && IsOk();
345 void wxZlibOutputStream::DoFlush(bool final
)
347 if (!m_deflate
|| !m_z_buffer
)
348 m_lasterror
= wxSTREAM_WRITE_ERROR
;
355 while (err
== Z_OK
|| err
== Z_STREAM_END
) {
356 size_t len
= m_z_size
- m_deflate
->avail_out
;
358 if (m_parent_o_stream
->Write(m_z_buffer
, len
).LastWrite() != len
) {
359 m_lasterror
= wxSTREAM_WRITE_ERROR
;
360 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
363 m_deflate
->next_out
= m_z_buffer
;
364 m_deflate
->avail_out
= m_z_size
;
369 err
= deflate(m_deflate
, final
? Z_FINISH
: Z_FULL_FLUSH
);
370 done
= m_deflate
->avail_out
!= 0 || err
== Z_STREAM_END
;
374 size_t wxZlibOutputStream::OnSysWrite(const void *buffer
, size_t size
)
376 wxASSERT_MSG(m_deflate
&& m_z_buffer
, wxT("Deflate stream not open"));
378 if (!m_deflate
|| !m_z_buffer
)
380 // notice that this will make IsOk() test just below return false
381 m_lasterror
= wxSTREAM_WRITE_ERROR
;
384 if (!IsOk() || !size
)
388 m_deflate
->next_in
= (unsigned char *)buffer
;
389 m_deflate
->avail_in
= size
;
391 while (err
== Z_OK
&& m_deflate
->avail_in
> 0) {
392 if (m_deflate
->avail_out
== 0) {
393 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
394 if (m_parent_o_stream
->LastWrite() != m_z_size
) {
395 m_lasterror
= wxSTREAM_WRITE_ERROR
;
396 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
400 m_deflate
->next_out
= m_z_buffer
;
401 m_deflate
->avail_out
= m_z_size
;
404 err
= deflate(m_deflate
, Z_NO_FLUSH
);
408 m_lasterror
= wxSTREAM_WRITE_ERROR
;
409 wxString
msg(m_deflate
->msg
, *wxConvCurrent
);
411 msg
= wxString::Format(_("zlib error %d"), err
);
412 wxLogError(_("Can't write to deflate stream: %s"), msg
.c_str());
415 size
-= m_deflate
->avail_in
;
420 /* static */ bool wxZlibOutputStream::CanHandleGZip()
422 return wxZlibInputStream::CanHandleGZip();
425 bool wxZlibOutputStream::SetDictionary(const char *data
, const size_t datalen
)
427 return (deflateSetDictionary(m_deflate
, (Bytef
*)data
, datalen
) == Z_OK
);
430 bool wxZlibOutputStream::SetDictionary(const wxMemoryBuffer
&buf
)
432 return SetDictionary((char*)buf
.GetData(), buf
.GetDataLen());
436 // wxUSE_ZLIB && wxUSE_STREAMS