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