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