ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
mdl_export_rabook_io.c
Go to the documentation of this file.
1
13
14#include <string.h>
15
17
29
43RA8_INTERNAL static uint32_t internal_temp_seed(const char* destination)
44{
45 uint32_t hash = (uint32_t)k_rabook_fnv_offset;
46 for (size_t index = 0U; destination[index] != '\0'; ++index) {
47 hash ^= (uint8_t)destination[index];
48 hash *= (uint32_t)k_rabook_fnv_prime;
49 }
50 return hash;
51}
52
66RA8_INTERNAL static void internal_temp_leaf(uint32_t value, char leaf[13])
67{
68 static const char k_hex[] = "0123456789ABCDEF";
69 leaf[0] = 'R';
70 for (uint32_t index = 0U; index < (uint32_t)k_rabook_hex_digits; ++index) {
71 const uint32_t shift = ((uint32_t)k_rabook_hex_digits - 1U - index) * 4U;
72 leaf[index + 1U] = k_hex[(value >> shift) & (uint32_t)k_rabook_hex_mask];
73 }
74 (void)memcpy(&leaf[8], ".EPB", (size_t)k_rabook_suffix_bytes);
75}
76
78 mdl_storage_t* storage,
79 const char* directory,
80 const char* destination,
81 char* path,
82 size_t capacity)
83{
84 if ((output == nullptr) || (storage == nullptr) || (directory == nullptr) ||
85 (destination == nullptr) || (path == nullptr) || (capacity == 0U)) {
87 }
88 const uint32_t seed = internal_temp_seed(destination) & (uint32_t)k_rabook_value_mask;
89 for (uint32_t attempt = 0U; attempt < (uint32_t)k_rabook_temp_attempts; ++attempt) {
90 char leaf[13] = {};
91 const uint32_t value = (seed + attempt) & (uint32_t)k_rabook_value_mask;
92 internal_temp_leaf(value, leaf);
93 if (priv_mdl_export_path_join(path, capacity, directory, leaf) != k_ra8_ok) {
95 }
96 const ra8_err_t error =
98 if (error == k_ra8_ok) {
99 return k_ra8_ok;
100 }
101 if (error != k_ra8_err_exists) {
102 return error;
103 }
104 }
105 return k_ra8_err_exists;
106}
107
109 mdl_storage_t* storage,
110 const char* path)
111{
112 if ((source == nullptr) || (storage == nullptr) || (storage->fs == nullptr) ||
113 (path == nullptr)) {
115 }
116 fw_fs_stat_t stat = {};
117 ra8_err_t error = fw_fs_stat(&storage->fs->names, path, &stat);
118 if ((error == k_ra8_ok) &&
119 (!stat.exists || (stat.type != k_fw_fs_node_file) || (stat.size_bytes == 0U))) {
120 error = k_ra8_err_invalid_arg;
121 }
122 *source = (mdl_rabook_epub_source_t){};
123 if (error == k_ra8_ok) {
124 source->size_bytes = stat.size_bytes;
125 error = fw_fs_open(&storage->fs->streams,
126 path,
128 &source->file,
129 storage->file_workspace,
130 storage->file_workspace_bytes);
131 }
132 uint64_t open_size = 0U;
133 if (error == k_ra8_ok) {
134 error = fw_fs_file_size(&source->file, &open_size);
135 }
136 if ((error == k_ra8_ok) && (open_size != source->size_bytes)) {
138 }
139 if ((error != k_ra8_ok) && source->file.is_open) {
140 (void)fw_fs_close(&source->file);
141 }
142 if (error != k_ra8_ok) {
143 *source = (mdl_rabook_epub_source_t){};
144 }
145 return error;
146}
147
149{
150 if ((source == nullptr) || !source->file.is_open) {
152 }
153 const ra8_err_t error = fw_fs_close(&source->file);
154 if (error == k_ra8_ok) {
155 *source = (mdl_rabook_epub_source_t){};
156 }
157 return error;
158}
159
161 uint64_t offset,
162 void* destination,
163 size_t length)
164{
166 if ((source == nullptr) || (destination == nullptr) || !source->file.is_open ||
167 (source->error != k_ra8_ok) || (length > UINT32_MAX) || (offset > source->size_bytes) ||
168 ((uint64_t)length > (source->size_bytes - offset))) {
169 if ((source != nullptr) && (source->error == k_ra8_ok)) {
171 }
172 return 0U;
173 }
174 ra8_err_t error = fw_fs_seek(&source->file, offset);
175 size_t done = 0U;
176 while ((error == k_ra8_ok) && (done < length)) {
177 if (source->calls >= (uint32_t)k_rabook_read_calls) {
179 break;
180 }
181 uint32_t got = 0U;
182 error =
183 fw_fs_read(&source->file, &((uint8_t*)destination)[done], (uint32_t)(length - done), &got);
184 ++source->calls;
185 if ((error == k_ra8_ok) && (got == 0U)) {
187 }
188 done += got;
189 }
190 if (error != k_ra8_ok) {
191 source->error = error;
192 return 0U;
193 }
194 return done;
195}
196
198 uint32_t offset,
199 uint8_t* destination,
200 uint32_t requested,
201 uint32_t* out_read)
202{
204 if (out_read != nullptr) {
205 *out_read = 0U;
206 }
207 if ((source == nullptr) || (destination == nullptr) || (out_read == nullptr)) {
209 }
210 if ((offset > source->size_bytes) || (requested > (source->size_bytes - offset))) {
212 }
213 (void)memcpy(destination, &source->bytes[offset], requested);
214 *out_read = requested;
215 return k_ra8_ok;
216}
ra8_err_t fw_fs_open(const fw_fs_stream_port_t *streams, const char *path, fw_fs_open_mode_t mode, fw_fs_file_t *file, void *workspace, uint32_t workspace_size)
Open a file into a caller-owned handle and backend workspace.
Definition fw_if_fs.c:648
ra8_err_t fw_fs_stat(const fw_fs_namespace_t *names, const char *path, fw_fs_stat_t *out)
Query a path; a miss is success with out->exists == false.
Definition fw_if_fs.c:480
ra8_err_t fw_fs_read(fw_fs_file_t *file, uint8_t *dst, uint32_t cap, uint32_t *out_read)
Read up to cap bytes; zero bytes is EOF.
Definition fw_if_fs.c:699
ra8_err_t fw_fs_close(fw_fs_file_t *file)
Close and consume an open handle.
Definition fw_if_fs.c:783
ra8_err_t fw_fs_seek(fw_fs_file_t *file, uint64_t absolute_offset)
Seek to an absolute byte offset from the beginning.
Definition fw_if_fs.c:736
ra8_err_t fw_fs_file_size(fw_fs_file_t *file, uint64_t *out_size)
Report the open file's current length.
Definition fw_if_fs.c:758
@ k_fw_fs_node_file
Regular byte stream.
@ k_fw_fs_open_read
Existing file, read-only.
ra8_err_t priv_mdl_export_path_join(char *out, size_t capacity, const char *directory, const char *leaf)
Join a canonical directory and leaf without truncation.
ra8_err_t priv_mdl_export_output_begin_new(mdl_export_output_t *output, mdl_storage_t *storage, const char *destination, mdl_format_t format)
Bind a create-new validated publication transaction.
Private bounded RABOOK export contracts for the media downloader.
ra8_err_t priv_mdl_rabook_temp_begin(mdl_export_output_t *output, mdl_storage_t *storage, const char *directory, const char *destination, char *path, size_t capacity)
Reserve one collision-free temporary EPUB publication path.
ra8_err_t priv_mdl_rabook_flat_read(void *opaque, uint32_t offset, uint8_t *destination, uint32_t requested, uint32_t *out_read)
Serve one exact flat-blob read to the RBKC writer.
mdl_rabook_io_limit_t
Bounded I/O and collision-attempt limits.
@ k_rabook_read_calls
EPUB random-read ceiling.
@ k_rabook_value_mask
Low 28 temporary-name bits.
@ k_rabook_fnv_prime
FNV-1a multiplier.
@ k_rabook_fnv_offset
FNV-1a seed for temp names.
@ k_rabook_temp_attempts
Create-new sibling candidates.
@ k_rabook_hex_digits
Hex digits in a temp leaf.
@ k_rabook_hex_mask
One hexadecimal nibble.
@ k_rabook_suffix_bytes
.EPB plus NUL terminator.
size_t priv_mdl_rabook_epub_read(void *opaque, uint64_t offset, void *destination, size_t length)
Serve one exact random EPUB read to the streamed reader.
static void internal_temp_leaf(uint32_t value, char leaf[13])
Render one collision candidate as an 8.3-compatible EPUB leaf.
ra8_err_t priv_mdl_rabook_epub_close(mdl_rabook_epub_source_t *source)
Close one temporary EPUB stream.
static uint32_t internal_temp_seed(const char *destination)
Hash one destination into a stable temporary-name seed.
ra8_err_t priv_mdl_rabook_epub_open(mdl_rabook_epub_source_t *source, mdl_storage_t *storage, const char *path)
Open one validated temporary EPUB as a portable random-read stream.
@ k_mdl_format_epub
Fixed-layout EPUB (.epub).
Definition mdl_format.h:42
#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_out_of_range
Sensor or peripheral output out of valid range.
Definition ra8_err.h:337
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_err_exists
Item already exists – cannot create again.
Definition ra8_err.h:216
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
Definition ra8_err.h:161
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_protocol_error
Protocol-level error (e.g.
Definition ra8_err.h:429
@ 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.
bool is_open
Facade lifecycle guard.
Result of a portable metadata query.
uint64_t size_bytes
File length; zero for a directory.
bool exists
False means a clean lookup miss.
fw_fs_node_type_t type
Kind of node at the path.
fw_fs_stream_port_t streams
Byte-stream operations.
fw_fs_namespace_t names
Namespace operations.
One validated staged publication, including random ZIP backfill.
Open portable EPUB stream retained during one compile.
uint32_t calls
Bounded random-read call tally.
uint64_t size_bytes
Immutable complete EPUB extent.
fw_fs_file_t file
Borrowed portable file handle.
ra8_err_t error
First read/seek protocol failure.
Resident flat RABOOK1 source for the chunked container writer.
const uint8_t * bytes
Immutable flat blob bytes.
uint32_t size_bytes
Complete flat blob extent.
One non-reentrant downloader filesystem dependency bundle.
Definition mdl_storage.h:40
uint32_t file_workspace_bytes
File workspace extent.
Definition mdl_storage.h:45
const fw_fs_t * fs
Injected portable filesystem.
Definition mdl_storage.h:41
void * file_workspace
Open-file backend state.
Definition mdl_storage.h:42