ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
epub_fs.c
Go to the documentation of this file.
1
19
20/* Bridge only when the ra8_fs storage layer is on the include path (firmware that
21 * mounts a filesystem, and the host test build). Otherwise this TU is empty. */
22#if __has_include("ra8_fs.h")
23
24#include "epub_fs.h"
25
26#include <stdint.h>
27
28#include "epub_fs_internal.h"
29#include "ra8_attributes.h"
30
31RA8_PRIV size_t priv_epub_fs_stream_read(void* ctx, uint64_t offset, void* buf, size_t len)
32{
34 if ((io == nullptr) || (io->file == nullptr) || (buf == nullptr)) {
35 return 0U;
36 }
37 if (offset > (uint64_t)UINT32_MAX) {
38 return 0U; /* GCOVR_EXCL_LINE -- ra8_fs is 32-bit; a >4 GiB .epub is rejected before here */
39 }
40 if (ra8_fs_seek(io->file, (uint32_t)offset) != k_ra8_ok) {
41 return 0U; /* GCOVR_EXCL_LINE -- seek clamps to size and cannot fail on a live handle */
42 }
43 /* GCOVR_EXCL_BR_START -- len is a bounded miniz IO chunk, never > 4 GiB */
44 const uint32_t want = (len > (size_t)UINT32_MAX) ? UINT32_MAX : (uint32_t)len;
45 /* GCOVR_EXCL_BR_STOP */
46 uint32_t got = 0U;
47 if (ra8_fs_read(io->file, (uint8_t*)buf, want, &got) != k_ra8_ok) {
48 return 0U;
49 }
50 return (size_t)got;
51}
52
54 const char* path,
56 epub_book_t* out_book)
57{
58 if ((mount == nullptr) || (path == nullptr) || (io == nullptr) || (out_book == nullptr)) {
59 return k_ra8_err_null_ptr;
60 }
61 io->file = nullptr;
62
63 ra8_fs_file_t* file = nullptr;
64 ra8_err_t err = ra8_fs_open(mount, path, k_ra8_fs_mode_read, &file);
65 if (err != k_ra8_ok) {
66 return err;
67 }
68
69 /* Size the archive for miniz. ra8_fs_size cannot fail on the handle ra8_fs_open
70 * just returned (in_use set, both out-params non-NULL); on any future contract
71 * change `size` stays 0 and epub_open_streamed rejects it as invalid_arg,
72 * so no separate (untestable) error branch is needed here. */
73 uint64_t size64 = 0U;
74 (void)ra8_fs_size(file, &size64);
75 /* The EPUB pipeline sizes archives in 32 bits; an over-4-GiB file is not a
76 * plausible EPUB and is clamped to the reject path (0 -> invalid_arg). */
77 const uint32_t size = (size64 <= (uint64_t)UINT32_MAX) ? (uint32_t)size64 : 0U;
78
79 io->file = file;
80 const epub_stream_media_t media = {
82 .ctx = io,
83 .size = (uint64_t)size,
84 };
85 err = epub_open_streamed(&media, path, out_book);
86 if (err != k_ra8_ok) {
87 (void)ra8_fs_close(file);
88 io->file = nullptr;
89 return err;
90 }
91 return k_ra8_ok;
92}
93
95{
96 if ((io == nullptr) || (book == nullptr)) {
97 return k_ra8_err_null_ptr;
98 }
99 const ra8_err_t err = epub_close(book);
100 if (io->file != nullptr) {
101 (void)ra8_fs_close(io->file);
102 io->file = nullptr;
103 }
104 return err;
105}
106
107#endif /* __has_include("ra8_fs.h") */
ra8_err_t epub_close(epub_book_t *book)
Close a previously opened EPUB book.
Definition epub_open.c:507
ra8_err_t epub_open_streamed(const epub_stream_media_t *media, const char *path, epub_book_t *out_book)
Open an EPUB book from a seekable stream, with no whole-file residency (#151).
Definition epub_open.c:455
ra8_fs -> epub bridge: open a .epub straight off a filesystem.
ra8_err_t epub_open_streamed_fs(ra8_fs_mount_t *mount, const char *path, epub_stream_fs_ctx_t *io, epub_book_t *out_book)
Stream-open an EPUB directly off a mounted ra8_fs volume, no residency (#151).
ra8_err_t epub_close_streamed_fs(epub_stream_fs_ctx_t *io, epub_book_t *book)
Close a book opened by epub_open_streamed_fs() and its source file.
Test-access surface for the EPUB filesystem stream callback.
size_t priv_epub_fs_stream_read(void *ctx, uint64_t offset, void *buf, size_t len)
Read one bounded span from a live streamed-filesystem context.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_null_ptr
Pointer was NULL where a valid pointer was required.
Definition ra8_err.h:478
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
ra8_err_t ra8_fs_size(const ra8_fs_file_t *file, uint64_t *out_bytes)
Report the file's size in bytes (64-bit on exFAT, #676).
ra8_err_t ra8_fs_read(ra8_fs_file_t *file, uint8_t *buf, uint32_t max_len, uint32_t *got_len)
Read up to max_len bytes; advance the cluster chain on cluster crossings.
ra8_err_t ra8_fs_open(ra8_fs_mount_t *handle, const char *path, ra8_fs_mode_t mode, ra8_fs_file_t **out_file)
Open or create a file by path, 8.3 or long.
ra8_err_t ra8_fs_close(ra8_fs_file_t *file)
Close an open file, stamping its final modification time.
ra8_err_t ra8_fs_seek(ra8_fs_file_t *file, uint64_t offset_bytes)
Move the file offset to offset_bytes (clamped to size).
@ k_ra8_fs_mode_read
Read-only, must exist.
Opened EPUB book.
Definition epub.h:286
Backing state for a streamed ra8_fs EPUB open (#151).
Definition epub_fs.h:55
Seekable EPUB media descriptor – opens with no whole-file residency (#151).
Definition epub.h:232
Open-file state.
Cached parse of one mounted FAT volume.