|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
RAR 5.0 entropy front-end: bit reader, canonical Huffman, block tables. More...
#include <string.h>#include "ra8_attributes.h"#include "ra8_rar5.h"#include "ra8_rar5_internal.h"Go to the source code of this file.
Functions | |
| static uint8_t | internal_fetch_byte (ra8_rar5_state_t *st) |
| Pull the next packed byte, refilling the window from the backing reader. | |
| static void | internal_ensure (ra8_rar5_state_t *st, uint32_t n) |
Ensure at least n bits are buffered in the accumulator. | |
| static uint32_t | internal_peek (ra8_rar5_state_t *st, uint32_t n) |
Peek the next n bits without consuming them. | |
| static void | internal_drop (ra8_rar5_state_t *st, uint32_t n) |
Consume n previously-peeked bits. | |
| uint32_t | priv_rar5_get (ra8_rar5_state_t *st, uint32_t n) |
Implementation of priv_rar5_get() – internal_peek then internal_drop of n bits. | |
| static void | internal_align (ra8_rar5_state_t *st) |
| Discard bits up to the next byte boundary. | |
| static void | internal_tab_limits (ra8_rar5_dtab_t *d, const uint32_t *count) |
| Fill a decode table's per-length upper-limit and start-position arrays. | |
| static void | internal_make_tables (ra8_rar5_dtab_t *d, const uint8_t *lengths, uint16_t size) |
| Build a canonical Huffman decode table from a bit-length vector. | |
| uint32_t | priv_rar5_decode_num (ra8_rar5_state_t *st, const ra8_rar5_dtab_t *d) |
| Implementation of priv_rar5_decode_num() – limit-compare canonical decode. | |
| static uint8_t | internal_checksum (uint32_t flags, uint64_t blocksize) |
| Compute the RAR5 block-header checksum byte. | |
| ra8_err_t | priv_rar5_read_block_header (ra8_rar5_state_t *st, r5_block_t *b) |
| Implementation of priv_rar5_read_block_header() – align, flags, size, checksum. | |
| uint32_t | priv_rar5_fill_zeros (uint8_t *out, uint32_t start, uint32_t count, uint32_t max) |
| Implementation of priv_rar5_fill_zeros() – bounded zero-length append. | |
| static ra8_err_t | internal_read_bd_lengths (ra8_rar5_state_t *st, uint8_t *out) |
| Read the 20-entry bit-length pre-table (BD) length list. | |
| ra8_err_t | priv_rar5_apply_run (ra8_rar5_state_t *st, uint8_t *tbl, uint32_t *idx, uint32_t num) |
| Implementation of priv_rar5_apply_run() – copy-previous / zero run append. | |
| static ra8_err_t | internal_read_full_table (ra8_rar5_state_t *st, uint8_t *tbl) |
| Decode the combined length table for the four LZ alphabets. | |
| ra8_err_t | priv_rar5_read_tables (ra8_rar5_state_t *st) |
| Implementation of priv_rar5_read_tables() – BD pre-table then the four LZ tables. | |
RAR 5.0 entropy front-end: bit reader, canonical Huffman, block tables.
The lower half of the clean-room RAR 5.0 decompressor (paired with the LZ driver in ra8_rar5.c). Three layers, bottom-up:
The driver in ra8_rar5.c calls the four ::ra8_rar5_internal.h cross-TU entry points here; nothing in this unit calls back into the driver.
Definition in file ra8_rar5_tables.c.
|
static |
Discard bits up to the next byte boundary.
Drops the sub-byte residue so the following read starts on a whole packed byte, as each RAR5 block header requires.
| [in,out] | st | Decoder state (non-NULL). |
st is a bound decoder state. Definition at line 165 of file ra8_rar5_tables.c.
References internal_drop(), k_r5_low3_mask, and RA8_INTERNAL.
Referenced by priv_rar5_read_block_header().
|
static |
Compute the RAR5 block-header checksum byte.
XOR of the seed, the flags, and the three low bytes of the block size – the value the header stores for validation.
| [in] | flags | Block-flags byte. |
| [in] | blocksize | Decoded block size. |
| 0 | When the XOR of all inputs is zero. |
flags is the raw block-flags byte. blocksize is the decoded block size. Definition at line 277 of file ra8_rar5_tables.c.
References k_r5_byte_bits, k_r5_byte_mask, k_r5_hdr_chk_seed, and RA8_INTERNAL.
Referenced by priv_rar5_read_block_header().
|
static |
Consume n previously-peeked bits.
Reduces the buffered count and the running consumed-bit total, keeping the accumulator masked to its remaining valid bits.
| [in,out] | st | Decoder state (non-NULL). |
| [in] | n | Bits to consume (<= currently buffered). |
st is a bound decoder state. n <= st::nbits. n and st->consumed increased by n. st::acc retains only its remaining valid bits. Definition at line 134 of file ra8_rar5_tables.c.
References k_r5_acc_bits, and RA8_INTERNAL.
Referenced by internal_align(), priv_rar5_decode_num(), and priv_rar5_get().
|
static |
Ensure at least n bits are buffered in the accumulator.
Shifts whole bytes in from internal_fetch_byte until the buffered bit count reaches n; past-end fetches contribute zero bits.
| [in,out] | st | Decoder state (non-NULL). |
| [in] | n | Bits required (1..32). |
st is a bound decoder state. n <= 32. st::acc holds the buffered bits, oldest at the top. Definition at line 89 of file ra8_rar5_tables.c.
References internal_fetch_byte(), k_r5_byte_bits, and RA8_INTERNAL.
Referenced by internal_peek().
|
static |
Pull the next packed byte, refilling the window from the backing reader.
Serves bytes from the ra8_rar5_state_t::refill window, refetching a fresh window through the archive reader when it drains. Past the packed member end it returns 0 and latches ra8_rar5_state_t::overrun.
| [in,out] | st | Decoder state (non-NULL). |
| 0 | The packed member is exhausted (overrun latched). |
st was bound with a valid archive reader. st::base + st::packlen is within the archive. st::refill_pos advanced by one. st::overrun is true. Definition at line 50 of file ra8_rar5_tables.c.
References k_ra8_rar5_refill_bytes, and RA8_INTERNAL.
Referenced by internal_ensure().
|
static |
Build a canonical Huffman decode table from a bit-length vector.
Counts codes per length, computes the prefix limits via internal_tab_limits, then assigns each non-zero-length symbol to its canonical code slot in symbol order. Zero-length symbols are absent from the table.
| [out] | d | Table to populate (non-NULL). |
| [in] | lengths | Per-symbol bit lengths (size entries). |
| [in] | size | Alphabet size (<= k_ra8_rar5_nc). |
lengths holds size readable bytes. size <= k_ra8_rar5_nc. Definition at line 217 of file ra8_rar5_tables.c.
References internal_tab_limits(), k_r5_maxbits, k_r5_nibble_mask, ra8_rar5_dtab_t::max, memset(), ra8_rar5_dtab_t::num, and ra8_rar5_dtab_t::pos.
Referenced by priv_rar5_read_tables().
|
static |
Peek the next n bits without consuming them.
Ensures the bits are buffered, then returns the top n bits of the accumulator as a right-aligned value.
| [in,out] | st | Decoder state (non-NULL). |
| [in] | n | Bits to read (1..32). |
n bits, oldest bit most significant. | 0 | When the next n bits are all zero (or all padding). |
st is a bound decoder state. n <= 32. st::consumed is unchanged (a pure peek). n bits remain buffered. Definition at line 113 of file ra8_rar5_tables.c.
References internal_ensure(), and RA8_INTERNAL.
Referenced by priv_rar5_decode_num(), and priv_rar5_get().
|
static |
Read the 20-entry bit-length pre-table (BD) length list.
Each entry is a 4-bit length; the value 15 escapes to a following 4-bit field that is either a literal length-15 code (0) or a zero run (n+2).
| [in,out] | st | Decoder state (non-NULL). |
| [out] | out | Receives k_ra8_rar5_bc bit lengths (non-NULL). |
| k_ra8_ok | The 20 lengths were read. |
st is a bound decoder state. out holds k_ra8_rar5_bc writable bytes. Definition at line 335 of file ra8_rar5_tables.c.
References k_r5_len_escape, k_r5_zeros_extra, k_ra8_ok, k_ra8_rar5_bc, priv_rar5_fill_zeros(), priv_rar5_get(), and RA8_INTERNAL.
Referenced by priv_rar5_read_tables().
|
static |
Decode the combined length table for the four LZ alphabets.
Reads k_ra8_rar5_huff_total bit lengths through the BD table: codes 0-15 are literal lengths, 16-19 are copy-previous / zero runs.
| [in,out] | st | Decoder state (non-NULL, ra8_rar5_state_t::bd built). |
| [out] | tbl | Receives the combined length table (non-NULL). |
| k_ra8_ok | The full table was decoded. |
| k_ra8_err_validation_failed | A run code without a previous entry. |
st::bd was built by internal_make_tables. tbl holds k_ra8_rar5_huff_total writable bytes. Definition at line 400 of file ra8_rar5_tables.c.
References k_ra8_ok, k_ra8_rar5_huff_total, priv_rar5_apply_run(), priv_rar5_decode_num(), and RA8_INTERNAL.
Referenced by priv_rar5_read_tables().
|
static |
Fill a decode table's per-length upper-limit and start-position arrays.
Turns a per-bit-length code count into the left-aligned upper-limit code (ra8_rar5_dtab_t::len) and the first-symbol index (ra8_rar5_dtab_t::pos) for each length, the canonical-Huffman prefix.
| [out] | d | Table to populate (non-NULL). |
| [in] | count | Per-length code counts (index 1..15 used). |
count has at least 16 entries with count[0] == 0. d is writable. Definition at line 187 of file ra8_rar5_tables.c.
References k_r5_bf_bits, k_r5_maxbits, ra8_rar5_dtab_t::len, ra8_rar5_dtab_t::pos, and RA8_INTERNAL.
Referenced by internal_make_tables().
| ra8_err_t priv_rar5_apply_run | ( | ra8_rar5_state_t * | st, |
| uint8_t * | tbl, | ||
| uint32_t * | idx, | ||
| uint32_t | num ) |
Implementation of priv_rar5_apply_run() – copy-previous / zero run append.
Append one run (copy-previous or zero) to the length table.
Definition at line 357 of file ra8_rar5_tables.c.
References k_r5_run_long_add, k_r5_run_long_bits, k_r5_tbl_copy_long, k_r5_tbl_zero_long, k_r5_tbl_zero_short, k_ra8_err_validation_failed, k_ra8_ok, k_ra8_rar5_huff_total, priv_rar5_get(), and RA8_PRIV.
Referenced by internal_read_full_table().
| uint32_t priv_rar5_decode_num | ( | ra8_rar5_state_t * | st, |
| const ra8_rar5_dtab_t * | d ) |
Implementation of priv_rar5_decode_num() – limit-compare canonical decode.
Decode one Huffman symbol from d, consuming its code bits.
Definition at line 241 of file ra8_rar5_tables.c.
References internal_drop(), internal_peek(), k_r5_bf_bits, k_r5_bf_mask, k_r5_maxbits, ra8_rar5_dtab_t::len, ra8_rar5_dtab_t::max, ra8_rar5_dtab_t::num, ra8_rar5_dtab_t::pos, and RA8_PRIV.
Referenced by internal_decode_distance(), internal_decode_token(), internal_do_repdist(), and internal_read_full_table().
| uint32_t priv_rar5_fill_zeros | ( | uint8_t * | out, |
| uint32_t | start, | ||
| uint32_t | count, | ||
| uint32_t | max ) |
Implementation of priv_rar5_fill_zeros() – bounded zero-length append.
Extend out with count zero bit-lengths, bounded by max.
Definition at line 310 of file ra8_rar5_tables.c.
References RA8_PRIV.
Referenced by internal_read_bd_lengths().
| uint32_t priv_rar5_get | ( | ra8_rar5_state_t * | st, |
| uint32_t | n ) |
Implementation of priv_rar5_get() – internal_peek then internal_drop of n bits.
Peek and consume n bits from the streaming reader in one step.
Definition at line 145 of file ra8_rar5_tables.c.
References internal_drop(), internal_peek(), and RA8_PRIV.
Referenced by internal_decode_distance(), internal_read_bd_lengths(), internal_read_filter(), internal_read_filter_data(), internal_slot_to_length(), priv_rar5_apply_run(), and priv_rar5_read_block_header().
| ra8_err_t priv_rar5_read_block_header | ( | ra8_rar5_state_t * | st, |
| r5_block_t * | b ) |
Implementation of priv_rar5_read_block_header() – align, flags, size, checksum.
Read and validate one RAR5 block header at the current bit position.
Definition at line 287 of file ra8_rar5_tables.c.
References internal_align(), internal_checksum(), k_r5_bf_bcount_mask, k_r5_bf_bcount_shift, k_r5_bf_bitsize_mask, k_r5_bf_last, k_r5_bf_tables, k_r5_byte_bits, k_ra8_err_validation_failed, k_ra8_ok, priv_rar5_get(), and RA8_PRIV.
Referenced by internal_open_block().
| ra8_err_t priv_rar5_read_tables | ( | ra8_rar5_state_t * | st | ) |
Implementation of priv_rar5_read_tables() – BD pre-table then the four LZ tables.
Parse a table block: build the BD pre-table then the four LZ tables.
Definition at line 419 of file ra8_rar5_tables.c.
References internal_make_tables(), internal_read_bd_lengths(), internal_read_full_table(), k_ra8_ok, k_ra8_rar5_bc, k_ra8_rar5_dc, k_ra8_rar5_huff_total, k_ra8_rar5_ldc, k_ra8_rar5_nc, k_ra8_rar5_rc, and RA8_PRIV.
Referenced by internal_open_block().