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

Internal implementation of xz and its LZMA2 payload format. More...

Classes

struct  block_record
 Size metadata collected while decoding one xz block. More...
 
class  crc64_accumulator
 Incremental CRC-64/XZ accumulator for streamed block data. More...
 
struct  lzma_model
 Adaptive probability tables and state used by LZMA. More...
 
class  range_decoder
 One-shot LZMA range decoder. More...
 
class  range_encoder
 One-shot LZMA range encoder used by the deterministic literal encoder. More...
 

Functions

std::uint32_t crc32 (std::span< std::byte const > input) noexcept
 Computes the reflected CRC-32 used by xz metadata and checks.
 
std::uint64_t read_little_endian (std::span< std::byte const > input, std::size_t &position, std::uint8_t byte_count)
 Reads a fixed-width little-endian integer.
 
std::uint64_t read_variable_integer (std::span< std::byte const > input, std::size_t &position)
 Decodes one minimal xz variable-length integer.
 
std::size_t check_size (std::uint8_t check_identifier)
 Returns the number of bytes in an xz integrity check.
 
void update_literal_state (std::uint8_t &state) noexcept
 Updates an LZMA state after decoding a literal.
 
std::uint32_t decode_length (range_decoder &decoder, lzma_model::length_model &model, std::size_t position_state)
 Decodes one LZMA match length.
 
void copy_match (std::vector< std::byte > &output, std::size_t history_begin, std::uint32_t dictionary_size, std::uint32_t distance, std::uint32_t length, std::size_t output_limit)
 Copies one validated LZMA match into the output dictionary.
 
void decode_lzma_chunk (std::span< std::byte const > encoded, std::size_t uncompressed_size, lzma_model &model, std::vector< std::byte > &output, std::size_t history_begin, std::uint32_t dictionary_size, std::size_t output_limit, std::size_t dictionary_position_offset=0U)
 Decodes one LZMA range-coded chunk into an LZMA2 dictionary.
 
std::vector< std::byte > compress_lzma_literals (std::span< std::byte const > input, lzma_model &model, std::size_t dictionary_position, std::uint8_t previous_byte)
 Encodes a literal-only LZMA range-coded chunk.
 
std::uint32_t decode_dictionary_size (std::uint8_t properties)
 Decodes the one-byte LZMA2 dictionary-size property.
 
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 xz stream containing LZMA2.
 
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 xz streams containing LZMA2.
 

Variables

constexpr std::uint32_t probability_total = 1U << 11U
 LZMA probability-model total.
 
constexpr std::uint32_t probability_move_bits = 5U
 Adaptation shift applied after every probability decision.
 
constexpr std::uint32_t range_top = 1U << 24U
 Range threshold below which the arithmetic coder normalizes.
 
constexpr std::size_t state_count = 12U
 Number of LZMA state-machine states.
 
constexpr std::size_t position_state_count = 16U
 Maximum number of position states.
 
constexpr std::size_t literal_coder_count = 16U
 Number of literal contexts retained by the supported properties.
 
constexpr std::size_t literal_coder_size = 0x300U
 Probability count in one LZMA literal coder.
 

Detailed Description

Internal implementation of xz and its LZMA2 payload format.

Function Documentation

◆ check_size()

std::size_t rpnx::compression::xz_codec::check_size ( std::uint8_t check_identifier)
inlinenodiscard

Returns the number of bytes in an xz integrity check.

Parameters
check_identifierFour-bit xz check identifier.
Returns
Encoded check size.

Definition at line 484 of file xz.hpp.

Referenced by decompress().

◆ compress()

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

Compresses an iterator range as an xz stream containing LZMA2.

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 9.
Returns
Destination advanced past the xz footer.

Definition at line 757 of file xz.hpp.

References compress_lzma_literals(), crc32(), rpnx::compression::invalid_option, rpnx::compression::compression_options::level, rpnx::compression::xz_codec::lzma_model::set_properties(), rpnx::compression::implementation::to_byte(), rpnx::compression::implementation::crc32_accumulator::update(), rpnx::compression::xz_codec::crc64_accumulator::update(), rpnx::compression::implementation::crc32_accumulator::value(), rpnx::compression::xz_codec::crc64_accumulator::value(), rpnx::compression::implementation::write_byte(), and rpnx::compression::xz.

◆ compress_lzma_literals()

std::vector< std::byte > rpnx::compression::xz_codec::compress_lzma_literals ( std::span< std::byte const > input,
lzma_model & model,
std::size_t dictionary_position,
std::uint8_t previous_byte )
inlinenodiscard

Encodes a literal-only LZMA range-coded chunk.

Parameters
inputSource literals.
modelAdaptive model updated in place.
dictionary_positionAbsolute dictionary position of the first literal.
previous_byteByte preceding the chunk, or zero at dictionary start.
Returns
Range-coded chunk payload.

Definition at line 701 of file xz.hpp.

