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