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"
22 #include "wx/versioninfo.h"
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)
36 #include "../zlib/zlib.h"
42 ZSTREAM_BUFFER_SIZE
= 16384,
43 ZSTREAM_GZIP
= 0x10, // gzip header
44 ZSTREAM_AUTO
= 0x20 // auto detect between gzip and zlib
48 wxVersionInfo
wxGetZlibVersionInfo()
50 return wxVersionInfo("zlib",
52 (ZLIB_VERNUM
>> 8) & 0x0F,
53 (ZLIB_VERNUM
& 0xFF) / 0x10);
56 /////////////////////////////////////////////////////////////////////////////
59 IMPLEMENT_DYNAMIC_CLASS(wxZlibClassFactory
, wxFilterClassFactory
)
61 static wxZlibClassFactory g_wxZlibClassFactory
;
63 wxZlibClassFactory::wxZlibClassFactory()
65 if (this == &g_wxZlibClassFactory
)
69 const wxChar
* const *
70 wxZlibClassFactory::GetProtocols(wxStreamProtocolType type
) const
72 static const wxChar
*mimes
[] = { wxT("application/x-deflate"), NULL
};
73 static const wxChar
*encs
[] = { wxT("deflate"), NULL
};
74 static const wxChar
*empty
[] = { NULL
};
77 case wxSTREAM_MIMETYPE
: return mimes
;
78 case wxSTREAM_ENCODING
: return encs
;
79 default: return empty
;
84 /////////////////////////////////////////////////////////////////////////////
87 IMPLEMENT_DYNAMIC_CLASS(wxGzipClassFactory
, wxFilterClassFactory
)
89 static wxGzipClassFactory g_wxGzipClassFactory
;
91 wxGzipClassFactory::wxGzipClassFactory()
93 if (this == &g_wxGzipClassFactory
&& wxZlibInputStream::CanHandleGZip())
97 const wxChar
* const *
98 wxGzipClassFactory::GetProtocols(wxStreamProtocolType type
) const
100 static const wxChar
*protos
[] =
101 { wxT("gzip"), NULL
};
102 static const wxChar
*mimes
[] =
103 { wxT("application/gzip"), wxT("application/x-gzip"), NULL
};
104 static const wxChar
*encs
[] =
105 { wxT("gzip"), NULL
};
106 static const wxChar
*exts
[] =
107 { wxT(".gz"), wxT(".gzip"), NULL
};
108 static const wxChar
*empty
[] =
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
;
121 //////////////////////
123 //////////////////////
125 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
, int flags
)
126 : wxFilterInputStream(stream
)
131 wxZlibInputStream::wxZlibInputStream(wxInputStream
*stream
, int flags
)
132 : wxFilterInputStream(stream
)
137 void wxZlibInputStream::Init(int flags
)
140 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
141 m_z_size
= ZSTREAM_BUFFER_SIZE
;
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
151 wxLogError(_("Gzip not supported by this version of zlib"));
152 m_lasterror
= wxSTREAM_READ_ERROR
;
158 m_inflate
= new z_stream_s
;
161 memset(m_inflate
, 0, sizeof(z_stream_s
));
163 // see zlib.h for documentation on windowBits
164 int windowBits
= MAX_WBITS
;
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"));
173 if (inflateInit2(m_inflate
, windowBits
) == Z_OK
)
178 wxLogError(_("Can't initialize zlib inflate stream."));
179 m_lasterror
= wxSTREAM_READ_ERROR
;
182 wxZlibInputStream::~wxZlibInputStream()
184 inflateEnd(m_inflate
);
187 delete [] m_z_buffer
;
190 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
192 wxASSERT_MSG(m_inflate
&& m_z_buffer
, wxT("Inflate stream not open"));
194 if (!m_inflate
|| !m_z_buffer
)
195 m_lasterror
= wxSTREAM_READ_ERROR
;
196 if (!IsOk() || !size
)
200 m_inflate
->next_out
= (unsigned char *)buffer
;
201 m_inflate
->avail_out
= size
;
203 while (err
== Z_OK
&& m_inflate
->avail_out
> 0) {
204 if (m_inflate
->avail_in
== 0 && m_parent_i_stream
->IsOk()) {
205 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
206 m_inflate
->next_in
= m_z_buffer
;
207 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
209 err
= inflate(m_inflate
, Z_SYNC_FLUSH
);
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
) {
222 m_parent_i_stream
->Reset();
223 m_parent_i_stream
->Ungetch(m_inflate
->next_in
, m_inflate
->avail_in
);
224 m_inflate
->avail_in
= 0;
226 m_lasterror
= wxSTREAM_EOF
;
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())
237 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
242 wxString
msg(m_inflate
->msg
, *wxConvCurrent
);
244 msg
= wxString::Format(_("zlib error %d"), err
);
245 wxLogError(_("Can't read from inflate stream: %s"), msg
.c_str());
246 m_lasterror
= wxSTREAM_READ_ERROR
;
249 size
-= m_inflate
->avail_out
;
254 /* static */ bool wxZlibInputStream::CanHandleGZip()
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);
262 bool wxZlibInputStream::SetDictionary(const char *data
, const size_t datalen
)
264 return (inflateSetDictionary(m_inflate
, (Bytef
*)data
, datalen
) == Z_OK
);
267 bool wxZlibInputStream::SetDictionary(const wxMemoryBuffer
&buf
)
269 return SetDictionary((char*)buf
.GetData(), buf
.GetDataLen());
273 //////////////////////
274 // wxZlibOutputStream
275 //////////////////////
277 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
,
280 : wxFilterOutputStream(stream
)
285 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
*stream
,
288 : wxFilterOutputStream(stream
)
293 void wxZlibOutputStream::Init(int level
, int flags
)
296 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
297 m_z_size
= ZSTREAM_BUFFER_SIZE
;
302 level
= Z_DEFAULT_COMPRESSION
;
306 wxASSERT_MSG(level
>= 0 && level
<= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
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
;
317 m_deflate
= new z_stream_s
;
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
;
324 // see zlib.h for documentation on windowBits
325 int windowBits
= MAX_WBITS
;
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"));
333 if (deflateInit2(m_deflate
, level
, Z_DEFLATED
, windowBits
,
334 8, Z_DEFAULT_STRATEGY
) == Z_OK
)
339 wxLogError(_("Can't initialize zlib deflate stream."));
340 m_lasterror
= wxSTREAM_WRITE_ERROR
;
343 bool wxZlibOutputStream::Close()
346 deflateEnd(m_deflate
);
348 wxDELETEA(m_z_buffer
);
350 return wxFilterOutputStream::Close() && IsOk();
353 void wxZlibOutputStream::DoFlush(bool final
)
355 if (!m_deflate
|| !m_z_buffer
)
356 m_lasterror
= wxSTREAM_WRITE_ERROR
;
363 while (err
== Z_OK
|| err
== Z_STREAM_END
) {
364 size_t len
= m_z_size
- m_deflate
->avail_out
;
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"));
371 m_deflate
->next_out
= m_z_buffer
;
372 m_deflate
->avail_out
= m_z_size
;
377 err
= deflate(m_deflate
, final
? Z_FINISH
: Z_FULL_FLUSH
);
378 done
= m_deflate
->avail_out
!= 0 || err
== Z_STREAM_END
;
382 size_t wxZlibOutputStream::OnSysWrite(const void *buffer
, size_t size
)
384 wxASSERT_MSG(m_deflate
&& m_z_buffer
, wxT("Deflate stream not open"));
386 if (!m_deflate
|| !m_z_buffer
)
388 // notice that this will make IsOk() test just below return false
389 m_lasterror
= wxSTREAM_WRITE_ERROR
;
392 if (!IsOk() || !size
)
396 m_deflate
->next_in
= (unsigned char *)buffer
;
397 m_deflate
->avail_in
= size
;
399 while (err
== Z_OK
&& m_deflate
->avail_in
> 0) {
400 if (m_deflate
->avail_out
== 0) {
401 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
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"));
408 m_deflate
->next_out
= m_z_buffer
;
409 m_deflate
->avail_out
= m_z_size
;
412 err
= deflate(m_deflate
, Z_NO_FLUSH
);
416 m_lasterror
= wxSTREAM_WRITE_ERROR
;
417 wxString
msg(m_deflate
->msg
, *wxConvCurrent
);
419 msg
= wxString::Format(_("zlib error %d"), err
);
420 wxLogError(_("Can't write to deflate stream: %s"), msg
.c_str());
423 size
-= m_deflate
->avail_in
;
428 /* static */ bool wxZlibOutputStream::CanHandleGZip()
430 return wxZlibInputStream::CanHandleGZip();
433 bool wxZlibOutputStream::SetDictionary(const char *data
, const size_t datalen
)
435 return (deflateSetDictionary(m_deflate
, (Bytef
*)data
, datalen
) == Z_OK
);
438 bool wxZlibOutputStream::SetDictionary(const wxMemoryBuffer
&buf
)
440 return SetDictionary((char*)buf
.GetData(), buf
.GetDataLen());
444 // wxUSE_ZLIB && wxUSE_STREAMS