qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
compression.h File Reference

Data compression utilities for the QB IO library. More...

#include <functional>
#include <limits>
#include <stdexcept>
#include <string>
#include <zlib.h>
#include "qb/system/allocator/pipe.h"
Include dependency graph for compression.h:

Go to the source code of this file.

Classes

struct  qb::compression::operation_result
 Result of a compression/decompression operation. More...
class  qb::compression::compress_provider
 Abstract interface for compression algorithm providers. More...
class  qb::compression::decompress_provider
 Abstract interface for decompression algorithm providers. More...
class  qb::compression::compress_factory
 Factory interface for creating compression providers. More...
class  qb::compression::decompress_factory
 Factory interface for creating decompression providers. More...
struct  qb::compression::deflate::to_compress
 Structure for passing compression parameters for deflate. More...
struct  qb::compression::deflate::to_uncompress
 Structure for passing decompression parameters for deflate. More...
struct  qb::compression::gzip::to_compress
 Structure for passing gzip compression parameters. More...
struct  qb::compression::gzip::to_uncompress
 Structure for passing gzip decompression parameters. More...

Namespaces

namespace  builtin
 Namespace containing built-in compression implementations.
namespace  algorithm
 Namespace containing algorithm constants and utilities.
namespace  deflate
 Namespace containing deflate compression utilities.
namespace  gzip
 Namespace containing gzip compression utilities.
namespace  qb::gzip
 Namespace alias for gzip compression utilities.
namespace  qb::deflate
 Namespace alias for deflate compression utilities.

Enumerations

enum  qb::compression::operation_hint { qb::compression::is_last , qb::compression::has_more }
 Hints for compression/decompression operations. More...

Functions

bool qb::compression::builtin::supported ()
 Check if compression support is available.
bool qb::compression::builtin::algorithm::supported (const std::string &algorithm)
 Check if a specific compression algorithm is supported.
std::unique_ptr< compress_providerqb::compression::builtin::make_compressor (const std::string &algorithm)
 Create a compressor for the specified algorithm.
std::unique_ptr< decompress_providerqb::compression::builtin::make_decompressor (const std::string &algorithm)
 Create a decompressor for the specified algorithm.
const std::vector< std::shared_ptr< compress_factory > > qb::compression::builtin::get_compress_factories ()
 Get all available compression factories.
std::shared_ptr< compress_factoryqb::compression::builtin::get_compress_factory (const std::string &algorithm)
 Get a specific compression factory by algorithm name.
const std::vector< std::shared_ptr< decompress_factory > > qb::compression::builtin::get_decompress_factories ()
 Get all available decompression factories.
std::shared_ptr< decompress_factoryqb::compression::builtin::get_decompress_factory (const std::string &algorithm)
 Get a specific decompression factory by algorithm name.
std::unique_ptr< compress_providerqb::compression::builtin::make_gzip_compressor (int compressionLevel, int method, int strategy, int memLevel)
 Create a gzip compressor with custom parameters.
std::unique_ptr< compress_providerqb::compression::builtin::make_deflate_compressor (int compressionLevel, int method, int strategy, int memLevel)
 Create a deflate compressor with custom parameters.
std::shared_ptr< compress_factoryqb::compression::make_compress_factory (const std::string &algorithm, std::function< std::unique_ptr< compress_provider >()> make_compressor)
 Create a custom compression factory.
std::shared_ptr< decompress_factoryqb::compression::make_decompress_factory (const std::string &algorithm, uint16_t weight, std::function< std::unique_ptr< decompress_provider >()> make_decompressor)
 Create a custom decompression factory.
template<typename Output>
size_t qb::compression::compress (Output &output, const char *data, std::size_t size, int level, int window_bits)
 Compress data using a generic output container.
template<>
size_t qb::compression::compress (qb::allocator::pipe< char > &output, const char *data, std::size_t size, int level, int window_bits)
 Specialization of compress for pipe allocator.
template<typename Output>
std::size_t qb::compression::uncompress (Output &output, const char *data, std::size_t size, std::size_t max, int window_bits)
 Uncompress data using a generic output container.
