ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
book_internal.h File Reference

book DOM-walk helpers shared across the library's translation units. More...

#include <stddef.h>
#include <stdint.h>
#include "ra8_attributes.h"
#include "ra8_err.h"
Include dependency graph for book_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  book_container_field_off_t : uint8_t {
  k_book_cont_off_chunk_bytes = 4U ,
  k_book_cont_off_total = 8U ,
  k_book_cont_off_count = 16U ,
  k_book_cont_off_reserved = 20U
}
 Byte offsets of the fixed "RBKC" container-header fields. More...
enum  book_xhtml_bound_t : uint32_t {
  k_book_xhtml_stack = 512U ,
  k_book_xhtml_iter_x = 4U
}
 Bounds for the iterative, recursion-free DOM walks (resident + paged). More...

Functions

ra8_err_t priv_book_container_header_fields (const uint8_t *hdr, uint32_t *out_chunk_bytes, uint64_t *out_total, uint32_t *out_count)
 Parse + validate the fixed 24-byte "RBKC" container header.
uint64_t priv_book_container_table_entry (const uint8_t *table, uint32_t idx)
 Decode one uint64 LE chunk-table entry from unaligned container bytes.
uint32_t priv_book_crc32_extend (uint32_t crc, const uint8_t *data, size_t len)
 Extend a finalized CRC-32/ISO-HDLC with another byte span.
bool priv_book_is_block (const char *name)
 Test whether an element name is a block-level HTML element.
bool priv_book_emit_text (char *out, size_t cap, size_t *pos, const char *str, bool *at_break)
 Append a whitespace-collapsed text run to the output buffer.
bool priv_book_emit_break (char *out, size_t cap, size_t *pos, bool *at_break)
 Append a paragraph break, collapsing consecutive block-level breaks.

Detailed Description

book DOM-walk helpers shared across the library's translation units.

