|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Bounded URL naming and portable image-type readers. More...
#include "mdl_urlname.h"#include <stdint.h>#include <string.h>#include "mdl_sanitize.h"#include "ra8_attributes.h"Go to the source code of this file.
Enumerations | |
| enum | mdl_urlname_const_t : uint16_t { k_urlname_raw_bytes = 256 , k_urlname_case_gap = 32 , k_urlname_scan_max = 2048 , k_urlname_magic_bytes = 16 , k_urlname_read_calls = 17 } |
| On-stack buffers and parse radix for the URL-name helpers. More... | |
| enum | mdl_chapter_num_limit_t : uint32_t { k_chapter_whole_max = 999999999U , k_chapter_frac_max = 6U , k_chapter_radix = 10U } |
| Numeric bounds keep conversion loops and sortable values finite. More... | |
| enum | mdl_image_signature_offset_t : uint8_t { k_webp_sig_bytes = 12U , k_webp_e_offset = 9U , k_webp_b_offset = 10U , k_webp_p_offset = 11U , k_gif_a_offset = 5U } |
| Signature byte offsets not already exempted by the numeric policy. More... | |
| enum | mdl_image_signature_byte_t : uint8_t { k_jpeg_marker_byte = 0xFFU , k_jpeg_soi_byte = 0xD8U , k_png_lead_byte = 0x89U } |
| Non-ASCII fixed bytes used by the JPEG and PNG signatures. More... | |
Functions | |
| static size_t | internal_urlname_path_end (const char *url) |
| End offset of a URL's path, before any ?query/#fragment. | |
| void | mdl_urlname_last_segment (const char *url, char *out, size_t cap) |
| Sanitised last non-empty path segment of a URL. | |
| static size_t | internal_urlname_path_start (const char *url, size_t end) |
| Start offset of the path, excluding any URL scheme and authority. | |
| static const char * | internal_urlname_last_path_marker (const char *url, size_t begin, size_t end, const char *marker) |
| Locate the last chapter marker wholly inside [begin,end). | |
| static bool | internal_urlname_parse_chapter_digits (const char *start, const char *end, double *out, const char **out_next) |
| Parse digits at start, optionally followed by a decimal fraction. | |
| bool | mdl_urlname_chapter_parse (const char *url, double *out) |
| Parse an explicitly-marked integral or decimal chapter value. | |
| bool | mdl_urlname_chapter_text_parse (const char *text, double *out) |
| Parse a selector result as one complete bounded chapter number. | |
| double | mdl_urlname_chapter_value (const char *url) |
| Parse a chapter URL as a possibly-decimal chapter value. | |
| long | mdl_urlname_chapter_number (const char *url) |
| Parse a chapter URL's integral chapter number. | |
| static char | internal_urlname_to_lower_ascii (char c) |
| Lower-case an ASCII byte. | |
| static bool | internal_urlname_is_known_ext (const char *ext) |
| True when ext is one of the accepted raster image extensions. | |
| static void | internal_urlname_copy (char *out, size_t capacity, const char *value) |
| Copy one NUL-terminated value into bounded caller storage. | |
| void | mdl_urlname_ext (const char *url, char *out, size_t cap) |
| Choose a lower-case image file extension from a URL. | |
| static bool | internal_sniff_magic_image (const void *buf, size_t buf_len, const char **ext, const char **mime) |
| Classify a supported image from its leading magic bytes. | |
| static bool | internal_sniff_content_type (const char *content_type, const char **ext, const char **mime) |
| Classify a supported image from an HTTP Content-Type value. | |
| static void | internal_copy_image_type (const char *ext, const char *mime, char *out_ext, size_t ext_cap, char *out_mime, size_t mime_cap) |
| Copy one canonical image classification to bounded outputs. | |
| bool | mdl_urlname_sniff_image_type (const void *buf, size_t buf_len, const char *content_type, char *out_ext, size_t ext_cap, char *out_mime, size_t mime_cap) |
| Sniff true image extension and MIME type from magic bytes and/or HTTP Content-Type. | |
| ra8_err_t | mdl_urlname_sniff_file (mdl_storage_t *storage, const char *file_path, const char *content_type, char *out_ext, size_t ext_cap, char *out_mime, size_t mime_cap) |
| Sniff true image extension and MIME type from portable storage. | |
Variables | |
| static const double | s_chapter_fraction_step = 0.1 |
| Initial decimal place multiplier for a fractional chapter. | |
Bounded URL naming and portable image-type readers.
Converts bounded URL path components into safe display and file names, recognises explicit chapter markers, and identifies supported image types from signatures or content type. All outputs use caller-owned fixed buffers and no helper allocates memory. File signatures are read only through the injected downloader storage binding.
Definition in file mdl_urlname.c.
| enum mdl_chapter_num_limit_t : uint32_t |
Numeric bounds keep conversion loops and sortable values finite.
| Enumerator | |
|---|---|
| k_chapter_whole_max | Largest accepted integral chapter. |
| k_chapter_frac_max | Fractional digits retained. |
| k_chapter_radix | Decimal chapter-number radix. |
Definition at line 31 of file mdl_urlname.c.
| enum mdl_image_signature_byte_t : uint8_t |
Non-ASCII fixed bytes used by the JPEG and PNG signatures.
| Enumerator | |
|---|---|
| k_jpeg_marker_byte | JPEG marker prefix. |
| k_jpeg_soi_byte | JPEG Start Of Image marker. |
| k_png_lead_byte | PNG signature lead byte. |
Definition at line 50 of file mdl_urlname.c.
| enum mdl_image_signature_offset_t : uint8_t |
Signature byte offsets not already exempted by the numeric policy.
Definition at line 41 of file mdl_urlname.c.
| enum mdl_urlname_const_t : uint16_t |
On-stack buffers and parse radix for the URL-name helpers.
Definition at line 22 of file mdl_urlname.c.
|
static |
Copy one canonical image classification to bounded outputs.
Copies extension and MIME independently only when the corresponding output buffer is present and nonempty, always preserving termination.
| [in] | ext | NUL-terminated canonical extension. |
| [in] | mime | NUL-terminated canonical MIME type. |
| [out] | out_ext | Optional extension output buffer. |
| [in] | ext_cap | Capacity of out_ext in bytes. |
| [out] | out_mime | Optional MIME output buffer. |
| [in] | mime_cap | Capacity of out_mime in bytes. |
ext and mime are non-NULL and NUL-terminated. Definition at line 508 of file mdl_urlname.c.
References internal_urlname_copy(), and RA8_INTERNAL.
Referenced by mdl_urlname_sniff_image_type().
|
static |
Classify a supported image from an HTTP Content-Type value.
Compares the media type case-insensitively while accepting bounded parameters after ;, then returns the canonical extension/MIME pair.
| [in] | content_type | NUL-terminated response Content-Type value. |
| [out] | ext | Receives a borrowed canonical extension pointer. |
| [out] | mime | Receives a borrowed canonical MIME pointer. |
| true | ext and mime were initialized consistently. |
| false | The value was missing, malformed, or unsupported. |
ext and mime are non-NULL. content_type, when non-NULL, is NUL-terminated. Definition at line 459 of file mdl_urlname.c.
References internal_urlname_to_lower_ascii(), and strstr().
Referenced by mdl_urlname_sniff_image_type().
|
static |
Classify a supported image from its leading magic bytes.
Checks bounded JPEG, PNG, GIF, WebP, and AVIF signatures in a fixed order and returns immutable canonical extension and MIME strings.
| [in] | buf | Readable response prefix. |
| [in] | buf_len | Number of readable bytes at buf. |
| [out] | ext | Receives a borrowed canonical extension pointer. |
| [out] | mime | Receives a borrowed canonical MIME pointer. |
| true | ext and mime were initialized consistently. |
| false | No supported complete signature was present. |
ext and mime are non-NULL. buf is non-NULL when buf_len is nonzero. Definition at line 402 of file mdl_urlname.c.
References k_gif_a_offset, k_jpeg_marker_byte, k_jpeg_soi_byte, k_png_lead_byte, k_webp_b_offset, k_webp_e_offset, k_webp_p_offset, and k_webp_sig_bytes.
Referenced by mdl_urlname_sniff_image_type().
|
static |
Copy one NUL-terminated value into bounded caller storage.
Copies at most capacity - 1 bytes and always terminates a writable, nonempty destination without consulting host stream state.
| [out] | out | Optional output buffer. |
| [in] | capacity | Writable output bytes. |
| [in] | value | NUL-terminated source value. |
value is non-NULL and NUL-terminated. out addresses capacity writable bytes. Definition at line 339 of file mdl_urlname.c.
References memcpy(), RA8_INTERNAL, and strnlen().
Referenced by internal_copy_image_type(), and mdl_urlname_ext().
|
static |
True when ext is one of the accepted raster image extensions.
Compares against the fixed lower-case extension allowlist.
| [in] | ext | NUL-terminated lower-case extension without a dot. |
| true | The extension is a supported raster type. |
| false | It is absent from the allowlist. |
ext is non-NULL and NUL-terminated. ext is unchanged. Definition at line 314 of file mdl_urlname.c.
References RA8_INTERNAL, and strcmp().
Referenced by mdl_urlname_ext().
|
static |
Locate the last chapter marker wholly inside [begin,end).
Definition at line 133 of file mdl_urlname.c.
References memcmp(), and strlen().
Referenced by mdl_urlname_chapter_parse().
|
static |
Parse digits at start, optionally followed by a decimal fraction.
Accumulates a bounded integral value, then accepts . or - as a fractional separator when followed by digits. More than the supported number of fractional digits or an overflowing integral component is rejected.
| [in] | start | First candidate digit. |
| [in] | end | One-past-last byte available to the parser. |
| [out] | out | Parsed non-negative chapter value on success. |
| [out] | out_next | Optional first unconsumed byte on success. |
| true | At least one digit was parsed without exceeding a bound. |
| false | Input did not start with a digit or exceeded a numeric bound. |
start and end delimit one readable contiguous range. out points to writable storage for one double. out contains the parsed non-negative value and a non-NULL out_next receives the first unconsumed byte. out is left unchanged. Definition at line 170 of file mdl_urlname.c.
References k_chapter_frac_max, k_chapter_radix, k_chapter_whole_max, RA8_INTERNAL, and s_chapter_fraction_step.
Referenced by mdl_urlname_chapter_parse(), and mdl_urlname_chapter_text_parse().
|
static |
End offset of a URL's path, before any ?query/#fragment.
Finds the first query or fragment delimiter, otherwise uses string length.
| [in] | url | NUL-terminated URL text. |
| other | A value from zero through strlen(url). |
url is non-NULL and NUL-terminated. url is unchanged. Definition at line 69 of file mdl_urlname.c.
References RA8_INTERNAL, and strlen().
Referenced by mdl_urlname_chapter_parse(), mdl_urlname_ext(), and mdl_urlname_last_segment().
|
static |
Start offset of the path, excluding any URL scheme and authority.
Relative URLs begin at zero. For an absolute URL, the first slash after :// begins the path; when no slash exists, the supplied end offset is returned so authority digits cannot be mistaken for a chapter.
| [in] | url | NUL-terminated URL containing at least end readable bytes. |
| [in] | end | Exclusive end offset of the path scan. |
url. | 0 | url is relative or has no scheme separator before end. |
| other | The first path slash, or end when an authority has no path. |
url is non-NULL and NUL-terminated. end does not exceed the URL length. end. url is never modified. end. Definition at line 120 of file mdl_urlname.c.
References memchr(), RA8_INTERNAL, strlen(), and strstr().
Referenced by mdl_urlname_chapter_parse().
|
static |
Lower-case an ASCII byte.
Maps uppercase ASCII letters and preserves every other byte.
| [in] | c | Character to map. |
| other | Mapped or unchanged character. |
c is representable as char. Definition at line 294 of file mdl_urlname.c.
References k_urlname_case_gap, and RA8_INTERNAL.
Referenced by internal_sniff_content_type(), and mdl_urlname_ext().
| long mdl_urlname_chapter_number | ( | const char * | url | ) |
Parse a chapter URL's integral chapter number.
Recognises chapter slugs such as chapter-137, chapter-108-5, and ch-5. Decimal slugs return their integral part here; use mdl_urlname_chapter_value when ordering decimal chapters.
| [in] | url | URL to parse (never NULL). |
| 0 | No decimal digit appears in url. |
url is non-NULL and NUL-terminated. url is not modified. Definition at line 276 of file mdl_urlname.c.
References mdl_urlname_chapter_value().
| bool mdl_urlname_chapter_parse | ( | const char * | url, |
| double * | out ) |
Parse an explicitly-marked integral or decimal chapter value.
Recognises chapter-N, /ch-N, and Pepper&Carrot-style /epN markers only within the URL path. Host digits, opaque trailing IDs, query parameters, and fragments are never treated as chapter numbers. A hyphen between the integral and fractional runs is interpreted as a decimal point, so chapter-108-5 is 108.5.
| [in] | url | Absolute or relative URL to inspect. |
| [out] | out | Parsed non-negative value on success. |
| true | An explicit, bounded chapter value was found. |
| false | Arguments were invalid, no marker/value was present, or the value exceeded the supported bound. |
url is NUL-terminated within the documented URL bound. out addresses writable storage for one double. out is set to 0.0 when it is non-NULL. url is never modified.Definition at line 210 of file mdl_urlname.c.
References internal_urlname_last_path_marker(), internal_urlname_parse_chapter_digits(), internal_urlname_path_end(), internal_urlname_path_start(), k_urlname_scan_max, strlen(), and strnlen().
Referenced by internal_list_range(), internal_mdl_fetch_process_chapter(), internal_select_window(), internal_sort_by_chapter_num(), and mdl_urlname_chapter_value().
| bool mdl_urlname_chapter_text_parse | ( | const char * | text, |
| double * | out ) |
Parse a selector result as one complete bounded chapter number.
Trims ASCII whitespace, then accepts only a non-negative decimal value. Both 108.5 and the URL-style fractional spelling 108-5 represent chapter 108.5. Signs, exponent notation, labels, NaN/infinity, trailing bytes, excessive precision, and values above the documented chapter bound are rejected.
| [in] | text | NUL-terminated selector text to parse. |
| [out] | out | Parsed non-negative value on success. |
| true | The complete trimmed input was accepted. |
| false | Arguments were invalid or the text was not a strict value. |
text is NUL-terminated within the URL-name scan bound. out addresses writable storage for one double. out is set to 0.0. text is never modified. Definition at line 238 of file mdl_urlname.c.
References internal_urlname_parse_chapter_digits(), k_urlname_scan_max, and strnlen().
Referenced by internal_mdl_fetch_select_chapter_number().
| double mdl_urlname_chapter_value | ( | const char * | url | ) |
Parse a chapter URL as a possibly-decimal chapter value.
A site slug such as chapter-108-5 represents chapter 108.5, not chapter 5. This helper recognises that convention and ordinary dotted decimals while ignoring unrelated digits in the host or earlier path.
| [in] | url | URL to parse; may be NULL. |
| 0.0 | No explicit bounded chapter marker was found. |
| other | The non-negative integral or decimal chapter value. |
url is NUL-terminated within the documented URL bound. url is accepted and treated as unnumbered. url is never modified.Definition at line 269 of file mdl_urlname.c.
References mdl_urlname_chapter_parse().
Referenced by internal_sort_by_chapter_num(), and mdl_urlname_chapter_number().
| void mdl_urlname_ext | ( | const char * | url, |
| char * | out, | ||
| size_t | cap ) |
Choose a lower-case image file extension from a URL.
Reads the extension of the URL's last path segment, lower-cases it, and accepts it only if it is a known raster type (jpg, jpeg, png, gif, webp, bmp); anything else – including no extension – yields jpg. Used to name a downloaded page file and to name the copy made when a byte-identical page is reused from another chapter.
| [in] | url | URL whose last segment carries the extension (never NULL). |
| [out] | out | Destination buffer for the NUL-terminated extension. |
| [in] | cap | Capacity of out in bytes (must be >= 5 for "jpeg"). |
url and out are non-NULL; url is NUL-terminated. cap is at least 5 so the longest accepted extension fits. out is a NUL-terminated, lower-case, known extension (default jpg). out contains no ?/# bytes from a query or fragment.Definition at line 349 of file mdl_urlname.c.
References internal_urlname_copy(), internal_urlname_is_known_ext(), internal_urlname_path_end(), and internal_urlname_to_lower_ascii().
Referenced by internal_mdl_fetch_page_leaf().
| void mdl_urlname_last_segment | ( | const char * | url, |
| char * | out, | ||
| size_t | cap ) |
Sanitised last non-empty path segment of a URL.
Strips any ?query/#fragment and trailing slashes, takes the final path segment, and passes it through mdl_sanitize_segment so the result is a single filesystem-safe name. Used for the series slug, per-chapter folder names, and the stable chapter identifier recorded in library state – an identifier derived from the URL path rather than the chapter's position in a freshly scraped list, so it stays fixed as the site adds or reorders chapters.
| [in] | url | Absolute URL to take the last segment of (never NULL). |
| [out] | out | Destination buffer for the sanitised segment (never NULL). |
| [in] | cap | Capacity of out in bytes (must be >= 2). |
url and out are non-NULL; url is NUL-terminated. cap is at least 2 so a one-character name plus a NUL fits. out is NUL-terminated and holds no /, .-only, or .. result. out is non-empty (a generated fallback when the segment was empty).Definition at line 75 of file mdl_urlname.c.
References internal_urlname_path_end(), k_urlname_raw_bytes, and mdl_sanitize_segment().
Referenced by internal_export_fresh_separate(), internal_extract_series_metadata(), internal_mdl_fetch_process_chapter(), internal_resolve_removal_target(), mdl_app_run_artifact(), and priv_mdl_app_prepare_series_dir().
|
nodiscard |
Sniff true image extension and MIME type from portable storage.
Reads only the bounded signature prefix needed by mdl_urlname_sniff_image_type, then applies the same magic-first type selection. The file is closed before this function returns.
| [in,out] | storage | Initialized filesystem binding and exclusive file workspace. |
| [in] | file_path | Canonical path beneath the bound filesystem root. |
| [in] | content_type | HTTP Content-Type header string (may be NULL or empty). |
| [out] | out_ext | Destination buffer for lower-case extension (e.g. "jpg"). May be NULL. |
| [in] | ext_cap | Capacity of out_ext in bytes. |
| [out] | out_mime | Destination buffer for exact MIME type (e.g. "image/jpeg"). May be NULL. |
| [in] | mime_cap | Capacity of out_mime in bytes. |
| k_ra8_ok | File magic or the HTTP content type identifies a supported image. |
| k_ra8_err_validation_failed | Clean EOF yielded no supported classification. |
| k_ra8_err_invalid_arg | The storage binding or path is invalid. |
| k_ra8_err_invalid_size | The backend did not reach the bounded prefix in time. |
| other | An open, read, or close error propagated unchanged. |
storage was initialized by mdl_storage_init and is exclusively owned. file_path is a NUL-terminated canonical portable path. Definition at line 540 of file mdl_urlname.c.
References mdl_storage_t::file_workspace, mdl_storage_t::file_workspace_bytes, mdl_storage_t::fs, fw_fs_close(), fw_fs_open(), fw_fs_read(), k_fw_fs_open_read, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_validation_failed, k_ra8_ok, k_urlname_magic_bytes, k_urlname_read_calls, mdl_urlname_sniff_image_type(), and fw_fs_t::streams.
Referenced by internal_cover_current(), internal_epub_media_type_from_sniff(), and priv_mdl_export_prepare_cover().
| bool mdl_urlname_sniff_image_type | ( | const void * | buf, |
| size_t | buf_len, | ||
| const char * | content_type, | ||
| char * | out_ext, | ||
| size_t | ext_cap, | ||
| char * | out_mime, | ||
| size_t | mime_cap ) |
Sniff true image extension and MIME type from magic bytes and/or HTTP Content-Type.
Inspects HTTP Content-Type header and magic bytes of buffer data:
| [in] | buf | Data buffer holding raw magic bytes (may be NULL if buf_len == 0). |
| [in] | buf_len | Length of buf in bytes. |
| [in] | content_type | HTTP Content-Type header string (may be NULL or empty). |
| [out] | out_ext | Destination buffer for lower-case extension (e.g. "jpg"). May be NULL. |
| [in] | ext_cap | Capacity of out_ext in bytes. |
| [out] | out_mime | Destination buffer for exact MIME type (e.g. "image/jpeg"). May be NULL. |
| [in] | mime_cap | Capacity of out_mime in bytes. |
| true | Magic bytes or the HTTP content type identify a supported image. |
| false | Neither input identifies a supported image. |
buf is readable for buf_len bytes when buf_len is non-zero. Definition at line 523 of file mdl_urlname.c.
References internal_copy_image_type(), internal_sniff_content_type(), and internal_sniff_magic_image().
Referenced by internal_body_begin(), and mdl_urlname_sniff_file().
|
static |
Initial decimal place multiplier for a fractional chapter.
Definition at line 38 of file mdl_urlname.c.
Referenced by internal_urlname_parse_chapter_digits().