template<>
size_t qb::compression::uncompress (qb::allocator::pipe< char > &output, const char *data, std::size_t size, std::size_t max, int window_bits)
 Specialization of uncompress for pipe allocator.
template<typename Output>
size_t qb::compression::deflate::compress (Output &output, const char *data, std::size_t size, int level=Z_DEFAULT_COMPRESSION)
 Compress data using deflate algorithm with a generic output container.
template<>
size_t qb::compression::deflate::compress (qb::allocator::pipe< char > &output, const char *data, std::size_t size, int level)
 Specialization of deflate compress for pipe allocator.
template<typename Output>
Output & qb::compression::deflate::compress (Output &output, to_compress &info)
 Compress data using the parameters in a to_compress structure.
std::string qb::compression::deflate::compress (const char *data, std::size_t size, int level=Z_DEFAULT_COMPRESSION)
 Compress data to a string.
template<typename Output>
std::size_t qb::compression::deflate::uncompress (Output &output, const char *data, std::size_t size, std::size_t max=0)
 Uncompress data using deflate algorithm with a generic output container.
template<>
size_t qb::compression::deflate::uncompress (qb::allocator::pipe< char > &output, const char *data, std::size_t size, std::size_t level)
 Specialization of deflate uncompress for pipe allocator.
template<typename Output>
Output & qb::compression::deflate::uncompress (Output &output, to_uncompress &info)
 Uncompress data using the parameters in a to_uncompress structure.
std::string qb::compression::deflate::uncompress (const char *data, std::size_t size)
 Uncompress data to a string.
bool qb::compression::gzip::is_compressed (const char *data, std::size_t size)
 Check if data is compressed using gzip or zlib format.
template<typename Output>
size_t qb::compression::gzip::compress (Output &output, const char *data, std::size_t size, int level=Z_DEFAULT_COMPRESSION)
 Compress data using gzip algorithm with a generic output container.
template<>
size_t qb::compression::gzip::compress (qb::allocator::pipe< char > &output, const char *data, std::size_t size, int level)
 Specialization of gzip compress for pipe allocator.
template<typename Output>
Output & qb::compression::gzip::compress (Output &output, to_compress &info)
 Compress data using the parameters in a to_compress structure.
std::string qb::compression::gzip::compress (const char *data, std::size_t size, int level=Z_DEFAULT_COMPRESSION)
 Compress data to a string using gzip.
template<typename Output>
std::size_t qb::compression::gzip::uncompress (Output &output, const char *data, std::size_t size, std::size_t max=0)
 Uncompress data using gzip algorithm with a generic output container.
template<>
size_t qb::compression::gzip::uncompress (qb::allocator::pipe< char > &output, const char *data, std::size_t size, std::size_t level)
 Specialization of gzip uncompress for pipe allocator.
template<typename Output>
Output & qb::compression::gzip::uncompress (Output &output, to_uncompress &info)
 Uncompress data using the parameters in a to_uncompress structure.
std::string qb::compression::gzip::uncompress (const char *data, std::size_t size)
 Uncompress gzip data to a string.

Variables

constexpr const char *const qb::compression::builtin::algorithm::GZIP = "gzip"
 Identifier for the gzip compression algorithm.
constexpr const char *const qb::compression::builtin::algorithm::DEFLATE = "deflate"
 Identifier for the deflate compression algorithm.

Detailed Description

Data compression utilities for the QB IO library.

This file provides data compression and decompression functionality using the zlib library. It includes support for both deflate and gzip algorithms, with comprehensive interfaces for encoding and decoding data streams.

The implementation includes templated functions for flexible buffer management, provider classes for algorithm abstraction, and utility functions for common compression operations.

Author
qb - C++ Actor Framework

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Function Documentation

◆ supported() [1/2]

bool qb::compression::builtin::supported ( )

Check if compression support is available.

Returns
true if compression is supported, false otherwise

◆ supported() [2/2]

bool qb::compression::builtin::algorithm::supported ( const std::string & algorithm)

Check if a specific compression algorithm is supported.

Parameters
algorithmName of the algorithm to check
Returns
true if the algorithm is supported, false otherwise

◆ make_compressor()

