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
/// 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
);
/// @}
/// 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);
/// 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
/// 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
);
/// @}
// 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");
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;
// 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
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
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;
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;
}
}
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;
// 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");
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;
// 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
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))
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;
}
}
}
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