2004-10-04 10:49:41 +00:00
|
|
|
//===- llvm/Support/Compressor.h --------------------------------*- C++ -*-===//
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2004-10-04 10:49:41 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2005-04-21 20:48:15 +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 20:48:15 +00:00
|
|
|
//
|
2004-10-04 10:49:41 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares the llvm::Compressor class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_COMPRESSOR_H
|
|
|
|
#define LLVM_SUPPORT_COMPRESSOR_H
|
|
|
|
|
2004-10-04 17:29:25 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2005-01-29 17:16:07 +00:00
|
|
|
#include <iosfwd>
|
2004-10-04 10:49:41 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2004-11-25 19:37:42 +00:00
|
|
|
/// This class provides an abstraction for compression and decompression of
|
|
|
|
/// a block of memory. The algorithm used here is currently bzip2 but that
|
|
|
|
/// may change without notice. Should newer algorithms prove to compress
|
|
|
|
/// bytecode better than bzip2, that newer algorithm will be added, but won't
|
2005-04-21 20:48:15 +00:00
|
|
|
/// replace bzip2. This interface allows us to abstract the notion of
|
|
|
|
/// compression and deal with alternate compression schemes over time.
|
|
|
|
/// The type of compression used can be determined by inspecting the
|
|
|
|
/// first byte of the compressed output. Currently value '0' means no
|
2004-11-25 19:37:42 +00:00
|
|
|
/// compression was used (for very small files) and value '2' means bzip2
|
2005-04-21 20:48:15 +00:00
|
|
|
/// compression was used. The Compressor is intended for use with memory
|
2004-11-25 19:37:42 +00:00
|
|
|
/// mapped files where the entire data block to be compressed or decompressed
|
|
|
|
/// is available in memory. However, output can be gathered in repeated calls
|
2005-04-21 20:48:15 +00:00
|
|
|
/// to a callback. Utilities for sending compressed or decompressed output
|
2004-11-25 19:37:42 +00:00
|
|
|
/// to a stream or directly to a memory block are also provided.
|
2004-10-04 10:49:41 +00:00
|
|
|
/// @since 1.4
|
2004-10-04 17:29:25 +00:00
|
|
|
/// @brief An abstraction for memory to memory data (de)compression
|
2004-10-04 10:49:41 +00:00
|
|
|
class Compressor {
|
2004-11-14 21:50:00 +00:00
|
|
|
/// @name High Level Interface
|
|
|
|
/// @{
|
|
|
|
public:
|
2005-04-21 20:48:15 +00:00
|
|
|
/// This method compresses a block of memory pointed to by \p in with
|
|
|
|
/// size \p size to a block of memory, \p out, that is allocated with
|
2004-11-14 21:50:00 +00:00
|
|
|
/// malloc. It is the caller's responsibility to free \p out. The \p hint
|
|
|
|
/// indicates which type of compression the caller would *prefer*.
|
|
|
|
/// @throws std::string explaining error if a compression error occurs
|
|
|
|
/// @returns The size of the output buffer \p out.
|
|
|
|
/// @brief Compress memory to a new memory buffer.
|
2005-01-29 17:16:07 +00:00
|
|
|
static size_t compressToNewBuffer(
|
2004-11-14 21:50:00 +00:00
|
|
|
const char* in, ///< The buffer to be compressed
|
2005-01-29 17:16:07 +00:00
|
|
|
size_t size, ///< The size of the buffer to be compressed
|
2006-07-07 17:00:12 +00:00
|
|
|
char*&out, ///< The returned output buffer
|
|
|
|
std::string* error = 0 ///< Optional error message
|
2004-11-14 21:50:00 +00:00
|
|
|
);
|
|
|
|
|
2005-04-21 20:48:15 +00:00
|
|
|
/// This method compresses a block of memory pointed to by \p in with
|
2004-11-14 21:50:00 +00:00
|
|
|
/// 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
|
2005-04-21 20:48:15 +00:00
|
|
|
/// this method. The \p hint argument indicates which type of
|
2004-11-14 21:50:00 +00:00
|
|
|
/// compression the caller would *prefer*.
|
|
|
|
/// @returns The amount of data written to \p out.
|
|
|
|
/// @brief Compress memory to a file.
|
2005-01-29 17:16:07 +00:00
|
|
|
static size_t compressToStream(
|
2004-11-14 21:50:00 +00:00
|
|
|
const char*in, ///< The buffer to be compressed
|
2005-01-29 17:16:07 +00:00
|
|
|
size_t size, ///< The size of the buffer to be compressed
|
2006-07-07 17:00:12 +00:00
|
|
|
std::ostream& out, ///< The output stream to write data on
|
|
|
|
std::string* error = 0 ///< Optional error message buffer
|
2004-11-14 21:50:00 +00:00
|
|
|
);
|
|
|
|
|
2005-04-21 20:48:15 +00:00
|
|
|
/// This method decompresses a block of memory pointed to by \p in with
|
2004-11-14 21:50:00 +00:00
|
|
|
/// size \p size to a new block of memory, \p out, \p that was allocated
|
2005-04-21 20:48:15 +00:00
|
|
|
/// by malloc. It is the caller's responsibility to free \p out.
|
2004-11-14 21:50:00 +00:00
|
|
|
/// @returns The size of the output buffer \p out.
|
|
|
|
/// @brief Decompress memory to a new memory buffer.
|
2005-01-29 17:16:07 +00:00
|
|
|
static size_t decompressToNewBuffer(
|
2004-11-14 21:50:00 +00:00
|
|
|
const char *in, ///< The buffer to be decompressed
|
2005-01-29 17:16:07 +00:00
|
|
|
size_t size, ///< Size of the buffer to be decompressed
|
2006-07-07 17:00:12 +00:00
|
|
|
char*&out, ///< The returned output buffer
|
|
|
|
std::string* error = 0 ///< Optional error message buffer
|
2004-11-14 21:50:00 +00:00
|
|
|
);
|
|
|
|
|
2005-04-21 20:48:15 +00:00
|
|
|
/// This method decompresses a block of memory pointed to by \p in with
|
2004-11-14 21:50:00 +00:00
|
|
|
/// 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
|
2005-04-21 20:48:15 +00:00
|
|
|
/// this method.
|
2004-11-14 21:50:00 +00:00
|
|
|
/// @returns The amount of data written to \p out.
|
|
|
|
/// @brief Decompress memory to a stream.
|
2005-01-29 17:16:07 +00:00
|
|
|
static size_t decompressToStream(
|
2004-11-14 21:50:00 +00:00
|
|
|
const char *in, ///< The buffer to be decompressed
|
2005-01-29 17:16:07 +00:00
|
|
|
size_t size, ///< Size of the buffer to be decompressed
|
2006-07-07 17:00:12 +00:00
|
|
|
std::ostream& out, ///< The stream to write write data on
|
|
|
|
std::string* error = 0 ///< Optional error message buffer
|
2004-11-14 21:50:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Low Level Interface
|
|
|
|
/// @{
|
|
|
|
public:
|
|
|
|
/// A callback function type used by the Compressor's low level interface
|
2005-04-21 20:48:15 +00:00
|
|
|
/// to get the next chunk of data to which (de)compressed output will be
|
|
|
|
/// written. This callback completely abstracts the notion of how to
|
2004-11-14 21:50:00 +00:00
|
|
|
/// handle the output data of compression or decompression. The callback
|
2005-04-21 20:48:15 +00:00
|
|
|
/// is responsible for determining both the storage location and the size
|
2004-11-14 21:50:00 +00:00
|
|
|
/// of the output. The callback may also do other things with the data
|
|
|
|
/// such as write it, transmit it, etc. Note that providing very small
|
|
|
|
/// values for \p size will make the compression run very inefficiently.
|
|
|
|
/// It is recommended that \p size be chosen based on the some multiple or
|
|
|
|
/// fraction of the object being decompressed or compressed, respetively.
|
2004-10-04 10:49:41 +00:00
|
|
|
/// @returns 0 for success, 1 for failure
|
|
|
|
/// @brief Output callback function type
|
2005-01-29 17:16:07 +00:00
|
|
|
typedef size_t (OutputDataCallback)(char*& buffer, size_t& size,
|
2004-10-04 17:29:25 +00:00
|
|
|
void* context);
|
2004-10-04 10:49:41 +00:00
|
|
|
|
|
|
|
/// This function does the compression work. The block of memory starting
|
|
|
|
/// at \p in and extending for \p size bytes is compressed. The compressed
|
|
|
|
/// output is written to memory blocks returned by the \p cb callback. The
|
|
|
|
/// caller must provide an implementation of the OutputDataCallback
|
|
|
|
/// function type and provide its address as \p cb. Note that the callback
|
|
|
|
/// function will be called as many times as necessary to complete the
|
|
|
|
/// compression of the \p in block but that the total size will generally
|
|
|
|
/// be less 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. 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.
|
|
|
|
/// @returns the total size of the compressed data
|
|
|
|
/// @brief Compress a block of memory.
|
2005-01-29 17:16:07 +00:00
|
|
|
static size_t compress(
|
2004-11-14 21:50:00 +00:00
|
|
|
const char* in, ///< The buffer to be compressed
|
2005-01-29 17:16:07 +00:00
|
|
|
size_t size, ///< The size of the buffer to be compressed
|
2004-11-14 21:50:00 +00:00
|
|
|
OutputDataCallback* cb, ///< Call back for memory allocation
|
2006-07-07 17:00:12 +00:00
|
|
|
void* context = 0, ///< Context for callback
|
|
|
|
std::string* error = 0 ///< Optional error message
|
2004-11-14 21:50:00 +00:00
|
|
|
);
|
2004-10-04 10:49:41 +00:00
|
|
|
|
|
|
|
/// This function does the decompression work. The block of memory
|
|
|
|
/// starting at \p in and extending for \p size bytes is decompressed. The
|
|
|
|
/// decompressed output is written to memory blocks returned by the \p cb
|
|
|
|
/// callback. The caller must provide an implementation of the
|
|
|
|
/// OutputDataCallback function type and provide its address as \p cb.
|
|
|
|
/// Note that the callback function will be called as many times as
|
|
|
|
/// necessary to complete the compression of the \p in block but that the
|
|
|
|
/// total size will generally be greater than \p size. It is a good idea
|
2005-04-21 20:48:15 +00:00
|
|
|
/// to provide as large a value to the callback's \p size parameter as
|
2004-10-04 10:49:41 +00:00
|
|
|
/// possible so that fewer calls to the callback are made.
|
|
|
|
/// @returns the total size of the decompressed data
|
|
|
|
/// @brief Decompress a block of memory.
|
2005-01-29 17:16:07 +00:00
|
|
|
static size_t decompress(
|
2004-11-14 21:50:00 +00:00
|
|
|
const char *in, ///< The buffer to be decompressed
|
2005-01-29 17:16:07 +00:00
|
|
|
size_t size, ///< Size of the buffer to be decompressed
|
2004-11-14 21:50:00 +00:00
|
|
|
OutputDataCallback* cb, ///< Call back for memory allocation
|
2006-07-07 17:00:12 +00:00
|
|
|
void* context = 0, ///< Context for callback
|
|
|
|
std::string* error = 0 ///< Optional error message
|
2004-11-14 21:50:00 +00:00
|
|
|
);
|
2004-10-04 10:49:41 +00:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim: sw=2 ai
|
|
|
|
|
|
|
|
#endif
|