std::unique_ptr< compress_provider > qb::compression::builtin::make_compressor ( const std::string & algorithm)

Create a compressor for the specified algorithm.

Parameters
algorithmName of the compression algorithm
Returns
Unique pointer to a compressor provider, or nullptr if the algorithm is not supported

◆ make_decompressor()

std::unique_ptr< decompress_provider > qb::compression::builtin::make_decompressor ( const std::string & algorithm)

Create a decompressor for the specified algorithm.

Parameters
algorithmName of the decompression algorithm
Returns
Unique pointer to a decompressor provider, or nullptr if the algorithm is not supported

◆ get_compress_factories()

const std::vector< std::shared_ptr< compress_factory > > qb::compression::builtin::get_compress_factories ( )

Get all available compression factories.

Returns
Vector of shared pointers to compression factories

◆ get_compress_factory()

std::shared_ptr< compress_factory > qb::compression::builtin::get_compress_factory ( const std::string & algorithm)

Get a specific compression factory by algorithm name.

Parameters
algorithmName of the compression algorithm
Returns
Shared pointer to the compression factory, or nullptr if not found

◆ get_decompress_factories()

const std::vector< std::shared_ptr< decompress_factory > > qb::compression::builtin::get_decompress_factories ( )

Get all available decompression factories.

Returns
Vector of shared pointers to decompression factories

◆ get_decompress_factory()

std::shared_ptr< decompress_factory > qb::compression::builtin::get_decompress_factory ( const std::string & algorithm)

Get a specific decompression factory by algorithm name.

Parameters
algorithmName of the decompression algorithm
Returns
Shared pointer to the decompression factory, or nullptr if not found

◆ make_gzip_compressor()

std::unique_ptr< compress_provider > qb::compression::builtin::make_gzip_compressor ( int compressionLevel,
int method,
int strategy,
int memLevel )

Create a gzip compressor with custom parameters.

Parameters
compressionLevelCompression level (1-9, where 9 is max compression)
methodCompression method (usually Z_DEFLATED)
strategyCompression strategy (e.g., Z_DEFAULT_STRATEGY)
memLevelMemory usage level (1-9, where 9 uses most memory)
Returns
Unique pointer to a gzip compressor provider

◆ make_deflate_compressor()

std::unique_ptr< compress_provider > qb::compression::builtin::make_deflate_compressor ( int compressionLevel,
int method,
int strategy,
int memLevel )

Create a deflate compressor with custom parameters.

Parameters
compressionLevelCompression level (1-9, where 9 is max compression)
methodCompression method (usually Z_DEFLATED)
strategyCompression strategy (e.g., Z_DEFAULT_STRATEGY)
memLevelMemory usage level (1-9, where 9 uses most memory)
Returns
Unique pointer to a deflate compressor provider

◆ make_compress_factory()

std::shared_ptr< compress_factory > qb::compression::make_compress_factory ( const std::string & algorithm,
std::function< std::unique_ptr< compress_provider >()> make_compressor )

Create a custom compression factory.

Parameters
algorithmName of the compression algorithm
make_compressorFunction that creates compressor instances
Returns
Shared pointer to the created compression factory

◆ make_decompress_factory()

std::shared_ptr< decompress_factory > qb::compression::make_decompress_factory ( const std::string & algorithm,
uint16_t weight,
std::function< std::unique_ptr< decompress_provider >()> make_decompressor )

Create a custom decompression factory.

Parameters
algorithmName of the decompression algorithm
weightPriority weight for the algorithm
make_decompressorFunction that creates decompressor instances
Returns
Shared pointer to the created decompression factory

◆ compress() [1/10]

template<typename Output>
size_t qb::compression::compress ( Output & output,
const char * data,
std::size_t size,
int level,
int window_bits )

Compress data using a generic output container.

This template function compresses the provided data using the zlib library and stores the result in an output container that supports resize() and size() operations.

Template Parameters
OutputType of the output container
Parameters
outputContainer for the compressed data (will be resized as needed)
dataPointer to the data to compress
sizeSize of the data in bytes
levelCompression level (1-9, where 9 is max compression)
window_bitsWindow size bits with encoding format flag
Returns
Size of the compressed data in bytes
Exceptions
std::runtime_errorIf initialization fails or input is too large

