]>
Commit | Line | Data |
---|---|---|
0915d0b2 | 1 | ////////////////////////////////////////////////////////////////////////////// |
79c3e0e1 GL |
2 | // Name: zstream.cpp |
3 | // Purpose: Compressed stream classes | |
4 | // Author: Guilhem Lavaux | |
0915d0b2 | 5 | // Modified by: Mike Wetherell |
79c3e0e1 GL |
6 | // Created: 11/07/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
65571936 | 9 | // Licence: wxWindows licence |
79c3e0e1 | 10 | ///////////////////////////////////////////////////////////////////////////// |
ce4169a4 | 11 | |
79c3e0e1 GL |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
ac57418f | 14 | |
ce4169a4 RR |
15 | #ifdef __BORLANDC__ |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
ce4169a4 | 19 | #if wxUSE_ZLIB && wxUSE_STREAMS |
ac57418f | 20 | |
ce4169a4 | 21 | #include "wx/zstream.h" |
ac57418f RR |
22 | #include "wx/utils.h" |
23 | #include "wx/intl.h" | |
3069ac4e | 24 | #include "wx/log.h" |
d1af991f | 25 | |
f6bcfd97 BP |
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) | |
31e78e0c | 31 | #include "../zlib/zlib.h" |
d1af991f | 32 | #else |
ac2834ab | 33 | #include "zlib.h" |
d1af991f | 34 | #endif |
79c3e0e1 | 35 | |
0915d0b2 | 36 | enum { |
4c68a102 VS |
37 | ZSTREAM_BUFFER_SIZE = 16384, |
38 | ZSTREAM_GZIP = 0x10, // gzip header | |
39 | ZSTREAM_AUTO = 0x20 // auto detect between gzip and zlib | |
0915d0b2 | 40 | }; |
6d44bf31 | 41 | |
79c3e0e1 GL |
42 | ////////////////////// |
43 | // wxZlibInputStream | |
44 | ////////////////////// | |
45 | ||
0915d0b2 | 46 | wxZlibInputStream::wxZlibInputStream(wxInputStream& stream, int flags) |
79c3e0e1 GL |
47 | : wxFilterInputStream(stream) |
48 | { | |
0915d0b2 VZ |
49 | m_inflate = NULL; |
50 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
51 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
52 | m_pos = 0; | |
6d44bf31 | 53 | |
4c68a102 | 54 | #if WXWIN_COMPATIBILITY_2_4 |
3103e8a9 | 55 | // treat compatibility mode as auto |
4c68a102 VS |
56 | m_24compatibilty = flags == wxZLIB_24COMPATIBLE; |
57 | if (m_24compatibilty) | |
58 | flags = wxZLIB_AUTO; | |
59 | #endif | |
60 | ||
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 | |
65 | flags = wxZLIB_ZLIB; | |
66 | } | |
67 | else { | |
68 | wxLogError(_("Gzip not supported by this version of zlib")); | |
69 | m_lasterror = wxSTREAM_READ_ERROR; | |
70 | return; | |
71 | } | |
72 | } | |
73 | ||
0915d0b2 VZ |
74 | if (m_z_buffer) { |
75 | m_inflate = new z_stream_s; | |
79c3e0e1 | 76 | |
0915d0b2 | 77 | if (m_inflate) { |
5fc01d13 | 78 | memset(m_inflate, 0, sizeof(z_stream_s)); |
79c3e0e1 | 79 | |
4c68a102 VS |
80 | // see zlib.h for documentation on windowBits |
81 | int windowBits = MAX_WBITS; | |
82 | switch (flags) { | |
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")); | |
88 | } | |
6d44bf31 | 89 | |
4c68a102 | 90 | if (inflateInit2(m_inflate, windowBits) == Z_OK) |
0915d0b2 VZ |
91 | return; |
92 | } | |
93 | } | |
94 | ||
95 | wxLogError(_("Can't initialize zlib inflate stream.")); | |
96 | m_lasterror = wxSTREAM_READ_ERROR; | |
79c3e0e1 GL |
97 | } |
98 | ||
99 | wxZlibInputStream::~wxZlibInputStream() | |
100 | { | |
856d2e52 GL |
101 | inflateEnd(m_inflate); |
102 | delete m_inflate; | |
45805ba3 VZ |
103 | |
104 | delete [] m_z_buffer; | |
79c3e0e1 GL |
105 | } |
106 | ||
75ed1d15 | 107 | size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size) |
79c3e0e1 | 108 | { |
0915d0b2 VZ |
109 | wxASSERT_MSG(m_inflate && m_z_buffer, wxT("Inflate stream not open")); |
110 | ||
111 | if (!m_inflate || !m_z_buffer) | |
112 | m_lasterror = wxSTREAM_READ_ERROR; | |
113 | if (!IsOk() || !size) | |
114 | return 0; | |
79c3e0e1 | 115 | |
0915d0b2 | 116 | int err = Z_OK; |
856d2e52 GL |
117 | m_inflate->next_out = (unsigned char *)buffer; |
118 | m_inflate->avail_out = size; | |
79c3e0e1 | 119 | |
0915d0b2 | 120 | while (err == Z_OK && m_inflate->avail_out > 0) { |
4c68a102 | 121 | if (m_inflate->avail_in == 0 && m_parent_i_stream->IsOk()) { |
0915d0b2 | 122 | m_parent_i_stream->Read(m_z_buffer, m_z_size); |
856d2e52 GL |
123 | m_inflate->next_in = m_z_buffer; |
124 | m_inflate->avail_in = m_parent_i_stream->LastRead(); | |
4c68a102 VS |
125 | } |
126 | err = inflate(m_inflate, Z_SYNC_FLUSH); | |
127 | } | |
6d44bf31 | 128 | |
4c68a102 VS |
129 | switch (err) { |
130 | case Z_OK: | |
0915d0b2 | 131 | break; |
4c68a102 VS |
132 | |
133 | case Z_STREAM_END: | |
134 | // Unread any data taken from past the end of the deflate stream, so that | |
135 | // any additional data can be read from the underlying stream (the crc | |
136 | // in a gzip for example) | |
137 | if (m_inflate->avail_in) { | |
138 | m_parent_i_stream->Ungetch(m_inflate->next_in, m_inflate->avail_in); | |
139 | m_inflate->avail_in = 0; | |
777fd647 | 140 | } |
4c68a102 VS |
141 | m_lasterror = wxSTREAM_EOF; |
142 | break; | |
f6bcfd97 | 143 | |
4c68a102 VS |
144 | case Z_BUF_ERROR: |
145 | // Indicates that zlib was expecting more data, but the parent stream | |
146 | // has none. Other than Eof the error will have been already reported | |
147 | // by the parent strean, | |
148 | m_lasterror = wxSTREAM_READ_ERROR; | |
149 | if (m_parent_i_stream->Eof()) | |
150 | #if WXWIN_COMPATIBILITY_2_4 | |
151 | if (m_24compatibilty) | |
152 | m_lasterror = wxSTREAM_EOF; | |
153 | else | |
154 | #endif | |
155 | wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream.")); | |
156 | break; | |
157 | ||
158 | default: | |
159 | wxString msg(m_inflate->msg, *wxConvCurrent); | |
160 | if (!msg) | |
2a1f999f | 161 | msg = wxString::Format(_("zlib error %d"), err); |
4c68a102 VS |
162 | wxLogError(_("Can't read from inflate stream: %s"), msg.c_str()); |
163 | m_lasterror = wxSTREAM_READ_ERROR; | |
79c3e0e1 GL |
164 | } |
165 | ||
0915d0b2 VZ |
166 | size -= m_inflate->avail_out; |
167 | m_pos += size; | |
168 | return size; | |
79c3e0e1 GL |
169 | } |
170 | ||
4c68a102 VS |
171 | /* static */ bool wxZlibInputStream::CanHandleGZip() |
172 | { | |
173 | const char *dot = strchr(zlibVersion(), '.'); | |
174 | int major = atoi(zlibVersion()); | |
175 | int minor = dot ? atoi(dot + 1) : 0; | |
176 | return major > 1 || (major == 1 && minor >= 2); | |
177 | } | |
178 | ||
179 | ||
79c3e0e1 GL |
180 | ////////////////////// |
181 | // wxZlibOutputStream | |
182 | ////////////////////// | |
183 | ||
0915d0b2 VZ |
184 | wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream, |
185 | int level, | |
186 | int flags) | |
79c3e0e1 GL |
187 | : wxFilterOutputStream(stream) |
188 | { | |
0915d0b2 VZ |
189 | m_deflate = NULL; |
190 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
191 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
192 | m_pos = 0; | |
79c3e0e1 | 193 | |
af850422 VZ |
194 | if ( level == -1 ) |
195 | { | |
0915d0b2 | 196 | level = Z_DEFAULT_COMPRESSION; |
af850422 VZ |
197 | } |
198 | else | |
199 | { | |
200 | wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!")); | |
201 | } | |
5824f314 | 202 | |
4c68a102 VS |
203 | // if gzip is asked for but not supported... |
204 | if (flags == wxZLIB_GZIP && !CanHandleGZip()) { | |
205 | wxLogError(_("Gzip not supported by this version of zlib")); | |
206 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
207 | return; | |
208 | } | |
6d44bf31 | 209 | |
4c68a102 VS |
210 | if (m_z_buffer) { |
211 | m_deflate = new z_stream_s; | |
0915d0b2 | 212 | |
4c68a102 VS |
213 | if (m_deflate) { |
214 | memset(m_deflate, 0, sizeof(z_stream_s)); | |
215 | m_deflate->next_out = m_z_buffer; | |
216 | m_deflate->avail_out = m_z_size; | |
cab1a605 | 217 | |
4c68a102 VS |
218 | // see zlib.h for documentation on windowBits |
219 | int windowBits = MAX_WBITS; | |
220 | switch (flags) { | |
221 | case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break; | |
222 | case wxZLIB_ZLIB: windowBits = MAX_WBITS; break; | |
223 | case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break; | |
224 | default: wxFAIL_MSG(wxT("Invalid zlib flag")); | |
225 | } | |
226 | ||
cab1a605 | 227 | if (deflateInit2(m_deflate, level, Z_DEFLATED, windowBits, |
4c68a102 VS |
228 | 8, Z_DEFAULT_STRATEGY) == Z_OK) |
229 | return; | |
230 | } | |
0915d0b2 VZ |
231 | } |
232 | ||
233 | wxLogError(_("Can't initialize zlib deflate stream.")); | |
234 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
79c3e0e1 GL |
235 | } |
236 | ||
8f0ff178 RN |
237 | bool wxZlibOutputStream::Close() |
238 | { | |
239 | DoFlush(true); | |
240 | deflateEnd(m_deflate); | |
241 | delete m_deflate; | |
6d44bf31 | 242 | |
8f0ff178 RN |
243 | m_deflate = NULL; |
244 | delete[] m_z_buffer; | |
245 | m_z_buffer = NULL; | |
246 | return IsOk(); | |
247 | } | |
79c3e0e1 | 248 | |
0915d0b2 | 249 | void wxZlibOutputStream::DoFlush(bool final) |
6d44bf31 | 250 | { |
0915d0b2 VZ |
251 | if (!m_deflate || !m_z_buffer) |
252 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
253 | if (!IsOk()) | |
6d44bf31 | 254 | return; |
6d44bf31 | 255 | |
0915d0b2 VZ |
256 | int err = Z_OK; |
257 | bool done = false; | |
babd4308 | 258 | |
0915d0b2 VZ |
259 | while (err == Z_OK || err == Z_STREAM_END) { |
260 | size_t len = m_z_size - m_deflate->avail_out; | |
261 | if (len) { | |
262 | if (m_parent_o_stream->Write(m_z_buffer, len).LastWrite() != len) { | |
263 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
264 | wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream")); | |
265 | break; | |
266 | } | |
267 | m_deflate->next_out = m_z_buffer; | |
268 | m_deflate->avail_out = m_z_size; | |
269 | } | |
270 | ||
271 | if (done) | |
272 | break; | |
273 | err = deflate(m_deflate, final ? Z_FINISH : Z_FULL_FLUSH); | |
274 | done = m_deflate->avail_out != 0 || err == Z_STREAM_END; | |
275 | } | |
6d44bf31 GL |
276 | } |
277 | ||
75ed1d15 | 278 | size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size) |
79c3e0e1 | 279 | { |
0915d0b2 | 280 | wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open")); |
79c3e0e1 | 281 | |
0915d0b2 VZ |
282 | if (!m_deflate || !m_z_buffer) |
283 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
284 | if (!IsOk() || !size) | |
285 | return 0; | |
286 | ||
287 | int err = Z_OK; | |
856d2e52 GL |
288 | m_deflate->next_in = (unsigned char *)buffer; |
289 | m_deflate->avail_in = size; | |
79c3e0e1 | 290 | |
0915d0b2 | 291 | while (err == Z_OK && m_deflate->avail_in > 0) { |
856d2e52 | 292 | if (m_deflate->avail_out == 0) { |
6d44bf31 | 293 | m_parent_o_stream->Write(m_z_buffer, m_z_size); |
0915d0b2 VZ |
294 | if (m_parent_o_stream->LastWrite() != m_z_size) { |
295 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
296 | wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream")); | |
297 | break; | |
298 | } | |
6d44bf31 | 299 | |
856d2e52 GL |
300 | m_deflate->next_out = m_z_buffer; |
301 | m_deflate->avail_out = m_z_size; | |
79c3e0e1 | 302 | } |
6d44bf31 | 303 | |
856d2e52 | 304 | err = deflate(m_deflate, Z_NO_FLUSH); |
79c3e0e1 | 305 | } |
0915d0b2 VZ |
306 | |
307 | if (err != Z_OK) { | |
308 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
301deecc RN |
309 | wxString msg(m_deflate->msg, *wxConvCurrent); |
310 | if (!msg) | |
2a1f999f | 311 | msg = wxString::Format(_("zlib error %d"), err); |
4c68a102 | 312 | wxLogError(_("Can't write to deflate stream: %s"), msg.c_str()); |
0915d0b2 VZ |
313 | } |
314 | ||
315 | size -= m_deflate->avail_in; | |
316 | m_pos += size; | |
6d44bf31 | 317 | return size; |
79c3e0e1 | 318 | } |
ac57418f | 319 | |
4c68a102 | 320 | /* static */ bool wxZlibOutputStream::CanHandleGZip() |
cab1a605 | 321 | { |
4c68a102 VS |
322 | return wxZlibInputStream::CanHandleGZip(); |
323 | } | |
324 | ||
ac57418f | 325 | #endif |
ce4169a4 | 326 | // wxUSE_ZLIB && wxUSE_STREAMS |
cab1a605 | 327 |