1 //===- lib/Support/Compressor.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the llvm::Compressor class, an abstraction for memory
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Compressor.h"
17 #include "llvm/ADT/StringExtras.h"
31 inline int getdata(char*& buffer, unsigned& size,
32 llvm::Compressor::OutputDataCallback* cb, void* context) {
35 int result = (*cb)(buffer, size, context);
36 assert(buffer != 0 && "Invalid result from Compressor callback");
37 assert(size != 0 && "Invalid result from Compressor callback");
41 //===----------------------------------------------------------------------===//
42 //=== NULLCOMP - a compression like set of routines that just copies data
43 //=== without doing any compression. This is provided so that if the
44 //=== configured environment doesn't have a compression library the
45 //=== program can still work, albeit using more data/memory.
46 //===----------------------------------------------------------------------===//
48 struct NULLCOMP_stream {
49 // User provided fields
56 uint64_t output_count; // Total count of output bytes
59 void NULLCOMP_init(NULLCOMP_stream* s) {
63 bool NULLCOMP_compress(NULLCOMP_stream* s) {
64 assert(s && "Invalid NULLCOMP_stream");
65 assert(s->next_in != 0);
66 assert(s->next_out != 0);
67 assert(s->avail_in >= 1);
68 assert(s->avail_out >= 1);
70 if (s->avail_out >= s->avail_in) {
71 ::memcpy(s->next_out, s->next_in, s->avail_in);
72 s->output_count += s->avail_in;
73 s->avail_out -= s->avail_in;
74 s->next_in += s->avail_in;
78 ::memcpy(s->next_out, s->next_in, s->avail_out);
79 s->output_count += s->avail_out;
80 s->avail_in -= s->avail_out;
81 s->next_in += s->avail_out;
87 bool NULLCOMP_decompress(NULLCOMP_stream* s) {
88 assert(s && "Invalid NULLCOMP_stream");
89 assert(s->next_in != 0);
90 assert(s->next_out != 0);
91 assert(s->avail_in >= 1);
92 assert(s->avail_out >= 1);
94 if (s->avail_out >= s->avail_in) {
95 ::memcpy(s->next_out, s->next_in, s->avail_in);
96 s->output_count += s->avail_in;
97 s->avail_out -= s->avail_in;
98 s->next_in += s->avail_in;
102 ::memcpy(s->next_out, s->next_in, s->avail_out);
103 s->output_count += s->avail_out;
104 s->avail_in -= s->avail_out;
105 s->next_in += s->avail_out;
111 void NULLCOMP_end(NULLCOMP_stream* strm) {
118 // Compress in one of three ways
119 uint64_t Compressor::compress(char* in, unsigned size, OutputDataCallback* cb,
120 Algorithm hint, void* context ) {
121 assert(in && "Can't compress null buffer");
122 assert(size && "Can't compress empty buffer");
123 assert(cb && "Can't compress without a callback function");
128 case COMP_TYPE_BZIP2: {
129 #if defined(HAVE_BZIP2)
130 // Set up the bz_stream
136 bzdata.avail_in = size;
138 bzdata.avail_out = 0;
139 switch ( BZ2_bzCompressInit(&bzdata, 9, 0, 100) ) {
140 case BZ_CONFIG_ERROR: throw std::string("bzip2 library mis-compiled");
141 case BZ_PARAM_ERROR: throw std::string("Compressor internal error");
142 case BZ_MEM_ERROR: throw std::string("Out of memory");
148 // Get a block of memory
149 if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb,context)) {
150 BZ2_bzCompressEnd(&bzdata);
151 throw std::string("Can't allocate output buffer");
154 // Put compression code in first byte
155 (*bzdata.next_out++) = COMP_TYPE_BZIP2;
159 int bzerr = BZ_FINISH_OK;
160 while (BZ_FINISH_OK == (bzerr = BZ2_bzCompress(&bzdata, BZ_FINISH))) {
161 if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb,context)) {
162 BZ2_bzCompressEnd(&bzdata);
163 throw std::string("Can't allocate output buffer");
167 case BZ_SEQUENCE_ERROR:
168 case BZ_PARAM_ERROR: throw std::string("Param/Sequence error");
170 case BZ_STREAM_END: break;
171 default: throw std::string("Oops: ") + utostr(unsigned(bzerr));
175 result = (static_cast<uint64_t>(bzdata.total_out_hi32) << 32) |
176 bzdata.total_out_lo32 + 1;
178 BZ2_bzCompressEnd(&bzdata);
185 case COMP_TYPE_ZLIB: {
186 #if defined(HAVE_ZLIB)
188 zdata.zalloc = Z_NULL;
189 zdata.zfree = Z_NULL;
190 zdata.opaque = Z_NULL;
191 zdata.next_in = reinterpret_cast<Bytef*>(in);
192 zdata.avail_in = size;
193 if (Z_OK != deflateInit(&zdata,Z_BEST_COMPRESSION))
194 throw std::string(zdata.msg ? zdata.msg : "zlib error");
196 if (0 != getdata((char*&)(zdata.next_out), zdata.avail_out,cb,context)) {
198 throw std::string("Can't allocate output buffer");
201 (*zdata.next_out++) = COMP_TYPE_ZLIB;
205 while ( Z_OK == deflate(&zdata,0) && zdata.avail_out == 0) {
206 if (0 != getdata((char*&)zdata.next_out, zdata.avail_out, cb,context)) {
208 throw std::string("Can't allocate output buffer");
212 while ( Z_STREAM_END != deflate(&zdata, Z_FINISH)) {
213 if (0 != getdata((char*&)zdata.next_out, zdata.avail_out, cb,context)) {
215 throw std::string("Can't allocate output buffer");
219 result = static_cast<uint64_t>(zdata.total_out) + 1;
228 case COMP_TYPE_SIMPLE: {
229 NULLCOMP_stream sdata;
231 sdata.avail_in = size;
232 NULLCOMP_init(&sdata);
234 if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
235 throw std::string("Can't allocate output buffer");
238 *(sdata.next_out++) = COMP_TYPE_SIMPLE;
241 while (!NULLCOMP_compress(&sdata)) {
242 if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
243 throw std::string("Can't allocate output buffer");
247 result = sdata.output_count + 1;
248 NULLCOMP_end(&sdata);
252 throw std::string("Invalid compression type hint");
257 // Decompress in one of three ways
258 uint64_t Compressor::decompress(char *in, unsigned size,
259 OutputDataCallback* cb, void* context) {
260 assert(in && "Can't decompress null buffer");
261 assert(size > 1 && "Can't decompress empty buffer");
262 assert(cb && "Can't decompress without a callback function");
267 case COMP_TYPE_BZIP2: {
268 #if !defined(HAVE_BZIP2)
269 throw std::string("Can't decompress BZIP2 data");
271 // Set up the bz_stream
277 bzdata.avail_in = size - 1;
279 bzdata.avail_out = 0;
280 switch ( BZ2_bzDecompressInit(&bzdata, 0, 0) ) {
281 case BZ_CONFIG_ERROR: throw std::string("bzip2 library mis-compiled");
282 case BZ_PARAM_ERROR: throw std::string("Compressor internal error");
283 case BZ_MEM_ERROR: throw std::string("Out of memory");
289 // Get a block of memory
290 if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb,context)) {
291 BZ2_bzDecompressEnd(&bzdata);
292 throw std::string("Can't allocate output buffer");
297 while (BZ_OK == (bzerr = BZ2_bzDecompress(&bzdata))) {
298 if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb,context)) {
299 BZ2_bzDecompressEnd(&bzdata);
300 throw std::string("Can't allocate output buffer");
305 case BZ_PARAM_ERROR: throw std::string("Compressor internal error");
306 case BZ_MEM_ERROR: throw std::string("Out of memory");
307 case BZ_DATA_ERROR: throw std::string("Data integrity error");
308 case BZ_DATA_ERROR_MAGIC:throw std::string("Data is not BZIP2");
309 default: throw("Ooops");
315 result = (static_cast<uint64_t>(bzdata.total_out_hi32) << 32) |
316 bzdata.total_out_lo32;
317 BZ2_bzDecompressEnd(&bzdata);
322 case COMP_TYPE_ZLIB: {
323 #if !defined(HAVE_ZLIB)
324 throw std::string("Can't decompress ZLIB data");
327 zdata.zalloc = Z_NULL;
328 zdata.zfree = Z_NULL;
329 zdata.opaque = Z_NULL;
330 zdata.next_in = reinterpret_cast<Bytef*>(in);
331 zdata.avail_in = size - 1;
332 if ( Z_OK != inflateInit(&zdata))
333 throw std::string(zdata.msg ? zdata.msg : "zlib error");
335 if (0 != getdata((char*&)zdata.next_out, zdata.avail_out,cb,context)) {
337 throw std::string("Can't allocate output buffer");
341 while (Z_OK == (zerr = inflate(&zdata,0))) {
342 if (0 != getdata((char*&)zdata.next_out, zdata.avail_out,cb,context)) {
344 throw std::string("Can't allocate output buffer");
348 if (zerr != Z_STREAM_END)
349 throw std::string(zdata.msg?zdata.msg:"zlib error");
351 result = static_cast<uint64_t>(zdata.total_out);
357 case COMP_TYPE_SIMPLE: {
358 NULLCOMP_stream sdata;
360 sdata.avail_in = size - 1;
361 NULLCOMP_init(&sdata);
363 if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
364 throw std::string("Can't allocate output buffer");
367 while (!NULLCOMP_decompress(&sdata)) {
368 if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
369 throw std::string("Can't allocate output buffer");
373 result = sdata.output_count;
374 NULLCOMP_end(&sdata);
379 throw std::string("Unknown type of compressed data");