blob: 6aadde132a86bc3e62551a32967c4e2bf3861c23 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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), bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
29{
30 zs = new z_stream;
31 zs->zalloc = Z_NULL;
32 zs->zfree = Z_NULL;
33 zs->opaque = Z_NULL;
34 if (deflateInit(zs, compressLevel) != Z_OK) {
35 delete zs;
36 throw Exception("ZlibOutStream: deflateInit failed");
37 }
38 ptr = start = new U8[bufSize];
39 end = start + bufSize;
40}
41
42ZlibOutStream::~ZlibOutStream()
43{
44 try {
45 flush();
46 } catch (Exception&) {
47 }
48 delete [] start;
49 deflateEnd(zs);
50 delete zs;
51}
52
53void ZlibOutStream::setUnderlying(OutStream* os)
54{
55 underlying = os;
56}
57
58int ZlibOutStream::length()
59{
60 return offset + ptr - start;
61}
62
63void ZlibOutStream::flush()
64{
65 zs->next_in = start;
66 zs->avail_in = ptr - start;
67
68// fprintf(stderr,"zos flush: avail_in %d\n",zs->avail_in);
69
70 while (zs->avail_in != 0) {
71
72 do {
73 underlying->check(1);
74 zs->next_out = underlying->getptr();
75 zs->avail_out = underlying->getend() - underlying->getptr();
76
77// fprintf(stderr,"zos flush: calling deflate, avail_in %d, avail_out %d\n",
78// zs->avail_in,zs->avail_out);
79 int rc = deflate(zs, Z_SYNC_FLUSH);
80 if (rc != Z_OK) throw Exception("ZlibOutStream: deflate failed");
81
82// fprintf(stderr,"zos flush: after deflate: %d bytes\n",
83// zs->next_out-underlying->getptr());
84
85 underlying->setptr(zs->next_out);
86 } while (zs->avail_out == 0);
87 }
88
89 offset += ptr - start;
90 ptr = start;
91}
92
93int ZlibOutStream::overrun(int itemSize, int nItems)
94{
95// fprintf(stderr,"ZlibOutStream overrun\n");
96
97 if (itemSize > bufSize)
98 throw Exception("ZlibOutStream overrun: max itemSize exceeded");
99
100 while (end - ptr < itemSize) {
101 zs->next_in = start;
102 zs->avail_in = ptr - start;
103
104 do {
105 underlying->check(1);
106 zs->next_out = underlying->getptr();
107 zs->avail_out = underlying->getend() - underlying->getptr();
108
109// fprintf(stderr,"zos overrun: calling deflate, avail_in %d, avail_out %d\n",
110// zs->avail_in,zs->avail_out);
111
112 int rc = deflate(zs, 0);
113 if (rc != Z_OK) throw Exception("ZlibOutStream: deflate failed");
114
115// fprintf(stderr,"zos overrun: after deflate: %d bytes\n",
116// zs->next_out-underlying->getptr());
117
118 underlying->setptr(zs->next_out);
119 } while (zs->avail_out == 0);
120
121 // output buffer not full
122
123 if (zs->avail_in == 0) {
124 offset += ptr - start;
125 ptr = start;
126 } else {
127 // but didn't consume all the data? try shifting what's left to the
128 // start of the buffer.
129 fprintf(stderr,"z out buf not full, but in data not consumed\n");
130 memmove(start, zs->next_in, ptr - zs->next_in);
131 offset += zs->next_in - start;
132 ptr -= zs->next_in - start;
133 }
134 }
135
136 if (itemSize * nItems > end - ptr)
137 nItems = (end - ptr) / itemSize;
138
139 return nItems;
140}