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> |
Adam Tkac | 20e0d71 | 2008-11-14 14:48:21 +0000 | [diff] [blame] | 24 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 25 | #include <zlib.h> |
| 26 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 27 | #undef ZLIBOUT_DEBUG |
| 28 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 29 | using namespace rdr; |
| 30 | |
| 31 | enum { DEFAULT_BUF_SIZE = 16384 }; |
| 32 | |
| 33 | ZlibOutStream::ZlibOutStream(OutStream* os, int bufSize_, int compressLevel) |
| 34 | : underlying(os), compressionLevel(compressLevel), newLevel(compressLevel), |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 35 | bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 36 | { |
| 37 | zs = new z_stream; |
| 38 | zs->zalloc = Z_NULL; |
| 39 | zs->zfree = Z_NULL; |
| 40 | zs->opaque = Z_NULL; |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 41 | zs->next_in = Z_NULL; |
| 42 | zs->avail_in = 0; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 43 | if (deflateInit(zs, compressLevel) != Z_OK) { |
| 44 | delete zs; |
| 45 | throw Exception("ZlibOutStream: deflateInit failed"); |
| 46 | } |
| 47 | ptr = start = new U8[bufSize]; |
| 48 | end = start + bufSize; |
| 49 | } |
| 50 | |
| 51 | ZlibOutStream::~ZlibOutStream() |
| 52 | { |
| 53 | try { |
| 54 | flush(); |
| 55 | } catch (Exception&) { |
| 56 | } |
| 57 | delete [] start; |
| 58 | deflateEnd(zs); |
| 59 | delete zs; |
| 60 | } |
| 61 | |
| 62 | void ZlibOutStream::setUnderlying(OutStream* os) |
| 63 | { |
| 64 | underlying = os; |
| 65 | } |
| 66 | |
| 67 | void ZlibOutStream::setCompressionLevel(int level) |
| 68 | { |
| 69 | if (level < -1 || level > 9) |
| 70 | level = -1; // Z_DEFAULT_COMPRESSION |
| 71 | |
| 72 | newLevel = level; |
| 73 | } |
| 74 | |
| 75 | int ZlibOutStream::length() |
| 76 | { |
| 77 | return offset + ptr - start; |
| 78 | } |
| 79 | |
| 80 | void ZlibOutStream::flush() |
| 81 | { |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 82 | checkCompressionLevel(); |
| 83 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 84 | zs->next_in = start; |
| 85 | zs->avail_in = ptr - start; |
| 86 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 87 | #ifdef ZLIBOUT_DEBUG |
| 88 | fprintf(stderr,"zos flush: avail_in %d\n",zs->avail_in); |
| 89 | #endif |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 90 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 91 | // Force out everything from the zlib encoder |
| 92 | deflate(Z_SYNC_FLUSH); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 93 | |
| 94 | offset += ptr - start; |
| 95 | ptr = start; |
| 96 | } |
| 97 | |
| 98 | int ZlibOutStream::overrun(int itemSize, int nItems) |
| 99 | { |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 100 | #ifdef ZLIBOUT_DEBUG |
| 101 | fprintf(stderr,"zos overrun\n"); |
| 102 | #endif |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 103 | |
| 104 | if (itemSize > bufSize) |
| 105 | throw Exception("ZlibOutStream overrun: max itemSize exceeded"); |
| 106 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 107 | checkCompressionLevel(); |
DRC | ff5ca2d | 2011-08-09 20:19:59 +0000 | [diff] [blame] | 108 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 109 | while (end - ptr < itemSize) { |
| 110 | zs->next_in = start; |
| 111 | zs->avail_in = ptr - start; |
| 112 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 113 | deflate(Z_NO_FLUSH); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 114 | |
| 115 | // output buffer not full |
| 116 | |
| 117 | if (zs->avail_in == 0) { |
| 118 | offset += ptr - start; |
| 119 | ptr = start; |
| 120 | } else { |
| 121 | // but didn't consume all the data? try shifting what's left to the |
| 122 | // start of the buffer. |
| 123 | fprintf(stderr,"z out buf not full, but in data not consumed\n"); |
| 124 | memmove(start, zs->next_in, ptr - zs->next_in); |
| 125 | offset += zs->next_in - start; |
| 126 | ptr -= zs->next_in - start; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (itemSize * nItems > end - ptr) |
| 131 | nItems = (end - ptr) / itemSize; |
| 132 | |
| 133 | return nItems; |
| 134 | } |
| 135 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 136 | void ZlibOutStream::deflate(int flush) |
| 137 | { |
| 138 | int rc; |
| 139 | |
| 140 | if (!underlying) |
| 141 | throw Exception("ZlibOutStream: underlying OutStream has not been set"); |
| 142 | |
| 143 | if ((flush == Z_NO_FLUSH) && (zs->avail_in == 0)) |
| 144 | return; |
| 145 | |
| 146 | do { |
| 147 | underlying->check(1); |
| 148 | zs->next_out = underlying->getptr(); |
| 149 | zs->avail_out = underlying->getend() - underlying->getptr(); |
| 150 | |
| 151 | #ifdef ZLIBOUT_DEBUG |
| 152 | fprintf(stderr,"zos: calling deflate, avail_in %d, avail_out %d\n", |
| 153 | zs->avail_in,zs->avail_out); |
| 154 | #endif |
| 155 | |
| 156 | rc = ::deflate(zs, flush); |
| 157 | if (rc != Z_OK) { |
| 158 | // Silly zlib returns an error if you try to flush something twice |
| 159 | if ((rc == Z_BUF_ERROR) && (flush != Z_NO_FLUSH)) |
| 160 | break; |
| 161 | |
| 162 | throw Exception("ZlibOutStream: deflate failed"); |
| 163 | } |
| 164 | |
| 165 | #ifdef ZLIBOUT_DEBUG |
| 166 | fprintf(stderr,"zos: after deflate: %d bytes\n", |
| 167 | zs->next_out-underlying->getptr()); |
| 168 | #endif |
| 169 | |
| 170 | underlying->setptr(zs->next_out); |
| 171 | } while (zs->avail_out == 0); |
| 172 | } |
| 173 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 174 | void ZlibOutStream::checkCompressionLevel() |
| 175 | { |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 176 | int rc; |
| 177 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 178 | if (newLevel != compressionLevel) { |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 179 | #ifdef ZLIBOUT_DEBUG |
| 180 | fprintf(stderr,"zos change: avail_in %d\n",zs->avail_in); |
| 181 | #endif |
DRC | ff5ca2d | 2011-08-09 20:19:59 +0000 | [diff] [blame] | 182 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 183 | // zlib is just horribly stupid. It does an implicit flush on |
| 184 | // parameter changes, but the flush it does is not one that forces |
| 185 | // out all the data. And since you cannot flush things again, we |
| 186 | // cannot force out our data after the parameter change. Hence we |
| 187 | // need to do a more proper flush here first. |
| 188 | deflate(Z_SYNC_FLUSH); |
DRC | ff5ca2d | 2011-08-09 20:19:59 +0000 | [diff] [blame] | 189 | |
Pierre Ossman | b5822f3 | 2011-10-18 14:27:07 +0000 | [diff] [blame] | 190 | rc = deflateParams (zs, newLevel, Z_DEFAULT_STRATEGY); |
| 191 | if (rc != Z_OK) { |
| 192 | // The implicit flush can result in this error, caused by the |
| 193 | // explicit flush we did above. It should be safe to ignore though |
| 194 | // as the first flush should have left things in a stable state... |
| 195 | if (rc != Z_BUF_ERROR) |
| 196 | throw Exception("ZlibOutStream: deflateParams failed"); |
DRC | ff5ca2d | 2011-08-09 20:19:59 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 199 | compressionLevel = newLevel; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 200 | } |
| 201 | } |