blob: c39aa55dcc4bd0762e81ed3760e9e293ecd3d260 [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();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091
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();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000123
124// fprintf(stderr,"zos overrun: after deflate: %d bytes\n",
125// zs->next_out-underlying->getptr());
126
127 underlying->setptr(zs->next_out);
128 } while (zs->avail_out == 0);
129
130 // output buffer not full
131
132 if (zs->avail_in == 0) {
133 offset += ptr - start;
134 ptr = start;
135 } else {
136 // but didn't consume all the data? try shifting what's left to the
137 // start of the buffer.
138 fprintf(stderr,"z out buf not full, but in data not consumed\n");
139 memmove(start, zs->next_in, ptr - zs->next_in);
140 offset += zs->next_in - start;
141 ptr -= zs->next_in - start;
142 }
143 }
144
145 if (itemSize * nItems > end - ptr)
146 nItems = (end - ptr) / itemSize;
147
148 return nItems;
149}
150
151void ZlibOutStream::checkCompressionLevel()
152{
153 if (newLevel != compressionLevel) {
DRC4b187ad2011-06-18 07:22:01 +0000154 int rc = deflate(zs, Z_SYNC_FLUSH);
155 if (rc != Z_OK)
156 throw Exception("ZlibOutStream: deflate failed");
157 if (deflateParams (zs, newLevel, Z_DEFAULT_STRATEGY) != Z_OK)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158 throw Exception("ZlibOutStream: deflateParams failed");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000159 compressionLevel = newLevel;
DRC4b187ad2011-06-18 07:22:01 +0000160 } else {
161 int rc = deflate(zs, Z_SYNC_FLUSH);
162 if (rc != Z_OK)
163 throw Exception("ZlibOutStream: deflate failed");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 }
165}