]>
git.saurik.com Git - wxWidgets.git/blob - src/common/zstream.cpp
1 //////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "zstream.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_ZLIB && wxUSE_STREAMS
25 #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
46 //////////////////////
48 //////////////////////
50 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
, int flags
)
51 : wxFilterInputStream(stream
)
54 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
55 m_z_size
= ZSTREAM_BUFFER_SIZE
;
58 #if WXWIN_COMPATIBILITY_2_4
59 // treat compatibilty mode as auto
60 m_24compatibilty
= flags
== wxZLIB_24COMPATIBLE
;
65 // if gzip is asked for but not supported...
66 if ((flags
== wxZLIB_GZIP
|| flags
== wxZLIB_AUTO
) && !CanHandleGZip()) {
67 if (flags
== wxZLIB_AUTO
) {
68 // an error will come later if the input turns out not to be a zlib
72 wxLogError(_("Gzip not supported by this version of zlib"));
73 m_lasterror
= wxSTREAM_READ_ERROR
;
79 m_inflate
= new z_stream_s
;
82 memset(m_inflate
, 0, sizeof(z_stream_s
));
84 // see zlib.h for documentation on windowBits
85 int windowBits
= MAX_WBITS
;
87 case wxZLIB_NO_HEADER
: windowBits
= -MAX_WBITS
; break;
88 case wxZLIB_ZLIB
: windowBits
= MAX_WBITS
; break;
89 case wxZLIB_GZIP
: windowBits
= MAX_WBITS
| ZSTREAM_GZIP
; break;
90 case wxZLIB_AUTO
: windowBits
= MAX_WBITS
| ZSTREAM_AUTO
; break;
91 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
94 if (inflateInit2(m_inflate
, windowBits
) == Z_OK
)
99 wxLogError(_("Can't initialize zlib inflate stream."));
100 m_lasterror
= wxSTREAM_READ_ERROR
;
103 wxZlibInputStream::~wxZlibInputStream()
105 inflateEnd(m_inflate
);
108 delete [] m_z_buffer
;
111 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
113 wxASSERT_MSG(m_inflate
&& m_z_buffer
, wxT("Inflate stream not open"));
115 if (!m_inflate
|| !m_z_buffer
)
116 m_lasterror
= wxSTREAM_READ_ERROR
;
117 if (!IsOk() || !size
)
121 m_inflate
->next_out
= (unsigned char *)buffer
;
122 m_inflate
->avail_out
= size
;
124 while (err
== Z_OK
&& m_inflate
->avail_out
> 0) {
125 if (m_inflate
->avail_in
== 0 && m_parent_i_stream
->IsOk()) {
126 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
127 m_inflate
->next_in
= m_z_buffer
;
128 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
130 err
= inflate(m_inflate
, Z_SYNC_FLUSH
);
138 // Unread any data taken from past the end of the deflate stream, so that
139 // any additional data can be read from the underlying stream (the crc
140 // in a gzip for example)
141 if (m_inflate
->avail_in
) {
142 m_parent_i_stream
->Ungetch(m_inflate
->next_in
, m_inflate
->avail_in
);
143 m_inflate
->avail_in
= 0;
145 m_lasterror
= wxSTREAM_EOF
;
149 // Indicates that zlib was expecting more data, but the parent stream
150 // has none. Other than Eof the error will have been already reported
151 // by the parent strean,
152 m_lasterror
= wxSTREAM_READ_ERROR
;
153 if (m_parent_i_stream
->Eof())
154 #if WXWIN_COMPATIBILITY_2_4
155 if (m_24compatibilty
)
156 m_lasterror
= wxSTREAM_EOF
;
159 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
163 wxString
msg(m_inflate
->msg
, *wxConvCurrent
);
165 msg
= wxString::Format(_("zlib error %d"), err
);
166 wxLogError(_("Can't read from inflate stream: %s"), msg
.c_str());
167 m_lasterror
= wxSTREAM_READ_ERROR
;
170 size
-= m_inflate
->avail_out
;
175 /* static */ bool wxZlibInputStream::CanHandleGZip()
177 const char *dot
= strchr(zlibVersion(), '.');
178 int major
= atoi(zlibVersion());
179 int minor
= dot
? atoi(dot
+ 1) : 0;
180 return major
> 1 || (major
== 1 && minor
>= 2);
184 //////////////////////
185 // wxZlibOutputStream
186 //////////////////////
188 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
,
191 : wxFilterOutputStream(stream
)
194 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
195 m_z_size
= ZSTREAM_BUFFER_SIZE
;
200 level
= Z_DEFAULT_COMPRESSION
;
204 wxASSERT_MSG(level
>= 0 && level
<= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
207 // if gzip is asked for but not supported...
208 if (flags
== wxZLIB_GZIP
&& !CanHandleGZip()) {
209 wxLogError(_("Gzip not supported by this version of zlib"));
210 m_lasterror
= wxSTREAM_WRITE_ERROR
;
215 m_deflate
= new z_stream_s
;
218 memset(m_deflate
, 0, sizeof(z_stream_s
));
219 m_deflate
->next_out
= m_z_buffer
;
220 m_deflate
->avail_out
= m_z_size
;
222 // see zlib.h for documentation on windowBits
223 int windowBits
= MAX_WBITS
;
225 case wxZLIB_NO_HEADER
: windowBits
= -MAX_WBITS
; break;
226 case wxZLIB_ZLIB
: windowBits
= MAX_WBITS
; break;
227 case wxZLIB_GZIP
: windowBits
= MAX_WBITS
| ZSTREAM_GZIP
; break;
228 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
231 if (deflateInit2(m_deflate
, level
, Z_DEFLATED
, windowBits
,
232 8, Z_DEFAULT_STRATEGY
) == Z_OK
)
237 wxLogError(_("Can't initialize zlib deflate stream."));
238 m_lasterror
= wxSTREAM_WRITE_ERROR
;
241 bool wxZlibOutputStream::Close()
244 deflateEnd(m_deflate
);
253 void wxZlibOutputStream::DoFlush(bool final
)
255 if (!m_deflate
|| !m_z_buffer
)
256 m_lasterror
= wxSTREAM_WRITE_ERROR
;
263 while (err
== Z_OK
|| err
== Z_STREAM_END
) {
264 size_t len
= m_z_size
- m_deflate
->avail_out
;
266 if (m_parent_o_stream
->Write(m_z_buffer
, len
).LastWrite() != len
) {
267 m_lasterror
= wxSTREAM_WRITE_ERROR
;
268 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
271 m_deflate
->next_out
= m_z_buffer
;
272 m_deflate
->avail_out
= m_z_size
;
277 err
= deflate(m_deflate
, final
? Z_FINISH
: Z_FULL_FLUSH
);
278 done
= m_deflate
->avail_out
!= 0 || err
== Z_STREAM_END
;
282 size_t wxZlibOutputStream::OnSysWrite(const void *buffer
, size_t size
)
284 wxASSERT_MSG(m_deflate
&& m_z_buffer
, wxT("Deflate stream not open"));
286 if (!m_deflate
|| !m_z_buffer
)
287 m_lasterror
= wxSTREAM_WRITE_ERROR
;
288 if (!IsOk() || !size
)
292 m_deflate
->next_in
= (unsigned char *)buffer
;
293 m_deflate
->avail_in
= size
;
295 while (err
== Z_OK
&& m_deflate
->avail_in
> 0) {
296 if (m_deflate
->avail_out
== 0) {
297 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
298 if (m_parent_o_stream
->LastWrite() != m_z_size
) {
299 m_lasterror
= wxSTREAM_WRITE_ERROR
;
300 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
304 m_deflate
->next_out
= m_z_buffer
;
305 m_deflate
->avail_out
= m_z_size
;
308 err
= deflate(m_deflate
, Z_NO_FLUSH
);
312 m_lasterror
= wxSTREAM_WRITE_ERROR
;
313 wxString
msg(m_deflate
->msg
, *wxConvCurrent
);
315 msg
= wxString::Format(_("zlib error %d"), err
);
316 wxLogError(_("Can't write to deflate stream: %s"), msg
.c_str());
319 size
-= m_deflate
->avail_in
;
324 /* static */ bool wxZlibOutputStream::CanHandleGZip()
326 return wxZlibInputStream::CanHandleGZip();
330 // wxUSE_ZLIB && wxUSE_STREAMS