]>
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 | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
ce4169a4 | 11 | |
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
79c3e0e1 GL |
13 | #pragma implementation "zstream.h" |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
ac57418f | 18 | |
ce4169a4 RR |
19 | #ifdef __BORLANDC__ |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
ce4169a4 | 23 | #if wxUSE_ZLIB && wxUSE_STREAMS |
ac57418f | 24 | |
ce4169a4 | 25 | #include "wx/zstream.h" |
ac57418f RR |
26 | #include "wx/utils.h" |
27 | #include "wx/intl.h" | |
3069ac4e | 28 | #include "wx/log.h" |
d1af991f | 29 | |
f6bcfd97 BP |
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) | |
31e78e0c | 35 | #include "../zlib/zlib.h" |
d1af991f | 36 | #else |
ac2834ab | 37 | #include "zlib.h" |
d1af991f | 38 | #endif |
79c3e0e1 | 39 | |
0915d0b2 | 40 | enum { |
0915d0b2 | 41 | ZSTREAM_BUFFER_SIZE = 16384 |
0915d0b2 | 42 | }; |
6d44bf31 | 43 | |
79c3e0e1 GL |
44 | ////////////////////// |
45 | // wxZlibInputStream | |
46 | ////////////////////// | |
47 | ||
0915d0b2 | 48 | wxZlibInputStream::wxZlibInputStream(wxInputStream& stream, int flags) |
79c3e0e1 GL |
49 | : wxFilterInputStream(stream) |
50 | { | |
0915d0b2 VZ |
51 | m_inflate = NULL; |
52 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
53 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
54 | m_pos = 0; | |
6d44bf31 | 55 | |
0915d0b2 VZ |
56 | if (m_z_buffer) { |
57 | m_inflate = new z_stream_s; | |
79c3e0e1 | 58 | |
0915d0b2 | 59 | if (m_inflate) { |
5fc01d13 | 60 | memset(m_inflate, 0, sizeof(z_stream_s)); |
79c3e0e1 | 61 | |
301deecc RN |
62 | wxASSERT((flags & ~(wxZLIB_ZLIB | wxZLIB_GZIP)) == 0); |
63 | ||
64 | // when autodetecting between gzip & zlib, silently drop gzip flag | |
65 | // if the version of zlib doesn't support it | |
66 | if (flags == (wxZLIB_ZLIB | wxZLIB_GZIP) | |
67 | && strcmp(zlib_version, "1.2.") < 0) | |
68 | flags &= ~wxZLIB_GZIP; | |
69 | ||
70 | int bits = flags ? MAX_WBITS : -MAX_WBITS; | |
71 | if (flags & wxZLIB_GZIP) | |
72 | bits |= (flags & wxZLIB_ZLIB) ? 0x20 : 0x10; | |
6d44bf31 | 73 | |
0915d0b2 VZ |
74 | if (inflateInit2(m_inflate, bits) == Z_OK) |
75 | return; | |
76 | } | |
77 | } | |
78 | ||
79 | wxLogError(_("Can't initialize zlib inflate stream.")); | |
80 | m_lasterror = wxSTREAM_READ_ERROR; | |
79c3e0e1 GL |
81 | } |
82 | ||
83 | wxZlibInputStream::~wxZlibInputStream() | |
84 | { | |
856d2e52 GL |
85 | inflateEnd(m_inflate); |
86 | delete m_inflate; | |
45805ba3 VZ |
87 | |
88 | delete [] m_z_buffer; | |
79c3e0e1 GL |
89 | } |
90 | ||
75ed1d15 | 91 | size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size) |
79c3e0e1 | 92 | { |
0915d0b2 VZ |
93 | wxASSERT_MSG(m_inflate && m_z_buffer, wxT("Inflate stream not open")); |
94 | ||
95 | if (!m_inflate || !m_z_buffer) | |
96 | m_lasterror = wxSTREAM_READ_ERROR; | |
97 | if (!IsOk() || !size) | |
98 | return 0; | |
79c3e0e1 | 99 | |
0915d0b2 | 100 | int err = Z_OK; |
856d2e52 GL |
101 | m_inflate->next_out = (unsigned char *)buffer; |
102 | m_inflate->avail_out = size; | |
79c3e0e1 | 103 | |
0915d0b2 | 104 | while (err == Z_OK && m_inflate->avail_out > 0) { |
856d2e52 | 105 | if (m_inflate->avail_in == 0) { |
0915d0b2 | 106 | m_parent_i_stream->Read(m_z_buffer, m_z_size); |
856d2e52 GL |
107 | m_inflate->next_in = m_z_buffer; |
108 | m_inflate->avail_in = m_parent_i_stream->LastRead(); | |
6d44bf31 | 109 | |
0915d0b2 VZ |
110 | if (m_inflate->avail_in == 0) { |
111 | if (m_parent_i_stream->Eof()) | |
112 | wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream.")); | |
113 | m_lasterror = wxSTREAM_READ_ERROR; | |
114 | break; | |
777fd647 | 115 | } |
0915d0b2 VZ |
116 | } |
117 | err = inflate(m_inflate, Z_NO_FLUSH); | |
118 | } | |
f6bcfd97 | 119 | |
0915d0b2 VZ |
120 | if (err == Z_STREAM_END) { |
121 | // Unread any data taken from past the end of the deflate stream, so that | |
122 | // any additional data can be read from the underlying stream (the crc | |
123 | // in a gzip for example) | |
124 | if (m_inflate->avail_in) { | |
125 | m_parent_i_stream->Ungetch(m_inflate->next_in, m_inflate->avail_in); | |
126 | m_inflate->avail_in = 0; | |
79c3e0e1 | 127 | } |
0915d0b2 VZ |
128 | m_lasterror = wxSTREAM_EOF; |
129 | } else if (err != Z_OK) { | |
301deecc RN |
130 | wxString msg(m_inflate->msg, *wxConvCurrent); |
131 | if (!msg) | |
132 | msg.Format(_("zlib error %d"), err); | |
133 | wxLogError(_("Can't read from inflate stream: %s\n"), msg.c_str()); | |
0915d0b2 | 134 | m_lasterror = wxSTREAM_READ_ERROR; |
79c3e0e1 GL |
135 | } |
136 | ||
0915d0b2 VZ |
137 | size -= m_inflate->avail_out; |
138 | m_pos += size; | |
139 | return size; | |
79c3e0e1 GL |
140 | } |
141 | ||
79c3e0e1 GL |
142 | ////////////////////// |
143 | // wxZlibOutputStream | |
144 | ////////////////////// | |
145 | ||
0915d0b2 VZ |
146 | wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream, |
147 | int level, | |
148 | int flags) | |
79c3e0e1 GL |
149 | : wxFilterOutputStream(stream) |
150 | { | |
0915d0b2 VZ |
151 | m_deflate = NULL; |
152 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
153 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
154 | m_pos = 0; | |
79c3e0e1 | 155 | |
af850422 VZ |
156 | if ( level == -1 ) |
157 | { | |
0915d0b2 | 158 | level = Z_DEFAULT_COMPRESSION; |
af850422 VZ |
159 | } |
160 | else | |
161 | { | |
162 | wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!")); | |
163 | } | |
5824f314 | 164 | |
0915d0b2 VZ |
165 | if (m_z_buffer) { |
166 | m_deflate = new z_stream_s; | |
6d44bf31 | 167 | |
0915d0b2 | 168 | if (m_deflate) { |
5fc01d13 | 169 | memset(m_deflate, 0, sizeof(z_stream_s)); |
0915d0b2 VZ |
170 | m_deflate->next_out = m_z_buffer; |
171 | m_deflate->avail_out = m_z_size; | |
172 | ||
301deecc RN |
173 | wxASSERT(flags == 0 || flags == wxZLIB_ZLIB || flags == wxZLIB_GZIP); |
174 | ||
175 | int bits = flags ? MAX_WBITS : -MAX_WBITS; | |
176 | if (flags & wxZLIB_GZIP) | |
177 | bits |= 0x10; | |
6d44bf31 | 178 | |
0915d0b2 VZ |
179 | if (deflateInit2(m_deflate, level, Z_DEFLATED, bits, |
180 | 8, Z_DEFAULT_STRATEGY) == Z_OK) | |
181 | return; | |
182 | } | |
183 | } | |
184 | ||
185 | wxLogError(_("Can't initialize zlib deflate stream.")); | |
186 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
79c3e0e1 GL |
187 | } |
188 | ||
189 | wxZlibOutputStream::~wxZlibOutputStream() | |
190 | { | |
0915d0b2 VZ |
191 | if (m_deflate && m_z_buffer) |
192 | DoFlush(true); | |
856d2e52 | 193 | deflateEnd(m_deflate); |
778d618a | 194 | delete m_deflate; |
6d44bf31 GL |
195 | |
196 | delete[] m_z_buffer; | |
79c3e0e1 GL |
197 | } |
198 | ||
0915d0b2 | 199 | void wxZlibOutputStream::DoFlush(bool final) |
6d44bf31 | 200 | { |
0915d0b2 | 201 | wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open")); |
6d44bf31 | 202 | |
0915d0b2 VZ |
203 | if (!m_deflate || !m_z_buffer) |
204 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
205 | if (!IsOk()) | |
6d44bf31 | 206 | return; |
6d44bf31 | 207 | |
0915d0b2 VZ |
208 | int err = Z_OK; |
209 | bool done = false; | |
babd4308 | 210 | |
0915d0b2 VZ |
211 | while (err == Z_OK || err == Z_STREAM_END) { |
212 | size_t len = m_z_size - m_deflate->avail_out; | |
213 | if (len) { | |
214 | if (m_parent_o_stream->Write(m_z_buffer, len).LastWrite() != len) { | |
215 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
216 | wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream")); | |
217 | break; | |
218 | } | |
219 | m_deflate->next_out = m_z_buffer; | |
220 | m_deflate->avail_out = m_z_size; | |
221 | } | |
222 | ||
223 | if (done) | |
224 | break; | |
225 | err = deflate(m_deflate, final ? Z_FINISH : Z_FULL_FLUSH); | |
226 | done = m_deflate->avail_out != 0 || err == Z_STREAM_END; | |
227 | } | |
6d44bf31 GL |
228 | } |
229 | ||
75ed1d15 | 230 | size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size) |
79c3e0e1 | 231 | { |
0915d0b2 | 232 | wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open")); |
79c3e0e1 | 233 | |
0915d0b2 VZ |
234 | if (!m_deflate || !m_z_buffer) |
235 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
236 | if (!IsOk() || !size) | |
237 | return 0; | |
238 | ||
239 | int err = Z_OK; | |
856d2e52 GL |
240 | m_deflate->next_in = (unsigned char *)buffer; |
241 | m_deflate->avail_in = size; | |
79c3e0e1 | 242 | |
0915d0b2 | 243 | while (err == Z_OK && m_deflate->avail_in > 0) { |
856d2e52 | 244 | if (m_deflate->avail_out == 0) { |
6d44bf31 | 245 | m_parent_o_stream->Write(m_z_buffer, m_z_size); |
0915d0b2 VZ |
246 | if (m_parent_o_stream->LastWrite() != m_z_size) { |
247 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
248 | wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream")); | |
249 | break; | |
250 | } | |
6d44bf31 | 251 | |
856d2e52 GL |
252 | m_deflate->next_out = m_z_buffer; |
253 | m_deflate->avail_out = m_z_size; | |
79c3e0e1 | 254 | } |
6d44bf31 | 255 | |
856d2e52 | 256 | err = deflate(m_deflate, Z_NO_FLUSH); |
79c3e0e1 | 257 | } |
0915d0b2 VZ |
258 | |
259 | if (err != Z_OK) { | |
260 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
301deecc RN |
261 | wxString msg(m_deflate->msg, *wxConvCurrent); |
262 | if (!msg) | |
263 | msg.Format(_("zlib error %d"), err); | |
264 | wxLogError(_("Can't write to deflate stream: %s\n"), msg.c_str()); | |
0915d0b2 VZ |
265 | } |
266 | ||
267 | size -= m_deflate->avail_in; | |
268 | m_pos += size; | |
6d44bf31 | 269 | return size; |
79c3e0e1 | 270 | } |
ac57418f RR |
271 | |
272 | #endif | |
ce4169a4 | 273 | // wxUSE_ZLIB && wxUSE_STREAMS |
ac57418f | 274 |