]>
git.saurik.com Git - wxWidgets.git/blob - src/common/zstream.cpp
1 //////////////////////////////////////////////////////////////////////////////
3 // Purpose: Compressed stream classes
4 // Author: Guilhem Lavaux
5 // Modified by: Mike Wetherell
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "zstream.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_ZLIB && wxUSE_STREAMS
25 #include "wx/zstream.h"
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"
41 ZSTREAM_BUFFER_SIZE
= 16384
44 //////////////////////
46 //////////////////////
48 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
, int flags
)
49 : wxFilterInputStream(stream
)
52 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
53 m_z_size
= ZSTREAM_BUFFER_SIZE
;
57 m_inflate
= new z_stream_s
;
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
;
67 wxASSERT((flags
& ~(wxZLIB_ZLIB
| wxZLIB_GZIP
)) == 0);
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
;
75 int bits
= flags
? MAX_WBITS
: -MAX_WBITS
;
76 if (flags
& wxZLIB_GZIP
)
77 bits
|= (flags
& wxZLIB_ZLIB
) ? 0x20 : 0x10;
79 if (inflateInit2(m_inflate
, bits
) == Z_OK
)
84 wxLogError(_("Can't initialize zlib inflate stream."));
85 m_lasterror
= wxSTREAM_READ_ERROR
;
88 wxZlibInputStream::~wxZlibInputStream()
90 inflateEnd(m_inflate
);
96 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
98 wxASSERT_MSG(m_inflate
&& m_z_buffer
, wxT("Inflate stream not open"));
100 if (!m_inflate
|| !m_z_buffer
)
101 m_lasterror
= wxSTREAM_READ_ERROR
;
102 if (!IsOk() || !size
)
106 m_inflate
->next_out
= (unsigned char *)buffer
;
107 m_inflate
->avail_out
= size
;
109 while (err
== Z_OK
&& m_inflate
->avail_out
> 0) {
110 if (m_inflate
->avail_in
== 0) {
111 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
112 m_inflate
->next_in
= m_z_buffer
;
113 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
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
;
122 err
= inflate(m_inflate
, Z_NO_FLUSH
);
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;
133 m_lasterror
= wxSTREAM_EOF
;
134 } else if (err
!= Z_OK
) {
135 wxString
msg(m_inflate
->msg
, *wxConvCurrent
);
137 msg
.Format(_("zlib error %d"), err
);
138 wxLogError(_("Can't read from inflate stream: %s\n"), msg
.c_str());
139 m_lasterror
= wxSTREAM_READ_ERROR
;
142 size
-= m_inflate
->avail_out
;
147 //////////////////////
148 // wxZlibOutputStream
149 //////////////////////
151 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
,
154 : wxFilterOutputStream(stream
)
157 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
158 m_z_size
= ZSTREAM_BUFFER_SIZE
;
163 level
= Z_DEFAULT_COMPRESSION
;
167 wxASSERT_MSG(level
>= 0 && level
<= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
171 m_deflate
= new z_stream_s
;
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
;
181 wxASSERT(flags
== 0 || flags
== wxZLIB_ZLIB
|| flags
== wxZLIB_GZIP
);
183 int bits
= flags
? MAX_WBITS
: -MAX_WBITS
;
184 if (flags
& wxZLIB_GZIP
)
187 if (deflateInit2(m_deflate
, level
, Z_DEFLATED
, bits
,
188 8, Z_DEFAULT_STRATEGY
) == Z_OK
)
193 wxLogError(_("Can't initialize zlib deflate stream."));
194 m_lasterror
= wxSTREAM_WRITE_ERROR
;
197 wxZlibOutputStream::~wxZlibOutputStream()
199 if (m_deflate
&& m_z_buffer
)
201 deflateEnd(m_deflate
);
207 void wxZlibOutputStream::DoFlush(bool final
)
209 wxASSERT_MSG(m_deflate
&& m_z_buffer
, wxT("Deflate stream not open"));
211 if (!m_deflate
|| !m_z_buffer
)
212 m_lasterror
= wxSTREAM_WRITE_ERROR
;
219 while (err
== Z_OK
|| err
== Z_STREAM_END
) {
220 size_t len
= m_z_size
- m_deflate
->avail_out
;
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"));
227 m_deflate
->next_out
= m_z_buffer
;
228 m_deflate
->avail_out
= m_z_size
;
233 err
= deflate(m_deflate
, final
? Z_FINISH
: Z_FULL_FLUSH
);
234 done
= m_deflate
->avail_out
!= 0 || err
== Z_STREAM_END
;
238 size_t wxZlibOutputStream::OnSysWrite(const void *buffer
, size_t size
)
240 wxASSERT_MSG(m_deflate
&& m_z_buffer
, wxT("Deflate stream not open"));
242 if (!m_deflate
|| !m_z_buffer
)
243 m_lasterror
= wxSTREAM_WRITE_ERROR
;
244 if (!IsOk() || !size
)
248 m_deflate
->next_in
= (unsigned char *)buffer
;
249 m_deflate
->avail_in
= size
;
251 while (err
== Z_OK
&& m_deflate
->avail_in
> 0) {
252 if (m_deflate
->avail_out
== 0) {
253 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
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"));
260 m_deflate
->next_out
= m_z_buffer
;
261 m_deflate
->avail_out
= m_z_size
;
264 err
= deflate(m_deflate
, Z_NO_FLUSH
);
268 m_lasterror
= wxSTREAM_WRITE_ERROR
;
269 wxString
msg(m_deflate
->msg
, *wxConvCurrent
);
271 msg
.Format(_("zlib error %d"), err
);
272 wxLogError(_("Can't write to deflate stream: %s\n"), msg
.c_str());
275 size
-= m_deflate
->avail_in
;
281 // wxUSE_ZLIB && wxUSE_STREAMS