ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
book.h
Go to the documentation of this file.
1
71#pragma once
72
73#include <stddef.h>
74#include <stdint.h>
75
76#include "ra8_err.h"
77
85typedef enum : uint32_t {
88
118
128typedef enum : uint32_t {
129 k_book_nil = 0xFFFFFFFFU,
131
147typedef enum : uint32_t {
148 k_book_flag_rtl = 0x00000001U,
155
161typedef enum : uint8_t {
165
174typedef enum : uint8_t {
178
212typedef enum : uint8_t {
217
235
246typedef struct {
247 char magic[8];
248 uint32_t format_version;
249 uint32_t total_size;
250 uint32_t flags;
251 uint32_t title_off;
252 uint32_t author_off;
253 uint32_t language_off;
254 uint32_t identifier_off;
256 uint32_t chapter_count;
257 uint32_t chapter_off;
258 uint32_t node_count;
259 uint32_t node_off;
260 uint32_t attr_count;
261 uint32_t attr_off;
263 uint32_t stylesheet_off;
264 uint32_t image_count;
265 uint32_t image_off;
266 uint32_t string_off;
267 uint32_t string_size;
268 uint32_t image_pool_off;
270 uint32_t crc32_val;
272
273static_assert(sizeof(book_header_t) == k_book_sizeof_header, "book_header_t size pinned");
274
280typedef struct {
281 uint32_t title_off;
282 uint32_t href_off;
283 uint32_t root_node;
285
286static_assert(sizeof(book_chapter_t) == k_book_sizeof_chapter, "book_chapter_t size pinned");
287
299typedef struct {
300 uint8_t kind;
301 uint8_t reserved;
302 uint16_t attr_count;
303 uint32_t name_off;
304 uint32_t text_off;
305 uint32_t first_attr;
306 uint32_t first_child;
307 uint32_t next_sibling;
309
310static_assert(sizeof(book_node_t) == k_book_sizeof_node, "book_node_t size pinned");
311
317typedef struct {
318 uint32_t name_off;
319 uint32_t value_off;
321
322static_assert(sizeof(book_attr_t) == k_book_sizeof_attr, "book_attr_t size pinned");
323
332typedef struct {
333 uint32_t source_off;
334 uint32_t scope_chapter;
336
337static_assert(sizeof(book_stylesheet_t) == k_book_sizeof_stylesheet,
338 "book_stylesheet_t size pinned");
339
354typedef struct {
355 uint32_t id_off;
356 uint16_t width;
357 uint16_t height;
358 uint8_t format;
359 uint8_t pixel_format;
360 uint16_t reserved2;
361 uint32_t data_off;
362 uint32_t data_size;
363 uint32_t raw_size;
365
366static_assert(sizeof(book_image_t) == k_book_sizeof_image, "book_image_t size pinned");
367
368/* -------------------------------------------------------------------------- */
369/* Inline accessors. All take a validated blob base; see book_validate(). */
370/* -------------------------------------------------------------------------- */
371
382static inline const book_header_t* book_header(const void* base)
383{
384 return (const book_header_t*)base;
385}
386
411static inline bool book_is_rtl(const void* base)
412{
413 return (book_header(base)->flags & (uint32_t)k_book_flag_rtl) != 0U;
414}
415
427static inline const void* book_at(const void* base, uint32_t off)
428{
429 return (const void*)&((const uint8_t*)base)[off];
430}
431
443static inline const char* book_string(const void* base, uint32_t off)
444{
445 return (const char*)book_at(base, book_header(base)->string_off + off);
446}
447
458static inline const book_chapter_t* book_chapters(const void* base)
459{
460 return (const book_chapter_t*)book_at(base, book_header(base)->chapter_off);
461}
462
473static inline const book_node_t* book_nodes(const void* base)
474{
475 return (const book_node_t*)book_at(base, book_header(base)->node_off);
476}
477
488static inline const book_attr_t* book_attrs(const void* base)
489{
490 return (const book_attr_t*)book_at(base, book_header(base)->attr_off);
491}
492
503static inline const book_stylesheet_t* book_stylesheets(const void* base)
504{
505 return (const book_stylesheet_t*)book_at(base, book_header(base)->stylesheet_off);
506}
507
518static inline const book_image_t* book_images(const void* base)
519{
520 return (const book_image_t*)book_at(base, book_header(base)->image_off);
521}
522
534static inline const char* book_node_name(const void* base, const book_node_t* node)
535{
536 return book_string(base, node->name_off);
537}
538
550static inline const char* book_node_text(const void* base, const book_node_t* node)
551{
552 return book_string(base, node->text_off);
553}
554
566static inline const uint8_t* book_image_data(const void* base, const book_image_t* img)
567{
568 return &((const uint8_t*)book_at(base, book_header(base)->image_pool_off))[img->data_off];
569}
570
600{
602}
603
640ra8_err_t book_validate(const void* base, size_t size);
641
655typedef ra8_err_t (
656 *book_inflate_fn)(const void* src, size_t src_len, void* dst, size_t dst_cap, size_t* out_len);
657
699ra8_err_t book_open(const void* file,
700 size_t file_len,
701 book_inflate_fn inflate,
702 void* scratch,
703 size_t scratch_cap,
704 const void** out_base,
705 size_t* out_size);
706
740ra8_err_t book_chapter_to_xhtml(const void* base,
741 uint32_t chapter_idx,
742 char* out,
743 size_t cap,
744 size_t* out_len);
745
778book_chapter_text(const void* base, uint32_t chapter_idx, char* out, size_t cap, size_t* out_len);
ra8_err_t book_chapter_text(const void *base, uint32_t chapter_idx, char *out, size_t cap, size_t *out_len)
Extract one chapter's readable plain text from the DOM.
Definition book_xhtml.c:683
static const book_image_t * book_images(const void *base)
Base of the image table.
Definition book.h:518
book_struct_size_t
Pinned on-disk sizes of the blob's fixed-layout records.
Definition book.h:227
@ 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
static const void * book_at(const void *base, uint32_t off)
Resolve a blob-relative byte offset to a pointer.
Definition book.h:427
book_node_kind_t
Discriminates the two kinds of DOM node.
Definition book.h:161
@ k_book_node_element
An element: has a tag name and attributes.
Definition book.h:162
@ k_book_node_text
A text run: carries a string, no children.
Definition book.h:163
static const book_stylesheet_t * book_stylesheets(const void *base)
Base of the stylesheet table.
Definition book.h:503
book_version_t
Wire-format version stamped into every blob header.
Definition book.h:85
@ k_book_format_version
Current .rabook layout revision.
Definition book.h:86
book_sentinel_t
Reserved index value meaning "no such element".
Definition book.h:128
@ k_book_nil
Absent index / "applies to all chapters".
Definition book.h:129
book_flag_t
Feature-flag bits carried in book_header_t.flags.
Definition book.h:147
@ k_book_flag_rtl
Right-to-left reading order (manga): spine pages advance leaf-first, and the reader mirrors its page-...
Definition book.h:148
@ k_book_flag_mask_known
Every bit this firmware understands; a set bit outside this mask fails book_validate().
Definition book.h:151
static const book_chapter_t * book_chapters(const void *base)
Base of the chapter table.
Definition book.h:458
ra8_err_t book_chapter_to_xhtml(const void *base, uint32_t chapter_idx, char *out, size_t cap, size_t *out_len)
Serialize one chapter's DOM subtree back to XHTML for the renderer.
Definition book_xhtml.c:702
static const char * book_node_name(const void *base, const book_node_t *node)
Tag name of an element node.
Definition book.h:534
static const book_header_t * book_header(const void *base)
View the blob base as its header.
Definition book.h:382
book_image_format_t
Pixel encoding of an entry in the image pool.
Definition book.h:174
@ k_book_image_svg
Verbatim UTF-8 SVG source (vector; on-device rasterized).
Definition book.h:176
@ k_book_image_gray4
4bpp gray, 2px/byte; pixel (x,y) is at flat index y*width + x.
Definition book.h:175
static book_image_pixfmt_t book_image_pixfmt(const book_image_t *img)
Declared pixel depth of one image descriptor.
Definition book.h:599
book_image_pixfmt_t
Pixel depth of a k_book_image_gray4 raster in the image pool.
Definition book.h:212
@ k_book_pixfmt_gray4
4bpp packed grayscale, 2px/byte (default; every pre-field blob).
Definition book.h:213
@ k_book_pixfmt_gray8
8bpp grayscale, 1px/byte (lossless against any grey panel).
Definition book.h:215
static const book_node_t * book_nodes(const void *base)
Base of the DOM node table.
Definition book.h:473
static const book_attr_t * book_attrs(const void *base)
Base of the attribute table.
Definition book.h:488
static const char * book_string(const void *base, uint32_t off)
Resolve a string-pool offset to a NUL-terminated UTF-8 string.
Definition book.h:443
static const char * book_node_text(const void *base, const book_node_t *node)
Text of a text node.
Definition book.h:550
book_container_t
Constants of the on-disk .rabook chunked compression container.
Definition book.h:113
@ k_book_container_header_len
Fixed header bytes ahead of the chunk table.
Definition book.h:115
@ k_book_container_entry_len
One chunk-table entry (uint64 LE offset).
Definition book.h:116
@ k_book_container_magic_len
Length of the "RBKC" magic.
Definition book.h:114
static const uint8_t * book_image_data(const void *base, const book_image_t *img)
Image-pool pointer to one image's compressed pixel data.
Definition book.h:566
ra8_err_t book_validate(const void *base, size_t size)
Validate that a byte buffer is a well-formed, intact .rabook blob.
Definition book.c:231
ra8_err_t(* book_inflate_fn)(const void *src, size_t src_len, void *dst, size_t dst_cap, size_t *out_len)
Caller-supplied DEFLATE inflater used by book_open().
Definition book.h:656
ra8_err_t book_open(const void *file, size_t file_len, book_inflate_fn inflate, void *scratch, size_t scratch_cap, const void **out_base, size_t *out_size)
Open a .rabook file: check the container, inflate, validate the blob.
Definition book.c:543
static bool book_is_rtl(const void *base)
Whether the book declares right-to-left reading order (manga).
Definition book.h:411
Error Code Definitions for ra8-firmware.
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
One name="value" attribute on an element.
Definition book.h:317
uint32_t value_off
String-pool offset of the attribute value.
Definition book.h:319
uint32_t name_off
String-pool offset of the attribute name.
Definition book.h:318
One spine document (a renderable chapter) plus its TOC label.
Definition book.h:280
uint32_t root_node
Node-table index of this chapter's root element.
Definition book.h:283
uint32_t href_off
String-pool offset of the source spine href.
Definition book.h:282
uint32_t title_off
String-pool offset of the TOC label ("" if none).
Definition book.h:281
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
Descriptor for one transcoded image in the image pool.
Definition book.h:354
uint16_t height
Pixel height.
Definition book.h:357
uint32_t id_off
String-pool offset of the source href / manifest id.
Definition book.h:355
uint32_t raw_size
Inflated length in bytes.
Definition book.h:363
uint8_t format
book_image_format_t.
Definition book.h:358
uint32_t data_size
Compressed length in bytes.
Definition book.h:362
uint8_t pixel_format
book_image_pixfmt_t (0 == gray4; SVG: 0).
Definition book.h:359
uint16_t width
Pixel width.
Definition book.h:356
uint32_t data_off
Image-pool offset of the compressed pixel data.
Definition book.h:361
uint16_t reserved2
Padding; 0.
Definition book.h:360
One DOM node.
Definition book.h:299
uint8_t kind
book_node_kind_t.
Definition book.h:300
uint32_t text_off
Text: string-pool offset of the run (element: 0).
Definition book.h:304
uint16_t attr_count
Element: number of attributes (text: 0).
Definition book.h:302
uint8_t reserved
Padding; 0.
Definition book.h:301
uint32_t next_sibling
Index of next sibling node, or nil.
Definition book.h:307
uint32_t name_off
Element: string-pool offset of tag name (text: 0).
Definition book.h:303
uint32_t first_child
Index of first child node, or nil.
Definition book.h:306
uint32_t first_attr
Index of first attribute, or nil.
Definition book.h:305
A preserved CSS stylesheet and the chapter it scopes to.
Definition book.h:332
uint32_t scope_chapter
Chapter index this applies to, or nil (= all).
Definition book.h:334
uint32_t source_off
String-pool offset of the verbatim CSS text.
Definition book.h:333