]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: zstream.cpp | |
3 | // Purpose: Compressed stream classes | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 11/07/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "zstream.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #if wxUSE_ZLIB && wxUSE_STREAMS | |
24 | ||
25 | #include "wx/zstream.h" | |
26 | #include "wx/utils.h" | |
27 | #include "wx/intl.h" | |
28 | #include "wx/log.h" | |
29 | ||
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) | |
35 | #include "../zlib/zlib.h" | |
36 | #elif defined(__WXMAC__) && defined(__UNIX__) | |
37 | #include <Kernel/net/zlib.h> | |
38 | #else | |
39 | #include "zlib.h" | |
40 | #endif | |
41 | ||
42 | #define ZSTREAM_BUFFER_SIZE 1024 | |
43 | ||
44 | ////////////////////// | |
45 | // wxZlibInputStream | |
46 | ////////////////////// | |
47 | ||
48 | wxZlibInputStream::wxZlibInputStream(wxInputStream& stream) | |
49 | : wxFilterInputStream(stream) | |
50 | { | |
51 | // I need a private stream buffer. | |
52 | m_inflate = new z_stream_s; | |
53 | ||
54 | m_inflate->zalloc = (alloc_func)0; | |
55 | m_inflate->zfree = (free_func)0; | |
56 | m_inflate->opaque = (voidpf)0; | |
57 | ||
58 | int err = inflateInit(m_inflate); | |
59 | if (err != Z_OK) { | |
60 | inflateEnd(m_inflate); | |
61 | delete m_inflate; | |
62 | return; | |
63 | } | |
64 | ||
65 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
66 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
67 | ||
68 | m_inflate->avail_in = 0; | |
69 | m_inflate->next_in = NULL; | |
70 | } | |
71 | ||
72 | wxZlibInputStream::~wxZlibInputStream() | |
73 | { | |
74 | inflateEnd(m_inflate); | |
75 | delete m_inflate; | |
76 | ||
77 | delete [] m_z_buffer; | |
78 | } | |
79 | ||
80 | size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size) | |
81 | { | |
82 | int err; | |
83 | ||
84 | m_inflate->next_out = (unsigned char *)buffer; | |
85 | m_inflate->avail_out = size; | |
86 | ||
87 | while (m_inflate->avail_out > 0) { | |
88 | if (m_inflate->avail_in == 0) { | |
89 | ||
90 | m_parent_i_stream->Read(m_z_buffer, wxMin(m_z_size, size)); | |
91 | m_inflate->next_in = m_z_buffer; | |
92 | m_inflate->avail_in = m_parent_i_stream->LastRead(); | |
93 | ||
94 | if (m_parent_i_stream->LastError() != wxStream_NOERROR && | |
95 | m_parent_i_stream->LastError() != wxStream_EOF) | |
96 | { | |
97 | m_lasterror = m_parent_i_stream->LastError(); | |
98 | return 0; // failed to read anything | |
99 | } | |
100 | ||
101 | if ( m_inflate->avail_in == 0 ) | |
102 | { | |
103 | // EOF | |
104 | m_lasterror = wxStream_EOF; | |
105 | break; | |
106 | } | |
107 | } | |
108 | err = inflate(m_inflate, Z_FINISH); | |
109 | if (err == Z_STREAM_END) | |
110 | return (size - m_inflate->avail_out); | |
111 | } | |
112 | ||
113 | return size-m_inflate->avail_out; | |
114 | } | |
115 | ||
116 | ////////////////////// | |
117 | // wxZlibOutputStream | |
118 | ////////////////////// | |
119 | ||
120 | wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream, int level) | |
121 | : wxFilterOutputStream(stream) | |
122 | { | |
123 | int err; | |
124 | ||
125 | m_deflate = new z_stream_s; | |
126 | ||
127 | m_deflate->zalloc = (alloc_func)0; | |
128 | m_deflate->zfree = (free_func)0; | |
129 | m_deflate->opaque = (voidpf)0; | |
130 | ||
131 | if (level == -1) level = Z_DEFAULT_COMPRESSION; | |
132 | wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!")); | |
133 | ||
134 | err = deflateInit(m_deflate, level); | |
135 | if (err != Z_OK) { | |
136 | deflateEnd(m_deflate); | |
137 | return; | |
138 | } | |
139 | ||
140 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
141 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
142 | ||
143 | m_deflate->avail_in = 0; | |
144 | m_deflate->next_out = m_z_buffer; | |
145 | m_deflate->avail_out = m_z_size; | |
146 | } | |
147 | ||
148 | wxZlibOutputStream::~wxZlibOutputStream() | |
149 | { | |
150 | int err; | |
151 | ||
152 | Sync(); | |
153 | ||
154 | err = deflate(m_deflate, Z_FINISH); | |
155 | if (err != Z_STREAM_END) | |
156 | { | |
157 | wxLogDebug( wxT("wxZlibOutputStream: an error occured while closing the stream.\n") ); | |
158 | return; | |
159 | } | |
160 | ||
161 | deflateEnd(m_deflate); | |
162 | delete m_deflate; | |
163 | ||
164 | delete[] m_z_buffer; | |
165 | } | |
166 | ||
167 | void wxZlibOutputStream::Sync() | |
168 | { | |
169 | int err; | |
170 | ||
171 | m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out); | |
172 | m_deflate->next_out = m_z_buffer; | |
173 | m_deflate->avail_out = m_z_size; | |
174 | ||
175 | err = deflate(m_deflate, Z_FULL_FLUSH); | |
176 | if (err != Z_OK) { | |
177 | return; | |
178 | } | |
179 | ||
180 | // Fixed by "Stefan Csomor" <csomor@advancedconcepts.ch> | |
181 | while( m_deflate->avail_out == 0 ) | |
182 | { | |
183 | m_parent_o_stream->Write(m_z_buffer, m_z_size ); | |
184 | m_deflate->next_out = m_z_buffer; | |
185 | m_deflate->avail_out = m_z_size; | |
186 | err = deflate(m_deflate, Z_FULL_FLUSH); | |
187 | if (err != Z_OK) { | |
188 | return; | |
189 | } | |
190 | } | |
191 | // End | |
192 | ||
193 | m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out); | |
194 | m_deflate->next_out = m_z_buffer; | |
195 | m_deflate->avail_out = m_z_size; | |
196 | } | |
197 | ||
198 | size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size) | |
199 | { | |
200 | int err; | |
201 | ||
202 | m_deflate->next_in = (unsigned char *)buffer; | |
203 | m_deflate->avail_in = size; | |
204 | ||
205 | while (m_deflate->avail_in > 0) { | |
206 | ||
207 | if (m_deflate->avail_out == 0) { | |
208 | m_parent_o_stream->Write(m_z_buffer, m_z_size); | |
209 | if (m_parent_o_stream->LastError() != wxStream_NOERROR) | |
210 | return (size - m_deflate->avail_in); | |
211 | ||
212 | m_deflate->next_out = m_z_buffer; | |
213 | m_deflate->avail_out = m_z_size; | |
214 | } | |
215 | ||
216 | err = deflate(m_deflate, Z_NO_FLUSH); | |
217 | if (err != Z_OK) | |
218 | return (size - m_deflate->avail_in); | |
219 | } | |
220 | return size; | |
221 | } | |
222 | ||
223 | #endif | |
224 | // wxUSE_ZLIB && wxUSE_STREAMS | |
225 |