blob: df1dbeec93d28e2d71ee11b6ce7cd1aa99c0ac09 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <rdr/ZlibOutStream.h>
20#include <rdr/Exception.h>
21#include <zlib.h>
22
23using namespace rdr;
24
25enum { DEFAULT_BUF_SIZE = 16384 };
26
27ZlibOutStream::ZlibOutStream(OutStream* os, int bufSize_, int compressLevel)
28 : underlying(os), compressionLevel(compressLevel), newLevel(compressLevel),
29 bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
30{
31 zs = new z_stream;
32 zs->zalloc = Z_NULL;
33 zs->zfree = Z_NULL;
34 zs->opaque = Z_NULL;
35 if (deflateInit(zs, compressLevel) != Z_OK) {
36 delete zs;
37 throw Exception("ZlibOutStream: deflateInit failed");
38 }
39 ptr = start = new U8[bufSize];
40 end = start + bufSize;
41}
42
43ZlibOutStream::~ZlibOutStream()
44{
45 try {
46 flush();
47 } catch (Exception&) {
48 }
49 delete [] start;
50 deflateEnd(zs);
51 delete zs;
52}
53
54void ZlibOutStream::setUnderlying(OutStream* os)
55{
56 underlying = os;
57}
58
59void ZlibOutStream::setCompressionLevel(int level)
60{
61 if (level < -1 || level > 9)
62 level = -1; // Z_DEFAULT_COMPRESSION
63
64 newLevel = level;
65}
66
67int ZlibOutStream::length()
68{
69 return offset + ptr - start;
70}
71
72void ZlibOutStream::flush()
73{
74 zs->next_in = start;
75 zs->avail_in = ptr - start;
76
77// fprintf(stderr,"zos flush: avail_in %d\n",zs->avail_in);
78
79 while (zs->avail_in != 0) {
80
81 do {
82 underlying->check(1);
83 zs->next_out = underlying->getptr();
84 zs->avail_out = underlying->getend() - underlying->getptr();
85
86// fprintf(stderr,"zos flush: calling deflate, avail_in %d, avail_out %d\n",
87// zs->avail_in,zs->avail_out);
88 checkCompressionLevel();
89 int rc = deflate(zs, Z_SYNC_FLUSH);
90 if (rc != Z_OK) throw Exception("ZlibOutStream: deflate failed");
91
92// fprintf(stderr,"zos flush: after deflate: %d bytes\n",
93// zs->next_out-underlying->getptr());
94
95 underlying->setptr(zs->next_out);
96 } while (zs->avail_out == 0);
97 }
98
99 offset += ptr - start;
100 ptr = start;
101}
102
103int ZlibOutStream::overrun(int itemSize, int nItems)
104{
105// fprintf(stderr,"ZlibOutStream overrun\n");
106
107 if (itemSize > bufSize)
108 throw Exception("ZlibOutStream overrun: max itemSize exceeded");
109
110 while (end - ptr < itemSize) {
111 zs->next_in = start;
112 zs->avail_in = ptr - start;
113
114 do {
115 underlying->check(1);
116 zs->next_out = underlying->getptr();
117 zs->avail_out = underlying->getend() - underlying->getptr();
118
119// fprintf(stderr,"zos overrun: calling deflate, avail_in %d, avail_out %d\n",
120// zs->avail_in,zs->avail_out);
121
122 checkCompressionLevel();
123 int rc = deflate(zs, 0);
124 if (rc != Z_OK) throw Exception("ZlibOutStream: deflate failed");
125
126// fprintf(stderr,"zos overrun: after deflate: %d bytes\n",
127// zs->next_out-underlying->getptr());
128
129 underlying->setptr(zs->next_out);
130 } while (zs->avail_out == 0);
131
132 // output buffer not full
133
134 if (zs->avail_in == 0) {
135 offset += ptr - start;
136 ptr = start;
137 } else {
138 // but didn't consume all the data? try shifting what's left to the
139 // start of the buffer.
140 fprintf(stderr,"z out buf not full, but in data not consumed\n");
141 memmove(start, zs->next_in, ptr - zs->next_in);
142 offset += zs->next_in - start;
143 ptr -= zs->next_in - start;
144 }
145 }
146
147 if (itemSize * nItems > end - ptr)
148 nItems = (end - ptr) / itemSize;
149
150 return nItems;
151}
152
153void ZlibOutStream::checkCompressionLevel()
154{
155 if (newLevel != compressionLevel) {
156 if (deflateParams (zs, newLevel, Z_DEFAULT_STRATEGY) != Z_OK) {
157 throw Exception("ZlibOutStream: deflateParams failed");
158 }
159 compressionLevel = newLevel;
160 }
161}