References rpnx::compression::xz_codec::range_encoder::encode_bit(), rpnx::compression::xz_codec::range_encoder::finish(), rpnx::compression::xz_codec::lzma_model::is_match, rpnx::compression::xz_codec::lzma_model::literal, rpnx::compression::xz_codec::lzma_model::literal_context_bits, rpnx::compression::xz_codec::lzma_model::literal_position_bits, rpnx::compression::xz_codec::lzma_model::position_bits, rpnx::compression::xz_codec::lzma_model::state, and update_literal_state().

Referenced by compress().

◆ copy_match()

void rpnx::compression::xz_codec::copy_match ( std::vector< std::byte > & output,
std::size_t history_begin,
std::uint32_t dictionary_size,
std::uint32_t distance,
std::uint32_t length,
std::size_t output_limit )
inline

Copies one validated LZMA match into the output dictionary.

Parameters
outputStream output and dictionary storage.
history_beginStart of the current LZMA2 dictionary history.
dictionary_sizeDeclared maximum dictionary size.
distanceZero-based match distance.
lengthNumber of bytes to reproduce.
output_limitAbsolute output-size limit.

Definition at line 539 of file xz.hpp.

References rpnx::compression::invalid_data, rpnx::compression::output_limit_exceeded, and rpnx::compression::xz.

Referenced by decode_lzma_chunk().

◆ crc32()

std::uint32_t rpnx::compression::xz_codec::crc32 ( std::span< std::byte const > input)
inlinenodiscardnoexcept

Computes the reflected CRC-32 used by xz metadata and checks.

Parameters
inputBytes to checksum.
Returns
Finalized CRC-32.

Definition at line 384 of file xz.hpp.

Referenced by compress(), and decompress().

◆ decode_dictionary_size()

std::uint32_t rpnx::compression::xz_codec::decode_dictionary_size ( std::uint8_t properties)
inlinenodiscard

Decodes the one-byte LZMA2 dictionary-size property.

Parameters
propertiesEncoded property from the xz block filter flags.
Returns
Dictionary size in bytes.

Definition at line 730 of file xz.hpp.

References rpnx::compression::unsupported_feature, and rpnx::compression::xz.

Referenced by decompress().

◆ decode_length()

std::uint32_t rpnx::compression::xz_codec::decode_length ( range_decoder & decoder,
lzma_model::length_model & model,
std::size_t position_state )
inlinenodiscard

Decodes one LZMA match length.

Parameters
decoderRange decoder.
modelMatch or repeated-match length model updated in place.
position_stateCurrent low dictionary-position state.
Returns
Match length of at least two bytes.

Definition at line 517 of file xz.hpp.

References rpnx::compression::xz_codec::lzma_model::length_model::choice, rpnx::compression::xz_codec::lzma_model::length_model::choice2, rpnx::compression::xz_codec::range_decoder::decode_bit(), rpnx::compression::xz_codec::range_decoder::decode_tree(), rpnx::compression::xz_codec::lzma_model::length_model::high, rpnx::compression::xz_codec::lzma_model::length_model::low, and rpnx::compression::xz_codec::lzma_model::length_model::mid.

Referenced by decode_lzma_chunk().

◆ decode_lzma_chunk()

void rpnx::compression::xz_codec::decode_lzma_chunk ( std::span< std::byte const > encoded,
std::size_t uncompressed_size,
lzma_model & model,
std::vector< std::byte > & output,
std::size_t history_begin,
std::uint32_t dictionary_size,
std::size_t output_limit,
std::size_t dictionary_position_offset = 0U )
inline

Decodes one LZMA range-coded chunk into an LZMA2 dictionary.

Parameters
encodedComplete range-coded chunk.
uncompressed_sizeExact number of bytes the chunk must produce.
modelAdaptive model retained across eligible chunks.
outputStream output and dictionary storage.
history_beginStart of the current LZMA2 dictionary history.
dictionary_sizeDeclared maximum dictionary size.
output_limitAbsolute output-size limit.
dictionary_position_offsetPosition adjustment for chunk-local buffers.

Definition at line 567 of file xz.hpp.

References copy_match(), rpnx::compression::xz_codec::range_decoder::decode_bit(), rpnx::compression::xz_codec::range_decoder::decode_direct(), decode_length(), rpnx::compression::xz_codec::range_decoder::decode_reverse_tree(), rpnx::compression::xz_codec::range_decoder::decode_tree(), rpnx::compression::xz_codec::lzma_model::distance_align, rpnx::compression::xz_codec::lzma_model::distance_slot, rpnx::compression::xz_codec::lzma_model::distance_special, rpnx::compression::xz_codec::range_decoder::finish(), rpnx::compression::invalid_data, rpnx::compression::xz_codec::lzma_model::is_match, rpnx::compression::xz_codec::lzma_model::is_rep, rpnx::compression::xz_codec::lzma_model::is_rep0, rpnx::compression::xz_codec::lzma_model::is_rep0_long, rpnx::compression::xz_codec::lzma_model::is_rep1, rpnx::compression::xz_codec::lzma_model::is_rep2, rpnx::compression::xz_codec::lzma_model::literal, rpnx::compression::xz_codec::lzma_model::literal_context_bits, rpnx::compression::xz_codec::lzma_model::literal_position_bits, rpnx::compression::xz_codec::lzma_model::match_length, rpnx::compression::output_limit_exceeded, rpnx::compression::xz_codec::lzma_model::position_bits, rpnx::compression::xz_codec::lzma_model::rep0, rpnx::compression::xz_codec::lzma_model::rep1, rpnx::compression::xz_codec::lzma_model::rep2, rpnx::compression::xz_codec::lzma_model::rep3, rpnx::compression::xz_codec::lzma_model::repeated_length, rpnx::compression::xz_codec::lzma_model::state, update_literal_state(), and rpnx::compression::xz.

