|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
RABOOK1 builder implementation (see rabook_compile.h). More...
#include "rabook_compile.h"#include <stdint.h>#include <string.h>#include "ra8_attributes.h"#include "ra8_check.h"Go to the source code of this file.
Enumerations | |
| enum | ra8_rabook_buffer_ptr_count_t : uint8_t { k_rabook_buffer_ptr_count = 8U } |
| Number of caller-owned arena pointers validated by init. More... | |
| enum | ra8_rabook_pool_mode_t : uint8_t { k_rabook_pool_none = 0U , k_rabook_pool_internal = 1U , k_rabook_pool_external = 2U } |
| Image-pool backing selected by the first non-empty image append. More... | |
Functions | |
| static ra8_err_t | internal_check_buffer_members (const ra8_rabook_buffers_t *buf) |
| Reject a buffers struct with any NULL arena pointer. | |
| ra8_err_t | rabook_compile_init (ra8_rabook_ctx_t *ctx, const ra8_rabook_buffers_t *buf) |
| Bind a builder context to its caller-provided arenas. | |
| uint32_t | ra8_rabook_intern (ra8_rabook_ctx_t *ctx, const char *str) |
| Intern a NUL-terminated UTF-8 string into the pool, de-duplicated. | |
| static uint32_t | internal_append_attrs (ra8_rabook_ctx_t *ctx, const book_attr_t *attrs, uint16_t attr_count) |
Append attr_count attribute records to the attr table contiguously. | |
| uint32_t | ra8_rabook_add_element (ra8_rabook_ctx_t *ctx, uint32_t name_off, const book_attr_t *attrs, uint16_t attr_count) |
| Append an element node with its attributes; return its node index. | |
| uint32_t | ra8_rabook_add_text (ra8_rabook_ctx_t *ctx, uint32_t text_off) |
Append a text node carrying text_off; return its node index. | |
| ra8_err_t | ra8_rabook_link_child (ra8_rabook_ctx_t *ctx, uint32_t parent, uint32_t child) |
Set parent's first child to child. | |
| ra8_err_t | ra8_rabook_link_sibling (ra8_rabook_ctx_t *ctx, uint32_t previous_node, uint32_t next_sibling) |
Set previous_node's next sibling to next_sibling. | |
| uint32_t | ra8_rabook_add_chapter (ra8_rabook_ctx_t *ctx, uint32_t title_off, uint32_t href_off, uint32_t root_node) |
| Append a spine chapter; return its chapter index. | |
| static uint32_t | internal_append_image_descriptor (ra8_rabook_ctx_t *ctx, uint32_t id_off, uint16_t width, uint16_t height, uint8_t format, uint8_t pixel_format, uint32_t data_off, uint32_t data_size) |
| Append one already-bounded image descriptor. | |
| uint32_t | ra8_rabook_add_image (ra8_rabook_ctx_t *ctx, uint32_t id_off, uint16_t width, uint16_t height, uint8_t format, uint8_t pixel_format, const uint8_t *data, uint32_t data_size) |
| Append an image descriptor and copy its pixel/SVG bytes into the pool. | |
| uint32_t | ra8_rabook_add_image_external (ra8_rabook_ctx_t *ctx, uint32_t id_off, uint16_t width, uint16_t height, uint8_t format, uint8_t pixel_format, uint32_t data_size, uint32_t *out_data_off) |
| Append an image descriptor whose bytes live in a caller-owned spool. | |
| uint32_t | ra8_rabook_add_stylesheet (ra8_rabook_ctx_t *ctx, uint32_t source_off, uint32_t scope_chapter) |
| Append a preserved stylesheet; return its stylesheet index. | |
| ra8_err_t | ra8_rabook_set_metadata (ra8_rabook_ctx_t *ctx, uint32_t title_off, uint32_t author_off, uint32_t language_off, uint32_t identifier_off, uint32_t cover_image_index) |
| Record the book metadata offsets that land in the blob header. | |
Variables | |
| static const char *const | s_tag_rabook = "rabook_compile" |
| Component tag for log messages from this module. | |
RABOOK1 builder implementation (see rabook_compile.h).
Zero-heap builder back-end of the #149 on-device EPUB -> .rabook compiler. Each builder call appends into a caller-provided arena and latches capacity failures in the context. The sibling finalizer module owns canonical layout, CRC-32, and output publication. Keeping construction separate from serialization makes the mutable builder lifecycle independent of its memory or streaming destination.
Definition in file rabook_compile.c.
| enum ra8_rabook_buffer_ptr_count_t : uint8_t |
Number of caller-owned arena pointers validated by init.
| Enumerator | |
|---|---|
| k_rabook_buffer_ptr_count | chapters..out member pointers. |
Definition at line 34 of file rabook_compile.c.
| enum ra8_rabook_pool_mode_t : uint8_t |
Image-pool backing selected by the first non-empty image append.
| Enumerator | |
|---|---|
| k_rabook_pool_none | No non-empty image has selected storage. |
| k_rabook_pool_internal | Bytes live in buf.image_pool. |
| k_rabook_pool_external | Bytes live behind the read callback. |
Definition at line 39 of file rabook_compile.c.
|
static |
Append attr_count attribute records to the attr table contiguously.
Copies each record and advances ctx->attr_count. The caller must have already checked that attr_count attributes fit the remaining attr arena, so this helper never overflows and never sets failed.
| [in,out] | ctx | Builder context (non-NULL, capacity pre-checked). |
| [in] | attrs | Attribute records to copy (non-NULL iff attr_count > 0). |
| [in] | attr_count | Number of attribute records to append. |
attr_count is 0. | k_book_nil | attr_count is 0 (no attributes appended). |
ctx and (when attr_count > 0) attrs are non-NULL. attr_count. Definition at line 177 of file rabook_compile.c.
References ra8_rabook_ctx_t::attr_count, ra8_rabook_buffers_t::attrs, ra8_rabook_ctx_t::buf, and k_book_nil.
Referenced by ra8_rabook_add_element().
|
static |
Append one already-bounded image descriptor.
Initializes every wire field and advances the descriptor count; pool ownership and capacity checks remain with the public append APIs.
| [in,out] | ctx | Builder with descriptor capacity available. |
| [in] | id_off | Image identifier string offset. |
| [in] | width | Image width in pixels. |
| [in] | height | Image height in pixels. |
| [in] | format | Encoded image format. |
| [in] | pixel_format | Decoded pixel format. |
| [in] | data_off | Logical image-pool byte offset. |
| [in] | data_size | Encoded byte count. |
| uint32_t | Valid image-table index. |
ctx is non-NULL and has image descriptor capacity. data_off and data_size describe a reserved logical pool span. Definition at line 338 of file rabook_compile.c.
References ra8_rabook_ctx_t::buf, book_image_t::data_off, book_image_t::data_size, book_image_t::format, book_image_t::height, book_image_t::id_off, ra8_rabook_ctx_t::image_count, ra8_rabook_buffers_t::images, book_image_t::pixel_format, book_image_t::raw_size, and book_image_t::width.
Referenced by ra8_rabook_add_image(), and ra8_rabook_add_image_external().
|
static |
Reject a buffers struct with any NULL arena pointer.
Validates buf then each of its k_rabook_buffer_ptr_count arena pointers in turn, naming the first NULL member in the log line. Keeps rabook_compile_init flat (one table-driven check instead of ten inline guards).
| [in] | buf | Caller-owned arenas to validate (checked for NULL). |
| k_ra8_ok | buf and every member pointer are non-NULL. |
| k_ra8_err_null_ptr | buf or one of its member pointers is NULL. |
buf, if non-NULL, addresses a fully-constructed buffers struct. buf is guaranteed non-NULL. Definition at line 63 of file rabook_compile.c.
References ra8_rabook_buffers_t::attrs, ra8_rabook_buffers_t::chapters, ra8_rabook_buffers_t::image_pool, ra8_rabook_buffers_t::images, k_ra8_ok, k_rabook_buffer_ptr_count, ra8_rabook_buffers_t::nodes, ra8_rabook_buffers_t::out, RA8_CHECK_NULL_PTR, s_tag_rabook, ra8_rabook_buffers_t::string_pool, and ra8_rabook_buffers_t::stylesheets.
Referenced by rabook_compile_init().
| uint32_t ra8_rabook_add_chapter | ( | ra8_rabook_ctx_t * | ctx, |
| uint32_t | title_off, | ||
| uint32_t | href_off, | ||
| uint32_t | root_node ) |
Append a spine chapter; return its chapter index.
Records one chapter-table entry pointing at its root DOM node plus the interned TOC label and spine href. Chapters are appended in spine (reading) order, so the returned index doubles as the chapter's position in the book.
| [in,out] | ctx | Builder context (non-NULL, initialised). |
| [in] | title_off | String-pool offset of the TOC label ("" if none). |
| [in] | href_off | String-pool offset of the spine href. |
| [in] | root_node | Node index of this chapter's root element. |
| k_book_nil | ctx is NULL or the chapter arena is full. |
ctx was initialised. Definition at line 291 of file rabook_compile.c.
References ra8_rabook_ctx_t::buf, ra8_rabook_buffers_t::chapter_cap, ra8_rabook_ctx_t::chapter_count, ra8_rabook_buffers_t::chapters, ra8_rabook_ctx_t::failed, book_chapter_t::href_off, k_book_nil, book_chapter_t::root_node, and book_chapter_t::title_off.
Referenced by internal_append_chapter(), and ra8_rabook_xml_parse_chapter().
| uint32_t ra8_rabook_add_element | ( | ra8_rabook_ctx_t * | ctx, |
| uint32_t | name_off, | ||
| const book_attr_t * | attrs, | ||
| uint16_t | attr_count ) |
Append an element node with its attributes; return its node index.
Writes a k_book_node_element node carrying name_off and appends attr_count attribute records contiguously, linking the node's first_attr / attr_count. Child / sibling links default to nil; set them with ra8_rabook_link_child / ra8_rabook_link_sibling.
| [in,out] | ctx | Builder context (non-NULL, initialised). |
| [in] | name_off | String-pool offset of the tag name. |
| [in] | attrs | Attribute records (may be NULL iff attr_count is 0). |
| [in] | attr_count | Number of attributes for this element. |
| k_book_nil | ctx is NULL, the node/attr arena is full, or attrs is NULL with a non-zero attr_count. |
ctx was initialised. attr_count attrs fits the node / attr arenas. Definition at line 191 of file rabook_compile.c.
References ra8_rabook_buffers_t::attr_cap, book_node_t::attr_count, ra8_rabook_ctx_t::attr_count, ra8_rabook_ctx_t::buf, ra8_rabook_ctx_t::failed, book_node_t::first_attr, book_node_t::first_child, internal_append_attrs(), k_book_nil, k_book_node_element, book_node_t::kind, book_node_t::name_off, book_node_t::next_sibling, ra8_rabook_buffers_t::node_cap, ra8_rabook_ctx_t::node_count, ra8_rabook_buffers_t::nodes, and book_node_t::text_off.
Referenced by internal_append_chapter(), and internal_element().
| uint32_t ra8_rabook_add_image | ( | ra8_rabook_ctx_t * | ctx, |
| uint32_t | id_off, | ||
| uint16_t | width, | ||
| uint16_t | height, | ||
| uint8_t | format, | ||
| uint8_t | pixel_format, | ||
| const uint8_t * | data, | ||
| uint32_t | data_size ) |
Append an image descriptor and copy its pixel/SVG bytes into the pool.
Copies data_size bytes into the image pool and records a descriptor with data_off set to the bytes' pool offset and raw_size == data_size (the pool is uncompressed; the whole blob is DEFLATE-wrapped on disk).
| [in,out] | ctx | Builder context (non-NULL, initialised). |
| [in] | id_off | String-pool offset of the source href / manifest id. |
| [in] | width | Pixel width (0 for SVG). |
| [in] | height | Pixel height (0 for SVG). |
| [in] | format | book_image_format_t (gray4 or svg). |
| [in] | pixel_format | book_image_pixfmt_t depth of a gray4-format raster (gray4 = 4bpp packed, gray8 = 8bpp); pass k_book_pixfmt_gray4 (0) for an SVG entry. |
| [in] | data | Pixel / SVG bytes to copy (non-NULL iff data_size > 0). |
| [in] | data_size | Byte length of data. |
| k_book_nil | ctx is NULL, the image table or pool is full, or data is NULL with a non-zero data_size. |
ctx was initialised. data_size bytes fit the remaining image-pool capacity. pixel_format. Definition at line 362 of file rabook_compile.c.
References ra8_rabook_ctx_t::buf, ra8_rabook_ctx_t::failed, ra8_rabook_buffers_t::image_cap, ra8_rabook_ctx_t::image_count, ra8_rabook_buffers_t::image_pool, ra8_rabook_buffers_t::image_pool_cap, ra8_rabook_ctx_t::image_pool_mode, ra8_rabook_ctx_t::image_pool_size, internal_append_image_descriptor(), k_book_nil, k_rabook_pool_external, k_rabook_pool_internal, and memcpy().
Referenced by internal_add_manifest_image(), and internal_transcode_image().
| uint32_t ra8_rabook_add_image_external | ( | ra8_rabook_ctx_t * | ctx, |
| uint32_t | id_off, | ||
| uint16_t | width, | ||
| uint16_t | height, | ||
| uint8_t | format, | ||
| uint8_t | pixel_format, | ||
| uint32_t | data_size, | ||
| uint32_t * | out_data_off ) |
Append an image descriptor whose bytes live in a caller-owned spool.
Reserves the next contiguous span of the logical image pool without copying bytes into ra8_rabook_buffers_t::image_pool. The caller stores exactly data_size bytes at the returned out_data_off and later supplies a matching read callback to ra8_rabook_finalize_stream. Internal and external non-empty images cannot be mixed in one context.
| [in,out] | ctx | Builder context. |
| [in] | id_off | Interned image identifier offset. |
| [in] | width | Image width in pixels. |
| [in] | height | Image height in pixels. |
| [in] | format | book_image_format_t value. |
| [in] | pixel_format | book_image_pixfmt_t value. |
| [in] | data_size | Reserved external byte count. |
| [out] | out_data_off | Receives the logical image-pool offset. |
| k_book_nil | NULL arguments, image-table exhaustion, mixed storage modes, or a logical image-pool size overflow. |
ctx was initialized and out_data_off is writable. data_size bytes are available in the caller's spool before finalize. Definition at line 413 of file rabook_compile.c.
References ra8_rabook_ctx_t::buf, ra8_rabook_ctx_t::failed, ra8_rabook_buffers_t::image_cap, ra8_rabook_ctx_t::image_count, ra8_rabook_ctx_t::image_pool_mode, ra8_rabook_ctx_t::image_pool_size, internal_append_image_descriptor(), k_book_nil, k_rabook_pool_external, and k_rabook_pool_internal.
Referenced by internal_store_image().
| uint32_t ra8_rabook_add_stylesheet | ( | ra8_rabook_ctx_t * | ctx, |
| uint32_t | source_off, | ||
| uint32_t | scope_chapter ) |
Append a preserved stylesheet; return its stylesheet index.
Records one verbatim-CSS stylesheet entry in the stylesheet table. scope_chapter limits it to a single chapter, or k_book_nil applies it book-wide; the CSS text itself is interned in the string pool and referenced by source_off.
| [in,out] | ctx | Builder context (non-NULL, initialised). |
| [in] | source_off | String-pool offset of the verbatim CSS text. |
| [in] | scope_chapter | Chapter index it scopes to, or k_book_nil. |
| k_book_nil | ctx is NULL or the stylesheet arena is full. |
ctx was initialised. Definition at line 466 of file rabook_compile.c.
References ra8_rabook_ctx_t::buf, ra8_rabook_ctx_t::failed, k_book_nil, book_stylesheet_t::scope_chapter, book_stylesheet_t::source_off, ra8_rabook_buffers_t::stylesheet_cap, ra8_rabook_ctx_t::stylesheet_count, and ra8_rabook_buffers_t::stylesheets.
Referenced by internal_compile_stylesheets().
| uint32_t ra8_rabook_add_text | ( | ra8_rabook_ctx_t * | ctx, |
| uint32_t | text_off ) |
Append a text node carrying text_off; return its node index.
Writes a k_book_node_text node whose text_off is text_off; a text node has no attributes and no children. Sibling links default to nil; set them with ra8_rabook_link_sibling.
| [in,out] | ctx | Builder context (non-NULL, initialised). |
| [in] | text_off | String-pool offset of the text run. |
| k_book_nil | ctx is NULL or the node arena is full. |
ctx was initialised. Definition at line 233 of file rabook_compile.c.
References book_node_t::attr_count, ra8_rabook_ctx_t::buf, ra8_rabook_ctx_t::failed, book_node_t::first_attr, book_node_t::first_child, k_book_nil, k_book_node_text, book_node_t::kind, book_node_t::name_off, book_node_t::next_sibling, ra8_rabook_buffers_t::node_cap, ra8_rabook_ctx_t::node_count, ra8_rabook_buffers_t::nodes, and book_node_t::text_off.
Referenced by internal_text().
| uint32_t ra8_rabook_intern | ( | ra8_rabook_ctx_t * | ctx, |
| const char * | str ) |
Intern a NUL-terminated UTF-8 string into the pool, de-duplicated.
Scans the existing pool string-by-string for an exact match; on a hit returns that offset, otherwise appends str plus its NUL and returns the new offset. Identical strings therefore share one offset (the first-interned occurrence), matching the desktop StringPool so a renderer can intern-compare tags by offset.
| [in,out] | ctx | Builder context (non-NULL, initialised). |
| [in] | str | NUL-terminated string to intern (non-NULL). |
| k_book_nil | ctx or str is NULL, or the pool is full. |
ctx was initialised by rabook_compile_init. str is NUL-terminated; its length is measured with strlen. (Pool overflow is handled gracefully – see the| / |
str. Definition at line 113 of file rabook_compile.c.
References ra8_rabook_ctx_t::buf, ra8_rabook_ctx_t::failed, k_book_nil, memcpy(), ra8_rabook_buffers_t::string_cap, ra8_rabook_buffers_t::string_pool, ra8_rabook_ctx_t::string_size, and strlen().
Referenced by internal_add_manifest_image(), internal_compile_metadata(), internal_compile_stylesheets(), internal_intern(), ra8_rabook_xml_parse_chapter(), and rabook_compile_init().
| ra8_err_t ra8_rabook_link_child | ( | ra8_rabook_ctx_t * | ctx, |
| uint32_t | parent, | ||
| uint32_t | child ) |
Set parent's first child to child.
Records the head of parent's child sibling-chain. Call once per parent with the first child; chain the rest via ra8_rabook_link_sibling.
| [in,out] | ctx | Builder context (non-NULL, initialised). |
| [in] | parent | Node index of the parent element (valid, an element). |
| [in] | child | Node index of the first child (valid). |
| k_ra8_ok | Link recorded. |
| k_ra8_err_null_ptr | ctx is NULL. |
| k_ra8_err_invalid_arg | parent or child is out of range. |
ctx was initialised. parent and child are indices of nodes already appended. Definition at line 260 of file rabook_compile.c.
References ra8_rabook_ctx_t::buf, book_node_t::first_child, k_ra8_err_invalid_arg, k_ra8_ok, ra8_rabook_ctx_t::node_count, ra8_rabook_buffers_t::nodes, RA8_CHECK_NULL_PTR, ra8_log_error, and s_tag_rabook.
Referenced by internal_append_chapter(), and internal_link().
| ra8_err_t ra8_rabook_link_sibling | ( | ra8_rabook_ctx_t * | ctx, |
| uint32_t | previous_node, | ||
| uint32_t | next_sibling ) |
Set previous_node's next sibling to next_sibling.
Extends a child sibling-chain by one. Walk a parent's children by reading first_child then each node's next_sibling.
| [in,out] | ctx | Builder context (non-NULL, initialised). |
| [in] | previous_node | Node index whose sibling is set (valid). |
| [in] | next_sibling | Node index of the next sibling (valid). |
| k_ra8_ok | Link recorded. |
| k_ra8_err_null_ptr | ctx is NULL. |
| k_ra8_err_invalid_arg | previous_node or next_sibling is out of range. |
ctx was initialised. previous_node and next_sibling are indices of nodes already appended. Definition at line 276 of file rabook_compile.c.
References ra8_rabook_ctx_t::buf, k_ra8_err_invalid_arg, k_ra8_ok, book_node_t::next_sibling, ra8_rabook_ctx_t::node_count, ra8_rabook_buffers_t::nodes, RA8_CHECK_NULL_PTR, ra8_log_error, and s_tag_rabook.
Referenced by internal_link().
| ra8_err_t ra8_rabook_set_metadata | ( | ra8_rabook_ctx_t * | ctx, |
| uint32_t | title_off, | ||
| uint32_t | author_off, | ||
| uint32_t | language_off, | ||
| uint32_t | identifier_off, | ||
| uint32_t | cover_image_index ) |
Record the book metadata offsets that land in the blob header.
Stashes the title / author / language / identifier string-pool offsets plus the cover image index; ra8_rabook_finalize copies them into the fixed 100-byte RABOOK1 header. The strings must already be interned in this builder so the offsets stay valid through finalize. If the builder has already latched its sticky failed flag the call is a no-op that reports k_ra8_err_no_mem (the same way finalize does).
| [in,out] | ctx | Builder context (non-NULL, initialised). |
| [in] | title_off | String-pool offset of the title. |
| [in] | author_off | String-pool offset of the author. |
| [in] | language_off | String-pool offset of the BCP-47 language. |
| [in] | identifier_off | String-pool offset of the unique id. |
| [in] | cover_image_index | Image index of the cover, or k_book_nil. |
| k_ra8_ok | Metadata recorded. |
| k_ra8_err_null_ptr | ctx is NULL. |
| k_ra8_err_no_mem | A prior builder call overflowed an arena (sticky failed latched); the metadata is not recorded. |
ctx was initialised. Definition at line 487 of file rabook_compile.c.
References ra8_rabook_ctx_t::author_off, ra8_rabook_ctx_t::cover_image_index, ra8_rabook_ctx_t::failed, ra8_rabook_ctx_t::identifier_off, k_ra8_err_no_mem, k_ra8_ok, ra8_rabook_ctx_t::language_off, RA8_CHECK_NULL_PTR, ra8_log_error, s_tag_rabook, and ra8_rabook_ctx_t::title_off.
Referenced by internal_compile_metadata(), and internal_set_metadata().
| ra8_err_t rabook_compile_init | ( | ra8_rabook_ctx_t * | ctx, |
| const ra8_rabook_buffers_t * | buf ) |
Bind a builder context to its caller-provided arenas.
Zero-inits the running counts and clears the sticky-fail flag. All metadata offsets default to 0 and the cover index to k_book_nil until ra8_rabook_set_metadata overrides them. String-pool offset 0 is reserved for the empty string – init interns "" first so offset 0 is the empty-string sentinel that text/element nodes store, matching the desktop StringPool.__init__ in tools/epub_compile/src/epub_compile.py.
| [out] | ctx | Builder context to initialise (non-NULL). |
| [in] | buf | Caller-owned arenas (non-NULL; all member pointers non-NULL). |
| k_ra8_ok | Context bound and ready. |
| k_ra8_err_null_ptr | ctx or buf (or a member pointer) is NULL. |
buf's arenas each hold at least their declared _cap. ctx is not aliased by another live builder. Definition at line 94 of file rabook_compile.c.
References ra8_rabook_ctx_t::buf, ra8_rabook_ctx_t::cover_image_index, internal_check_buffer_members(), k_book_nil, k_ra8_ok, RA8_CHECK_NULL_PTR, ra8_rabook_intern(), and s_tag_rabook.
Referenced by internal_compile_to_blob(), and ra8_rabook_comic_init().
|
static |
Component tag for log messages from this module.
Definition at line 27 of file rabook_compile.c.