blob: f60a620b6249b34410ded83925e7281ac538b5b0 [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>
Adam Tkac20e0d712008-11-14 14:48:21 +000021#include <os/print.h>
22
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000023#include <zlib.h>
24
25using namespace rdr;
26
27enum { DEFAULT_BUF_SIZE = 16384 };
28
29ZlibOutStream::ZlibOutStream(OutStream* os, int bufSize_, int compressLevel)
30 : underlying(os), compressionLevel(compressLevel), newLevel(compressLevel),
31 bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
32{
33 zs = new z_stream;
34 zs->zalloc = Z_NULL;
35 zs->zfree = Z_NULL;
36 zs->opaque = Z_NULL;
37 if (deflateInit(zs, compressLevel) != Z_OK) {
38 delete zs;
39 throw Exception("ZlibOutStream: deflateInit failed");
40 }
41 ptr = start = new U8[bufSize];
42 end = start + bufSize;
43}
44
45ZlibOutStream::~ZlibOutStream()
46{
47 try {
48 flush();
49 } catch (Exception&) {
50 }
51 delete [] start;
52 deflateEnd(zs);
53 delete zs;
54}
55
56void ZlibOutStream::setUnderlying(OutStream* os)
57{
58 underlying = os;
59}
60
61void ZlibOutStream::setCompressionLevel(int level)
62{
63 if (level < -1 || level > 9)
64 level = -1; // Z_DEFAULT_COMPRESSION
65
66 newLevel = level;
67}
68
69int ZlibOutStream::length()
70{
71 return offset + ptr - start;
72}
73
74void ZlibOutStream::flush()
75{
76 zs->next_in = start;
77 zs->avail_in = ptr - start;
78
79// fprintf(stderr,"zos flush: avail_in %d\n",zs->avail_in);
80
81 while (zs->avail_in != 0) {
82
83 do {
84 underlying->check(1);
85 zs->next_out = underlying->getptr();
86 zs->avail_out = underlying->getend() - underlying->getptr();
87
88// fprintf(stderr,"zos flush: calling deflate, avail_in %d, avail_out %d\n",
89// zs->avail_in,zs->avail_out);
90 checkCompressionLevel();
91 int rc = deflate(zs, Z_SYNC_FLUSH);
92 if (rc != Z_OK) throw Exception("ZlibOutStream: deflate failed");
93
94// fprintf(stderr,"zos flush: after deflate: %d bytes\n",
95// zs->next_out-underlying->getptr());
96
97 underlying->setptr(zs->next_out);
98 } while (zs->avail_out == 0);
99 }
100
101 offset += ptr - start;
102 ptr = start;
103}
104
105int ZlibOutStream::overrun(int itemSize, int nItems)
106{
107// fprintf(stderr,"ZlibOutStream overrun\n");
108
109 if (itemSize > bufSize)
110 throw Exception("ZlibOutStream overrun: max itemSize exceeded");
111
112 while (end - ptr < itemSize) {
113 zs->next_in = start;
114 zs->avail_in = ptr - start;
115
116 do {
117 underlying->check(1);
118 zs->next_out = underlying->getptr();
119 zs->avail_out = underlying->getend() - underlying->getptr();
120
121// fprintf(stderr,"zos overrun: calling deflate, avail_in %d, avail_out %d\n",
122// zs->avail_in,zs->avail_out);
123
124 checkCompressionLevel();
125 int rc = deflate(zs, 0);
126 if (rc != Z_OK) throw Exception("ZlibOutStream: deflate failed");
127
128// fprintf(stderr,"zos overrun: after deflate: %d bytes\n",
129// zs->next_out-underlying->getptr());
130
131 underlying->setptr(zs->next_out);
132 } while (zs->avail_out == 0);
133
134 // output buffer not full
135
136 if (zs->avail_in == 0) {
137 offset += ptr - start;
138 ptr = start;
139 } else {
140 // but didn't consume all the data? try shifting what's left to the
141 // start of the buffer.
142 fprintf(stderr,"z out buf not full, but in data not consumed\n");
143 memmove(start, zs->next_in, ptr - zs->next_in);
144 offset += zs->next_in - start;
145 ptr -= zs->next_in - start;
146 }
147 }
148
149 if (itemSize * nItems > end - ptr)
150 nItems = (end - ptr) / itemSize;
151
152 return nItems;
153}
154
155void ZlibOutStream::checkCompressionLevel()
156{
157 if (newLevel != compressionLevel) {
158 if (deflateParams (zs, newLevel, Z_DEFAULT_STRATEGY) != Z_OK) {
159 throw Exception("ZlibOutStream: deflateParams failed");
160 }
161 compressionLevel = newLevel;
162 }
163}