ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
mdl_hash.c File Reference

FNV-1a 64 content hashing over an injected portable filesystem. More...

#include "mdl_hash.h"
#include <stdint.h>
#include <string.h>
#include "ra8_attributes.h"
Include dependency graph for mdl_hash.c:

Go to the source code of this file.

Enumerations

enum  mdl_hash_bound_t : uint64_t { k_hash_max_read_calls = 2000001ULL }
 Exact regular-file bound accepted by mdl_hash_file. More...

Functions

uint64_t mdl_hash_bytes_seed (const void *data, size_t len, uint64_t seed)
 Continue an FNV-1a 64 fold over a byte range from a running state.
uint64_t mdl_hash_bytes (const void *data, size_t len)
 FNV-1a 64 hash of a byte range.
uint64_t mdl_hash_str (const char *s)
 FNV-1a 64 hash of a NUL-terminated string (excluding the NUL).
ra8_err_t mdl_hash_stream (fw_fs_file_t *file, uint64_t file_size, uint8_t *buffer, uint32_t buffer_bytes, uint64_t *out)
 Fold an exact regular-file extent into a running FNV state.
ra8_err_t mdl_hash_file (mdl_storage_t *storage, const char *path, uint64_t *out)
 FNV-1a 64 hash of a file's full contents.

Detailed Description

FNV-1a 64 content hashing over an injected portable filesystem.

Provides deterministic buffer and streamed-file hashing with bounded caller-owned storage and no host file objects.

Definition in file mdl_hash.c.

Enumeration Type Documentation

◆ mdl_hash_bound_t

enum mdl_hash_bound_t : uint64_t

Exact regular-file bound accepted by mdl_hash_file.

Enumerator
k_hash_max_read_calls 

Short-read + EOF call ceiling.

Definition at line 17 of file mdl_hash.c.

Function Documentation

◆ mdl_hash_bytes()

uint64_t mdl_hash_bytes ( const void * data,
size_t len )

FNV-1a 64 hash of a byte range.

A single-shot fold of data starting from k_mdl_fnv_offset (it delegates to mdl_hash_bytes_seed with that seed). An empty range (len == 0) hashes to the offset basis, and a NULL data with a non-zero len is treated as empty rather than dereferenced.

Parameters
[in]dataFirst byte of the range, or NULL for an empty range.
[in]lenNumber of bytes to fold.
Returns
The 64-bit FNV-1a digest.
Return values
k_mdl_fnv_offsetWhen len is 0 or data is NULL.
Precondition
data, when non-NULL, addresses at least len readable bytes.
The caller uses the result only as an identity key, not a MAC.
Postcondition
No argument is modified.
Note
Thread-safe: depends only on its arguments.
See also
mdl_hash_bytes_seed
Since
0.1.0
Postcondition
Documented outputs and the return value describe the same outcome.

Definition at line 35 of file mdl_hash.c.

References k_mdl_fnv_offset, and mdl_hash_bytes_seed().

Referenced by internal_cache_publish(), mdl_hash_str(), and priv_mdl_cache_read_body().

◆ mdl_hash_bytes_seed()

uint64_t mdl_hash_bytes_seed ( const void * data,
size_t len,
uint64_t seed )

Continue an FNV-1a 64 fold over a byte range from a running state.

The streaming primitive: folds data into seed and returns the new running digest, so a large file can be hashed chunk by chunk with no dynamic allocation. Passing k_mdl_fnv_offset as seed for the first chunk makes a chunked hash identical to a single-shot mdl_hash_bytes of the whole range.

Parameters
[in]dataFirst byte of the chunk, or NULL for an empty chunk.
[in]lenNumber of bytes to fold.
[in]seedRunning digest so far (k_mdl_fnv_offset to start fresh).
Returns
The digest after folding data into seed.
Return values
seedWhen len is 0 or data is NULL (nothing to fold).
Precondition
data, when non-NULL, addresses at least len readable bytes.
seed is either k_mdl_fnv_offset or a prior result of this function.
Postcondition
No argument is modified.
Note
Thread-safe: depends only on its arguments.
Since
0.1.0
Postcondition
Documented outputs and the return value describe the same outcome.

Definition at line 21 of file mdl_hash.c.

References k_mdl_fnv_prime.

Referenced by internal_build_export_metadata(), internal_cache_payload_identity(), internal_cache_read_record(), internal_copy_payload(), mdl_hash_bytes(), mdl_hash_stream(), mdl_storage_txn_write(), and priv_mdl_export_zip_read().

◆ mdl_hash_file()

ra8_err_t mdl_hash_file ( mdl_storage_t * storage,
const char * path,
uint64_t * out )

FNV-1a 64 hash of a file's full contents.

