37typedef enum : uint32_t {
47typedef enum : uint8_t {
130 const uint64_t ratio = (uint64_t)lim->
max_ratio;
131 if (in_total > (UINT64_MAX / ratio)) {
134 const uint64_t product = in_total * ratio;
136 if (product > (UINT64_MAX - grace)) {
139 return product + grace;
146 if (limits ==
nullptr) {
161 if (out_delta > (UINT64_MAX - b->out_bytes)) {
162 b->out_bytes = UINT64_MAX;
164 b->out_bytes += out_delta;
166 b->in_bytes = in_total;
167 if (b->out_bytes > b->limits.max_output_bytes) {
179 if (b->entries >= b->limits.max_entries) {
189 if (b->iters >= b->limits.max_iterations) {
199 if (b->depth >= b->limits.max_depth) {
202 b->depth = (uint8_t)(b->depth + 1U);
211 if (b->depth == 0U) {
214 b->depth = (uint8_t)(b->depth - 1U);
245 return (uint16_t)((uint16_t)bytes[0] | ((uint16_t)bytes[1] << 8U));
276 const uint8_t* chunk,
279 uint64_t archive_size,
283 for (
size_t i = count; i > 0U; --i) {
284 const size_t at = i - 1U;
288 const uint64_t position = start + (uint64_t)at;
289 if (read(ctx, position, eocd,
sizeof(eocd)) !=
sizeof(eocd)) {
293 if ((position +
sizeof(eocd) + (uint64_t)comment) != archive_size) {
313 const uint64_t lower = (candidates > window) ? (candidates - window) : 0U;
314 uint64_t end = candidates;
316 while (end > lower) {
320 const size_t count = (size_t)(end - start);
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
static uint16_t internal_zip_u16(const uint8_t *bytes)
Decode one little-endian 16-bit ZIP field.
priv_zip_signature_t
Classic ZIP EOCD signature bytes.
@ k_priv_zip_sig_p
ASCII P.
@ k_priv_zip_sig_k
ASCII K.
@ k_priv_zip_sig_eocd
EOCD record identifier.
@ k_priv_zip_sig_fixed
EOCD fixed suffix.
ra8_err_t ra8_decomp_budget_charge_output(ra8_decomp_budget_t *b, uint64_t in_total, uint64_t out_delta)
Charge decompressed output against the cap and ratio bounds.
static bool internal_limits_usable(const ra8_decomp_limits_t *lim)
Whether every field of a policy is non-zero (usable as a bound).
ra8_decomp_limits_t ra8_decomp_limits_default(void)
The owner-approved default decompression policy.
ra8_err_t ra8_decomp_check_declared(const ra8_decomp_limits_t *limits, uint64_t comp_size, uint64_t out_size)
Header-level check of a member's declared sizes against a policy.
ra8_err_t ra8_decomp_zip_entry_preflight(ra8_decomp_read_fn read, void *ctx, uint64_t archive_size)
Reject an over-cap ZIP from its EOCD before directory allocation.
priv_zip_preflight_t
Fixed classic-ZIP EOCD geometry used by the entry-cap preflight.
@ k_priv_zip_eocd_bytes
Fixed EOCD bytes before its comment.
@ k_priv_zip_entries_offset
Total-entry field offset in EOCD.
@ k_priv_zip_scan_chunk
Candidate offsets inspected per read.
@ k_priv_zip_sig_bytes
Bytes in the EOCD signature.
@ k_priv_zip_comment_offset
Comment-length field offset in EOCD.
@ k_priv_zip_comment_max
Maximum classic ZIP comment bytes.
static const uint8_t s_zip_eocd_signature[k_priv_zip_sig_bytes]
Classic ZIP end-of-central-directory signature bytes.
ra8_err_t ra8_decomp_budget_enter(ra8_decomp_budget_t *b)
Enter one stacked decode layer (nesting-depth guard).
void ra8_decomp_budget_leave(ra8_decomp_budget_t *b)
Leave one stacked decode layer (balances ra8_decomp_budget_enter).
ra8_err_t ra8_decomp_budget_charge_entry(ra8_decomp_budget_t *b)
Charge one enumerated archive entry against the entry cap.
static const char *const s_tag_decomp
Log tag for decompression-policy diagnostics.
ra8_err_t ra8_decomp_budget_charge_iter(ra8_decomp_budget_t *b)
Charge one decode-loop turn against the iteration budget.
static uint64_t internal_ratio_bound(const ra8_decomp_limits_t *lim, uint64_t in_total)
The saturating ratio bound in * max_ratio + grace for a policy.
static bool internal_zip_scan_window_for_eocd(ra8_decomp_read_fn read, void *ctx, const uint8_t *chunk, uint64_t start, size_t count, uint64_t archive_size, ra8_err_t *out_status)
Search one loaded scan window backward for a verified ZIP EOCD record.
ra8_err_t ra8_decomp_budget_init(ra8_decomp_budget_t *b, const ra8_decomp_limits_t *limits)
Bind a budget to a policy (or the default policy) and zero it.
Unified decompression-limits policy: one bound set for every decoder.
size_t(* ra8_decomp_read_fn)(void *ctx, uint64_t offset, void *buf, size_t len)
Positioned reader used by bounded container preflights.
@ k_ra8_decomp_def_max_iters
Loop iteration budget (1 Mi).
@ k_ra8_decomp_def_ratio_grace
Additive ratio grace (64 KiB).
@ k_ra8_decomp_def_max_depth
Stacked decode-layer cap.
@ k_ra8_decomp_def_max_entries
Per-archive entry cap.
@ k_ra8_decomp_def_output_bytes
Per-unit output cap (64 MiB).
@ k_ra8_decomp_def_max_ratio
Output:input ratio bound.
@ k_ra8_err_decomp_ratio
Compression ratio bound breached (ra8_decomp_limits_t).
@ k_ra8_err_invalid_arg
Invalid function argument.
@ k_ra8_err_decomp_output_cap
Decompression output cap breached (ra8_decomp_limits_t).
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
@ k_ra8_err_decomp_iterations
Decode-loop iteration budget exhausted (ra8_decomp_limits_t).
@ k_ra8_err_decomp_depth
Container nesting-depth cap breached (ra8_decomp_limits_t).
@ k_ra8_err_decomp_entries
Archive entry-count cap breached (ra8_decomp_limits_t).
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
Running consumption tracker charged by a decoder against its policy.
One decompression policy: the five resource bounds decoders enforce.
uint32_t max_ratio
Output:input ratio bound (multiplier).
uint32_t ratio_grace_bytes
Additive output grace before ratio applies.
uint32_t max_iterations
Per-decode-loop iteration budget.
uint32_t max_entries
Per-archive enumerated-entry cap.
uint8_t max_depth
Stacked decode-layer (nesting) cap.
uint64_t max_output_bytes
Per-decode-unit output cap, bytes.