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( static size_t compressToNewBuffer(
const char* in, ///< The buffer to be compressed const char* in, ///< The buffer to be compressed
size_t size, ///< The size of 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 /// 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 /// writing when this method is called. The stream will not be closed by
/// this method. The \p hint argument indicates which type of /// this method. The \p hint argument indicates which type of
/// compression the caller would *prefer*. /// compression the caller would *prefer*.
/// @throws std::string explaining error if a compression error occurs
/// @returns The amount of data written to \p out. /// @returns The amount of data written to \p out.
/// @brief Compress memory to a file. /// @brief Compress memory to a file.
static size_t compressToStream( static size_t compressToStream(
const char*in, ///< The buffer to be compressed const char*in, ///< The buffer to be compressed
size_t size, ///< The size of 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 /// 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 /// 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. /// 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. /// @returns The size of the output buffer \p out.
/// @brief Decompress memory to a new memory buffer. /// @brief Decompress memory to a new memory buffer.
static size_t decompressToNewBuffer( static size_t decompressToNewBuffer(
const char *in, ///< The buffer to be decompressed const char *in, ///< The buffer to be decompressed
size_t size, ///< Size of 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 /// 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 /// 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 /// writing when this method is called. The stream will not be closed by
/// this method. /// this method.
/// @throws std::string explaining error if a decompression error occurs
/// @returns The amount of data written to \p out. /// @returns The amount of data written to \p out.
/// @brief Decompress memory to a stream. /// @brief Decompress memory to a stream.
static size_t decompressToStream( static size_t decompressToStream(
const char *in, ///< The buffer to be decompressed const char *in, ///< The buffer to be decompressed
size_t size, ///< Size of 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 /// It is recommended that \p size be chosen based on the some multiple or
/// fraction of the object being decompressed or compressed, respetively. /// fraction of the object being decompressed or compressed, respetively.
/// @returns 0 for success, 1 for failure /// @returns 0 for success, 1 for failure
/// @throws nothing
/// @brief Output callback function type /// @brief Output callback function type
typedef size_t (OutputDataCallback)(char*& buffer, size_t& size, typedef size_t (OutputDataCallback)(char*& buffer, size_t& size,
void* context); void* context);
@ -123,14 +123,14 @@ namespace llvm {
/// the callback are made. The \p hint parameter tells the function which /// the callback are made. The \p hint parameter tells the function which
/// kind of compression to start with. However, if its not available on /// kind of compression to start with. However, if its not available on
/// the platform, the algorithm "falls back" from bzip2 -> zlib -> simple. /// 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 /// @returns the total size of the compressed data
/// @brief Compress a block of memory. /// @brief Compress a block of memory.
static size_t compress( static size_t compress(
const char* in, ///< The buffer to be compressed const char* in, ///< The buffer to be compressed
size_t size, ///< The size of the buffer to be compressed size_t size, ///< The size of the buffer to be compressed
OutputDataCallback* cb, ///< Call back for memory allocation 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 /// 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 /// 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 /// to provide as large a value to the callback's \p size parameter as
/// possible so that fewer calls to the callback are made. /// 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 /// @returns the total size of the decompressed data
/// @brief Decompress a block of memory. /// @brief Decompress a block of memory.
static size_t decompress( static size_t decompress(
const char *in, ///< The buffer to be decompressed const char *in, ///< The buffer to be decompressed
size_t size, ///< Size of the buffer to be decompressed size_t size, ///< Size of the buffer to be decompressed
OutputDataCallback* cb, ///< Call back for memory allocation 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 // Compress in one of three ways
size_t Compressor::compress(const char* in, size_t size, 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(in && "Can't compress null buffer");
assert(size && "Can't compress empty buffer"); assert(size && "Can't compress empty buffer");
assert(cb && "Can't compress without a callback function"); 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.next_out = 0;
bzdata.avail_out = 0; bzdata.avail_out = 0;
switch ( BZ2_bzCompressInit(&bzdata, 5, 0, 100) ) { switch ( BZ2_bzCompressInit(&bzdata, 5, 0, 100) ) {
case BZ_CONFIG_ERROR: throw std::string("bzip2 library mis-compiled"); case BZ_CONFIG_ERROR:
case BZ_PARAM_ERROR: throw std::string("Compressor internal error"); if (error)
case BZ_MEM_ERROR: throw std::string("Out of memory"); *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: case BZ_OK:
default: default:
break; break;
@ -293,7 +303,9 @@ size_t Compressor::compress(const char* in, size_t size,
// Get a block of memory // Get a block of memory
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) { if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzCompressEnd(&bzdata); 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 // 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))) { while (BZ_FINISH_OK == (bzerr = BZ2_bzCompress(&bzdata, BZ_FINISH))) {
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) { if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzCompressEnd(&bzdata); BZ2_bzCompressEnd(&bzdata);
throw std::string("Can't allocate output buffer"); if (error)
*error = "Can't allocate output buffer";
return result;
} }
} }
switch (bzerr) { switch (bzerr) {
case BZ_SEQUENCE_ERROR: 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_FINISH_OK:
case BZ_STREAM_END: break; 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 // Finish
@ -330,7 +350,9 @@ size_t Compressor::compress(const char* in, size_t size,
NULLCOMP_init(&sdata); NULLCOMP_init(&sdata);
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) { 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; *(sdata.next_out++) = COMP_TYPE_NONE;
@ -338,7 +360,9 @@ size_t Compressor::compress(const char* in, size_t size,
while (!NULLCOMP_compress(&sdata)) { while (!NULLCOMP_compress(&sdata)) {
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) { 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; 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); 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); bc.trimTo(result);
out = bc.buff; out = bc.buff;
return result; return result;
} }
size_t 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 // Set up the context and writer
WriterContext ctxt(&out, size / 2); WriterContext ctxt(&out, size / 2);
// Compress everything after the magic number (which we'll alter). // Compress everything after the magic number (which we'll alter).
size_t zipSize = Compressor::compress(in,size, 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); ctxt.write(zipSize - ctxt.written);
} }
return zipSize; return zipSize;
@ -373,7 +399,8 @@ Compressor::compressToStream(const char*in, size_t size, std::ostream& out) {
// Decompress in one of three ways // Decompress in one of three ways
size_t Compressor::decompress(const char *in, size_t size, 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(in && "Can't decompress null buffer");
assert(size > 1 && "Can't decompress empty buffer"); assert(size > 1 && "Can't decompress empty buffer");
assert(cb && "Can't decompress without a callback function"); 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.next_out = 0;
bzdata.avail_out = 0; bzdata.avail_out = 0;
switch ( BZ2_bzDecompressInit(&bzdata, 0, 0) ) { switch ( BZ2_bzDecompressInit(&bzdata, 0, 0) ) {
case BZ_CONFIG_ERROR: throw std::string("bzip2 library mis-compiled"); case BZ_CONFIG_ERROR:
case BZ_PARAM_ERROR: throw std::string("Compressor internal error"); if (error)
case BZ_MEM_ERROR: throw std::string("Out of memory"); *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: case BZ_OK:
default: default:
break; break;
@ -403,7 +439,9 @@ size_t Compressor::decompress(const char *in, size_t size,
// Get a block of memory // Get a block of memory
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) { if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzDecompressEnd(&bzdata); BZ2_bzDecompressEnd(&bzdata);
throw std::string("Can't allocate output buffer"); if (error)
*error = "Can't allocate output buffer";
return result;
} }
// Decompress it // Decompress it
@ -412,21 +450,46 @@ size_t Compressor::decompress(const char *in, size_t size,
bzdata.avail_in != 0 ) { bzdata.avail_in != 0 ) {
if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) { if (0 != getdata_uns(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzDecompressEnd(&bzdata); BZ2_bzDecompressEnd(&bzdata);
throw std::string("Can't allocate output buffer"); if (error)
*error = "Can't allocate output buffer";
return result;
} }
} }
switch (bzerr) { switch (bzerr) {
case BZ_PARAM_ERROR: throw std::string("Compressor internal error"); BZ2_bzDecompressEnd(&bzdata);
case BZ_MEM_ERROR: throw std::string("Out of memory"); case BZ_PARAM_ERROR:
case BZ_DATA_ERROR: throw std::string("Data integrity error"); if (error)
case BZ_DATA_ERROR_MAGIC:throw std::string("Data is not BZIP2"); *error = "Compressor internal error";
case BZ_OK: throw std::string("Insufficient input for bzip2"); 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; case BZ_STREAM_END: break;
default: throw("Ooops"); default:
BZ2_bzDecompressEnd(&bzdata);
if (error)
*error = "Unknown result code from bzDecompress";
return result;
} }
// Finish // Finish
result = bzdata.total_out_lo32; result = bzdata.total_out_lo32;
if (sizeof(size_t) == sizeof(uint64_t)) if (sizeof(size_t) == sizeof(uint64_t))
@ -442,12 +505,16 @@ size_t Compressor::decompress(const char *in, size_t size,
NULLCOMP_init(&sdata); NULLCOMP_init(&sdata);
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) { 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)) { while (!NULLCOMP_decompress(&sdata)) {
if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) { 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: default:
throw std::string("Unknown type of compressed data"); if (error)
*error = "Unknown type of compressed data";
return result;
} }
return result; return result;
} }
size_t 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); 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; out = bc.buff;
return result; return result;
} }
size_t 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 // Set up the context and writer
WriterContext ctxt(&out,size / 2); WriterContext ctxt(&out,size / 2);
// Decompress everything after the magic number (which we'll alter) // Decompress everything after the magic number (which we'll alter)
size_t zipSize = Compressor::decompress(in,size, 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); ctxt.write(zipSize - ctxt.written);
} }
return zipSize; return zipSize;
} }
// vim: sw=2 ai