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