◆ compress() [2/10]

template<>
size_t qb::compression::compress ( qb::allocator::pipe< char > & output,
const char * data,
std::size_t size,
int level,
int window_bits )

Specialization of compress for pipe allocator.

Parameters
outputPipe allocator for the compressed data
dataPointer to the data to compress
sizeSize of the data in bytes
levelCompression level (1-9, where 9 is max compression)
window_bitsWindow size bits with encoding format flag
Returns
Size of the compressed data in bytes

◆ uncompress() [1/10]

template<typename Output>
std::size_t qb::compression::uncompress ( Output & output,
const char * data,
std::size_t size,
std::size_t max,
int window_bits )

Uncompress data using a generic output container.

This template function uncompresses the provided data using the zlib library and stores the result in an output container that supports resize() and size() operations.

Template Parameters
OutputType of the output container
Parameters
outputContainer for the uncompressed data (will be resized as needed)
dataPointer to the compressed data
sizeSize of the compressed data in bytes
maxMaximum allowed output size (0 for unlimited)
window_bitsWindow size bits with encoding format flag
Returns
Size of the uncompressed data in bytes
Exceptions
std::runtime_errorIf initialization fails, input is too large, or max size exceeded

◆ uncompress() [2/10]

template<>
size_t qb::compression::uncompress ( qb::allocator::pipe< char > & output,
const char * data,
std::size_t size,
std::size_t max,
int window_bits )

Specialization of uncompress for pipe allocator.

Parameters
outputPipe allocator for the uncompressed data
dataPointer to the compressed data
sizeSize of the compressed data in bytes
maxMaximum allowed output size (0 for unlimited)
window_bitsWindow size bits with encoding format flag
Returns
Size of the uncompressed data in bytes

◆ compress() [3/10]

template<typename Output>
size_t qb::compression::deflate::compress ( Output & output,
const char * data,
std::size_t size,
int level = Z_DEFAULT_COMPRESSION )

Compress data using deflate algorithm with a generic output container.

Template Parameters
OutputType of the output container
Parameters
outputContainer for the compressed data (will be resized as needed)
dataPointer to the data to compress
sizeSize of the data in bytes
levelCompression level (1-9, where 9 is max compression)
Returns
Size of the compressed data in bytes

◆ compress() [4/10]

template<>
size_t qb::compression::deflate::compress ( qb::allocator::pipe< char > & output,
const char * data,
std::size_t size,
int level )

Specialization of deflate compress for pipe allocator.

Parameters
outputPipe allocator for the compressed data
dataPointer to the data to compress
sizeSize of the data in bytes
levelCompression level (1-9, where 9 is max compression)
Returns
Size of the compressed data in bytes

◆ compress() [5/10]

template<typename Output>
Output & qb::compression::deflate::compress ( Output & output,
to_compress & info )

Compress data using the parameters in a to_compress structure.

Template Parameters
OutputType of the output container
Parameters
outputContainer for the compressed data
infoStructure containing compression parameters and results
Returns
Reference to the output container

◆ compress() [6/10]

std::string qb::compression::deflate::compress ( const char * data,
std::size_t size,
int level = Z_DEFAULT_COMPRESSION )

Compress data to a string.

Parameters
dataPointer to the data to compress
sizeSize of the data in bytes
levelCompression level (1-9, where 9 is max compression)
Returns
String containing the compressed data

◆ uncompress() [3/10]

template<typename Output>
std::size_t qb::compression::deflate::uncompress ( Output & output,
const char * data,
std::size_t size,
std::size_t max = 0 )

Uncompress data using deflate algorithm with a generic output container.

Template Parameters
OutputType of the output container
Parameters
outputContainer for the uncompressed data
dataPointer to the compressed data
sizeSize of the compressed data in bytes
maxMaximum allowed output size (0 for unlimited)
Returns
Size of the uncompressed data in bytes

◆ uncompress() [4/10]

template<>
size_t qb::compression::deflate::uncompress ( qb::allocator::pipe< char > & output,
const char * data,
std::size_t size,
std::size_t level )