Module-private seam between book_xhtml.c (resident XHTML serialiser + text extractor) and book_paged.c (the #163 paged text extractor). The plain-text leaf helpers (whitespace collapse, paragraph break, block-element test) and the shared walk-bound constants are declared here so the paged walk can reuse them verbatim – guaranteeing paged output is byte-identical to the resident walk – without duplicating the logic. Also the seam between book.c (resident container open) and book_chunked.c (demand-paged chunk reader): both parse the "RBKC" container header through one helper so the format has a single in-firmware definition. Not part of the public book API; consumers use book.h / book_paged.h / book_chunked.h.

Since
Version 0.1.0

Definition in file book_internal.h.

Enumeration Type Documentation

◆ book_container_field_off_t

Byte offsets of the fixed "RBKC" container-header fields.

Mirrors the layout documented on book_container_t; shared by the resident open (book.c) and the demand-paged chunk reader (book_chunked.c) so both parse one definition of the format.

Since
Version 0.1.0
Enumerator
k_book_cont_off_chunk_bytes 

uint32 LE: inflated bytes per chunk.

k_book_cont_off_total 

uint64 LE: flat-blob inflated total.

k_book_cont_off_count 

uint32 LE: number of chunks.

k_book_cont_off_reserved 

uint32 LE: reserved, must be 0.

Definition at line 38 of file book_internal.h.

◆ book_xhtml_bound_t

enum book_xhtml_bound_t : uint32_t

Bounds for the iterative, recursion-free DOM walks (resident + paged).

Since
Version 0.1.0
Enumerator
k_book_xhtml_stack 

Max pending open/close walk entries.

k_book_xhtml_iter_x 

Iteration-guard multiplier over node_count.

Definition at line 136 of file book_internal.h.

Function Documentation

◆ priv_book_container_header_fields()

ra8_err_t priv_book_container_header_fields ( const uint8_t * hdr,
uint32_t * out_chunk_bytes,
uint64_t * out_total,
uint32_t * out_count )

Parse + validate the fixed 24-byte "RBKC" container header.

Checks the magic, requires a non-zero chunk size and inflated total, requires the reserved word to be zero, and requires the stored chunk count to equal ceil(inflated_total / chunk_bytes). Field decoding is memcpy-based so the header buffer needs no alignment. Bounds against the file length are each caller's job (the resident open and the chunk reader own different views of the file).

Parameters
[in]hdrFirst k_book_container_header_len bytes of the container file.
[out]out_chunk_bytesReceives the inflated bytes-per-chunk.
[out]out_totalReceives the flat-blob inflated total.
[out]out_countReceives the chunk count.
Returns
ra8_err_t Error code.
Return values
k_ra8_okHeader well-formed; outputs populated.
k_ra8_err_null_ptrA pointer argument was NULL.
k_ra8_err_invalid_argBad magic, zero chunk size or total, non-zero reserved word, or a chunk count that disagrees with ceil(total / chunk_bytes).
Precondition
hdr holds at least k_book_container_header_len readable bytes.
All three output pointers are distinct, writable locations.
Postcondition
On k_ra8_ok every output is populated and internally consistent.
On any error no output is modified.
Note
Thread-safe: reads only hdr, writes only the outputs.
Since
Version 0.1.0

Parse + validate the fixed 24-byte "RBKC" container header.

Definition at line 290 of file book.c.

References internal_copy_object(), k_book_cont_off_chunk_bytes, k_book_cont_off_count, k_book_cont_off_reserved, k_book_cont_off_total, k_book_container_magic_len, k_ra8_err_invalid_arg, k_ra8_ok, RA8_CHECK_NULL_PTR, RA8_PRIV, and s_tag_book.

Referenced by internal_chunked_open_body(), and internal_container_view().

◆ priv_book_container_table_entry()

uint64_t priv_book_container_table_entry ( const uint8_t * table,
uint32_t idx )

Decode one uint64 LE chunk-table entry from unaligned container bytes.

memcpy-based so the table may sit at any alignment (baked MRAM arrays are byte-aligned). Entry idx starts at table + idx * k_book_container_entry_len.

Parameters
[in]tableFirst byte of the chunk table.
[in]idxEntry index (<= chunk_count, caller-bounded).
Returns
The decoded payload-relative offset.
Return values
0For entry 0 of every well-formed table (offsets are payload-relative and the first stream starts at 0).
Precondition
table holds at least (idx + 1) * k_book_container_entry_len bytes.
idx was bounds-checked against the validated chunk count.
Postcondition
No state is modified.
The result is a pure function of the table bytes.
Note
Thread-safe: pure read.
Since
Version 0.1.0

Decode one uint64 LE chunk-table entry from unaligned container bytes.

Definition at line 325 of file book.c.

References internal_copy_object(), k_book_container_entry_len, and RA8_PRIV.

Referenced by internal_container_view(), and internal_inflate_chunks().

◆ priv_book_crc32_extend()

uint32_t priv_book_crc32_extend ( uint32_t crc,
const uint8_t * data,
size_t len )

Extend a finalized CRC-32/ISO-HDLC with another byte span.

Uses the same reflected polynomial and complement convention as zlib's crc32(): pass 0 for the first span, then feed the returned value into each subsequent call. This lets resident and streaming validators share one wire-integrity definition without retaining the whole RABOOK body.

Parameters
[in]crcCRC returned for all preceding spans, or 0 for the first.
[in]dataNext readable byte span (non-NULL when len is non-zero).
[in]lenNumber of bytes in data.
Returns
CRC-32/ISO-HDLC over the concatenation of prior and current spans.
Return values
UINT32_C(0)The concatenated byte stream's finalized CRC is zero.
UINT32_MAXThe concatenated byte stream's finalized CRC has every bit set.
Precondition
data addresses at least len readable bytes when len is non-zero.
crc is zero or a finalized value returned by an earlier call.
Postcondition
No caller memory or global state is modified.
Reusing the result with the next span preserves concatenation order.
Note
Thread-safe: reads only immutable input and a constant lookup table.
Since
Version 0.1.0

Extend a finalized CRC-32/ISO-HDLC with another byte span.

Computes a CRC-32/ISO-HDLC (reflected polynomial 0xEDB88320) over the byte array [data, data + len). The algorithm seeds the accumulator with k_book_crc_init, folds each byte through the reflected polynomial, then XORs the final value with k_book_crc_init again. The check value over "123456789" is 0xCBF43926, matching Python zlib.crc32_val.

Parameters
[in]crcPrevious finalized CRC value; use zero for the first span.
[in]dataPointer to the byte array to checksum; must not be NULL.
[in]lenNumber of bytes to process; zero preserves crc.
Returns
CRC-32 after extending crc with data.
Return values
0x00000000Returned for an empty first span.
0xCBF43926Check value for the ASCII string "123456789".
Precondition
data is not NULL when len is greater than 0.
len does not exceed the size of the allocation pointed to by data.
Postcondition
The returned value equals the CRC-32/ISO-HDLC of the input bytes.
Neither data nor any external state is modified.
Note
Not thread-safe if the read range overlaps a concurrent write.
Since
Version 0.1.0

Definition at line 86 of file book.c.

References k_book_crc_bits_per_byte, k_book_crc_init, k_book_crc_poly, and RA8_PRIV.

Referenced by internal_crc32(), and internal_validate_crc().

◆ priv_book_emit_break()

bool priv_book_emit_break ( char * out,
size_t cap,
size_t * pos,
bool * at_break )

Append a paragraph break, collapsing consecutive block-level breaks.

Trims any trailing spaces already written (decrementing *pos while the last byte is a space), sets *at_break to suppress leading whitespace next, then appends a single '\n' unless the last byte is already '\n' (so nested/adjacent block elements collapse to one blank line).

Parameters
[out]outDestination character buffer.
[in]capTotal capacity of out in bytes.
[in,out]posCurrent write offset; may be trimmed, then advanced by 1.
[in,out]at_breakSet true on return; entry value ignored.
Returns
bool Append result.
Return values
trueBreak emitted (or collapsed into an existing newline).
falseOutput buffer overflowed while writing the newline byte.
Precondition
out is a valid, writable buffer of at least cap bytes.
pos is non-null and *pos <= cap on entry.
Postcondition
*at_break is true on return.
Trailing spaces before *pos are removed; at most one '\n' added.
Note
Not thread-safe; callers must provide external synchronisation.
Since
Version 0.1.0

Trims any trailing space characters already written to out (by decrementing *pos while the last byte is ' '), then sets *at_break to true to suppress leading whitespace in the next text fragment. If the last byte already in out is '\n' no additional newline is written, which collapses runs of breaks produced by consecutive or nested block-level elements (e.g. a p inside a div) into a single blank line. Otherwise a single '\n' is appended via internal_emit.

Parameters
[out]outDestination character buffer.
[in]capTotal capacity of out in bytes.
[in,out]posCurrent write offset; may be decremented to trim trailing spaces, then advanced by 1 if a newline is emitted.
[in,out]at_breakSet to true on entry to suppress subsequent leading whitespace; value on entry is ignored.
Returns
bool Append result.
Return values
trueBreak emitted (or collapsed into an existing newline).
falseOutput buffer overflowed while writing the newline byte.
Precondition
out is a valid, writable buffer of at least cap bytes.
pos is non-null and *pos <= cap on entry.
Postcondition
*at_break is true on return.
Trailing space characters in out before the current *pos have been removed; at most one '\n' is appended.
Note
Not thread-safe; callers must provide external synchronisation.
Since
Version 0.1.0

Definition at line 583 of file book_xhtml.c.

References internal_emit(), and RA8_PRIV.

Referenced by internal_paged_visit_node(), and internal_walk_text().

◆ priv_book_emit_text()

bool priv_book_emit_text ( char * out,
size_t cap,
size_t * pos,
const char * str,
bool * at_break )

Append a whitespace-collapsed text run to the output buffer.

Folds any run of space / tab / CR / LF into at most a single ASCII space; non-whitespace is forwarded via the raw append. at_break carries inter-call state: when true, leading whitespace in the fragment is dropped (so indentation between inline elements does not leak into prose). It is set true after a collapsed space and false after any non-whitespace byte – so calling this repeatedly over chunks of one logical run yields the same result as one call.

Parameters
[out]outDestination character buffer.
[in]capTotal capacity of out in bytes.
[in,out]posCurrent write offset; advanced for each byte written.
[in]strNUL-terminated text fragment to collapse and append.
[in,out]at_breakWhitespace-collapse carry flag (in: prior state; out: trailing state after this fragment).
Returns
bool Append result.
Return values
trueFragment appended (or it was entirely suppressed whitespace).
falseOutput buffer overflowed; partial output may have been written.
Precondition
out is a valid, writable buffer of at least cap bytes.
str is a valid, NUL-terminated C string.
Postcondition
On success every character in str was processed; *pos advanced.
*at_break reflects the trailing whitespace state, success or not.
Note
Not thread-safe; callers must provide external synchronisation.
Since
Version 0.1.0

Iterates over str character by character applying the HTML whitespace-collapsing rule: any run of space (' '), tab ('\t'), carriage return ('\r'), or newline ('\n') characters is folded into at most a single ASCII space. Non-whitespace characters are passed through unchanged via internal_emit. The at_break flag carries inter-call state: when true any leading whitespace in the current fragment is silently dropped, preventing pretty-print indentation between inline elements from leaking into the rendered prose. at_break is set to true after a whitespace run is emitted and set to false after any non-whitespace character is emitted.

Parameters
[out]outDestination character buffer.
[in]capTotal capacity of out in bytes.
[in,out]posCurrent write offset; advanced for each byte written.
[in]strNUL-terminated text fragment to collapse and append.
[in,out]at_breakOn entry: true if the previous output ended with a break or collapsed space that suppresses leading whitespace. On exit: updated to reflect the trailing state of this fragment.
Returns
bool Append result.
Return values
trueText run appended successfully (or was entirely suppressed whitespace).
falseOutput buffer overflowed; partial output may have been written.
Precondition
out is a valid, writable buffer of at least cap bytes.
str is a valid, NUL-terminated C string.
Postcondition
On success every character in str has been processed and *pos reflects the bytes appended to out.
*at_break accurately reflects the trailing whitespace state after the call, regardless of success or failure.
Note
Not thread-safe; callers must provide external synchronisation.
Since
Version 0.1.0

Definition at line 526 of file book_xhtml.c.

References internal_emit().

Referenced by internal_paged_emit_run(), and internal_walk_text().

◆ priv_book_is_block()

bool priv_book_is_block ( const char * name)

Test whether an element name is a block-level HTML element.

Linear search through a fixed table of block-level element names used to decide where to insert paragraph-break newlines during plain-text extraction (p, h1..h6, li, ul, ol, div, br, hr, section, tr, pre, header, blockquote, article, aside, footer, figure, figcaption). The comparison is case-sensitive (DOM names are lower-cased at compile).

Parameters
[in]nameNUL-terminated element name to test (e.g. "p").
Returns
bool Query result.
Return values
truename is a block-level element; emit a break before it.
falsename is an inline element; no break.
Precondition
name is a valid, NUL-terminated C string.
name contains only lower-case ASCII letters (DOM invariant).
Postcondition
The block-element table is not modified.
The result is a pure function of name (no side effects).
Note
Not thread-safe; callers must provide external synchronisation.
Since
Version 0.1.0

Performs a linear search through a fixed table of block-level element names used to decide where to insert paragraph-break newlines during plain-text extraction. The checked names are: p, h1, h2, h3, h4, h5, h6, li, ul, ol, div, br, hr, section, tr, pre, header, blockquote, article, aside, footer, figure, and figcaption. The comparison is case-sensitive because compiled book DOM names are lower-cased during parsing.

Parameters
[in]nameNUL-terminated element name to test (e.g. "p").
Returns
bool Query result.
Return values
truename is a block-level element; the text walker should emit a line break before descending into this element.
falsename is an inline element; no break is inserted.
Precondition
name is a valid, NUL-terminated C string.
name contains only lower-case ASCII letters (DOM invariant).
Postcondition
The block-element table is not modified.
The return value is purely a function of name with no side effects.
Note
Not thread-safe; callers must provide external synchronisation.
Since
Version 0.1.0

Definition at line 470 of file book_xhtml.c.

References RA8_PRIV, and strcmp().

Referenced by internal_paged_visit_node(), and internal_walk_text().