Queries path through the injected portable filesystem, rejects non-regular objects before reading, then hashes a regular file up to the implementation's 8.192 GB safety limit through bounded reads and caller-owned scratch. The size is snapshotted before reading; short input, a trailing byte from a concurrent append, or an exhausted read-call ceiling fails rather than publishing a partial-prefix digest. Used to verify a page already on disk still matches the identity recorded in state (a torn file re-hashes to a different value and is refetched) and to record the identity of a freshly fetched page.

Parameters
[in,out]storageInitialized filesystem binding and file workspace.
[in]pathCanonical portable file path (never NULL).
[out]outReceives the 64-bit digest on success (never NULL).
Returns
An ra8_err_t result.
Return values
k_ra8_okFile read fully and hashed; *out is set.
k_ra8_err_invalid_argpath or out was NULL, or the path was a symlink or non-regular object.
k_ra8_err_invalid_sizeThe regular file exceeded the safety bound.
k_ra8_err_not_foundThe path or a path component did not exist.
k_ra8_err_access_deniedThe process lacked permission to open it.
k_ra8_failOther open/stat/read/close failure.
Precondition
storage, path, and out are non-NULL.
The bound adapter refuses symbolic-link traversal.
path directly names a readable regular file for success.
Postcondition
*out is written only on k_ra8_ok.
The file position/contents are not modified.
Note
Not thread-safe against concurrent writers of the same file.
See also
mdl_hash_bytes
Since
0.1.0

Definition at line 103 of file mdl_hash.c.

References fw_fs_stat_t::exists, mdl_storage_t::file_workspace, mdl_storage_t::file_workspace_bytes, mdl_storage_t::fs, fw_fs_close(), fw_fs_open(), fw_fs_stat(), mdl_storage_t::io_buffer, mdl_storage_t::io_buffer_bytes, k_fw_fs_node_file, k_fw_fs_open_read, k_mdl_hash_max_file_bytes, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_not_found, k_ra8_ok, mdl_hash_stream(), fw_fs_t::names, fw_fs_stat_t::size_bytes, fw_fs_t::streams, and fw_fs_stat_t::type.

Referenced by internal_mdl_fetch_try_reuse(), and internal_verify_page_rec().

◆ mdl_hash_str()

uint64_t mdl_hash_str ( const char * s)

FNV-1a 64 hash of a NUL-terminated string (excluding the NUL).

The dedup key for a page's source URL: two runs that scrape the same URL produce the same key, so a page already held is found without re-fetching.

Parameters
[in]sString to hash, or NULL.
Returns
The 64-bit FNV-1a digest of s's bytes.
Return values
k_mdl_fnv_offsetWhen s is NULL or empty.
Precondition
s, when non-NULL, is NUL-terminated.
The caller treats the result as an identity key only.
Postcondition
s is not modified.
Note
Thread-safe: depends only on its argument.
See also
mdl_hash_bytes
Since
0.1.0
Postcondition
Documented outputs and the return value describe the same outcome.

Definition at line 40 of file mdl_hash.c.

References k_mdl_fnv_offset, mdl_hash_bytes(), and strlen().

Referenced by internal_build_export_metadata(), internal_cache_paths(), internal_cache_prepare(), internal_cache_publish(), internal_cache_record_valid(), internal_mdl_fetch_one_page(), internal_mdl_fetch_prepare_page(), internal_mdl_fetch_publish_page(), and internal_mdl_fetch_resolve_not_modified().

◆ mdl_hash_stream()

ra8_err_t mdl_hash_stream ( fw_fs_file_t * file,
uint64_t file_size,
uint8_t * buffer,
uint32_t buffer_bytes,
uint64_t * out )

Fold an exact regular-file extent into a running FNV state.

Hash exactly one snapshotted extent from an already-open stream.

Parameters
[in,out]fileOpen generic file positioned at byte zero.
[in]file_sizeImmutable size snapshot obtained from fw_fs_stat.
[out]bufferCaller-owned read scratch.
[in]buffer_bytesNonzero extent of buffer.
[out]outDigest written only after exact EOF validation.
Returns
Canonical stream or content-stability status.
Precondition
file is a valid open regular file and out is non-NULL.
file_size is at most k_mdl_hash_max_file_bytes.
Postcondition
Success advances file through EOF and writes out.
Failure leaves out untouched.
Note
Not thread-safe against a concurrent writer of the same file.
Since
0.1.0

Definition at line 63 of file mdl_hash.c.

References fw_fs_read(), k_hash_max_read_calls, k_mdl_fnv_offset, k_mdl_hash_max_file_bytes, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_fail, k_ra8_ok, and mdl_hash_bytes_seed().

Referenced by internal_validate_stage(), mdl_hash_file(), and priv_mdl_export_source_verify_close().