Specialization of deflate uncompress for pipe allocator.

Parameters
outputPipe allocator for the uncompressed data
dataPointer to the compressed data
sizeSize of the compressed data in bytes
levelMaximum allowed output size (0 for unlimited)
Returns
Size of the uncompressed data in bytes

◆ uncompress() [5/10]

template<typename Output>
Output & qb::compression::deflate::uncompress ( Output & output,
to_uncompress & info )

Uncompress data using the parameters in a to_uncompress structure.

Template Parameters
OutputType of the output container
Parameters
outputContainer for the uncompressed data
infoStructure containing decompression parameters and results
Returns
Reference to the output container

◆ uncompress() [6/10]

std::string qb::compression::deflate::uncompress ( const char * data,
std::size_t size )

Uncompress data to a string.

Parameters
dataPointer to the compressed data
sizeSize of the compressed data in bytes
Returns
String containing the uncompressed data

◆ compress() [7/10]

template<typename Output>
size_t qb::compression::gzip::compress ( Output & output,
const char * data,
std::size_t size,
int level = Z_DEFAULT_COMPRESSION )

Compress data using gzip algorithm with a generic output container.

Template Parameters
OutputType of the output container
Parameters
outputContainer for the compressed data (will be resized as needed)
dataPointer to the data to compress
sizeSize of the data in bytes
levelCompression level (1-9, where 9 is max compression)
Returns
Size of the compressed data in bytes

◆ compress() [8/10]

template<>
size_t qb::compression::gzip::compress ( qb::allocator::pipe< char > & output,
const char * data,
std::size_t size,
int level )

Specialization of gzip compress for pipe allocator.

Parameters
outputPipe allocator for the compressed data
dataPointer to the data to compress
sizeSize of the data in bytes
levelCompression level (1-9, where 9 is max compression)
Returns
Size of the compressed data in bytes

◆ compress() [9/10]

template<typename Output>
Output & qb::compression::gzip::compress ( Output & output,
to_compress & info )

Compress data using the parameters in a to_compress structure.

Template Parameters
OutputType of the output container
Parameters
outputContainer for the compressed data
infoStructure containing compression parameters and results
Returns
Reference to the output container

◆ compress() [10/10]

std::string qb::compression::gzip::compress ( const char * data,
std::size_t size,
int level = Z_DEFAULT_COMPRESSION )

Compress data to a string using gzip.

Parameters
dataPointer to the data to compress
sizeSize of the data in bytes
levelCompression level (1-9, where 9 is max compression)
Returns
String containing the compressed data

◆ uncompress() [7/10]

template<typename Output>
std::size_t qb::compression::gzip::uncompress ( Output & output,
const char * data,
std::size_t size,
std::size_t max = 0 )

Uncompress data using gzip algorithm with a generic output container.

Template Parameters
OutputType of the output container
Parameters
outputContainer for the uncompressed data
dataPointer to the compressed data
sizeSize of the compressed data in bytes
maxMaximum allowed output size (0 for unlimited)
Returns
Size of the uncompressed data in bytes

◆ uncompress() [8/10]

template<>
size_t qb::compression::gzip::uncompress ( qb::allocator::pipe< char > & output,
const char * data,
std::size_t size,
std::size_t level )

Specialization of gzip uncompress for pipe allocator.

Parameters
outputPipe allocator for the uncompressed data
dataPointer to the compressed data
sizeSize of the compressed data in bytes
levelMaximum allowed output size (0 for unlimited)
Returns
Size of the uncompressed data in bytes

◆ uncompress() [9/10]

template<typename Output>
Output & qb::compression::gzip::uncompress ( Output & output,
to_uncompress & info )

Uncompress data using the parameters in a to_uncompress structure.

Template Parameters
OutputType of the output container
Parameters
outputContainer for the uncompressed data
infoStructure containing decompression parameters and results
Returns
Reference to the output container

◆ uncompress() [10/10]

std::string qb::compression::gzip::uncompress ( const char * data,
std::size_t size )

Uncompress gzip data to a string.

Parameters
dataPointer to the compressed data
sizeSize of the compressed data in bytes
Returns
String containing the uncompressed data