Referenced by decompress().

◆ decompress()

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

Decompresses one or more xz streams containing LZMA2.

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-stream policy.
Returns
Destination advanced past the uncompressed data.

Definition at line 925 of file xz.hpp.

References rpnx::compression::decompression_options::allow_concatenated_streams, check_size(), crc32(), decode_dictionary_size(), decode_lzma_chunk(), rpnx::compression::implementation::byte_reader< input_iterator, sentinel >::empty(), rpnx::compression::invalid_data, rpnx::compression::decompression_options::maximum_output_size, rpnx::compression::output_limit_exceeded, rpnx::compression::implementation::byte_reader< input_iterator, sentinel >::peek(), rpnx::compression::implementation::byte_reader< input_iterator, sentinel >::read(), read_little_endian(), read_variable_integer(), rpnx::compression::xz_codec::lzma_model::reset(), rpnx::compression::xz_codec::lzma_model::set_properties(), rpnx::compression::trailing_data, rpnx::compression::unsupported_feature, rpnx::compression::implementation::crc32_accumulator::update(), rpnx::compression::xz_codec::crc64_accumulator::update(), rpnx::compression::implementation::crc32_accumulator::value(), rpnx::compression::xz_codec::crc64_accumulator::value(), rpnx::compression::implementation::write_byte(), and rpnx::compression::xz.

◆ read_little_endian()

std::uint64_t rpnx::compression::xz_codec::read_little_endian ( std::span< std::byte const > input,
std::size_t & position,
std::uint8_t byte_count )
inlinenodiscard

Reads a fixed-width little-endian integer.

Parameters
inputSource bytes.
positionCurrent position, advanced by byte_count.
byte_countWidth from zero through eight bytes.
Returns
Decoded value.

Definition at line 435 of file xz.hpp.

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

Referenced by decompress().

◆ read_variable_integer()

std::uint64_t rpnx::compression::xz_codec::read_variable_integer ( std::span< std::byte const > input,
std::size_t & position )
inlinenodiscard

Decodes one minimal xz variable-length integer.

Parameters
inputSource bytes.
positionCurrent position, advanced past the encoded integer.
Returns
Decoded unsigned value.
Exceptions
compression_errorIf the integer is truncated, non-minimal, or oversized.

Definition at line 456 of file xz.hpp.

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

Referenced by decompress().

◆ update_literal_state()

void rpnx::compression::xz_codec::update_literal_state ( std::uint8_t & state)
inlinenoexcept

Updates an LZMA state after decoding a literal.

Parameters
stateState-machine value updated in place.

Definition at line 494 of file xz.hpp.

Referenced by compress_lzma_literals(), and decode_lzma_chunk().

Variable Documentation

◆ literal_coder_count

std::size_t rpnx::compression::xz_codec::literal_coder_count = 16U
inlineconstexpr

Number of literal contexts retained by the supported properties.

Definition at line 39 of file xz.hpp.

◆ literal_coder_size

std::size_t rpnx::compression::xz_codec::literal_coder_size = 0x300U
inlineconstexpr

Probability count in one LZMA literal coder.

Definition at line 41 of file xz.hpp.

◆ position_state_count

std::size_t rpnx::compression::xz_codec::position_state_count = 16U
inlineconstexpr

Maximum number of position states.

Definition at line 37 of file xz.hpp.

◆ probability_move_bits

std::uint32_t rpnx::compression::xz_codec::probability_move_bits = 5U
inlineconstexpr

Adaptation shift applied after every probability decision.

Definition at line 31 of file xz.hpp.

Referenced by rpnx::compression::xz_codec::range_decoder::decode_bit(), and rpnx::compression::xz_codec::range_encoder::encode_bit().

◆ probability_total

std::uint32_t rpnx::compression::xz_codec::probability_total = 1U << 11U
inlineconstexpr

LZMA probability-model total.

Definition at line 29 of file xz.hpp.

Referenced by rpnx::compression::xz_codec::range_decoder::decode_bit(), and rpnx::compression::xz_codec::range_encoder::encode_bit().

◆ range_top

std::uint32_t rpnx::compression::xz_codec::range_top = 1U << 24U
inlineconstexpr

Range threshold below which the arithmetic coder normalizes.

Definition at line 33 of file xz.hpp.

Referenced by rpnx::compression::xz_codec::range_encoder::encode_bit().

◆ state_count

std::size_t rpnx::compression::xz_codec::state_count = 12U
inlineconstexpr

Number of LZMA state-machine states.

Definition at line 35 of file xz.hpp.