ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
book_stream_wire.c
Go to the documentation of this file.
1
15#include <string.h>
16
18#include "ra8_attributes.h"
19
21 uint64_t off,
22 uint8_t* dst,
23 uint32_t len)
24{
25 if ((off > ctx->source_size) || ((uint64_t)len > (ctx->source_size - off))) {
27 }
28 return ctx->read(ctx->read_ctx, off, dst, len);
29}
30
44RA8_INTERNAL static void internal_decode_header(const uint8_t* raw, book_header_t* hdr)
45{
46 (void)memcpy(hdr->magic, raw, sizeof(hdr->magic));
70}
71
91static ra8_err_t
92internal_layout_segment(uint32_t off, uint32_t count, uint32_t elem, uint64_t* cursor)
93{
94 if ((uint64_t)off != *cursor) {
96 }
97 const uint64_t end = *cursor + ((uint64_t)count * (uint64_t)elem);
98 if (end > (uint64_t)UINT32_MAX) {
100 }
101 *cursor = end;
102 return k_ra8_ok;
103}
104
122{
123 static const char magic[8] = {'R', 'A', 'B', 'O', 'O', 'K', '1', '\0'};
124 for (size_t i = 0U; i < sizeof(magic); ++i) {
125 if (ctx->hdr.magic[i] != magic[i]) {
127 }
128 }
129 if ((ctx->hdr.format_version != (uint32_t)k_book_format_version) ||
130 ((ctx->hdr.flags & ~(uint32_t)k_book_flag_mask_known) != 0U)) {
132 }
133 if ((uint64_t)ctx->hdr.total_size != ctx->source_size) {
135 }
136 const uint64_t node_mark_bytes =
138 return (node_mark_bytes > (uint64_t)ctx->scratch_cap) ? k_ra8_err_invalid_size : k_ra8_ok;
139}
140
158{
159 uint64_t cursor = (uint64_t)k_book_sizeof_header;
161 if (err == k_ra8_ok) {
163 ctx->hdr.chapter_count,
164 (uint32_t)k_book_sizeof_chapter,
165 &cursor);
166 }
167 if (err == k_ra8_ok) {
169 ctx->hdr.node_count,
170 (uint32_t)k_book_sizeof_node,
171 &cursor);
172 }
173 if (err == k_ra8_ok) {
175 ctx->hdr.attr_count,
176 (uint32_t)k_book_sizeof_attr,
177 &cursor);
178 }
179 if (err == k_ra8_ok) {
182 (uint32_t)k_book_sizeof_stylesheet,
183 &cursor);
184 }
185 if (err == k_ra8_ok) {
187 ctx->hdr.image_count,
188 (uint32_t)k_book_sizeof_image,
189 &cursor);
190 }
191 if (err == k_ra8_ok) {
192 err = internal_layout_segment(ctx->hdr.string_off, ctx->hdr.string_size, 1U, &cursor);
193 }
194 if (err == k_ra8_ok) {
195 err = internal_layout_segment(ctx->hdr.image_pool_off, ctx->hdr.image_pool_size, 1U, &cursor);
196 }
197 if ((err == k_ra8_ok) && (cursor != (uint64_t)ctx->hdr.total_size)) {
199 }
200 return err;
201}
202
204{
205 uint8_t raw[k_book_sizeof_header] = {};
206 ra8_err_t err = priv_book_stream_read(ctx, 0U, raw, (uint32_t)sizeof(raw));
207 if (err == k_ra8_ok) {
208 internal_decode_header(raw, &ctx->hdr);
210 }
211 return err;
212}
213
215{
216 if (off >= ctx->hdr.string_size) {
218 }
219 if (off == 0U) {
220 return k_ra8_ok;
221 }
222 uint8_t preceding = 0U;
223 ra8_err_t err =
224 priv_book_stream_read(ctx, (uint64_t)ctx->hdr.string_off + (uint64_t)off - 1U, &preceding, 1U);
225 if ((err == k_ra8_ok) && (preceding != 0U)) {
227 }
228 return err;
229}
230
232{
234 uint8_t first = 0U;
235 if (err == k_ra8_ok) {
236 err = priv_book_stream_read(ctx, (uint64_t)ctx->hdr.string_off + off, &first, 1U);
237 }
238 if ((err == k_ra8_ok) && (first == 0U)) {
240 }
241 return err;
242}
243
245{
246 if (ctx->hdr.string_size == 0U) {
248 }
249 uint8_t first = 0U;
250 uint8_t last = 0U;
251 ra8_err_t err = priv_book_stream_read(ctx, ctx->hdr.string_off, &first, 1U);
252 if (err == k_ra8_ok) {
253 err = priv_book_stream_read(ctx,
254 (uint64_t)ctx->hdr.string_off + (uint64_t)ctx->hdr.string_size - 1U,
255 &last,
256 1U);
257 }
258 if ((err == k_ra8_ok) && ((first != 0U) || (last != 0U))) {
260 }
261 return err;
262}
@ k_book_sizeof_image
Bytes in book_image_t.
Definition book.h:233
@ k_book_sizeof_header
Bytes in book_header_t.
Definition book.h:228
@ k_book_sizeof_node
Bytes in book_node_t.
Definition book.h:230
@ k_book_sizeof_chapter
Bytes in book_chapter_t.
Definition book.h:229
@ k_book_sizeof_stylesheet
Bytes in book_stylesheet_t.
Definition book.h:232
@ k_book_sizeof_attr
Bytes in book_attr_t.
Definition book.h:231
@ k_book_format_version
Current .rabook layout revision.
Definition book.h:86
@ k_book_flag_mask_known
Every bit this firmware understands; a set bit outside this mask fails book_validate().
Definition book.h:151
Private wire geometry for strict RABOOK1 stream validation.
static uint32_t internal_book_stream_le32(const uint8_t *p)
Decode one little-endian 32-bit field from unaligned bytes.
@ k_stream_hdr_style_count
Stylesheet-table count field.
@ k_stream_hdr_chapter_count
Chapter-table count field.
@ k_stream_hdr_pool_off
Image-payload pool offset.
@ k_stream_hdr_node_count
DOM-node table count field.
@ k_stream_hdr_image_count
Image-table count field.
@ k_stream_hdr_string_size
Interned-string pool size.
@ k_stream_hdr_title
Title string-offset field.
@ k_stream_hdr_cover
Cover-image index field.
@ k_stream_hdr_attr_count
Attribute-table count field.
@ k_stream_hdr_total
Total-size field byte offset.
@ k_stream_hdr_string_off
Interned-string pool offset.
@ k_stream_hdr_version
Format-version field byte offset.
@ k_stream_hdr_identifier
Identifier string-offset field.
@ k_stream_hdr_crc
Body CRC-32 field byte offset.
@ k_stream_hdr_flags
Feature-flags field byte offset.
@ k_stream_hdr_image_off
Image-table byte offset.
@ k_stream_hdr_node_off
DOM-node table byte offset.
@ k_stream_hdr_author
Author string-offset field.
@ k_stream_hdr_style_off
Stylesheet-table byte offset.
@ k_stream_hdr_pool_size
Image-payload pool size.
@ k_stream_hdr_language
Language string-offset field.
@ k_stream_hdr_attr_off
Attribute-table byte offset.
@ k_stream_hdr_chapter_off
Chapter-table byte offset.
@ k_stream_bits_per_byte
Bits represented by one byte.
@ k_stream_mark_round
Ceiling-division numerator bias.
static ra8_err_t internal_validate_header_fields(const stream_validate_t *ctx)
Validate decoded header fields before walking the table layout.
ra8_err_t priv_book_stream_validate_string_envelope(const stream_validate_t *ctx)
Validate the string pool's leading and trailing NUL sentinels.
static void internal_decode_header(const uint8_t *raw, book_header_t *hdr)
Decode the fixed header from canonical little-endian wire bytes.
ra8_err_t priv_book_stream_nonempty_string_ref(const stream_validate_t *ctx, uint32_t off)
Require a string reference to name a non-empty interned string.
ra8_err_t priv_book_stream_read_validate_header(stream_validate_t *ctx)
Read, decode, and validate the canonical stream header and layout.
static ra8_err_t internal_layout_segment(uint32_t off, uint32_t count, uint32_t elem, uint64_t *cursor)
Require one segment to begin at the canonical cursor and advance it.
ra8_err_t priv_book_stream_read(const stream_validate_t *ctx, uint64_t off, uint8_t *dst, uint32_t len)
Read one exact, bounded source span.
ra8_err_t priv_book_stream_string_ref(const stream_validate_t *ctx, uint32_t off)
Require a referenced offset to name an interned-string boundary.
static ra8_err_t internal_validate_header_layout(const stream_validate_t *ctx)
Validate version, flags, exact source length, and canonical layout.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
Definition ra8_err.h:167
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
Fixed 100-byte prologue describing every table and pool in the blob.
Definition book.h:246
uint32_t author_off
String-pool offset of the author.
Definition book.h:252
uint32_t format_version
book_version_t of this blob.
Definition book.h:248
uint32_t crc32_val
CRC-32/ISO-HDLC of the body (all bytes after this header).
Definition book.h:270
uint32_t image_off
Offset to the image table.
Definition book.h:265
uint32_t image_pool_size
Image-pool length in bytes.
Definition book.h:269
uint32_t language_off
String-pool offset of the BCP-47 language.
Definition book.h:253
uint32_t attr_count
Number of attribute records.
Definition book.h:260
uint32_t node_count
Number of DOM nodes.
Definition book.h:258
uint32_t chapter_count
Number of spine chapters.
Definition book.h:256
uint32_t image_count
Number of image descriptors.
Definition book.h:264
uint32_t title_off
String-pool offset of the book title.
Definition book.h:251
uint32_t string_off
Offset to the string pool.
Definition book.h:266
uint32_t attr_off
Offset to the attribute table.
Definition book.h:261
uint32_t node_off
Offset to the node table.
Definition book.h:259
uint32_t identifier_off
String-pool offset of the unique book id.
Definition book.h:254
uint32_t string_size
String-pool length in bytes.
Definition book.h:267
uint32_t flags
book_flag_t bits; unknown bits are rejected.
Definition book.h:250
uint32_t stylesheet_off
Offset to the stylesheet table.
Definition book.h:263
char magic[8]
Always "RABOOK1" (7 chars + NUL).
Definition book.h:247
uint32_t chapter_off
Offset to the chapter table.
Definition book.h:257
uint32_t stylesheet_count
Number of preserved stylesheets.
Definition book.h:262
uint32_t image_pool_off
Offset to the image pool.
Definition book.h:268
uint32_t total_size
Total blob length in bytes.
Definition book.h:249
uint32_t cover_image_index
Image-table index of the cover, or nil.
Definition book.h:255
Immutable validation state shared by the bounded table passes.
uint32_t scratch_cap
Transfer-buffer capacity.
void * read_ctx
Callback context.
book_header_t hdr
Decoded host-order header.
uint64_t source_size
Exact flat-source byte size.
book_stream_read_fn read
Exact source callback.