Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 1 | //===- lib/Support/Compressor.cpp -------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the llvm::Compressor class, an abstraction for memory |
| 11 | // block compression. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Config/config.h" |
| 16 | #include "llvm/Support/Compressor.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include <cassert> |
| 19 | #include <string> |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 20 | #include <ostream> |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 21 | #include "bzip2/bzlib.h" |
Chris Lattner | 52b8752 | 2005-01-29 16:53:02 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 23 | |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 24 | enum CompressionTypes { |
| 25 | COMP_TYPE_NONE = '0', |
| 26 | COMP_TYPE_BZIP2 = '2', |
| 27 | }; |
| 28 | |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 29 | static int getdata(char*& buffer, size_t &size, |
Reid Spencer | 469c34b | 2004-10-04 17:29:25 +0000 | [diff] [blame] | 30 | llvm::Compressor::OutputDataCallback* cb, void* context) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 31 | buffer = 0; |
| 32 | size = 0; |
Reid Spencer | 469c34b | 2004-10-04 17:29:25 +0000 | [diff] [blame] | 33 | int result = (*cb)(buffer, size, context); |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 34 | assert(buffer != 0 && "Invalid result from Compressor callback"); |
| 35 | assert(size != 0 && "Invalid result from Compressor callback"); |
| 36 | return result; |
| 37 | } |
| 38 | |
Tanya Lattner | 5c3fa1e | 2005-01-29 23:29:55 +0000 | [diff] [blame^] | 39 | static int getdata_uns(char*& buffer, unsigned &size, |
| 40 | llvm::Compressor::OutputDataCallback* cb, void* context) { |
Tanya Lattner | 445cdd3 | 2005-01-29 23:08:01 +0000 | [diff] [blame] | 41 | size_t SizeOut; |
| 42 | int Res = getdata(buffer, SizeOut, cb, context); |
| 43 | size = SizeOut; |
| 44 | return Res; |
| 45 | } |
| 46 | |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Reid Spencer | 047c009 | 2004-10-04 17:45:44 +0000 | [diff] [blame] | 48 | //=== NULLCOMP - a compression like set of routines that just copies data |
| 49 | //=== without doing any compression. This is provided so that if the |
| 50 | //=== configured environment doesn't have a compression library the |
| 51 | //=== program can still work, albeit using more data/memory. |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Reid Spencer | 047c009 | 2004-10-04 17:45:44 +0000 | [diff] [blame] | 54 | struct NULLCOMP_stream { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 55 | // User provided fields |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 56 | char* next_in; |
| 57 | size_t avail_in; |
| 58 | char* next_out; |
| 59 | size_t avail_out; |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 60 | |
| 61 | // Information fields |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 62 | size_t output_count; // Total count of output bytes |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Chris Lattner | 52b8752 | 2005-01-29 16:53:02 +0000 | [diff] [blame] | 65 | static void NULLCOMP_init(NULLCOMP_stream* s) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 66 | s->output_count = 0; |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | 52b8752 | 2005-01-29 16:53:02 +0000 | [diff] [blame] | 69 | static bool NULLCOMP_compress(NULLCOMP_stream* s) { |
Reid Spencer | 047c009 | 2004-10-04 17:45:44 +0000 | [diff] [blame] | 70 | assert(s && "Invalid NULLCOMP_stream"); |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 71 | assert(s->next_in != 0); |
| 72 | assert(s->next_out != 0); |
| 73 | assert(s->avail_in >= 1); |
| 74 | assert(s->avail_out >= 1); |
| 75 | |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 76 | if (s->avail_out >= s->avail_in) { |
| 77 | ::memcpy(s->next_out, s->next_in, s->avail_in); |
| 78 | s->output_count += s->avail_in; |
| 79 | s->avail_out -= s->avail_in; |
| 80 | s->next_in += s->avail_in; |
| 81 | s->avail_in = 0; |
| 82 | return true; |
| 83 | } else { |
| 84 | ::memcpy(s->next_out, s->next_in, s->avail_out); |
| 85 | s->output_count += s->avail_out; |
| 86 | s->avail_in -= s->avail_out; |
| 87 | s->next_in += s->avail_out; |
| 88 | s->avail_out = 0; |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
Chris Lattner | 52b8752 | 2005-01-29 16:53:02 +0000 | [diff] [blame] | 93 | static bool NULLCOMP_decompress(NULLCOMP_stream* s) { |
Reid Spencer | 047c009 | 2004-10-04 17:45:44 +0000 | [diff] [blame] | 94 | assert(s && "Invalid NULLCOMP_stream"); |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 95 | assert(s->next_in != 0); |
| 96 | assert(s->next_out != 0); |
| 97 | assert(s->avail_in >= 1); |
| 98 | assert(s->avail_out >= 1); |
| 99 | |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 100 | if (s->avail_out >= s->avail_in) { |
| 101 | ::memcpy(s->next_out, s->next_in, s->avail_in); |
| 102 | s->output_count += s->avail_in; |
| 103 | s->avail_out -= s->avail_in; |
| 104 | s->next_in += s->avail_in; |
| 105 | s->avail_in = 0; |
| 106 | return true; |
| 107 | } else { |
| 108 | ::memcpy(s->next_out, s->next_in, s->avail_out); |
| 109 | s->output_count += s->avail_out; |
| 110 | s->avail_in -= s->avail_out; |
| 111 | s->next_in += s->avail_out; |
| 112 | s->avail_out = 0; |
| 113 | return false; |
| 114 | } |
| 115 | } |
| 116 | |
Chris Lattner | 52b8752 | 2005-01-29 16:53:02 +0000 | [diff] [blame] | 117 | static void NULLCOMP_end(NULLCOMP_stream* strm) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | 52b8752 | 2005-01-29 16:53:02 +0000 | [diff] [blame] | 120 | namespace { |
| 121 | |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 122 | /// This structure is only used when a bytecode file is compressed. |
| 123 | /// As bytecode is being decompressed, the memory buffer might need |
| 124 | /// to be reallocated. The buffer allocation is handled in a callback |
| 125 | /// and this structure is needed to retain information across calls |
| 126 | /// to the callback. |
| 127 | /// @brief An internal buffer object used for handling decompression |
| 128 | struct BufferContext { |
| 129 | char* buff; |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 130 | size_t size; |
| 131 | BufferContext(size_t compressedSize) { |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 132 | // Null to indicate malloc of a new block |
| 133 | buff = 0; |
| 134 | |
| 135 | // Compute the initial length of the uncompression buffer. Note that this |
| 136 | // is twice the length of the compressed buffer and will be doubled again |
| 137 | // in the callback for an initial allocation of 4x compressedSize. This |
| 138 | // calculation is based on the typical compression ratio of bzip2 on LLVM |
| 139 | // bytecode files which typically ranges in the 50%-75% range. Since we |
Chris Lattner | 8c2cb42 | 2005-01-29 17:05:56 +0000 | [diff] [blame] | 140 | // typically get at least 50%, doubling is insufficient. By using a 4x |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 141 | // multiplier on the first allocation, we minimize the impact of having to |
| 142 | // copy the buffer on reallocation. |
| 143 | size = compressedSize*2; |
| 144 | } |
| 145 | |
Chris Lattner | 8c2cb42 | 2005-01-29 17:05:56 +0000 | [diff] [blame] | 146 | /// trimTo - Reduce the size of the buffer down to the specified amount. This |
| 147 | /// is useful after have read in the bytecode file to discard extra unused |
| 148 | /// memory. |
| 149 | /// |
| 150 | void trimTo(size_t NewSize) { |
| 151 | buff = (char*)::realloc(buff, NewSize); |
| 152 | size = NewSize; |
| 153 | } |
| 154 | |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 155 | /// This function handles allocation of the buffer used for decompression of |
| 156 | /// compressed bytecode files. It is called by Compressor::decompress which is |
| 157 | /// called by BytecodeReader::ParseBytecode. |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 158 | static size_t callback(char*&buff, size_t &sz, void* ctxt){ |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 159 | // Case the context variable to our BufferContext |
| 160 | BufferContext* bc = reinterpret_cast<BufferContext*>(ctxt); |
| 161 | |
| 162 | // Compute the new, doubled, size of the block |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 163 | size_t new_size = bc->size * 2; |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 164 | |
| 165 | // Extend or allocate the block (realloc(0,n) == malloc(n)) |
| 166 | char* new_buff = (char*) ::realloc(bc->buff, new_size); |
| 167 | |
| 168 | // Figure out what to return to the Compressor. If this is the first call, |
| 169 | // then bc->buff will be null. In this case we want to return the entire |
| 170 | // buffer because there was no previous allocation. Otherwise, when the |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 171 | // buffer is reallocated, we save the new base pointer in the |
| 172 | // BufferContext.buff field but return the address of only the extension, |
| 173 | // mid-way through the buffer (since its size was doubled). Furthermore, |
| 174 | // the sz result must be 1/2 the total size of the buffer. |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 175 | if (bc->buff == 0 ) { |
| 176 | buff = bc->buff = new_buff; |
| 177 | sz = new_size; |
| 178 | } else { |
| 179 | bc->buff = new_buff; |
| 180 | buff = new_buff + bc->size; |
| 181 | sz = bc->size; |
| 182 | } |
| 183 | |
| 184 | // Retain the size of the allocated block |
| 185 | bc->size = new_size; |
| 186 | |
| 187 | // Make sure we fail (return 1) if we didn't get any memory. |
| 188 | return (bc->buff == 0 ? 1 : 0); |
| 189 | } |
| 190 | }; |
| 191 | |
Chris Lattner | 52b8752 | 2005-01-29 16:53:02 +0000 | [diff] [blame] | 192 | } // end anonymous namespace |
| 193 | |
| 194 | |
| 195 | namespace { |
| 196 | |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 197 | // This structure retains the context when compressing the bytecode file. The |
| 198 | // WriteCompressedData function below uses it to keep track of the previously |
| 199 | // filled chunk of memory (which it writes) and how many bytes have been |
| 200 | // written. |
| 201 | struct WriterContext { |
| 202 | // Initialize the context |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 203 | WriterContext(std::ostream*OS, size_t CS) |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 204 | : chunk(0), sz(0), written(0), compSize(CS), Out(OS) {} |
| 205 | |
| 206 | // Make sure we clean up memory |
| 207 | ~WriterContext() { |
| 208 | if (chunk) |
| 209 | delete [] chunk; |
| 210 | } |
| 211 | |
| 212 | // Write the chunk |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 213 | void write(size_t size = 0) { |
| 214 | size_t write_size = (size == 0 ? sz : size); |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 215 | Out->write(chunk,write_size); |
| 216 | written += write_size; |
| 217 | delete [] chunk; |
| 218 | chunk = 0; |
| 219 | sz = 0; |
| 220 | } |
| 221 | |
| 222 | // This function is a callback used by the Compressor::compress function to |
| 223 | // allocate memory for the compression buffer. This function fulfills that |
| 224 | // responsibility but also writes the previous (now filled) buffer out to the |
| 225 | // stream. |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 226 | static size_t callback(char*& buffer, size_t &size, void* context) { |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 227 | // Cast the context to the structure it must point to. |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 228 | WriterContext* ctxt = reinterpret_cast<WriterContext*>(context); |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 229 | |
| 230 | // If there's a previously allocated chunk, it must now be filled with |
| 231 | // compressed data, so we write it out and deallocate it. |
| 232 | if (ctxt->chunk != 0 && ctxt->sz > 0 ) { |
| 233 | ctxt->write(); |
| 234 | } |
| 235 | |
| 236 | // Compute the size of the next chunk to allocate. We attempt to allocate |
| 237 | // enough memory to handle the compression in a single memory allocation. In |
| 238 | // general, the worst we do on compression of bytecode is about 50% so we |
| 239 | // conservatively estimate compSize / 2 as the size needed for the |
| 240 | // compression buffer. compSize is the size of the compressed data, provided |
| 241 | // by WriteBytecodeToFile. |
| 242 | size = ctxt->sz = ctxt->compSize / 2; |
| 243 | |
| 244 | // Allocate the chunks |
| 245 | buffer = ctxt->chunk = new char [size]; |
| 246 | |
| 247 | // We must return 1 if the allocation failed so that the Compressor knows |
| 248 | // not to use the buffer pointer. |
| 249 | return (ctxt->chunk == 0 ? 1 : 0); |
| 250 | } |
| 251 | |
| 252 | char* chunk; // pointer to the chunk of memory filled by compression |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 253 | size_t sz; // size of chunk |
| 254 | size_t written; // aggregate total of bytes written in all chunks |
| 255 | size_t compSize; // size of the uncompressed buffer |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 256 | std::ostream* Out; // The stream we write the data to. |
| 257 | }; |
| 258 | |
Chris Lattner | 52b8752 | 2005-01-29 16:53:02 +0000 | [diff] [blame] | 259 | } // end anonymous namespace |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 260 | |
| 261 | // Compress in one of three ways |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 262 | size_t Compressor::compress(const char* in, size_t size, |
| 263 | OutputDataCallback* cb, void* context) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 264 | assert(in && "Can't compress null buffer"); |
| 265 | assert(size && "Can't compress empty buffer"); |
| 266 | assert(cb && "Can't compress without a callback function"); |
| 267 | |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 268 | size_t result = 0; |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 269 | |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 270 | // For small files, we just don't bother compressing. bzip2 isn't very good |
| 271 | // with tiny files and can actually make the file larger, so we just avoid |
| 272 | // it altogether. |
Reid Spencer | a7a5cc8 | 2004-11-30 07:13:34 +0000 | [diff] [blame] | 273 | if (size > 64*1024) { |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 274 | // Set up the bz_stream |
| 275 | bz_stream bzdata; |
| 276 | bzdata.bzalloc = 0; |
| 277 | bzdata.bzfree = 0; |
| 278 | bzdata.opaque = 0; |
| 279 | bzdata.next_in = (char*)in; |
| 280 | bzdata.avail_in = size; |
| 281 | bzdata.next_out = 0; |
| 282 | bzdata.avail_out = 0; |
| 283 | switch ( BZ2_bzCompressInit(&bzdata, 5, 0, 100) ) { |
| 284 | case BZ_CONFIG_ERROR: throw std::string("bzip2 library mis-compiled"); |
| 285 | case BZ_PARAM_ERROR: throw std::string("Compressor internal error"); |
| 286 | case BZ_MEM_ERROR: throw std::string("Out of memory"); |
| 287 | case BZ_OK: |
| 288 | default: |
| 289 | break; |
| 290 | } |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 291 | |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 292 | // Get a block of memory |
Tanya Lattner | 5c3fa1e | 2005-01-29 23:29:55 +0000 | [diff] [blame^] | 293 | if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) { |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 294 | BZ2_bzCompressEnd(&bzdata); |
| 295 | throw std::string("Can't allocate output buffer"); |
| 296 | } |
| 297 | |
| 298 | // Put compression code in first byte |
| 299 | (*bzdata.next_out++) = COMP_TYPE_BZIP2; |
| 300 | bzdata.avail_out--; |
| 301 | |
| 302 | // Compress it |
| 303 | int bzerr = BZ_FINISH_OK; |
| 304 | while (BZ_FINISH_OK == (bzerr = BZ2_bzCompress(&bzdata, BZ_FINISH))) { |
Tanya Lattner | 5c3fa1e | 2005-01-29 23:29:55 +0000 | [diff] [blame^] | 305 | if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 306 | BZ2_bzCompressEnd(&bzdata); |
| 307 | throw std::string("Can't allocate output buffer"); |
| 308 | } |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 309 | } |
| 310 | switch (bzerr) { |
| 311 | case BZ_SEQUENCE_ERROR: |
| 312 | case BZ_PARAM_ERROR: throw std::string("Param/Sequence error"); |
| 313 | case BZ_FINISH_OK: |
| 314 | case BZ_STREAM_END: break; |
| 315 | default: throw std::string("Oops: ") + utostr(unsigned(bzerr)); |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 318 | // Finish |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 319 | result = bzdata.total_out_lo32 + 1; |
| 320 | if (sizeof(size_t) == sizeof(uint64_t)) |
| 321 | result |= static_cast<uint64_t>(bzdata.total_out_hi32) << 32; |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 322 | |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 323 | BZ2_bzCompressEnd(&bzdata); |
| 324 | } else { |
| 325 | // Do null compression, for small files |
| 326 | NULLCOMP_stream sdata; |
| 327 | sdata.next_in = (char*)in; |
| 328 | sdata.avail_in = size; |
| 329 | NULLCOMP_init(&sdata); |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 330 | |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 331 | if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) { |
| 332 | throw std::string("Can't allocate output buffer"); |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 335 | *(sdata.next_out++) = COMP_TYPE_NONE; |
| 336 | sdata.avail_out--; |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 337 | |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 338 | while (!NULLCOMP_compress(&sdata)) { |
Reid Spencer | 469c34b | 2004-10-04 17:29:25 +0000 | [diff] [blame] | 339 | if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 340 | throw std::string("Can't allocate output buffer"); |
| 341 | } |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 342 | } |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 343 | |
| 344 | result = sdata.output_count + 1; |
| 345 | NULLCOMP_end(&sdata); |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 346 | } |
| 347 | return result; |
| 348 | } |
| 349 | |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 350 | size_t Compressor::compressToNewBuffer(const char* in, size_t size, char*&out) { |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 351 | BufferContext bc(size); |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 352 | size_t result = compress(in,size,BufferContext::callback,(void*)&bc); |
Chris Lattner | 8c2cb42 | 2005-01-29 17:05:56 +0000 | [diff] [blame] | 353 | bc.trimTo(result); |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 354 | out = bc.buff; |
| 355 | return result; |
| 356 | } |
| 357 | |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 358 | size_t |
| 359 | Compressor::compressToStream(const char*in, size_t size, std::ostream& out) { |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 360 | // Set up the context and writer |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 361 | WriterContext ctxt(&out, size / 2); |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 363 | // Compress everything after the magic number (which we'll alter). |
| 364 | size_t zipSize = Compressor::compress(in,size, |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 365 | WriterContext::callback, (void*)&ctxt); |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 366 | |
| 367 | if (ctxt.chunk) { |
| 368 | ctxt.write(zipSize - ctxt.written); |
| 369 | } |
| 370 | return zipSize; |
| 371 | } |
| 372 | |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 373 | // Decompress in one of three ways |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 374 | size_t Compressor::decompress(const char *in, size_t size, |
| 375 | OutputDataCallback* cb, void* context) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 376 | assert(in && "Can't decompress null buffer"); |
| 377 | assert(size > 1 && "Can't decompress empty buffer"); |
| 378 | assert(cb && "Can't decompress without a callback function"); |
| 379 | |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 380 | size_t result = 0; |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 381 | |
| 382 | switch (*in++) { |
| 383 | case COMP_TYPE_BZIP2: { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 384 | // Set up the bz_stream |
| 385 | bz_stream bzdata; |
| 386 | bzdata.bzalloc = 0; |
| 387 | bzdata.bzfree = 0; |
| 388 | bzdata.opaque = 0; |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 389 | bzdata.next_in = (char*)in; |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 390 | bzdata.avail_in = size - 1; |
| 391 | bzdata.next_out = 0; |
| 392 | bzdata.avail_out = 0; |
| 393 | switch ( BZ2_bzDecompressInit(&bzdata, 0, 0) ) { |
| 394 | case BZ_CONFIG_ERROR: throw std::string("bzip2 library mis-compiled"); |
| 395 | case BZ_PARAM_ERROR: throw std::string("Compressor internal error"); |
| 396 | case BZ_MEM_ERROR: throw std::string("Out of memory"); |
| 397 | case BZ_OK: |
| 398 | default: |
| 399 | break; |
| 400 | } |
| 401 | |
| 402 | // Get a block of memory |
Tanya Lattner | 5c3fa1e | 2005-01-29 23:29:55 +0000 | [diff] [blame^] | 403 | if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 404 | BZ2_bzDecompressEnd(&bzdata); |
| 405 | throw std::string("Can't allocate output buffer"); |
| 406 | } |
| 407 | |
| 408 | // Decompress it |
| 409 | int bzerr = BZ_OK; |
| 410 | while (BZ_OK == (bzerr = BZ2_bzDecompress(&bzdata))) { |
Tanya Lattner | 5c3fa1e | 2005-01-29 23:29:55 +0000 | [diff] [blame^] | 411 | if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 412 | BZ2_bzDecompressEnd(&bzdata); |
| 413 | throw std::string("Can't allocate output buffer"); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | switch (bzerr) { |
| 418 | case BZ_PARAM_ERROR: throw std::string("Compressor internal error"); |
| 419 | case BZ_MEM_ERROR: throw std::string("Out of memory"); |
| 420 | case BZ_DATA_ERROR: throw std::string("Data integrity error"); |
| 421 | case BZ_DATA_ERROR_MAGIC:throw std::string("Data is not BZIP2"); |
| 422 | default: throw("Ooops"); |
| 423 | case BZ_STREAM_END: |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | // Finish |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 428 | result = bzdata.total_out_lo32; |
| 429 | if (sizeof(size_t) == sizeof(uint64_t)) |
| 430 | result |= (static_cast<uint64_t>(bzdata.total_out_hi32) << 32); |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 431 | BZ2_bzDecompressEnd(&bzdata); |
| 432 | break; |
Chris Lattner | ebe989c | 2004-10-04 16:33:25 +0000 | [diff] [blame] | 433 | } |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 434 | |
Reid Spencer | f6a0acd | 2004-11-25 19:38:16 +0000 | [diff] [blame] | 435 | case COMP_TYPE_NONE: { |
Reid Spencer | 047c009 | 2004-10-04 17:45:44 +0000 | [diff] [blame] | 436 | NULLCOMP_stream sdata; |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 437 | sdata.next_in = (char*)in; |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 438 | sdata.avail_in = size - 1; |
Reid Spencer | 047c009 | 2004-10-04 17:45:44 +0000 | [diff] [blame] | 439 | NULLCOMP_init(&sdata); |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 440 | |
Reid Spencer | 469c34b | 2004-10-04 17:29:25 +0000 | [diff] [blame] | 441 | if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 442 | throw std::string("Can't allocate output buffer"); |
| 443 | } |
| 444 | |
Reid Spencer | 047c009 | 2004-10-04 17:45:44 +0000 | [diff] [blame] | 445 | while (!NULLCOMP_decompress(&sdata)) { |
Reid Spencer | 469c34b | 2004-10-04 17:29:25 +0000 | [diff] [blame] | 446 | if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) { |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 447 | throw std::string("Can't allocate output buffer"); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | result = sdata.output_count; |
Reid Spencer | 047c009 | 2004-10-04 17:45:44 +0000 | [diff] [blame] | 452 | NULLCOMP_end(&sdata); |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 453 | break; |
| 454 | } |
| 455 | |
| 456 | default: |
| 457 | throw std::string("Unknown type of compressed data"); |
| 458 | } |
| 459 | |
| 460 | return result; |
| 461 | } |
| 462 | |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 463 | size_t |
| 464 | Compressor::decompressToNewBuffer(const char* in, size_t size, char*&out) { |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 465 | BufferContext bc(size); |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 466 | size_t result = decompress(in,size,BufferContext::callback,(void*)&bc); |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 467 | out = bc.buff; |
| 468 | return result; |
| 469 | } |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 470 | |
| 471 | size_t |
| 472 | Compressor::decompressToStream(const char*in, size_t size, std::ostream& out){ |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 473 | // Set up the context and writer |
| 474 | WriterContext ctxt(&out,size / 2); |
| 475 | |
| 476 | // Compress everything after the magic number (which we'll alter) |
Chris Lattner | 5ae3571 | 2005-01-29 17:17:18 +0000 | [diff] [blame] | 477 | size_t zipSize = Compressor::decompress(in,size, |
Reid Spencer | e3c6ad7 | 2004-11-14 22:04:46 +0000 | [diff] [blame] | 478 | WriterContext::callback, (void*)&ctxt); |
| 479 | |
| 480 | if (ctxt.chunk) { |
| 481 | ctxt.write(zipSize - ctxt.written); |
| 482 | } |
| 483 | return zipSize; |
| 484 | } |
| 485 | |
Reid Spencer | 460eb63 | 2004-10-04 10:49:41 +0000 | [diff] [blame] | 486 | // vim: sw=2 ai |