|
| 1 | +/* ### |
| 2 | + * IP: GHIDRA |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "compression.hh" |
| 17 | + |
| 18 | +namespace ghidra { |
| 19 | + |
| 20 | +/// The compression \b level ranges from 1-9 from faster/least compression to slower/most compression. |
| 21 | +/// Use a \b level of 0 for no compression and -1 for the \e default compression level. |
| 22 | +/// \param level is the compression level |
| 23 | +Compress::Compress(int4 level) |
| 24 | + |
| 25 | +{ |
| 26 | + compStream.zalloc = Z_NULL; |
| 27 | + compStream.zfree = Z_NULL; |
| 28 | + compStream.opaque = Z_NULL; |
| 29 | + int4 ret = deflateInit(&compStream, level); |
| 30 | + if (ret != Z_OK) |
| 31 | + throw LowlevelError("Could not initialize deflate stream state"); |
| 32 | +} |
| 33 | + |
| 34 | +Compress::~Compress(void) |
| 35 | + |
| 36 | +{ |
| 37 | + deflateEnd(&compStream); |
| 38 | +} |
| 39 | + |
| 40 | +/// Return the number of bytes of output space still available. Output may be limited by the amount |
| 41 | +/// of space in the output buffer or the amount of data available in the current input buffer. |
| 42 | +/// \param buffer is where compressed bytes are stored |
| 43 | +/// \param sz is the size, in bytes, of the buffer |
| 44 | +/// \param finish is set to \b true if this is the final buffer to add to the stream |
| 45 | +/// \return the number of output bytes still available |
| 46 | +int4 Compress::deflate(uint1 *buffer,int4 sz,bool finish) |
| 47 | + |
| 48 | +{ |
| 49 | + int flush = finish ? Z_FINISH : Z_NO_FLUSH; |
| 50 | + compStream.avail_out = sz; |
| 51 | + compStream.next_out = buffer; |
| 52 | + |
| 53 | + int ret = ::deflate(&compStream, flush); |
| 54 | + if (ret == Z_STREAM_ERROR) |
| 55 | + throw LowlevelError("Error compressing stream"); |
| 56 | + return compStream.avail_out; |
| 57 | +} |
| 58 | + |
| 59 | +Decompress::Decompress(void) |
| 60 | + |
| 61 | +{ |
| 62 | + streamFinished = false; |
| 63 | + compStream.zalloc = Z_NULL; |
| 64 | + compStream.zfree = Z_NULL; |
| 65 | + compStream.opaque = Z_NULL; |
| 66 | + compStream.avail_in = 0; |
| 67 | + compStream.next_in = Z_NULL; |
| 68 | + int ret = inflateInit(&compStream); |
| 69 | + if (ret != Z_OK) |
| 70 | + throw LowlevelError("Could not initialize inflate stream state"); |
| 71 | +} |
| 72 | + |
| 73 | +/// Return the number of bytes of output space still available. Output may be limited by the amount |
| 74 | +/// of space in the output buffer or the amount of data available in the current input buffer. |
| 75 | +/// \param buffer is where uncompressed bytes are stored |
| 76 | +/// \param sz is the size, in bytes, of the buffer |
| 77 | +/// \return the number of output bytes still available |
| 78 | +int4 Decompress::inflate(uint1 *buffer,int4 sz) |
| 79 | + |
| 80 | +{ |
| 81 | + compStream.avail_out = sz; |
| 82 | + compStream.next_out = buffer; |
| 83 | + |
| 84 | + int ret = ::inflate(&compStream, Z_NO_FLUSH); |
| 85 | + switch (ret) { |
| 86 | + case Z_NEED_DICT: |
| 87 | + case Z_DATA_ERROR: |
| 88 | + case Z_MEM_ERROR: |
| 89 | + case Z_STREAM_ERROR: |
| 90 | + throw LowlevelError("Error decompressing stream"); |
| 91 | + case Z_STREAM_END: |
| 92 | + streamFinished = true; |
| 93 | + break; |
| 94 | + default: |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + return compStream.avail_out; |
| 99 | +} |
| 100 | + |
| 101 | +Decompress::~Decompress(void) |
| 102 | + |
| 103 | +{ |
| 104 | + inflateEnd(&compStream); |
| 105 | +} |
| 106 | + |
| 107 | +const int4 CompressBuffer::IN_BUFFER_SIZE = 4096; |
| 108 | +const int4 CompressBuffer::OUT_BUFFER_SIZE = 4096; |
| 109 | + |
| 110 | +/// \param s is the backing output stream |
| 111 | +/// \param level is the level of compression |
| 112 | +CompressBuffer::CompressBuffer(ostream &s,int4 level) |
| 113 | + : outStream(s), compressor(level) |
| 114 | +{ |
| 115 | + inBuffer = new uint1[IN_BUFFER_SIZE]; |
| 116 | + outBuffer = new uint1[OUT_BUFFER_SIZE]; |
| 117 | + setp((char *)inBuffer,(char *)inBuffer + IN_BUFFER_SIZE-1); |
| 118 | +} |
| 119 | + |
| 120 | +CompressBuffer::~CompressBuffer(void) |
| 121 | + |
| 122 | +{ |
| 123 | + delete [] inBuffer; |
| 124 | + delete [] outBuffer; |
| 125 | +} |
| 126 | + |
| 127 | +/// The compressor is called repeatedly and its output is written to the backing stream |
| 128 | +/// until the compressor can no longer fill the \e output buffer. |
| 129 | +/// \param lastBuffer is \b true if this is the final set of bytes to add to the compressed stream |
| 130 | +void CompressBuffer::flushInput(bool lastBuffer) |
| 131 | + |
| 132 | +{ |
| 133 | + int len = pptr() - pbase(); |
| 134 | + compressor.input((uint1 *)pbase(),len); |
| 135 | + int4 outAvail; |
| 136 | + do { |
| 137 | + outAvail = OUT_BUFFER_SIZE; |
| 138 | + outAvail = compressor.deflate(outBuffer,outAvail,lastBuffer); |
| 139 | + outStream.write((char *)outBuffer,OUT_BUFFER_SIZE-outAvail); |
| 140 | + } while(outAvail == 0); |
| 141 | + pbump(-len); |
| 142 | +} |
| 143 | + |
| 144 | +/// \param c is the final character filling the buffer |
| 145 | +/// \return the written character |
| 146 | +int CompressBuffer::overflow(int c) |
| 147 | + |
| 148 | +{ |
| 149 | + if (c != EOF) { |
| 150 | + *pptr() = c; |
| 151 | + pbump(1); |
| 152 | + } |
| 153 | + flushInput(false); |
| 154 | + return c; |
| 155 | +} |
| 156 | + |
| 157 | +/// \return 0 for success |
| 158 | +int CompressBuffer::sync(void) |
| 159 | + |
| 160 | +{ |
| 161 | + flushInput(true); |
| 162 | + return 0; |
| 163 | +} |
| 164 | + |
| 165 | +} |
0 commit comments