This patch (written by Reid) changes compressor to never throw an exception.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29045 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2006-07-07 17:00:12 +00:00
parent e9c701fd86
commit bbf34398a0
2 changed files with 119 additions and 50 deletions

View File

@ -49,7 +49,8 @@ namespace llvm {
static size_t compressToNewBuffer(
const char* in, ///< The buffer to be compressed
size_t size, ///< The size of the buffer to be compressed
char*&out ///< The returned output buffer
char*&out, ///< The returned output buffer
std::string* error = 0 ///< Optional error message
);
/// This method compresses a block of memory pointed to by \p in with
@ -57,38 +58,38 @@ namespace llvm {
/// writing when this method is called. The stream will not be closed by
/// this method. The \p hint argument indicates which type of
/// compression the caller would *prefer*.
/// @throws std::string explaining error if a compression error occurs
/// @returns The amount of data written to \p out.
/// @brief Compress memory to a file.
static size_t compressToStream(
const char*in, ///< The buffer to be compressed
size_t size, ///< The size of the buffer to be compressed
std::ostream& out ///< The output stream to write data on
std::ostream& out, ///< The output stream to write data on
std::string* error = 0 ///< Optional error message buffer
);
/// This method decompresses a block of memory pointed to by \p in with
/// size \p size to a new block of memory, \p out, \p that was allocated
/// by malloc. It is the caller's responsibility to free \p out.
/// @throws std::string explaining error if a decompression error occurs
/// @returns The size of the output buffer \p out.
/// @brief Decompress memory to a new memory buffer.
static size_t decompressToNewBuffer(
const char *in, ///< The buffer to be decompressed
size_t size, ///< Size of the buffer to be decompressed
char*&out ///< The returned output buffer
char*&out, ///< The returned output buffer
std::string* error = 0 ///< Optional error message buffer
);
/// This method decompresses a block of memory pointed to by \p in with
/// size \p size to a stream. The stream \p out must be open and ready for
/// writing when this method is called. The stream will not be closed by
/// this method.
/// @throws std::string explaining error if a decompression error occurs
/// @returns The amount of data written to \p out.
/// @brief Decompress memory to a stream.
static size_t decompressToStream(
const char *in, ///< The buffer to be decompressed
size_t size, ///< Size of the buffer to be decompressed
std::ostream& out ///< The stream to write write data on
std::ostream& out, ///< The stream to write write data on
std::string* error = 0 ///< Optional error message buffer
);
/// @}
@ -106,7 +107,6 @@ namespace llvm {
/// It is recommended that \p size be chosen based on the some multiple or
/// fraction of the object being decompressed or compressed, respetively.
/// @returns 0 for success, 1 for failure
/// @throws nothing
/// @brief Output callback function type
typedef size_t (OutputDataCallback)(char*& buffer, size_t& size,
void* context);
@ -123,14 +123,14 @@ namespace llvm {
/// the callback are made. The \p hint parameter tells the function which
/// kind of compression to start with. However, if its not available on
/// the platform, the algorithm "falls back" from bzip2 -> zlib -> simple.
/// @throws std::string if an error occurs
/// @returns the total size of the compressed data
/// @brief Compress a block of memory.
static size_t compress(
const char* in, ///< The buffer to be compressed
size_t size, ///< The size of the buffer to be compressed
OutputDataCallback* cb, ///< Call back for memory allocation
void* context = 0 ///< Context for callback
void* context = 0, ///< Context for callback
std::string* error = 0 ///< Optional error message
);
/// This function does the decompression work. The block of memory
@ -143,14 +143,14 @@ namespace llvm {
/// total size will generally be greater than \p size. It is a good idea
/// to provide as large a value to the callback's \p size parameter as
/// possible so that fewer calls to the callback are made.
/// @throws std::string if an error occurs
/// @returns the total size of the decompressed data
/// @brief Decompress a block of memory.
static size_t decompress(
const char *in, ///< The buffer to be decompressed
size_t size, ///< Size of the buffer to be decompressed
OutputDataCallback* cb, ///< Call back for memory allocation
void* context = 0 ///< Context for callback
void* context = 0, ///< Context for callback
std::string* error = 0 ///< Optional error message
);
/// @}

View File

@ -261,7 +261,8 @@ struct WriterContext {
// Compress in one of three ways
size_t Compressor::compress(const char* in, size_t size,
OutputDataCallback* cb, void* context) {
OutputDataCallback* cb, void* context,
std::string* error ) {
assert(in && "Can't compress null buffer");
assert(size && "Can't compress empty buffer");
assert(cb && "Can't compress without a callback function");
@ -282,9 +283,18 @@ size_t Compressor::compress(const char* in, size_t 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_CONFIG_ERROR:
if (error)
*error = "bzip2 library mis-compiled";
return result;
case BZ_PARAM_ERROR:
if (error)
*error = "Compressor internal error";
return result;
case BZ_MEM_ERROR:
if (error)
*error = "Out of memory";
return result;
case BZ_OK:
default:
break;
@ -293,7 +303,9 @@ size_t Compressor::compress(const char* in, size_t size,
// Get a block of memory
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzCompressEnd(&bzdata);
throw std::string("Can't allocate output buffer");
if (error)
*error = "Can't allocate output buffer";
return result;
}
// Put compression code in first byte
@ -305,15 +317,23 @@ size_t Compressor::compress(const char* in, size_t size,
while (BZ_FINISH_OK == (bzerr = BZ2_bzCompress(&bzdata, BZ_FINISH))) {
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzCompressEnd(&bzdata);
throw std::string("Can't allocate output buffer");
if (error)
*error = "Can't allocate output buffer";
return result;
}
}
switch (bzerr) {
case BZ_SEQUENCE_ERROR:
case BZ_PARAM_ERROR: throw std::string("Param/Sequence error");
case BZ_PARAM_ERROR:
if (error)
*error = "Param/Sequence error";
return result;
case BZ_FINISH_OK:
case BZ_STREAM_END: break;
default: throw std::string("Oops: ") + utostr(unsigned(bzerr));
default:
if (error)
*error = "BZip2 Error: " + utostr(unsigned(bzerr));
return result;
}
// Finish
@ -330,7 +350,9 @@ size_t Compressor::compress(const char* in, size_t size,
NULLCOMP_init(&sdata);
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
throw std::string("Can't allocate output buffer");
if (error)
*error = "Can't allocate output buffer";
return result;
}
*(sdata.next_out++) = COMP_TYPE_NONE;
@ -338,7 +360,9 @@ size_t Compressor::compress(const char* in, size_t size,
while (!NULLCOMP_compress(&sdata)) {
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
throw std::string("Can't allocate output buffer");
if (error)
*error = "Can't allocate output buffer";
return result;
}
}
@ -348,24 +372,26 @@ size_t Compressor::compress(const char* in, size_t size,
return result;
}
size_t Compressor::compressToNewBuffer(const char* in, size_t size, char*&out) {
size_t Compressor::compressToNewBuffer(const char* in, size_t size, char*&out,
std::string* error) {
BufferContext bc(size);
size_t result = compress(in,size,BufferContext::callback,(void*)&bc);
size_t result = compress(in,size,BufferContext::callback,(void*)&bc,error);
bc.trimTo(result);
out = bc.buff;
return result;
}
size_t
Compressor::compressToStream(const char*in, size_t size, std::ostream& out) {
Compressor::compressToStream(const char*in, size_t size, std::ostream& out,
std::string* error) {
// Set up the context and writer
WriterContext ctxt(&out, size / 2);
// Compress everything after the magic number (which we'll alter).
size_t zipSize = Compressor::compress(in,size,
WriterContext::callback, (void*)&ctxt);
WriterContext::callback, (void*)&ctxt,error);
if (ctxt.chunk) {
if (zipSize && ctxt.chunk) {
ctxt.write(zipSize - ctxt.written);
}
return zipSize;
@ -373,7 +399,8 @@ Compressor::compressToStream(const char*in, size_t size, std::ostream& out) {
// Decompress in one of three ways
size_t Compressor::decompress(const char *in, size_t size,
OutputDataCallback* cb, void* context) {
OutputDataCallback* cb, void* context,
std::string* error) {
assert(in && "Can't decompress null buffer");
assert(size > 1 && "Can't decompress empty buffer");
assert(cb && "Can't decompress without a callback function");
@ -392,9 +419,18 @@ size_t Compressor::decompress(const char *in, size_t size,
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_CONFIG_ERROR:
if (error)
*error = "bzip2 library mis-compiled";
return result;
case BZ_PARAM_ERROR:
if (error)
*error = "Compressor internal error";
return result;
case BZ_MEM_ERROR:
if (error)
*error = "Out of memory";
return result;
case BZ_OK:
default:
break;
@ -403,7 +439,9 @@ size_t Compressor::decompress(const char *in, size_t size,
// Get a block of memory
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzDecompressEnd(&bzdata);
throw std::string("Can't allocate output buffer");
if (error)
*error = "Can't allocate output buffer";
return result;
}
// Decompress it
@ -412,21 +450,46 @@ size_t Compressor::decompress(const char *in, size_t size,
bzdata.avail_in != 0 ) {
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzDecompressEnd(&bzdata);
throw std::string("Can't allocate output buffer");
if (error)
*error = "Can't allocate output buffer";
return result;
}
}
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");
case BZ_OK: throw std::string("Insufficient input for bzip2");
BZ2_bzDecompressEnd(&bzdata);
case BZ_PARAM_ERROR:
if (error)
*error = "Compressor internal error";
return result;
case BZ_MEM_ERROR:
BZ2_bzDecompressEnd(&bzdata);
if (error)
*error = "Out of memory";
return result;
case BZ_DATA_ERROR:
BZ2_bzDecompressEnd(&bzdata);
if (error)
*error = "Data integrity error";
return result;
case BZ_DATA_ERROR_MAGIC:
BZ2_bzDecompressEnd(&bzdata);
if (error)
*error = "Data is not BZIP2";
return result;
case BZ_OK:
BZ2_bzDecompressEnd(&bzdata);
if (error)
*error = "Insufficient input for bzip2";
return result;
case BZ_STREAM_END: break;
default: throw("Ooops");
default:
BZ2_bzDecompressEnd(&bzdata);
if (error)
*error = "Unknown result code from bzDecompress";
return result;
}
// Finish
result = bzdata.total_out_lo32;
if (sizeof(size_t) == sizeof(uint64_t))
@ -442,12 +505,16 @@ size_t Compressor::decompress(const char *in, size_t size,
NULLCOMP_init(&sdata);
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
throw std::string("Can't allocate output buffer");
if (error)
*error = "Can't allocate output buffer";
return result;
}
while (!NULLCOMP_decompress(&sdata)) {
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
throw std::string("Can't allocate output buffer");
if (error)
*error = "Can't allocate output buffer";
return result;
}
}
@ -457,33 +524,35 @@ size_t Compressor::decompress(const char *in, size_t size,
}
default:
throw std::string("Unknown type of compressed data");
if (error)
*error = "Unknown type of compressed data";
return result;
}
return result;
}
size_t
Compressor::decompressToNewBuffer(const char* in, size_t size, char*&out) {
Compressor::decompressToNewBuffer(const char* in, size_t size, char*&out,
std::string* error) {
BufferContext bc(size);
size_t result = decompress(in,size,BufferContext::callback,(void*)&bc);
size_t result = decompress(in,size,BufferContext::callback,(void*)&bc,error);
out = bc.buff;
return result;
}
size_t
Compressor::decompressToStream(const char*in, size_t size, std::ostream& out){
Compressor::decompressToStream(const char*in, size_t size, std::ostream& out,
std::string* error) {
// Set up the context and writer
WriterContext ctxt(&out,size / 2);
// Decompress everything after the magic number (which we'll alter)
size_t zipSize = Compressor::decompress(in,size,
WriterContext::callback, (void*)&ctxt);
WriterContext::callback, (void*)&ctxt,error);
if (ctxt.chunk) {
if (zipSize && ctxt.chunk) {
ctxt.write(zipSize - ctxt.written);
}
return zipSize;
}
// vim: sw=2 ai