RPNX::Compress
Self-contained C++20 compression and ZIP library
 
Loading...
Searching...
No Matches
rpnx::compression::lz4_codec Namespace Reference

Internal implementation of LZ4 frame compression and decompression. More...

Classes

class  xxhash32_accumulator
 Incremental XXH32 accumulator for streamed LZ4 content checksums. More...
 

Functions

std::uint32_t rotate_left (std::uint32_t value, std::uint8_t count) noexcept
 Rotates a 32-bit value left without compiler-specific intrinsics.
 
std::uint32_t read_word (std::span< std::byte const > input, std::size_t offset) noexcept
 Reads an unchecked little-endian word from a known-valid range.
 
std::uint32_t xxhash32 (std::span< std::byte const > input, std::uint32_t seed=0U) noexcept
 Computes XXH32 as required by the LZ4 frame format.
 
void append_extended_length (std::vector< std::byte > &output, std::size_t length)
 Encodes an extended LZ4 literal or match length.
 
std::vector< std::byte > compress_block (std::span< std::byte const > input, std::int32_t level)
 Compresses one independent LZ4 block.
 
std::size_t read_extended_length (std::span< std::byte const > input, std::size_t &position, std::size_t initial)
 Reads an LZ4 extended length while checking block bounds.
 
template<std::input_iterator input_iterator, std::sentinel_for< input_iterator > sentinel, typename output_iterator>
output_iterator compress (input_iterator first, sentinel last, output_iterator output, compression_options const &options)
 Compresses an iterator range as an LZ4 frame.
 
template<std::input_iterator input_iterator, std::sentinel_for< input_iterator > sentinel, typename output_iterator>
output_iterator decompress (input_iterator first, sentinel last, output_iterator output, decompression_options const &options)
 Decompresses one or more LZ4 frames.
 

Detailed Description

Internal implementation of LZ4 frame compression and decompression.

Function Documentation

◆ append_extended_length()

void rpnx::compression::lz4_codec::append_extended_length ( std::vector< std::byte > & output,
std::size_t length )
inline

Encodes an extended LZ4 literal or match length.

Parameters
outputBlock buffer receiving extension bytes.
lengthAmount beyond the token's base length.

Definition at line 185 of file lz4.hpp.

Referenced by compress_block().

◆ compress()

template<std::input_iterator input_iterator, std::sentinel_for< input_iterator > sentinel, typename output_iterator>
output_iterator rpnx::compression::lz4_codec::compress ( input_iterator first,
sentinel last,
output_iterator output,
compression_options const & options )

Compresses an iterator range as an LZ4 frame.

Template Parameters
input_iteratorSingle-pass byte iterator.
sentinelSentinel for first.
output_iteratorDestination byte iterator.
Parameters
firstFirst source byte.
lastSentinel past the source.
outputDestination iterator.
optionsCompression level from 0 through 12.
Returns
Destination advanced past the frame end mark.

Definition at line 336 of file lz4.hpp.

References compress_block(), rpnx::compression::invalid_option, rpnx::compression::compression_options::level, rpnx::compression::lz4_frame, rpnx::compression::implementation::to_byte(), rpnx::compression::implementation::write_byte(), and xxhash32().

◆ compress_block()

std::vector< std::byte > rpnx::compression::lz4_codec::compress_block ( std::span< std::byte const > input,
std::int32_t level )
inlinenodiscard

Compresses one independent LZ4 block.

Parameters
inputUncompressed block containing at most 64 KiB.
levelSearch level from 0 through 12.
Returns
Encoded block without an LZ4 frame header.

Definition at line 201 of file lz4.hpp.

References append_extended_length(), and read_word().

Referenced by compress().

◆ decompress()

template<std::input_iterator input_iterator, std::sentinel_for< input_iterator > sentinel, typename output_iterator>
output_iterator rpnx::compression::lz4_codec::decompress ( input_iterator first,
sentinel last,
output_iterator output,
decompression_options const & options )

Decompresses one or more LZ4 frames.

Template Parameters
input_iteratorSingle-pass byte iterator.
sentinelSentinel for first.
output_iteratorDestination byte iterator.
Parameters
firstFirst compressed byte.
lastSentinel past the compressed input.
outputDestination iterator.
optionsOutput limit and concatenated-frame policy.
Returns
Destination advanced past the uncompressed data.

Definition at line 401 of file lz4.hpp.

References rpnx::compression::decompression_options::allow_concatenated_streams, rpnx::compression::implementation::byte_reader< input_iterator, sentinel >::empty(), rpnx::compression::invalid_data, rpnx::compression::lz4_frame, rpnx::compression::decompression_options::maximum_output_size, rpnx::compression::output_limit_exceeded, rpnx::compression::implementation::byte_reader< input_iterator, sentinel >::read(), read_extended_length(), rpnx::compression::trailing_data, rpnx::compression::unsupported_feature, rpnx::compression::lz4_codec::xxhash32_accumulator::update(), rpnx::compression::lz4_codec::xxhash32_accumulator::value(), rpnx::compression::implementation::write_byte(), and xxhash32().

◆ read_extended_length()

std::size_t rpnx::compression::lz4_codec::read_extended_length ( std::span< std::byte const > input,
std::size_t & position,
std::size_t initial )
inlinenodiscard

Reads an LZ4 extended length while checking block bounds.

Parameters
inputEncoded block.
positionCurrent byte position, advanced past extension bytes.
initialLength nibble from the token.
Returns
Complete literal or match length.
Exceptions
compression_errorIf the extension is truncated or overflows size_t.

Definition at line 298 of file lz4.hpp.

References rpnx::compression::invalid_data, and rpnx::compression::lz4_frame.

Referenced by decompress().

◆ read_word()

std::uint32_t rpnx::compression::lz4_codec::read_word ( std::span< std::byte const > input,
std::size_t offset )
inlinenodiscardnoexcept

Reads an unchecked little-endian word from a known-valid range.

Parameters
inputByte range containing at least four bytes at offset.
offsetPosition of the first byte.
Returns
Decoded 32-bit word.

Definition at line 43 of file lz4.hpp.

Referenced by compress_block(), rpnx::compression::lz4_codec::xxhash32_accumulator::update(), rpnx::compression::lz4_codec::xxhash32_accumulator::value(), and xxhash32().

◆ rotate_left()

std::uint32_t rpnx::compression::lz4_codec::rotate_left ( std::uint32_t value,
std::uint8_t count )
inlinenodiscardnoexcept

Rotates a 32-bit value left without compiler-specific intrinsics.

Parameters
valueValue to rotate.
countRotation distance in the range 1–31.
Returns
Rotated value.

Definition at line 32 of file lz4.hpp.

Referenced by rpnx::compression::lz4_codec::xxhash32_accumulator::value(), and xxhash32().

◆ xxhash32()

std::uint32_t rpnx::compression::lz4_codec::xxhash32 ( std::span< std::byte const > input,
std::uint32_t seed = 0U )
inlinenodiscardnoexcept

Computes XXH32 as required by the LZ4 frame format.

Parameters
inputBytes to hash.
seedXXH32 seed.
Returns
Finalized 32-bit hash.

Definition at line 54 of file lz4.hpp.

References read_word(), and rotate_left().

Referenced by compress(), and decompress().