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