|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Portable bounded structural validators for media artifacts. More...
#include "mdl_verify.h"#include <ctype.h>#include <limits.h>#include <string.h>#include "jof.h"#include "mdl_verify_internal.h"#include "mdl_verify_rabook_internal.h"#include "miniz.h"#include "ra8_attributes.h"Go to the source code of this file.
Data Structures | |
| struct | mdl_alloc_header_t |
| Header stored before each monotonic miniz allocation. More... | |
| struct | mdl_zip_scan_t |
| Semantic markers accumulated while scanning ZIP members. More... | |
Functions | |
| void * | priv_mdl_verify_workspace_take (mdl_export_workspace_t *workspace, size_t bytes, size_t alignment) |
| Reserve one aligned span from the verifier's caller-owned arena. | |
| static bool | internal_ends_ci (const char *text, const char *suffix) |
| Test a suffix without case sensitivity. | |
| bool | priv_mdl_verify_is_image (const char *name) |
| Recognize supported image suffixes. | |
| bool | priv_mdl_verify_safe_member_name (const char *name) |
| Reject unsafe archive member paths. | |
| ra8_err_t | mdl_format_from_path (const char *path, mdl_format_t *out_format) |
| Infer an artifact format from its complete path suffix. | |
| bool | mdl_format_is_verifiable (mdl_format_t format) |
| Report whether a format has an in-process structural validator. | |
| void * | priv_mdl_verify_arena_alloc (void *opaque, size_t items, size_t size) |
| Allocate one aligned miniz span from a monotonic arena. | |
| void | priv_mdl_verify_arena_free (void *opaque, void *address) |
| Accept a miniz free for a monotonic arena. | |
| static void * | internal_arena_realloc (void *opaque, void *address, size_t items, size_t size) |
| Grow a miniz span by copying it to the next arena allocation. | |
| static ra8_err_t | internal_io_open (mdl_storage_t *storage, const char *path, mdl_verify_io_t *io) |
| Open a portable read-only input. | |
| static ra8_err_t | internal_io_close (mdl_verify_io_t *io, ra8_err_t prior) |
| Close an input without masking prior failure. | |
| ra8_err_t | priv_mdl_verify_io_read_up_to (mdl_verify_io_t *io, uint8_t *destination, size_t length, size_t *out_read) |
| Read up to a requested portable span. | |
| static size_t | internal_zip_read (void *opaque, mz_uint64 offset, void *destination, size_t length) |
| Adapt portable reads to miniz. | |
| static size_t | internal_discard_zip (void *opaque, mz_uint64 offset, const void *data, size_t bytes) |
| Discard verified ZIP output. | |
| static ra8_err_t | internal_zip_member (mz_zip_archive *zip, mz_uint index, mdl_zip_scan_t *scan) |
| Validate one ZIP member. | |
| static ra8_err_t | internal_zip_semantics (mdl_format_t format, const mdl_zip_scan_t *scan, mdl_verify_report_t *report) |
| Enforce ZIP format semantics. | |
| static ra8_err_t | internal_verify_zip (mdl_verify_io_t *io, mdl_format_t format, mdl_export_workspace_t *workspace, mdl_verify_report_t *report) |
| Validate a ZIP-backed artifact. | |
| static ra8_err_t | internal_jof_pread (void *opaque, uint64_t offset, uint8_t *destination, size_t length, size_t *got) |
| Adapt JOF positioned reads. | |
| static ra8_err_t | internal_verify_jof (mdl_verify_io_t *io, mdl_verify_report_t *report) |
| Validate a JOF artifact. | |
| static ra8_err_t | internal_verify_borrowed (mdl_verify_io_t *io, mdl_format_t format, mdl_export_workspace_t *workspace, mdl_verify_report_t *report) |
| Dispatch validation over one borrowed input. | |
| ra8_err_t | mdl_verify_open_file (mdl_storage_t *storage, mdl_format_t format, fw_fs_file_t *file, uint64_t size_bytes, mdl_export_workspace_t *workspace, mdl_verify_report_t *report) |
| Validate an artifact through a borrowed open filesystem handle. | |
| ra8_err_t | mdl_verify_file (mdl_storage_t *storage, mdl_format_t format, const char *path, mdl_export_workspace_t *workspace, mdl_verify_report_t *report) |
| Validate a completed artifact using caller-owned scratch only. | |
Portable bounded structural validators for media artifacts.
Reads only through an injected mdl_storage_t. ZIP uses miniz's positioned callback and JOF uses its production pread seam; the incremental TAR and gzip validators live beside this file in mdl_verify_tarball.c.
Definition in file mdl_verify.c.
|
static |
Grow a miniz span by copying it to the next arena allocation.
Definition at line 170 of file mdl_verify.c.
References mdl_alloc_header_t::bytes, memcpy(), and priv_mdl_verify_arena_alloc().
Referenced by internal_verify_zip().
|
static |
Discard verified ZIP output.
Supplies a bounded sink so miniz computes and checks member CRCs.
| [in,out] | opaque | Unused callback context. |
| [in] | offset | Output offset. |
| [in] | data | Decoded bytes. |
| [in] | bytes | Byte count. |
| bytes | Always. |
Definition at line 295 of file mdl_verify.c.
Referenced by internal_zip_member().
|
static |
Test a suffix without case sensitivity.
Compares only the tail of a valid string.
| [in] | text | Candidate string. |
| [in] | suffix | Required suffix. |
| true | On a match. |
Definition at line 62 of file mdl_verify.c.
References RA8_INTERNAL, and strlen().
Referenced by internal_zip_member(), mdl_format_from_path(), and priv_mdl_verify_is_image().
|
static |
Close an input without masking prior failure.
A close error is returned only when prior succeeded.
| [in,out] | io | Open input state. |
| [in] | prior | Earlier operation status. |
| k_ra8_ok | When both stages succeed. |
Definition at line 223 of file mdl_verify.c.
References fw_fs_close(), k_ra8_ok, and RA8_INTERNAL.
Referenced by mdl_verify_file().
|
static |
Open a portable read-only input.
Opens through storage and snapshots the immutable validation size.
| [in] | storage | Bound storage facade. |
| [in] | path | Canonical bound-root path. |
| [in,out] | io | Output state. |
| k_ra8_ok | On success. |
Definition at line 195 of file mdl_verify.c.
References mdl_storage_t::file_workspace, mdl_storage_t::file_workspace_bytes, mdl_storage_t::fs, fw_fs_close(), fw_fs_file_size(), fw_fs_open(), k_fw_fs_open_read, k_ra8_ok, and fw_fs_t::streams.
Referenced by mdl_verify_file().
|
static |
Adapt JOF positioned reads.
Uses portable seek and bounded reads against the size snapshot.
| [in,out] | opaque | Verifier input. |
| [in] | offset | File offset. |
| [out] | destination | Output buffer. |
| [in] | length | Requested bytes. |
| [out] | got | Produced bytes. |
| k_ra8_ok | On data or EOF. |
Definition at line 421 of file mdl_verify.c.
References fw_fs_seek(), k_ra8_ok, priv_mdl_verify_io_read_up_to(), and mdl_verify_io_t::size_bytes.
Referenced by internal_verify_jof().
|
static |
Dispatch validation over one borrowed input.
Keeps format selection separate from ownership.
| [in,out] | io | Borrowed verifier input. |
| [in] | format | Expected format. |
| [in,out] | workspace | Scratch arena. |
| [in,out] | report | Candidate report. |
| k_ra8_ok | For a valid artifact. |
Definition at line 464 of file mdl_verify.c.
References internal_verify_jof(), internal_verify_zip(), k_mdl_format_cbr, k_mdl_format_cbt, k_mdl_format_cbt_gz, k_mdl_format_cbt_xz, k_mdl_format_cbz, k_mdl_format_epub, k_mdl_format_invalid, k_mdl_format_jof, k_mdl_format_loose, k_mdl_format_rabook, k_ra8_err_invalid_arg, k_ra8_err_not_supported, priv_mdl_verify_gzip_tar(), priv_mdl_verify_rabook(), priv_mdl_verify_tar(), and RA8_INTERNAL.
Referenced by mdl_verify_open_file().
|
static |
Validate a JOF artifact.
Invokes the production parser through the injected positioned-read adapter.
| [in,out] | io | Borrowed input. |
| [in,out] | report | Candidate report. |
| k_ra8_ok | For valid JOF. |
Definition at line 442 of file mdl_verify.c.
References internal_jof_pread(), jof_parse(), k_ra8_err_validation_failed, k_ra8_ok, RA8_INTERNAL, and jof_info_t::tile_count.
Referenced by internal_verify_borrowed().
|
static |
Validate a ZIP-backed artifact.
Runs miniz over borrowed reads and a caller-owned arena.
| [in,out] | io | Borrowed input. |
| [in] | format | CBZ or EPUB. |
| [in,out] | workspace | Scratch arena. |
| [in,out] | report | Candidate report. |
| k_ra8_ok | For a valid archive. |
Definition at line 376 of file mdl_verify.c.
References mdl_verify_arena_t::exhausted, internal_arena_realloc(), internal_zip_member(), internal_zip_read(), internal_zip_semantics(), k_ra8_err_invalid_size, k_ra8_err_validation_failed, k_ra8_ok, k_verify_member_max, priv_mdl_verify_arena_alloc(), priv_mdl_verify_arena_free(), and RA8_INTERNAL.
Referenced by internal_verify_borrowed().
|
static |
Validate one ZIP member.
Checks bounded safe names and forces CRC-checked extraction.
| [in,out] | zip | Open miniz archive. |
| [in] | index | Member index. |
| [in,out] | scan | Semantic accumulator. |
| k_ra8_ok | For a safe valid member. |
Definition at line 321 of file mdl_verify.c.
References internal_discard_zip(), internal_ends_ci(), k_ra8_err_validation_failed, k_ra8_ok, priv_mdl_verify_is_image(), priv_mdl_verify_safe_member_name(), and strcmp().
Referenced by internal_verify_zip().
|
static |
Adapt portable reads to miniz.
Seeks and fills the bounded random-read request.
| [in,out] | opaque | Verifier input. |
| [in] | offset | File offset. |
| [out] | destination | Output bytes. |
| [in] | length | Requested bytes. |
| 0 | On failure. |
Definition at line 264 of file mdl_verify.c.
References fw_fs_seek(), k_ra8_err_validation_failed, k_ra8_ok, priv_mdl_verify_io_read_up_to(), and mdl_verify_io_t::size_bytes.
Referenced by internal_verify_zip().
|
static |
Enforce ZIP format semantics.
Distinguishes CBZ image policy from EPUB mimetype requirements.
| [in] | format | Requested format. |
| [in,out] | scan | Completed scan. |
| [in,out] | report | Candidate report. |
| k_ra8_ok | When required members exist. |
Definition at line 352 of file mdl_verify.c.
References k_mdl_format_cbz, k_mdl_format_epub, k_ra8_err_validation_failed, and k_ra8_ok.
Referenced by internal_verify_zip().
| ra8_err_t mdl_format_from_path | ( | const char * | path, |
| mdl_format_t * | out_format ) |
Infer an artifact format from its complete path suffix.
Matches the complete, case-insensitive suffix so multi-dot formats such as .cbt.gz are not misclassified by their last extension.
| [in] | path | NUL-terminated artifact path to classify. |
| [out] | out_format | Receives the recognized format or invalid sentinel. |
| k_ra8_ok | A supported artifact suffix was recognized. |
| k_ra8_err_invalid_arg | Either pointer is NULL. |
| k_ra8_err_not_supported | The suffix is not supported. |
path, when non-NULL, is NUL-terminated. out_format, when non-NULL, addresses writable storage. out_format. < Complete artifact suffix.
< Corresponding format.
Definition at line 107 of file mdl_verify.c.
References internal_ends_ci(), k_mdl_format_cbt, k_mdl_format_cbt_gz, k_mdl_format_cbz, k_mdl_format_epub, k_mdl_format_invalid, k_mdl_format_jof, k_mdl_format_rabook, k_ra8_err_invalid_arg, k_ra8_err_not_supported, and k_ra8_ok.
Referenced by internal_verify_artifact_entry(), and mdl_app_run_artifact().
| bool mdl_format_is_verifiable | ( | mdl_format_t | format | ) |
Report whether a format has an in-process structural validator.
Distinguishes advertised native formats from reserved enum values whose readers or writers are not yet exposed by this host tool.
| [in] | format | Format enum value to query. |
| true | The format can be structurally validated in process. |
| false | The format is invalid, loose, or currently unsupported. |
format is represented by mdl_format_t. Definition at line 131 of file mdl_verify.c.
References k_mdl_format_cbt, k_mdl_format_cbt_gz, k_mdl_format_cbz, k_mdl_format_epub, k_mdl_format_jof, and k_mdl_format_rabook.
Referenced by internal_verify_artifact_entry(), mdl_app_run_artifact(), priv_mdl_export_output_begin(), and priv_mdl_export_output_begin_new().
| ra8_err_t mdl_verify_file | ( | mdl_storage_t * | storage, |
| mdl_format_t | format, | ||
| const char * | path, | ||
| mdl_export_workspace_t * | workspace, | ||
| mdl_verify_report_t * | report ) |
Validate a completed artifact using caller-owned scratch only.
Dispatches to the format-specific ZIP, tar, gzip, JOF, or strict RBKC reader, rejects unsafe member paths and missing required metadata, and resets the workspace so high_water describes this call alone. ZIP and JOF use positioned reads; TAR and gzip are streamed through bounded chunks, so no complete compressed or decoded archive is retained. Every opened stream is closed before return.
| [in,out] | storage | Injected filesystem and exclusive file workspace. |
| [in] | format | Expected artifact format. |
| [in] | path | NUL-terminated path to the completed artifact. |
| [in,out] | workspace | Caller-owned bounded validation workspace. |
| [out] | report | Structural counts populated only on success. |
| k_ra8_ok | The artifact is structurally valid for format. |
| k_ra8_err_invalid_arg | A pointer, workspace, or format is invalid. |
| k_ra8_err_invalid_size | The caller workspace is too small. |
| k_ra8_err_validation_failed | Container structure or metadata is bad. |
| k_ra8_err_not_supported | The reserved format has no validator. |
| other | A filesystem open/read/seek/size/close failure was propagated. |
path is canonical, NUL-terminated, and names a stable completed file. storage, workspace, and report are exclusive to this call. report contains format, member, page, and metadata data. report retains its entry value. Definition at line 520 of file mdl_verify.c.
References mdl_export_workspace::data, mdl_storage_t::file_workspace, mdl_storage_t::file_workspace_bytes, mdl_storage_t::fs, mdl_export_workspace::high_water, internal_io_close(), internal_io_open(), mdl_storage_t::io_buffer, mdl_storage_t::io_buffer_bytes, k_ra8_err_invalid_arg, k_ra8_ok, mdl_verify_open_file(), and mdl_export_workspace::used.
Referenced by internal_verify_artifact_entry(), and mdl_app_run_artifact().
| ra8_err_t mdl_verify_open_file | ( | mdl_storage_t * | storage, |
| mdl_format_t | format, | ||
| fw_fs_file_t * | file, | ||
| uint64_t | size_bytes, | ||
| mdl_export_workspace_t * | workspace, | ||
| mdl_verify_report_t * | report ) |
Validate an artifact through a borrowed open filesystem handle.
Dispatches to the same ZIP, tar, gzip, JOF, or strict RBKC validator used by mdl_verify_file after seeking file to offset zero. This entry point lets an exporter validate a staged transaction before commit without publishing or reopening the stage by name. The handle is borrowed: this function never closes it, and its final offset is unspecified. The size is an immutable caller-supplied snapshot.
| [in,out] | storage | Injected storage buffers used by streaming readers. |
| [in] | format | Expected artifact format. |
| [in,out] | file | Borrowed readable and seekable open handle. |
| [in] | size_bytes | Stable artifact extent in bytes. |
| [in,out] | workspace | Caller-owned bounded validation workspace. |
| [out] | report | Structural counts populated only on success. |
| k_ra8_ok | The staged artifact is structurally valid for format. |
| k_ra8_err_invalid_arg | A pointer, workspace, or format is invalid. |
| k_ra8_err_invalid_size | The caller workspace is too small. |
| k_ra8_err_validation_failed | Container structure or metadata is bad. |
| k_ra8_err_not_supported | The reserved format has no validator. |
| other | A seek or read failure was propagated. |
file remains exclusively borrowed for the complete call. size_bytes is the stable size of the staged artifact. file remains open and owned by the caller. report retains its entry value. Definition at line 491 of file mdl_verify.c.
References mdl_export_workspace::data, fw_fs_seek(), mdl_export_workspace::high_water, internal_verify_borrowed(), mdl_storage_t::io_buffer, mdl_storage_t::io_buffer_bytes, k_ra8_err_invalid_arg, k_ra8_ok, and mdl_export_workspace::used.
Referenced by internal_validate_stage(), and mdl_verify_file().
| void * priv_mdl_verify_arena_alloc | ( | void * | opaque, |
| size_t | items, | ||
| size_t | size ) |
Allocate one aligned miniz span from a monotonic arena.
Prefixes each payload with a bounded header recording its extent so a later reallocation can copy exactly the preserved bytes.
| [in,out] | opaque | Bound mdl_verify_arena_t callback context. |
| [in] | items | Element count requested by miniz. |
| [in] | size | Element size requested by miniz. |
| NULL | The product overflowed or the arena is exhausted. |
| other | One writable payload of items times size bytes. |
opaque addresses one arena bound to a reset workspace. Definition at line 138 of file mdl_verify.c.
References mdl_alloc_header_t::bytes, mdl_verify_arena_t::exhausted, priv_mdl_verify_workspace_take(), RA8_PRIV, and mdl_verify_arena_t::workspace.
Referenced by internal_arena_realloc(), internal_gzip_init_inflate(), and internal_verify_zip().
| void priv_mdl_verify_arena_free | ( | void * | opaque, |
| void * | address ) |
Accept a miniz free for a monotonic arena.
Individual allocations remain reserved until the owning verifier resets the workspace, so this release has nothing to reclaim.
| [in,out] | opaque | Borrowed arena context. |
| [in] | address | Allocation being released. |
opaque is the callback context handed to miniz. address is NULL or was produced by priv_mdl_verify_arena_alloc. address is not accessed. Definition at line 162 of file mdl_verify.c.
References RA8_PRIV.
Referenced by internal_gzip_init_inflate(), and internal_verify_zip().
| ra8_err_t priv_mdl_verify_io_read_up_to | ( | mdl_verify_io_t * | io, |
| uint8_t * | destination, | ||
| size_t | length, | ||
| size_t * | out_read ) |
Read up to a requested portable span.
Repeats short reads and stops only at end of file or on failure.
| [in,out] | io | Open verifier input. |
| [out] | destination | Writable destination buffer. |
| [in] | length | Maximum byte count to transfer. |
| [out] | out_read | Byte count actually produced. |
| k_ra8_ok | Data or a clean end of file was observed. |
| other | The portable read failed; out_read holds the prefix. |
io is open and destination spans length bytes. out_read is non-NULL and does not alias destination. out_read never exceeds length. Definition at line 233 of file mdl_verify.c.
References fw_fs_read(), k_ra8_ok, and RA8_PRIV.
Referenced by internal_io_read_exact(), internal_jof_pread(), internal_zip_read(), and priv_mdl_verify_tar().
| bool priv_mdl_verify_is_image | ( | const char * | name | ) |
Recognize supported image suffixes.
Applies the archive image allowlist case-insensitively.
| [in] | name | Archive member name. |
| true | The suffix is one of the supported image extensions. |
| false | No supported suffix matched. |
name is a NUL-terminated string. name remains valid for the call. name is unchanged. Definition at line 79 of file mdl_verify.c.
References internal_ends_ci(), and RA8_PRIV.
Referenced by internal_tar_member(), and internal_zip_member().
| bool priv_mdl_verify_safe_member_name | ( | const char * | name | ) |
Reject unsafe archive member paths.
Rejects absolute, empty, dot, parent, and backslash segments.
| [in] | name | Archive member name, or NULL. |
| true | The name is a safe relative path. |
| false | The name is NULL, absolute, or escapes its root. |
name is NULL or NUL-terminated. name is unchanged. Definition at line 86 of file mdl_verify.c.
References RA8_PRIV.
Referenced by internal_tar_member(), and internal_zip_member().
| void * priv_mdl_verify_workspace_take | ( | mdl_export_workspace_t * | workspace, |
| size_t | bytes, | ||
| size_t | alignment ) |
Reserve one aligned span from the verifier's caller-owned arena.
Rounds the bump cursor up to alignment, refuses a request that would leave the arena, and records the resulting high-water mark.
| [in,out] | workspace | Exclusive caller-owned bump arena. |
| [in] | bytes | Nonzero span extent to reserve. |
| [in] | alignment | Power-of-two alignment required by the caller. |
| NULL | The request was malformed or the arena is exhausted. |
| other | One writable span of bytes bytes inside the arena. |
workspace is non-NULL and owns writable arena storage. alignment is a nonzero power of two. Definition at line 30 of file mdl_verify.c.
References mdl_export_workspace::cap, mdl_export_workspace::data, mdl_export_workspace::high_water, memcpy(), and mdl_export_workspace::used.
Referenced by internal_gzip_init_inflate(), and priv_mdl_verify_arena_alloc().