2004-10-04 10:49:41 +00:00
|
|
|
//===- lib/Support/Compressor.cpp -------------------------------*- C++ -*-===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-10-04 10:49:41 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2005-04-21 22:55:34 +00:00
|
|
|
// This file was developed by Reid Spencer and is distributed under the
|
2004-10-04 10:49:41 +00:00
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-10-04 10:49:41 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the llvm::Compressor class, an abstraction for memory
|
|
|
|
// block compression.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Config/config.h"
|
|
|
|
#include "llvm/Support/Compressor.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
2005-01-29 17:17:18 +00:00
|
|
|
#include <ostream>
|
2004-11-25 19:38:16 +00:00
|
|
|
#include "bzip2/bzlib.h"
|
2005-01-29 16:53:02 +00:00
|
|
|
using namespace llvm;
|
2004-10-04 10:49:41 +00:00
|
|
|
|
2004-11-25 19:38:16 +00:00
|
|
|
enum CompressionTypes {
|
|
|
|
COMP_TYPE_NONE = '0',
|
|
|
|
COMP_TYPE_BZIP2 = '2',
|
|
|
|
};
|
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
static int getdata(char*& buffer, size_t &size,
|
2004-10-04 17:29:25 +00:00
|
|
|
llvm::Compressor::OutputDataCallback* cb, void* context) {
|
2004-10-04 10:49:41 +00:00
|
|
|
buffer = 0;
|
|
|
|
size = 0;
|
2004-10-04 17:29:25 +00:00
|
|
|
int result = (*cb)(buffer, size, context);
|
2004-10-04 10:49:41 +00:00
|
|
|
assert(buffer != 0 && "Invalid result from Compressor callback");
|
|
|
|
assert(size != 0 && "Invalid result from Compressor callback");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
static int getdata_uns(char*& buffer, unsigned &size,
|
2005-04-22 04:08:30 +00:00
|
|
|
llvm::Compressor::OutputDataCallback* cb, void* context)
|
|
|
|
{
|
2005-01-29 23:08:01 +00:00
|
|
|
size_t SizeOut;
|
|
|
|
int Res = getdata(buffer, SizeOut, cb, context);
|
|
|
|
size = SizeOut;
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2004-10-04 10:49:41 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//=== NULLCOMP - a compression like set of routines that just copies data
|
2004-10-04 17:45:44 +00:00
|
|
|
//=== without doing any compression. This is provided so that if the
|
|
|
|
//=== configured environment doesn't have a compression library the
|
|
|
|
//=== program can still work, albeit using more data/memory.
|
2004-10-04 10:49:41 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-10-04 17:45:44 +00:00
|
|
|
struct NULLCOMP_stream {
|
2004-10-04 10:49:41 +00:00
|
|
|
// User provided fields
|
2005-01-29 17:17:18 +00:00
|
|
|
char* next_in;
|
|
|
|
size_t avail_in;
|
|
|
|
char* next_out;
|
|
|
|
size_t avail_out;
|
2004-10-04 10:49:41 +00:00
|
|
|
|
|
|
|
// Information fields
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t output_count; // Total count of output bytes
|
2004-10-04 10:49:41 +00:00
|
|
|
};
|
|
|
|
|
2005-01-29 16:53:02 +00:00
|
|
|
static void NULLCOMP_init(NULLCOMP_stream* s) {
|
2004-10-04 10:49:41 +00:00
|
|
|
s->output_count = 0;
|
|
|
|
}
|
|
|
|
|
2005-01-29 16:53:02 +00:00
|
|
|
static bool NULLCOMP_compress(NULLCOMP_stream* s) {
|
2004-10-04 17:45:44 +00:00
|
|
|
assert(s && "Invalid NULLCOMP_stream");
|
2004-10-04 10:49:41 +00:00
|
|
|
assert(s->next_in != 0);
|
|
|
|
assert(s->next_out != 0);
|
|
|
|
assert(s->avail_in >= 1);
|
|
|
|
assert(s->avail_out >= 1);
|
|
|
|
|
|
|
|
if (s->avail_out >= s->avail_in) {
|
|
|
|
::memcpy(s->next_out, s->next_in, s->avail_in);
|
|
|
|
s->output_count += s->avail_in;
|
|
|
|
s->avail_out -= s->avail_in;
|
|
|
|
s->next_in += s->avail_in;
|
|
|
|
s->avail_in = 0;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
::memcpy(s->next_out, s->next_in, s->avail_out);
|
|
|
|
s->output_count += s->avail_out;
|
|
|
|
s->avail_in -= s->avail_out;
|
|
|
|
s->next_in += s->avail_out;
|
|
|
|
s->avail_out = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-29 16:53:02 +00:00
|
|
|
static bool NULLCOMP_decompress(NULLCOMP_stream* s) {
|
2004-10-04 17:45:44 +00:00
|
|
|
assert(s && "Invalid NULLCOMP_stream");
|
2004-10-04 10:49:41 +00:00
|
|
|
assert(s->next_in != 0);
|
|
|
|
assert(s->next_out != 0);
|
|
|
|
assert(s->avail_in >= 1);
|
|
|
|
assert(s->avail_out >= 1);
|
|
|
|
|
|
|
|
if (s->avail_out >= s->avail_in) {
|
|
|
|
::memcpy(s->next_out, s->next_in, s->avail_in);
|
|
|
|
s->output_count += s->avail_in;
|
|
|
|
s->avail_out -= s->avail_in;
|
|
|
|
s->next_in += s->avail_in;
|
|
|
|
s->avail_in = 0;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
::memcpy(s->next_out, s->next_in, s->avail_out);
|
|
|
|
s->output_count += s->avail_out;
|
|
|
|
s->avail_in -= s->avail_out;
|
|
|
|
s->next_in += s->avail_out;
|
|
|
|
s->avail_out = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-29 16:53:02 +00:00
|
|
|
static void NULLCOMP_end(NULLCOMP_stream* strm) {
|
2004-10-04 10:49:41 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 16:53:02 +00:00
|
|
|
namespace {
|
|
|
|
|
2004-11-14 22:04:46 +00:00
|
|
|
/// This structure is only used when a bytecode file is compressed.
|
|
|
|
/// As bytecode is being decompressed, the memory buffer might need
|
2005-04-21 22:55:34 +00:00
|
|
|
/// to be reallocated. The buffer allocation is handled in a callback
|
2004-11-14 22:04:46 +00:00
|
|
|
/// and this structure is needed to retain information across calls
|
|
|
|
/// to the callback.
|
|
|
|
/// @brief An internal buffer object used for handling decompression
|
|
|
|
struct BufferContext {
|
|
|
|
char* buff;
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t size;
|
2005-04-21 22:55:34 +00:00
|
|
|
BufferContext(size_t compressedSize) {
|
2004-11-14 22:04:46 +00:00
|
|
|
// Null to indicate malloc of a new block
|
2005-04-21 22:55:34 +00:00
|
|
|
buff = 0;
|
2004-11-14 22:04:46 +00:00
|
|
|
|
|
|
|
// Compute the initial length of the uncompression buffer. Note that this
|
|
|
|
// is twice the length of the compressed buffer and will be doubled again
|
2005-04-21 22:55:34 +00:00
|
|
|
// in the callback for an initial allocation of 4x compressedSize. This
|
|
|
|
// calculation is based on the typical compression ratio of bzip2 on LLVM
|
|
|
|
// bytecode files which typically ranges in the 50%-75% range. Since we
|
|
|
|
// typically get at least 50%, doubling is insufficient. By using a 4x
|
2004-11-14 22:04:46 +00:00
|
|
|
// multiplier on the first allocation, we minimize the impact of having to
|
|
|
|
// copy the buffer on reallocation.
|
2005-04-21 22:55:34 +00:00
|
|
|
size = compressedSize*2;
|
2004-11-14 22:04:46 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 17:05:56 +00:00
|
|
|
/// trimTo - Reduce the size of the buffer down to the specified amount. This
|
|
|
|
/// is useful after have read in the bytecode file to discard extra unused
|
|
|
|
/// memory.
|
|
|
|
///
|
|
|
|
void trimTo(size_t NewSize) {
|
|
|
|
buff = (char*)::realloc(buff, NewSize);
|
|
|
|
size = NewSize;
|
|
|
|
}
|
|
|
|
|
2004-11-14 22:04:46 +00:00
|
|
|
/// This function handles allocation of the buffer used for decompression of
|
|
|
|
/// compressed bytecode files. It is called by Compressor::decompress which is
|
2005-04-21 22:55:34 +00:00
|
|
|
/// called by BytecodeReader::ParseBytecode.
|
2005-01-29 17:17:18 +00:00
|
|
|
static size_t callback(char*&buff, size_t &sz, void* ctxt){
|
2004-11-14 22:04:46 +00:00
|
|
|
// Case the context variable to our BufferContext
|
|
|
|
BufferContext* bc = reinterpret_cast<BufferContext*>(ctxt);
|
|
|
|
|
|
|
|
// Compute the new, doubled, size of the block
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t new_size = bc->size * 2;
|
2004-11-14 22:04:46 +00:00
|
|
|
|
|
|
|
// Extend or allocate the block (realloc(0,n) == malloc(n))
|
|
|
|
char* new_buff = (char*) ::realloc(bc->buff, new_size);
|
|
|
|
|
|
|
|
// Figure out what to return to the Compressor. If this is the first call,
|
|
|
|
// then bc->buff will be null. In this case we want to return the entire
|
|
|
|
// buffer because there was no previous allocation. Otherwise, when the
|
2005-04-21 22:55:34 +00:00
|
|
|
// buffer is reallocated, we save the new base pointer in the
|
|
|
|
// BufferContext.buff field but return the address of only the extension,
|
|
|
|
// mid-way through the buffer (since its size was doubled). Furthermore,
|
2004-11-25 19:38:16 +00:00
|
|
|
// the sz result must be 1/2 the total size of the buffer.
|
2004-11-14 22:04:46 +00:00
|
|
|
if (bc->buff == 0 ) {
|
|
|
|
buff = bc->buff = new_buff;
|
|
|
|
sz = new_size;
|
|
|
|
} else {
|
|
|
|
bc->buff = new_buff;
|
|
|
|
buff = new_buff + bc->size;
|
|
|
|
sz = bc->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retain the size of the allocated block
|
|
|
|
bc->size = new_size;
|
|
|
|
|
|
|
|
// Make sure we fail (return 1) if we didn't get any memory.
|
|
|
|
return (bc->buff == 0 ? 1 : 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
} // end anonymous namespace
|
2005-01-29 16:53:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2004-11-14 22:04:46 +00:00
|
|
|
// This structure retains the context when compressing the bytecode file. The
|
|
|
|
// WriteCompressedData function below uses it to keep track of the previously
|
2005-04-21 22:55:34 +00:00
|
|
|
// filled chunk of memory (which it writes) and how many bytes have been
|
2004-11-14 22:04:46 +00:00
|
|
|
// written.
|
|
|
|
struct WriterContext {
|
|
|
|
// Initialize the context
|
2005-04-21 22:55:34 +00:00
|
|
|
WriterContext(std::ostream*OS, size_t CS)
|
2004-11-14 22:04:46 +00:00
|
|
|
: chunk(0), sz(0), written(0), compSize(CS), Out(OS) {}
|
|
|
|
|
|
|
|
// Make sure we clean up memory
|
|
|
|
~WriterContext() {
|
|
|
|
if (chunk)
|
|
|
|
delete [] chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the chunk
|
2005-01-29 17:17:18 +00:00
|
|
|
void write(size_t size = 0) {
|
|
|
|
size_t write_size = (size == 0 ? sz : size);
|
2004-11-14 22:04:46 +00:00
|
|
|
Out->write(chunk,write_size);
|
|
|
|
written += write_size;
|
|
|
|
delete [] chunk;
|
|
|
|
chunk = 0;
|
|
|
|
sz = 0;
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
// This function is a callback used by the Compressor::compress function to
|
2004-11-14 22:04:46 +00:00
|
|
|
// allocate memory for the compression buffer. This function fulfills that
|
|
|
|
// responsibility but also writes the previous (now filled) buffer out to the
|
2005-04-21 22:55:34 +00:00
|
|
|
// stream.
|
2005-01-29 17:17:18 +00:00
|
|
|
static size_t callback(char*& buffer, size_t &size, void* context) {
|
2004-11-14 22:04:46 +00:00
|
|
|
// Cast the context to the structure it must point to.
|
2005-01-29 17:17:18 +00:00
|
|
|
WriterContext* ctxt = reinterpret_cast<WriterContext*>(context);
|
2004-11-14 22:04:46 +00:00
|
|
|
|
|
|
|
// If there's a previously allocated chunk, it must now be filled with
|
|
|
|
// compressed data, so we write it out and deallocate it.
|
|
|
|
if (ctxt->chunk != 0 && ctxt->sz > 0 ) {
|
|
|
|
ctxt->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the size of the next chunk to allocate. We attempt to allocate
|
|
|
|
// enough memory to handle the compression in a single memory allocation. In
|
|
|
|
// general, the worst we do on compression of bytecode is about 50% so we
|
|
|
|
// conservatively estimate compSize / 2 as the size needed for the
|
|
|
|
// compression buffer. compSize is the size of the compressed data, provided
|
|
|
|
// by WriteBytecodeToFile.
|
|
|
|
size = ctxt->sz = ctxt->compSize / 2;
|
|
|
|
|
|
|
|
// Allocate the chunks
|
|
|
|
buffer = ctxt->chunk = new char [size];
|
|
|
|
|
|
|
|
// We must return 1 if the allocation failed so that the Compressor knows
|
|
|
|
// not to use the buffer pointer.
|
|
|
|
return (ctxt->chunk == 0 ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* chunk; // pointer to the chunk of memory filled by compression
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t sz; // size of chunk
|
|
|
|
size_t written; // aggregate total of bytes written in all chunks
|
|
|
|
size_t compSize; // size of the uncompressed buffer
|
2004-11-14 22:04:46 +00:00
|
|
|
std::ostream* Out; // The stream we write the data to.
|
|
|
|
};
|
|
|
|
|
2005-01-29 16:53:02 +00:00
|
|
|
} // end anonymous namespace
|
2004-10-04 10:49:41 +00:00
|
|
|
|
|
|
|
// Compress in one of three ways
|
2005-04-21 22:55:34 +00:00
|
|
|
size_t Compressor::compress(const char* in, size_t size,
|
2005-01-29 17:17:18 +00:00
|
|
|
OutputDataCallback* cb, void* context) {
|
2004-10-04 10:49:41 +00:00
|
|
|
assert(in && "Can't compress null buffer");
|
|
|
|
assert(size && "Can't compress empty buffer");
|
|
|
|
assert(cb && "Can't compress without a callback function");
|
|
|
|
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t result = 0;
|
2004-10-04 10:49:41 +00:00
|
|
|
|
2004-11-25 19:38:16 +00:00
|
|
|
// For small files, we just don't bother compressing. bzip2 isn't very good
|
|
|
|
// with tiny files and can actually make the file larger, so we just avoid
|
|
|
|
// it altogether.
|
2004-11-30 07:13:34 +00:00
|
|
|
if (size > 64*1024) {
|
2004-11-25 19:38:16 +00:00
|
|
|
// Set up the bz_stream
|
|
|
|
bz_stream bzdata;
|
|
|
|
bzdata.bzalloc = 0;
|
|
|
|
bzdata.bzfree = 0;
|
|
|
|
bzdata.opaque = 0;
|
|
|
|
bzdata.next_in = (char*)in;
|
|
|
|
bzdata.avail_in = size;
|
|
|
|
bzdata.next_out = 0;
|
|
|
|
bzdata.avail_out = 0;
|
|
|
|
switch ( BZ2_bzCompressInit(&bzdata, 5, 0, 100) ) {
|
|
|
|
case BZ_CONFIG_ERROR: throw std::string("bzip2 library mis-compiled");
|
|
|
|
case BZ_PARAM_ERROR: throw std::string("Compressor internal error");
|
|
|
|
case BZ_MEM_ERROR: throw std::string("Out of memory");
|
|
|
|
case BZ_OK:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2004-10-04 10:49:41 +00:00
|
|
|
|
2004-11-25 19:38:16 +00:00
|
|
|
// Get a block of memory
|
2005-01-29 23:29:55 +00:00
|
|
|
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
|
2004-10-04 10:49:41 +00:00
|
|
|
BZ2_bzCompressEnd(&bzdata);
|
2004-11-25 19:38:16 +00:00
|
|
|
throw std::string("Can't allocate output buffer");
|
2004-10-04 10:49:41 +00:00
|
|
|
}
|
|
|
|
|
2004-11-25 19:38:16 +00:00
|
|
|
// Put compression code in first byte
|
|
|
|
(*bzdata.next_out++) = COMP_TYPE_BZIP2;
|
|
|
|
bzdata.avail_out--;
|
2004-10-04 10:49:41 +00:00
|
|
|
|
2004-11-25 19:38:16 +00:00
|
|
|
// Compress it
|
|
|
|
int bzerr = BZ_FINISH_OK;
|
|
|
|
while (BZ_FINISH_OK == (bzerr = BZ2_bzCompress(&bzdata, BZ_FINISH))) {
|
2005-01-29 23:29:55 +00:00
|
|
|
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
|
2004-11-25 19:38:16 +00:00
|
|
|
BZ2_bzCompressEnd(&bzdata);
|
|
|
|
throw std::string("Can't allocate output buffer");
|
2004-10-04 10:49:41 +00:00
|
|
|
}
|
2004-11-25 19:38:16 +00:00
|
|
|
}
|
|
|
|
switch (bzerr) {
|
|
|
|
case BZ_SEQUENCE_ERROR:
|
|
|
|
case BZ_PARAM_ERROR: throw std::string("Param/Sequence error");
|
|
|
|
case BZ_FINISH_OK:
|
|
|
|
case BZ_STREAM_END: break;
|
|
|
|
default: throw std::string("Oops: ") + utostr(unsigned(bzerr));
|
|
|
|
}
|
2004-10-04 10:49:41 +00:00
|
|
|
|
2004-11-25 19:38:16 +00:00
|
|
|
// Finish
|
2005-01-29 17:17:18 +00:00
|
|
|
result = bzdata.total_out_lo32 + 1;
|
|
|
|
if (sizeof(size_t) == sizeof(uint64_t))
|
|
|
|
result |= static_cast<uint64_t>(bzdata.total_out_hi32) << 32;
|
2004-10-04 10:49:41 +00:00
|
|
|
|
2004-11-25 19:38:16 +00:00
|
|
|
BZ2_bzCompressEnd(&bzdata);
|
|
|
|
} else {
|
|
|
|
// Do null compression, for small files
|
|
|
|
NULLCOMP_stream sdata;
|
|
|
|
sdata.next_in = (char*)in;
|
|
|
|
sdata.avail_in = size;
|
|
|
|
NULLCOMP_init(&sdata);
|
|
|
|
|
|
|
|
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
|
|
|
|
throw std::string("Can't allocate output buffer");
|
2004-10-04 10:49:41 +00:00
|
|
|
}
|
|
|
|
|
2004-11-25 19:38:16 +00:00
|
|
|
*(sdata.next_out++) = COMP_TYPE_NONE;
|
|
|
|
sdata.avail_out--;
|
2004-10-04 10:49:41 +00:00
|
|
|
|
2004-11-25 19:38:16 +00:00
|
|
|
while (!NULLCOMP_compress(&sdata)) {
|
2004-10-04 17:29:25 +00:00
|
|
|
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
|
2004-10-04 10:49:41 +00:00
|
|
|
throw std::string("Can't allocate output buffer");
|
|
|
|
}
|
|
|
|
}
|
2004-11-25 19:38:16 +00:00
|
|
|
|
|
|
|
result = sdata.output_count + 1;
|
|
|
|
NULLCOMP_end(&sdata);
|
2004-10-04 10:49:41 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t Compressor::compressToNewBuffer(const char* in, size_t size, char*&out) {
|
2004-11-14 22:04:46 +00:00
|
|
|
BufferContext bc(size);
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t result = compress(in,size,BufferContext::callback,(void*)&bc);
|
2005-01-29 17:05:56 +00:00
|
|
|
bc.trimTo(result);
|
2004-11-14 22:04:46 +00:00
|
|
|
out = bc.buff;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
size_t
|
2005-01-29 17:17:18 +00:00
|
|
|
Compressor::compressToStream(const char*in, size_t size, std::ostream& out) {
|
2004-11-14 22:04:46 +00:00
|
|
|
// Set up the context and writer
|
2005-01-29 17:17:18 +00:00
|
|
|
WriterContext ctxt(&out, size / 2);
|
2004-11-14 22:04:46 +00:00
|
|
|
|
2005-01-29 17:17:18 +00:00
|
|
|
// Compress everything after the magic number (which we'll alter).
|
|
|
|
size_t zipSize = Compressor::compress(in,size,
|
2004-11-25 19:38:16 +00:00
|
|
|
WriterContext::callback, (void*)&ctxt);
|
2004-11-14 22:04:46 +00:00
|
|
|
|
|
|
|
if (ctxt.chunk) {
|
|
|
|
ctxt.write(zipSize - ctxt.written);
|
|
|
|
}
|
|
|
|
return zipSize;
|
|
|
|
}
|
|
|
|
|
2004-10-04 10:49:41 +00:00
|
|
|
// Decompress in one of three ways
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t Compressor::decompress(const char *in, size_t size,
|
|
|
|
OutputDataCallback* cb, void* context) {
|
2004-10-04 10:49:41 +00:00
|
|
|
assert(in && "Can't decompress null buffer");
|
|
|
|
assert(size > 1 && "Can't decompress empty buffer");
|
|
|
|
assert(cb && "Can't decompress without a callback function");
|
|
|
|
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t result = 0;
|
2004-10-04 10:49:41 +00:00
|
|
|
|
|
|
|
switch (*in++) {
|
|
|
|
case COMP_TYPE_BZIP2: {
|
|
|
|
// Set up the bz_stream
|
|
|
|
bz_stream bzdata;
|
|
|
|
bzdata.bzalloc = 0;
|
|
|
|
bzdata.bzfree = 0;
|
|
|
|
bzdata.opaque = 0;
|
2004-11-14 22:04:46 +00:00
|
|
|
bzdata.next_in = (char*)in;
|
2004-10-04 10:49:41 +00:00
|
|
|
bzdata.avail_in = size - 1;
|
|
|
|
bzdata.next_out = 0;
|
|
|
|
bzdata.avail_out = 0;
|
|
|
|
switch ( BZ2_bzDecompressInit(&bzdata, 0, 0) ) {
|
|
|
|
case BZ_CONFIG_ERROR: throw std::string("bzip2 library mis-compiled");
|
|
|
|
case BZ_PARAM_ERROR: throw std::string("Compressor internal error");
|
|
|
|
case BZ_MEM_ERROR: throw std::string("Out of memory");
|
|
|
|
case BZ_OK:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a block of memory
|
2005-01-29 23:29:55 +00:00
|
|
|
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
|
2004-10-04 10:49:41 +00:00
|
|
|
BZ2_bzDecompressEnd(&bzdata);
|
|
|
|
throw std::string("Can't allocate output buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decompress it
|
|
|
|
int bzerr = BZ_OK;
|
2005-07-27 06:12:32 +00:00
|
|
|
while ( BZ_OK == (bzerr = BZ2_bzDecompress(&bzdata)) &&
|
2005-05-13 07:05:37 +00:00
|
|
|
bzdata.avail_in != 0 ) {
|
2005-01-29 23:29:55 +00:00
|
|
|
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
|
2004-10-04 10:49:41 +00:00
|
|
|
BZ2_bzDecompressEnd(&bzdata);
|
|
|
|
throw std::string("Can't allocate output buffer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (bzerr) {
|
|
|
|
case BZ_PARAM_ERROR: throw std::string("Compressor internal error");
|
|
|
|
case BZ_MEM_ERROR: throw std::string("Out of memory");
|
|
|
|
case BZ_DATA_ERROR: throw std::string("Data integrity error");
|
|
|
|
case BZ_DATA_ERROR_MAGIC:throw std::string("Data is not BZIP2");
|
2005-05-13 07:05:37 +00:00
|
|
|
case BZ_OK: throw std::string("Insufficient input for bzip2");
|
|
|
|
case BZ_STREAM_END: break;
|
2004-10-04 10:49:41 +00:00
|
|
|
default: throw("Ooops");
|
|
|
|
}
|
|
|
|
|
2005-05-13 07:05:37 +00:00
|
|
|
|
2004-10-04 10:49:41 +00:00
|
|
|
// Finish
|
2005-01-29 17:17:18 +00:00
|
|
|
result = bzdata.total_out_lo32;
|
|
|
|
if (sizeof(size_t) == sizeof(uint64_t))
|
|
|
|
result |= (static_cast<uint64_t>(bzdata.total_out_hi32) << 32);
|
2004-10-04 10:49:41 +00:00
|
|
|
BZ2_bzDecompressEnd(&bzdata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-11-25 19:38:16 +00:00
|
|
|
case COMP_TYPE_NONE: {
|
2004-10-04 17:45:44 +00:00
|
|
|
NULLCOMP_stream sdata;
|
2004-11-14 22:04:46 +00:00
|
|
|
sdata.next_in = (char*)in;
|
2004-10-04 10:49:41 +00:00
|
|
|
sdata.avail_in = size - 1;
|
2004-10-04 17:45:44 +00:00
|
|
|
NULLCOMP_init(&sdata);
|
2004-10-04 10:49:41 +00:00
|
|
|
|
2004-10-04 17:29:25 +00:00
|
|
|
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
|
2004-10-04 10:49:41 +00:00
|
|
|
throw std::string("Can't allocate output buffer");
|
|
|
|
}
|
|
|
|
|
2004-10-04 17:45:44 +00:00
|
|
|
while (!NULLCOMP_decompress(&sdata)) {
|
2004-10-04 17:29:25 +00:00
|
|
|
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
|
2004-10-04 10:49:41 +00:00
|
|
|
throw std::string("Can't allocate output buffer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = sdata.output_count;
|
2004-10-04 17:45:44 +00:00
|
|
|
NULLCOMP_end(&sdata);
|
2004-10-04 10:49:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw std::string("Unknown type of compressed data");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
size_t
|
2005-01-29 17:17:18 +00:00
|
|
|
Compressor::decompressToNewBuffer(const char* in, size_t size, char*&out) {
|
2004-11-14 22:04:46 +00:00
|
|
|
BufferContext bc(size);
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t result = decompress(in,size,BufferContext::callback,(void*)&bc);
|
2004-11-14 22:04:46 +00:00
|
|
|
out = bc.buff;
|
|
|
|
return result;
|
|
|
|
}
|
2005-01-29 17:17:18 +00:00
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
size_t
|
2005-01-29 17:17:18 +00:00
|
|
|
Compressor::decompressToStream(const char*in, size_t size, std::ostream& out){
|
2004-11-14 22:04:46 +00:00
|
|
|
// Set up the context and writer
|
|
|
|
WriterContext ctxt(&out,size / 2);
|
|
|
|
|
2005-05-13 07:05:37 +00:00
|
|
|
// Decompress everything after the magic number (which we'll alter)
|
2005-01-29 17:17:18 +00:00
|
|
|
size_t zipSize = Compressor::decompress(in,size,
|
2004-11-14 22:04:46 +00:00
|
|
|
WriterContext::callback, (void*)&ctxt);
|
|
|
|
|
|
|
|
if (ctxt.chunk) {
|
|
|
|
ctxt.write(zipSize - ctxt.written);
|
|
|
|
}
|
|
|
|
return zipSize;
|
|
|
|
}
|
|
|
|
|
2004-10-04 10:49:41 +00:00
|
|
|
// vim: sw=2 ai
|