Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
DRC | f4a341b | 2011-08-09 11:12:55 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011 D. R. Commander. All Rights Reserved. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
Pierre Ossman | 5ad4d06 | 2014-07-07 14:13:46 +0200 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 22 | #include <rdr/ZlibOutStream.h> |
| 23 | #include <rdr/Exception.h> |
Pierre Ossman | e9e7da9 | 2016-04-20 09:38:06 +0200 | [diff] [blame^] | 24 | #include <rfb/LogWriter.h> |
Adam Tkac | 20e0d71 | 2008-11-14 14:48:21 +0000 | [diff] [blame] | 25 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | #include <zlib.h> |
| 27 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 28 | #undef ZLIBOUT_DEBUG |
| 29 | |
Pierre Ossman | e9e7da9 | 2016-04-20 09:38:06 +0200 | [diff] [blame^] | 30 | static rfb::LogWriter vlog("ZlibOutStream"); |
| 31 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 32 | using namespace rdr; |
| 33 | |
| 34 | enum { DEFAULT_BUF_SIZE = 16384 }; |
| 35 | |
| 36 | ZlibOutStream::ZlibOutStream(OutStream* os, int bufSize_, int compressLevel) |
| 37 | : underlying(os), compressionLevel(compressLevel), newLevel(compressLevel), |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 38 | bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 39 | { |
| 40 | zs = new z_stream; |
| 41 | zs->zalloc = Z_NULL; |
| 42 | zs->zfree = Z_NULL; |
| 43 | zs->opaque = Z_NULL; |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 44 | zs->next_in = Z_NULL; |
| 45 | zs->avail_in = 0; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 46 | if (deflateInit(zs, compressLevel) != Z_OK) { |
| 47 | delete zs; |
| 48 | throw Exception("ZlibOutStream: deflateInit failed"); |
| 49 | } |
| 50 | ptr = start = new U8[bufSize]; |
| 51 | end = start + bufSize; |
| 52 | } |
| 53 | |
| 54 | ZlibOutStream::~ZlibOutStream() |
| 55 | { |
| 56 | try { |
| 57 | flush(); |
| 58 | } catch (Exception&) { |
| 59 | } |
| 60 | delete [] start; |
| 61 | deflateEnd(zs); |
| 62 | delete zs; |
| 63 | } |
| 64 | |
| 65 | void ZlibOutStream::setUnderlying(OutStream* os) |
| 66 | { |
| 67 | underlying = os; |
| 68 | } |
| 69 | |
| 70 | void ZlibOutStream::setCompressionLevel(int level) |
| 71 | { |
| 72 | if (level < -1 || level > 9) |
| 73 | level = -1; // Z_DEFAULT_COMPRESSION |
| 74 | |
| 75 | newLevel = level; |
| 76 | } |
| 77 | |
| 78 | int ZlibOutStream::length() |
| 79 | { |
| 80 | return offset + ptr - start; |
| 81 | } |
| 82 | |
| 83 | void ZlibOutStream::flush() |
| 84 | { |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 85 | checkCompressionLevel(); |
| 86 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 87 | zs->next_in = start; |
| 88 | zs->avail_in = ptr - start; |
| 89 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 90 | #ifdef ZLIBOUT_DEBUG |
Pierre Ossman | e9e7da9 | 2016-04-20 09:38:06 +0200 | [diff] [blame^] | 91 | vlog.debug("flush: avail_in %d",zs->avail_in); |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 92 | #endif |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 93 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 94 | // Force out everything from the zlib encoder |
| 95 | deflate(Z_SYNC_FLUSH); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 96 | |
| 97 | offset += ptr - start; |
| 98 | ptr = start; |
| 99 | } |
| 100 | |
| 101 | int ZlibOutStream::overrun(int itemSize, int nItems) |
| 102 | { |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 103 | #ifdef ZLIBOUT_DEBUG |
Pierre Ossman | e9e7da9 | 2016-04-20 09:38:06 +0200 | [diff] [blame^] | 104 | vlog.debug("overrun"); |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 105 | #endif |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 106 | |
| 107 | if (itemSize > bufSize) |
| 108 | throw Exception("ZlibOutStream overrun: max itemSize exceeded"); |
| 109 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 110 | checkCompressionLevel(); |
DRC | ff5ca2d | 2011-08-09 20:19:59 +0000 | [diff] [blame] | 111 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 112 | while (end - ptr < itemSize) { |
| 113 | zs->next_in = start; |
| 114 | zs->avail_in = ptr - start; |
| 115 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 116 | deflate(Z_NO_FLUSH); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 117 | |
| 118 | // output buffer not full |
| 119 | |
| 120 | if (zs->avail_in == 0) { |
| 121 | offset += ptr - start; |
| 122 | ptr = start; |
| 123 | } else { |
| 124 | // but didn't consume all the data? try shifting what's left to the |
| 125 | // start of the buffer. |
Pierre Ossman | e9e7da9 | 2016-04-20 09:38:06 +0200 | [diff] [blame^] | 126 | vlog.info("z out buf not full, but in data not consumed"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 127 | memmove(start, zs->next_in, ptr - zs->next_in); |
| 128 | offset += zs->next_in - start; |
| 129 | ptr -= zs->next_in - start; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (itemSize * nItems > end - ptr) |
| 134 | nItems = (end - ptr) / itemSize; |
| 135 | |
| 136 | return nItems; |
| 137 | } |
| 138 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 139 | void ZlibOutStream::deflate(int flush) |
| 140 | { |
| 141 | int rc; |
| 142 | |
| 143 | if (!underlying) |
| 144 | throw Exception("ZlibOutStream: underlying OutStream has not been set"); |
| 145 | |
| 146 | if ((flush == Z_NO_FLUSH) && (zs->avail_in == 0)) |
| 147 | return; |
| 148 | |
| 149 | do { |
| 150 | underlying->check(1); |
| 151 | zs->next_out = underlying->getptr(); |
| 152 | zs->avail_out = underlying->getend() - underlying->getptr(); |
| 153 | |
| 154 | #ifdef ZLIBOUT_DEBUG |
Pierre Ossman | e9e7da9 | 2016-04-20 09:38:06 +0200 | [diff] [blame^] | 155 | vlog.debug("calling deflate, avail_in %d, avail_out %d", |
| 156 | zs->avail_in,zs->avail_out); |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 157 | #endif |
| 158 | |
| 159 | rc = ::deflate(zs, flush); |
| 160 | if (rc != Z_OK) { |
| 161 | // Silly zlib returns an error if you try to flush something twice |
| 162 | if ((rc == Z_BUF_ERROR) && (flush != Z_NO_FLUSH)) |
| 163 | break; |
| 164 | |
| 165 | throw Exception("ZlibOutStream: deflate failed"); |
| 166 | } |
| 167 | |
| 168 | #ifdef ZLIBOUT_DEBUG |
Pierre Ossman | e9e7da9 | 2016-04-20 09:38:06 +0200 | [diff] [blame^] | 169 | vlog.debug("after deflate: %d bytes", |
| 170 | zs->next_out-underlying->getptr()); |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 171 | #endif |
| 172 | |
| 173 | underlying->setptr(zs->next_out); |
| 174 | } while (zs->avail_out == 0); |
| 175 | } |
| 176 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 177 | void ZlibOutStream::checkCompressionLevel() |
| 178 | { |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 179 | int rc; |
| 180 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 181 | if (newLevel != compressionLevel) { |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 182 | #ifdef ZLIBOUT_DEBUG |
Pierre Ossman | e9e7da9 | 2016-04-20 09:38:06 +0200 | [diff] [blame^] | 183 | vlog.debug("change: avail_in %d",zs->avail_in); |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 184 | #endif |
DRC | ff5ca2d | 2011-08-09 20:19:59 +0000 | [diff] [blame] | 185 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 186 | // zlib is just horribly stupid. It does an implicit flush on |
| 187 | // parameter changes, but the flush it does is not one that forces |
| 188 | // out all the data. And since you cannot flush things again, we |
| 189 | // cannot force out our data after the parameter change. Hence we |
| 190 | // need to do a more proper flush here first. |
| 191 | deflate(Z_SYNC_FLUSH); |
DRC | ff5ca2d | 2011-08-09 20:19:59 +0000 | [diff] [blame] | 192 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 193 | rc = deflateParams (zs, newLevel, Z_DEFAULT_STRATEGY); |
| 194 | if (rc != Z_OK) { |
| 195 | // The implicit flush can result in this error, caused by the |
| 196 | // explicit flush we did above. It should be safe to ignore though |
| 197 | // as the first flush should have left things in a stable state... |
| 198 | if (rc != Z_BUF_ERROR) |
| 199 | throw Exception("ZlibOutStream: deflateParams failed"); |
DRC | ff5ca2d | 2011-08-09 20:19:59 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 202 | compressionLevel = newLevel; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 203 | } |
| 204 | } |