]>
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_ZLIB && wxUSE_STREAMS
21 #include "wx/zstream.h"
26 // normally, the compiler options should contain -I../zlib, but it is
27 // apparently not the case for all MSW makefiles and so, unless we use
28 // configure (which defines __WX_SETUP_H__) or it is explicitly overridden by
29 // the user (who can define wxUSE_ZLIB_H_IN_PATH), we hardcode the path here
30 #if defined(__WXMSW__) && !defined(__WX_SETUP_H__) && !defined(wxUSE_ZLIB_H_IN_PATH)
31 #include "../zlib/zlib.h"
37 ZSTREAM_BUFFER_SIZE
= 16384,
38 ZSTREAM_GZIP
= 0x10, // gzip header
39 ZSTREAM_AUTO
= 0x20 // auto detect between gzip and zlib
42 //////////////////////
44 //////////////////////
46 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
, int flags
)
47 : wxFilterInputStream(stream
)
50 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
51 m_z_size
= ZSTREAM_BUFFER_SIZE
;
54 #if WXWIN_COMPATIBILITY_2_4
55 // treat compatibility mode as auto
56 m_24compatibilty
= flags
== wxZLIB_24COMPATIBLE
;
61 // if gzip is asked for but not supported...
62 if ((flags
== wxZLIB_GZIP
|| flags
== wxZLIB_AUTO
) && !CanHandleGZip()) {
63 if (flags
== wxZLIB_AUTO
) {
64 // an error will come later if the input turns out not to be a zlib
68 wxLogError(_("Gzip not supported by this version of zlib"));
69 m_lasterror
= wxSTREAM_READ_ERROR
;
75 m_inflate
= new z_stream_s
;
78 memset(m_inflate
, 0, sizeof(z_stream_s
));
80 // see zlib.h for documentation on windowBits
81 int windowBits
= MAX_WBITS
;
83 case wxZLIB_NO_HEADER
: windowBits
= -MAX_WBITS
; break;
84 case wxZLIB_ZLIB
: windowBits
= MAX_WBITS
; break;
85 case wxZLIB_GZIP
: windowBits
= MAX_WBITS
| ZSTREAM_GZIP
; break;
86 case wxZLIB_AUTO
: windowBits
= MAX_WBITS
| ZSTREAM_AUTO
; break;
87 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
90 if (inflateInit2(m_inflate
, windowBits
) == Z_OK
)
95 wxLogError(_("Can't initialize zlib inflate stream."));
96 m_lasterror
= wxSTREAM_READ_ERROR
;
99 wxZlibInputStream::~wxZlibInputStream()
101 inflateEnd(m_inflate
);
104 delete [] m_z_buffer
;
107 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
109 wxASSERT_MSG(m_inflate
&& m_z_buffer
, wxT("Inflate stream not open"));
111 if (!m_inflate
|| !m_z_buffer
)
112 m_lasterror
= wxSTREAM_READ_ERROR
;
113 if (!IsOk() || !size
)
117 m_inflate
->next_out
= (unsigned char *)buffer
;
118 m_inflate
->avail_out
= size
;
120 while (err
== Z_OK
&& m_inflate
->avail_out
> 0) {
121 if (m_inflate
->avail_in
== 0 && m_parent_i_stream
->IsOk()) {
122 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
123 m_inflate
->next_in
= m_z_buffer
;
124 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
126 err
= inflate(m_inflate
, Z_SYNC_FLUSH
);
134 if (m_inflate
->avail_out
) {
135 // Unread any data taken from past the end of the deflate stream, so that
136 // any additional data can be read from the underlying stream (the crc
137 // in a gzip for example)
138 if (m_inflate
->avail_in
) {
139 m_parent_i_stream
->Ungetch(m_inflate
->next_in
, m_inflate
->avail_in
);
140 m_inflate
->avail_in
= 0;
142 m_lasterror
= wxSTREAM_EOF
;
147 // Indicates that zlib was expecting more data, but the parent stream
148 // has none. Other than Eof the error will have been already reported
149 // by the parent strean,
150 m_lasterror
= wxSTREAM_READ_ERROR
;
151 if (m_parent_i_stream
->Eof())
152 #if WXWIN_COMPATIBILITY_2_4
153 if (m_24compatibilty
)
154 m_lasterror
= wxSTREAM_EOF
;
157 wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream."));
161 wxString
msg(m_inflate
->msg
, *wxConvCurrent
);
163 msg
= wxString::Format(_("zlib error %d"), err
);
164 wxLogError(_("Can't read from inflate stream: %s"), msg
.c_str());
165 m_lasterror
= wxSTREAM_READ_ERROR
;
168 size
-= m_inflate
->avail_out
;
173 /* static */ bool wxZlibInputStream::CanHandleGZip()
175 const char *dot
= strchr(zlibVersion(), '.');
176 int major
= atoi(zlibVersion());
177 int minor
= dot
? atoi(dot
+ 1) : 0;
178 return major
> 1 || (major
== 1 && minor
>= 2);
182 //////////////////////
183 // wxZlibOutputStream
184 //////////////////////
186 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
,
189 : wxFilterOutputStream(stream
)
192 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
193 m_z_size
= ZSTREAM_BUFFER_SIZE
;
198 level
= Z_DEFAULT_COMPRESSION
;
202 wxASSERT_MSG(level
>= 0 && level
<= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
205 // if gzip is asked for but not supported...
206 if (flags
== wxZLIB_GZIP
&& !CanHandleGZip()) {
207 wxLogError(_("Gzip not supported by this version of zlib"));
208 m_lasterror
= wxSTREAM_WRITE_ERROR
;
213 m_deflate
= new z_stream_s
;
216 memset(m_deflate
, 0, sizeof(z_stream_s
));
217 m_deflate
->next_out
= m_z_buffer
;
218 m_deflate
->avail_out
= m_z_size
;
220 // see zlib.h for documentation on windowBits
221 int windowBits
= MAX_WBITS
;
223 case wxZLIB_NO_HEADER
: windowBits
= -MAX_WBITS
; break;
224 case wxZLIB_ZLIB
: windowBits
= MAX_WBITS
; break;
225 case wxZLIB_GZIP
: windowBits
= MAX_WBITS
| ZSTREAM_GZIP
; break;
226 default: wxFAIL_MSG(wxT("Invalid zlib flag"));
229 if (deflateInit2(m_deflate
, level
, Z_DEFLATED
, windowBits
,
230 8, Z_DEFAULT_STRATEGY
) == Z_OK
)
235 wxLogError(_("Can't initialize zlib deflate stream."));
236 m_lasterror
= wxSTREAM_WRITE_ERROR
;
239 bool wxZlibOutputStream::Close()
242 deflateEnd(m_deflate
);
251 void wxZlibOutputStream::DoFlush(bool final
)
253 if (!m_deflate
|| !m_z_buffer
)
254 m_lasterror
= wxSTREAM_WRITE_ERROR
;
261 while (err
== Z_OK
|| err
== Z_STREAM_END
) {
262 size_t len
= m_z_size
- m_deflate
->avail_out
;
264 if (m_parent_o_stream
->Write(m_z_buffer
, len
).LastWrite() != len
) {
265 m_lasterror
= wxSTREAM_WRITE_ERROR
;
266 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
269 m_deflate
->next_out
= m_z_buffer
;
270 m_deflate
->avail_out
= m_z_size
;
275 err
= deflate(m_deflate
, final
? Z_FINISH
: Z_FULL_FLUSH
);
276 done
= m_deflate
->avail_out
!= 0 || err
== Z_STREAM_END
;
280 size_t wxZlibOutputStream::OnSysWrite(const void *buffer
, size_t size
)
282 wxASSERT_MSG(m_deflate
&& m_z_buffer
, wxT("Deflate stream not open"));
284 if (!m_deflate
|| !m_z_buffer
)
285 m_lasterror
= wxSTREAM_WRITE_ERROR
;
286 if (!IsOk() || !size
)
290 m_deflate
->next_in
= (unsigned char *)buffer
;
291 m_deflate
->avail_in
= size
;
293 while (err
== Z_OK
&& m_deflate
->avail_in
> 0) {
294 if (m_deflate
->avail_out
== 0) {
295 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
296 if (m_parent_o_stream
->LastWrite() != m_z_size
) {
297 m_lasterror
= wxSTREAM_WRITE_ERROR
;
298 wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream"));
302 m_deflate
->next_out
= m_z_buffer
;
303 m_deflate
->avail_out
= m_z_size
;
306 err
= deflate(m_deflate
, Z_NO_FLUSH
);
310 m_lasterror
= wxSTREAM_WRITE_ERROR
;
311 wxString
msg(m_deflate
->msg
, *wxConvCurrent
);
313 msg
= wxString::Format(_("zlib error %d"), err
);
314 wxLogError(_("Can't write to deflate stream: %s"), msg
.c_str());
317 size
-= m_deflate
->avail_in
;
322 /* static */ bool wxZlibOutputStream::CanHandleGZip()
324 return wxZlibInputStream::CanHandleGZip();
328 // wxUSE_ZLIB && wxUSE_STREAMS