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