]>
git.saurik.com Git - wxWidgets.git/blob - src/common/zstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Compressed stream classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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 // When using configure, the path must be "zlib.h" I don't know
31 // what other ports (wxMac, wxMotif without configure) need here.
32 // If we are building with configure (defines __WX_SETUP_H__),
33 // we trust the zlib path is given as a -I option.
34 #if defined(__WXMSW__) && !defined(__WX_SETUP_H__)
35 #include "..\zlib\zlib.h"
40 #define ZSTREAM_BUFFER_SIZE 1024
42 //////////////////////
44 //////////////////////
46 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
)
47 : wxFilterInputStream(stream
)
51 // I need a private stream buffer.
52 m_inflate
= new z_stream_s
;
54 m_inflate
->zalloc
= (alloc_func
)0;
55 m_inflate
->zfree
= (free_func
)0;
56 m_inflate
->opaque
= (voidpf
)0;
58 err
= inflateInit(m_inflate
);
60 inflateEnd(m_inflate
);
65 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
66 m_z_size
= ZSTREAM_BUFFER_SIZE
;
68 m_inflate
->avail_in
= 0;
69 m_inflate
->next_in
= NULL
;
72 wxZlibInputStream::~wxZlibInputStream()
74 inflateEnd(m_inflate
);
78 size_t wxZlibInputStream::OnSysRead(void *buffer
, size_t size
)
82 m_inflate
->next_out
= (unsigned char *)buffer
;
83 m_inflate
->avail_out
= size
;
85 while (m_inflate
->avail_out
> 0) {
86 if (m_inflate
->avail_in
== 0) {
88 m_parent_i_stream
->Read(m_z_buffer
, m_z_size
);
89 m_inflate
->next_in
= m_z_buffer
;
90 m_inflate
->avail_in
= m_parent_i_stream
->LastRead();
92 if (m_parent_i_stream
->LastError() != wxStream_NOERROR
)
93 return (size
- m_inflate
->avail_in
);
95 err
= inflate(m_inflate
, Z_FINISH
);
96 if (err
== Z_STREAM_END
)
97 return (size
- m_inflate
->avail_in
);
100 return size
-m_inflate
->avail_in
;
103 //////////////////////
104 // wxZlibOutputStream
105 //////////////////////
107 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
108 : wxFilterOutputStream(stream
)
112 m_deflate
= new z_stream_s
;
114 m_deflate
->zalloc
= (alloc_func
)0;
115 m_deflate
->zfree
= (free_func
)0;
116 m_deflate
->opaque
= (voidpf
)0;
118 err
= deflateInit(m_deflate
, Z_DEFAULT_COMPRESSION
);
120 deflateEnd(m_deflate
);
124 m_z_buffer
= new unsigned char[ZSTREAM_BUFFER_SIZE
];
125 m_z_size
= ZSTREAM_BUFFER_SIZE
;
127 m_deflate
->avail_in
= 0;
128 m_deflate
->next_out
= m_z_buffer
;
129 m_deflate
->avail_out
= m_z_size
;
132 wxZlibOutputStream::~wxZlibOutputStream()
138 err
= deflate(m_deflate
, Z_FINISH
);
139 if (err
!= Z_STREAM_END
)
141 wxLogDebug( _T("wxZlibOutputStream: an error occured while closing the stream.\n") );
145 deflateEnd(m_deflate
);
150 void wxZlibOutputStream::Sync()
154 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
->avail_out
);
155 m_deflate
->next_out
= m_z_buffer
;
156 m_deflate
->avail_out
= m_z_size
;
158 err
= deflate(m_deflate
, Z_FULL_FLUSH
);
163 // Fixed by "Stefan Csomor" <csomor@advancedconcepts.ch>
164 while( m_deflate
->avail_out
== 0 )
166 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
167 m_deflate
->next_out
= m_z_buffer
;
168 m_deflate
->avail_out
= m_z_size
;
169 err
= deflate(m_deflate
, Z_FULL_FLUSH
);
176 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
-m_deflate
->avail_out
);
177 m_deflate
->next_out
= m_z_buffer
;
178 m_deflate
->avail_out
= m_z_size
;
181 size_t wxZlibOutputStream::OnSysWrite(const void *buffer
, size_t size
)
185 m_deflate
->next_in
= (unsigned char *)buffer
;
186 m_deflate
->avail_in
= size
;
188 while (m_deflate
->avail_in
> 0) {
190 if (m_deflate
->avail_out
== 0) {
191 m_parent_o_stream
->Write(m_z_buffer
, m_z_size
);
192 if (m_parent_o_stream
->LastError() != wxStream_NOERROR
)
193 return (size
- m_deflate
->avail_in
);
195 m_deflate
->next_out
= m_z_buffer
;
196 m_deflate
->avail_out
= m_z_size
;
199 err
= deflate(m_deflate
, Z_NO_FLUSH
);
201 return (size
- m_deflate
->avail_in
);
207 // wxUSE_ZLIB && wxUSE_STREAMS