]>
Commit | Line | Data |
---|---|---|
79c3e0e1 GL |
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 | #ifdef __GNUG__ | |
12 | #pragma implementation "zstream.h" | |
13 | #endif | |
14 | ||
15 | // For compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | #include <wx/stream.h> | |
18 | #include <wx/zstream.h> | |
19 | #include <wx/utils.h> | |
1a5a8367 | 20 | #include <wx/intl.h> |
3069ac4e | 21 | #include "wx/log.h" |
e3e65dac | 22 | #include "../zlib/zlib.h" // don't change this, Robert |
79c3e0e1 GL |
23 | |
24 | #ifdef __BORLANDC__ | |
25 | #pragma hdrstop | |
26 | #endif | |
27 | ||
6d44bf31 GL |
28 | #define ZSTREAM_BUFFER_SIZE 1024 |
29 | ||
79c3e0e1 GL |
30 | ////////////////////// |
31 | // wxZlibInputStream | |
32 | ////////////////////// | |
33 | ||
34 | wxZlibInputStream::wxZlibInputStream(wxInputStream& stream) | |
35 | : wxFilterInputStream(stream) | |
36 | { | |
37 | int err; | |
38 | ||
6d44bf31 | 39 | // I need a private stream buffer. |
75ed1d15 | 40 | m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read); |
6d44bf31 | 41 | m_i_destroybuf = TRUE; |
856d2e52 | 42 | m_inflate = new z_stream_s; |
6d44bf31 | 43 | |
856d2e52 GL |
44 | m_inflate->zalloc = (alloc_func)0; |
45 | m_inflate->zfree = (free_func)0; | |
46 | m_inflate->opaque = (voidpf)0; | |
79c3e0e1 | 47 | |
856d2e52 | 48 | err = inflateInit(m_inflate); |
79c3e0e1 | 49 | if (err != Z_OK) { |
856d2e52 GL |
50 | inflateEnd(m_inflate); |
51 | delete m_inflate; | |
79c3e0e1 GL |
52 | return; |
53 | } | |
54 | ||
6d44bf31 GL |
55 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; |
56 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
57 | ||
856d2e52 GL |
58 | m_inflate->avail_in = 0; |
59 | m_inflate->next_in = NULL; | |
79c3e0e1 GL |
60 | } |
61 | ||
62 | wxZlibInputStream::~wxZlibInputStream() | |
63 | { | |
856d2e52 GL |
64 | inflateEnd(m_inflate); |
65 | delete m_inflate; | |
79c3e0e1 GL |
66 | } |
67 | ||
75ed1d15 | 68 | size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size) |
79c3e0e1 GL |
69 | { |
70 | int err; | |
71 | ||
856d2e52 GL |
72 | m_inflate->next_out = (unsigned char *)buffer; |
73 | m_inflate->avail_out = size; | |
79c3e0e1 | 74 | |
856d2e52 GL |
75 | while (m_inflate->avail_out > 0) { |
76 | if (m_inflate->avail_in == 0) { | |
6d44bf31 GL |
77 | |
78 | m_parent_i_stream->Read(m_z_buffer, m_z_size); | |
856d2e52 GL |
79 | m_inflate->next_in = m_z_buffer; |
80 | m_inflate->avail_in = m_parent_i_stream->LastRead(); | |
6d44bf31 | 81 | |
75ed1d15 | 82 | if (m_parent_i_stream->LastError() != wxStream_NOERROR) |
856d2e52 | 83 | return (size - m_inflate->avail_in); |
79c3e0e1 | 84 | } |
856d2e52 | 85 | err = inflate(m_inflate, Z_FINISH); |
6d44bf31 | 86 | if (err == Z_STREAM_END) |
856d2e52 | 87 | return (size - m_inflate->avail_in); |
79c3e0e1 GL |
88 | } |
89 | ||
856d2e52 | 90 | return size-m_inflate->avail_in; |
79c3e0e1 GL |
91 | } |
92 | ||
79c3e0e1 GL |
93 | ////////////////////// |
94 | // wxZlibOutputStream | |
95 | ////////////////////// | |
96 | ||
97 | wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream) | |
98 | : wxFilterOutputStream(stream) | |
99 | { | |
100 | int err; | |
101 | ||
75ed1d15 | 102 | m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write); |
6d44bf31 | 103 | m_o_destroybuf = TRUE; |
856d2e52 | 104 | m_deflate = new z_stream_s; |
6d44bf31 | 105 | |
856d2e52 GL |
106 | m_deflate->zalloc = (alloc_func)0; |
107 | m_deflate->zfree = (free_func)0; | |
108 | m_deflate->opaque = (voidpf)0; | |
79c3e0e1 | 109 | |
856d2e52 | 110 | err = deflateInit(m_deflate, Z_DEFAULT_COMPRESSION); |
79c3e0e1 | 111 | if (err != Z_OK) { |
856d2e52 | 112 | deflateEnd(m_deflate); |
79c3e0e1 GL |
113 | return; |
114 | } | |
6d44bf31 GL |
115 | |
116 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
117 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
118 | ||
856d2e52 GL |
119 | m_deflate->avail_in = 0; |
120 | m_deflate->next_out = m_z_buffer; | |
121 | m_deflate->avail_out = m_z_size; | |
79c3e0e1 GL |
122 | } |
123 | ||
124 | wxZlibOutputStream::~wxZlibOutputStream() | |
125 | { | |
126 | int err; | |
127 | ||
6d44bf31 GL |
128 | Sync(); |
129 | ||
856d2e52 | 130 | err = deflate(m_deflate, Z_FINISH); |
3069ac4e RR |
131 | if (err != Z_STREAM_END) |
132 | { | |
133 | wxLogDebug( "wxZlibOutputStream: an error occured while closing the stream.\n" ); | |
6d44bf31 | 134 | return; |
79c3e0e1 | 135 | } |
79c3e0e1 | 136 | |
856d2e52 | 137 | deflateEnd(m_deflate); |
6d44bf31 GL |
138 | |
139 | delete[] m_z_buffer; | |
79c3e0e1 GL |
140 | } |
141 | ||
6d44bf31 GL |
142 | void wxZlibOutputStream::Sync() |
143 | { | |
144 | int err; | |
145 | ||
856d2e52 GL |
146 | m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out); |
147 | m_deflate->next_out = m_z_buffer; | |
148 | m_deflate->avail_out = m_z_size; | |
6d44bf31 | 149 | |
856d2e52 | 150 | err = deflate(m_deflate, Z_FULL_FLUSH); |
6d44bf31 | 151 | if (err != Z_OK) { |
6d44bf31 GL |
152 | return; |
153 | } | |
154 | ||
856d2e52 GL |
155 | m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out); |
156 | m_deflate->next_out = m_z_buffer; | |
157 | m_deflate->avail_out = m_z_size; | |
6d44bf31 GL |
158 | } |
159 | ||
75ed1d15 | 160 | size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size) |
79c3e0e1 GL |
161 | { |
162 | int err; | |
163 | ||
856d2e52 GL |
164 | m_deflate->next_in = (unsigned char *)buffer; |
165 | m_deflate->avail_in = size; | |
79c3e0e1 | 166 | |
856d2e52 | 167 | while (m_deflate->avail_in > 0) { |
6d44bf31 | 168 | |
856d2e52 | 169 | if (m_deflate->avail_out == 0) { |
6d44bf31 | 170 | m_parent_o_stream->Write(m_z_buffer, m_z_size); |
75ed1d15 | 171 | if (m_parent_o_stream->LastError() != wxStream_NOERROR) |
856d2e52 | 172 | return (size - m_deflate->avail_in); |
6d44bf31 | 173 | |
856d2e52 GL |
174 | m_deflate->next_out = m_z_buffer; |
175 | m_deflate->avail_out = m_z_size; | |
79c3e0e1 | 176 | } |
6d44bf31 | 177 | |
856d2e52 | 178 | err = deflate(m_deflate, Z_NO_FLUSH); |
6d44bf31 | 179 | if (err != Z_OK) |
856d2e52 | 180 | return (size - m_deflate->avail_in); |
79c3e0e1 | 181 | } |
6d44bf31 | 182 | return size; |
79c3e0e1 | 183 | } |