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