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(__WINDOWS__) && !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()
54 if ( sscanf(zlibVersion(), "%d.%d.%d", &major
, &minor
, &build
) != 3 )
61 return wxVersionInfo("zlib", major
, minor
, build
);
64 /////////////////////////////////////////////////////////////////////////////
67 IMPLEMENT_DYNAMIC_CLASS(wxZlibClassFactory
, wxFilterClassFactory
)
69 static wxZlibClassFactory g_wxZlibClassFactory
;
71 wxZlibClassFactory::wxZlibClassFactory()
73 if (this == &g_wxZlibClassFactory
)
77 const wxChar
* const *
78 wxZlibClassFactory::GetProtocols(wxStreamProtocolType type
) const
80 static const wxChar
*mimes
[] = { wxT("application/x-deflate"), NULL
};
81 static const wxChar
*encs
[] = { wxT("deflate"), NULL
};
82 static const wxChar
*empty
[] = { NULL
};
85 case wxSTREAM_MIMETYPE
: return mimes
;
86 case wxSTREAM_ENCODING
: return encs
;
87 default: return empty
;
92 /////////////////////////////////////////////////////////////////////////////
95 IMPLEMENT_DYNAMIC_CLASS(wxGzipClassFactory
, wxFilterClassFactory
)
97 static wxGzipClassFactory g_wxGzipClassFactory
;
99 wxGzipClassFactory::wxGzipClassFactory()
101 if (this == &g_wxGzipClassFactory
&& wxZlibInputStream::CanHandleGZip())
105 const wxChar
* const *
106 wxGzipClassFactory::GetProtocols(wxStreamProtocolType type
) const
108 static const wxChar
*protos
[] =
109 { wxT("gzip"), NULL
};
110 static const wxChar
*mimes
[] =
111 { wxT("application/gzip"), wxT("application/x-gzip"), NULL
};
112 static const wxChar
*encs
[] =
113 { wxT("gzip"), NULL
};
114 static const wxChar
*exts
[] =
115 { wxT(".gz"), wxT(".gzip"), NULL
};
116 static const wxChar
*empty
[] =
120 case wxSTREAM_PROTOCOL
: return protos
;
121 case wxSTREAM_MIMETYPE
: return mimes
;
122 case wxSTREAM_ENCODING
: return encs
;
123 case wxSTREAM_FILEEXT
: return exts
;
124 default: return empty
;
129 //////////////////////
131 //////////////////////
133 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
, int flags
)
134 : wxFilterInputStream(stream
)
139 wxZlibInputStream::wxZlibInputStream(wxInputStream
*stream
, int flags
)
140 : wxFilterInputStream(stream
)
145 void wxZlibInputStream::Init(int flags
)
148 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
149 m_z_size
= ZSTREAM_BUFFER_SIZE
;
152 // if gzip is asked for but not supported...
153 if ((flags
== wxZLIB_GZIP
|| flags
== wxZLIB_AUTO
) && !CanHandleGZip()) {
154 if (flags
== wxZLIB_AUTO
) {
155 // an error will come later if the input turns out not to be a zlib
159 wxLogError(_("Gzip not supported by this version of zlib"));
160 m_lasterror
= wxSTREAM_READ_ERROR
;
166 m_inflate
= new z_stream_s
;
169 memset(m_inflate
, 0, sizeof(z_stream_s
));
171 // see zlib.h for documentation on windowBits
172 int windowBits
= MAX_WBITS
;
174 case wxZLIB_NO_HEADER
: windowBits
= -MAX_WBITS
; break;
175 case wxZLIB_ZLIB
: windowBits
= MAX_WBITS
; break;
176 case wxZLIB_GZIP
: windowBits
= MAX_WBITS
| ZSTREAM_GZIP
; break;
177 case wxZLIB_AUTO
: windowBits
= MAX_WBITS
| ZSTREAM_AUTO
; break;
178 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
181 if (inflateInit2(m_inflate
, windowBits
) == Z_OK
)
186 wxLogError(_("Can't initialize zlib inflate stream."));
187 m_lasterror
= wxSTREAM_READ_ERROR
;
190 wxZlibInputStream::~wxZlibInputStream()
192 inflateEnd(m_inflate
);
195 delete [] m_z_buffer
;
198 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
200 wxASSERT_MSG(m_inflate
&& m_z_buffer
, wxT("Inflate stream not open"));
202 if (!m_inflate
|| !m_z_buffer
)
203 m_lasterror
= wxSTREAM_READ_ERROR
;
204 if (!IsOk() || !size
)
208 m_inflate
->next_out
= (unsigned char *)buffer
;
209 m_inflate
->avail_out
= size
;
211 while (err
== Z_OK
&& m_inflate
->avail_out
> 0) {
212 if (m_inflate
->avail_in
== 0 && m_parent_i_stream
->IsOk()) {
213 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
214 m_inflate
->next_in
= m_z_buffer
;
215 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
217 err
= inflate(m_inflate
, Z_SYNC_FLUSH
);
225 if (m_inflate
->avail_out
) {
226 // Unread any data taken from past the end of the deflate stream, so that
227 // any additional data can be read from the underlying stream (the crc
228 // in a gzip for example)
229 if (m_inflate
->avail_in
) {
230 m_parent_i_stream
->Reset();
231 m_parent_i_stream
->Ungetch(m_inflate
->next_in
, m_inflate
->avail_in
);
232 m_inflate
->avail_in
= 0;
234 m_lasterror
= wxSTREAM_EOF
;
239 // Indicates that zlib was expecting more data, but the parent stream
240 // has none. Other than Eof the error will have been already reported
241 // by the parent strean,
242 m_lasterror
= wxSTREAM_READ_ERROR
;
243 if (m_parent_i_stream
->Eof())
245 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
250 wxString
msg(m_inflate
->msg
, *wxConvCurrent
);
252 msg
= wxString::Format(_("zlib error %d"), err
);
253 wxLogError(_("Can't read from inflate stream: %s"), msg
.c_str());
254 m_lasterror
= wxSTREAM_READ_ERROR
;
257 size
-= m_inflate
->avail_out
;
262 /* static */ bool wxZlibInputStream::CanHandleGZip()
264 const char *dot
= strchr(zlibVersion(), '.');
265 int major
= atoi(zlibVersion());
266 int minor
= dot
? atoi(dot
+ 1) : 0;
267 return major
> 1 || (major
== 1 && minor
>= 2);
270 bool wxZlibInputStream::SetDictionary(const char *data
, const size_t datalen
)
272 return (inflateSetDictionary(m_inflate
, (Bytef
*)data
, datalen
) == Z_OK
);
275 bool wxZlibInputStream::SetDictionary(const wxMemoryBuffer
&buf
)
277 return SetDictionary((char*)buf
.GetData(), buf
.GetDataLen());
281 //////////////////////
282 // wxZlibOutputStream
283 //////////////////////
285 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
,
288 : wxFilterOutputStream(stream
)
293 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
*stream
,
296 : wxFilterOutputStream(stream
)
301 void wxZlibOutputStream::Init(int level
, int flags
)
304 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
305 m_z_size
= ZSTREAM_BUFFER_SIZE
;
310 level
= Z_DEFAULT_COMPRESSION
;
314 wxASSERT_MSG(level
>= 0 && level
<= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
317 // if gzip is asked for but not supported...
318 if (flags
== wxZLIB_GZIP
&& !CanHandleGZip()) {
319 wxLogError(_("Gzip not supported by this version of zlib"));
320 m_lasterror
= wxSTREAM_WRITE_ERROR
;
325 m_deflate
= new z_stream_s
;
328 memset(m_deflate
, 0, sizeof(z_stream_s
));
329 m_deflate
->next_out
= m_z_buffer
;
330 m_deflate
->avail_out
= m_z_size
;
332 // see zlib.h for documentation on windowBits
333 int windowBits
= MAX_WBITS
;
335 case wxZLIB_NO_HEADER
: windowBits
= -MAX_WBITS
; break;
336 case wxZLIB_ZLIB
: windowBits
= MAX_WBITS
; break;
337 case wxZLIB_GZIP
: windowBits
= MAX_WBITS
| ZSTREAM_GZIP
; break;
338 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
341 if (deflateInit2(m_deflate
, level
, Z_DEFLATED
, windowBits
,
342 8, Z_DEFAULT_STRATEGY
) == Z_OK
)
347 wxLogError(_("Can't initialize zlib deflate stream."));
348 m_lasterror
= wxSTREAM_WRITE_ERROR
;
351 bool wxZlibOutputStream::Close()
354 deflateEnd(m_deflate
);
356 wxDELETEA(m_z_buffer
);
358 return wxFilterOutputStream::Close() && IsOk();
361 void wxZlibOutputStream::DoFlush(bool final
)
363 if (!m_deflate
|| !m_z_buffer
)
364 m_lasterror
= wxSTREAM_WRITE_ERROR
;
371 while (err
== Z_OK
|| err
== Z_STREAM_END
) {
372 size_t len
= m_z_size
- m_deflate
->avail_out
;
374 if (m_parent_o_stream
->Write(m_z_buffer
, len
).LastWrite() != len
) {
375 m_lasterror
= wxSTREAM_WRITE_ERROR
;
376 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
379 m_deflate
->next_out
= m_z_buffer
;
380 m_deflate
->avail_out
= m_z_size
;
385 err
= deflate(m_deflate
, final
? Z_FINISH
: Z_FULL_FLUSH
);
386 done
= m_deflate
->avail_out
!= 0 || err
== Z_STREAM_END
;
390 size_t wxZlibOutputStream::OnSysWrite(const void *buffer
, size_t size
)
392 wxASSERT_MSG(m_deflate
&& m_z_buffer
, wxT("Deflate stream not open"));
394 if (!m_deflate
|| !m_z_buffer
)
396 // notice that this will make IsOk() test just below return false
397 m_lasterror
= wxSTREAM_WRITE_ERROR
;
400 if (!IsOk() || !size
)
404 m_deflate
->next_in
= (unsigned char *)buffer
;
405 m_deflate
->avail_in
= size
;
407 while (err
== Z_OK
&& m_deflate
->avail_in
> 0) {
408 if (m_deflate
->avail_out
== 0) {
409 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
410 if (m_parent_o_stream
->LastWrite() != m_z_size
) {
411 m_lasterror
= wxSTREAM_WRITE_ERROR
;
412 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
416 m_deflate
->next_out
= m_z_buffer
;
417 m_deflate
->avail_out
= m_z_size
;
420 err
= deflate(m_deflate
, Z_NO_FLUSH
);
424 m_lasterror
= wxSTREAM_WRITE_ERROR
;
425 wxString
msg(m_deflate
->msg
, *wxConvCurrent
);
427 msg
= wxString::Format(_("zlib error %d"), err
);
428 wxLogError(_("Can't write to deflate stream: %s"), msg
.c_str());
431 size
-= m_deflate
->avail_in
;
436 /* static */ bool wxZlibOutputStream::CanHandleGZip()
438 return wxZlibInputStream::CanHandleGZip();
441 bool wxZlibOutputStream::SetDictionary(const char *data
, const size_t datalen
)
443 return (deflateSetDictionary(m_deflate
, (Bytef
*)data
, datalen
) == Z_OK
);
446 bool wxZlibOutputStream::SetDictionary(const wxMemoryBuffer
&buf
)
448 return SetDictionary((char*)buf
.GetData(), buf
.GetDataLen());
452 // wxUSE_ZLIB && wxUSE_STREAMS