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

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"
Include dependency graph for mdl_urlname.c:

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.

Detailed Description

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.

Enumeration Type Documentation

◆ mdl_chapter_num_limit_t

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.

◆ mdl_image_signature_byte_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.

◆ mdl_image_signature_offset_t

Signature byte offsets not already exempted by the numeric policy.

Enumerator
k_webp_sig_bytes 

RIFF header plus WEBP form type.

k_webp_e_offset 

'E' in the WEBP form type.

k_webp_b_offset 

'B' in the WEBP form type.

k_webp_p_offset 

'P' in the WEBP form type.

k_gif_a_offset 

'a' trailer in GIF87a/GIF89a.

Definition at line 41 of file mdl_urlname.c.

◆ mdl_urlname_const_t

enum mdl_urlname_const_t : uint16_t

On-stack buffers and parse radix for the URL-name helpers.

Enumerator
k_urlname_raw_bytes 

Pre-sanitise last-segment scratch bytes.

k_urlname_case_gap 

'a' - 'A': ASCII upper-to-lower offset.

k_urlname_scan_max 

Maximum URL bytes inspected.

k_urlname_magic_bytes 

Largest file-signature prefix consumed.

k_urlname_read_calls 

One-byte short reads plus clean EOF.

Definition at line 22 of file mdl_urlname.c.

Function Documentation

◆ internal_copy_image_type()

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 )
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.

Parameters
[in]extNUL-terminated canonical extension.
[in]mimeNUL-terminated canonical MIME type.
[out]out_extOptional extension output buffer.
[in]ext_capCapacity of out_ext in bytes.
[out]out_mimeOptional MIME output buffer.
[in]mime_capCapacity of out_mime in bytes.
Precondition
ext and mime are non-NULL and NUL-terminated.
Each non-NULL output references its declared writable capacity.
Postcondition
Every nonempty output is NUL-terminated within its capacity.
No input or output pointer is retained or transferred.
Note
Truncation is deterministic and confined to the affected output.
Since
0.1.0

Definition at line 508 of file mdl_urlname.c.

References internal_urlname_copy(), and RA8_INTERNAL.

Referenced by mdl_urlname_sniff_image_type().

◆ internal_sniff_content_type()

bool internal_sniff_content_type ( const char * content_type,
const char ** ext,
const char ** mime )
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.

Parameters
[in]content_typeNUL-terminated response Content-Type value.
[out]extReceives a borrowed canonical extension pointer.
[out]mimeReceives a borrowed canonical MIME pointer.
Returns
Whether a supported image media type was recognized.
Return values
trueext and mime were initialized consistently.
falseThe value was missing, malformed, or unsupported.
Precondition
ext and mime are non-NULL.
content_type, when non-NULL, is NUL-terminated.
Postcondition
Success initializes both outputs to process-lifetime constants.
The supplied header text and its ownership remain unchanged.
Note
Magic-byte classification takes precedence at the public seam.
Since
0.1.0

Definition at line 459 of file mdl_urlname.c.

References internal_urlname_to_lower_ascii(), and strstr().

Referenced by mdl_urlname_sniff_image_type().

◆ internal_sniff_magic_image()

bool internal_sniff_magic_image ( const void * buf,
size_t buf_len,
const char ** ext,
const char ** mime )
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.

Parameters
[in]bufReadable response prefix.
[in]buf_lenNumber of readable bytes at buf.
[out]extReceives a borrowed canonical extension pointer.
[out]mimeReceives a borrowed canonical MIME pointer.
Returns
Whether a complete supported signature was recognized.
Return values
trueext and mime were initialized consistently.
falseNo supported complete signature was present.
Precondition
ext and mime are non-NULL.
buf is non-NULL when buf_len is nonzero.
Postcondition
Success initializes both outputs to process-lifetime constants.
Input bytes and caller ownership remain unchanged.
Note
Partial signatures are rejected rather than guessed.
Since
0.1.0

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().

◆ internal_urlname_copy()

void internal_urlname_copy ( char * out,
size_t capacity,
const char * value )
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.

Parameters
[out]outOptional output buffer.
[in]capacityWritable output bytes.
[in]valueNUL-terminated source value.
Precondition
value is non-NULL and NUL-terminated.
A non-NULL out addresses capacity writable bytes.
Postcondition
A nonempty output is NUL-terminated, with deterministic truncation.
Source bytes are unchanged and no pointer is retained.
Note
Thread-safe across disjoint output buffers.
Since
0.1.0

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().

◆ internal_urlname_is_known_ext()

bool internal_urlname_is_known_ext ( const char * ext)
static

True when ext is one of the accepted raster image extensions.

Compares against the fixed lower-case extension allowlist.

Parameters
[in]extNUL-terminated lower-case extension without a dot.
Returns
Whether the extension is accepted.
Return values
trueThe extension is a supported raster type.
falseIt is absent from the allowlist.
Precondition
ext is non-NULL and NUL-terminated.
The caller has already normalised ASCII case.
Postcondition
ext is unchanged.
No state is modified.
Note
Thread-safe: reads constant storage.
Since
0.1.0

Definition at line 314 of file mdl_urlname.c.

References RA8_INTERNAL, and strcmp().

Referenced by mdl_urlname_ext().

◆ internal_urlname_last_path_marker()

const char * internal_urlname_last_path_marker ( const char * url,
size_t begin,
size_t end,
const char * marker )
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().

◆ internal_urlname_parse_chapter_digits()

bool internal_urlname_parse_chapter_digits ( const char * start,
const char * end,
double * out,
const char ** out_next )
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.

Parameters
[in]startFirst candidate digit.
[in]endOne-past-last byte available to the parser.
[out]outParsed non-negative chapter value on success.
[out]out_nextOptional first unconsumed byte on success.
Returns
Whether a bounded chapter value was parsed.
Return values
trueAt least one digit was parsed without exceeding a bound.
falseInput did not start with a digit or exceeded a numeric bound.
Precondition
start and end delimit one readable contiguous range.
out points to writable storage for one double.
Postcondition
On true, out contains the parsed non-negative value and a non-NULL out_next receives the first unconsumed byte.
On false, out is left unchanged.
Note
Bytes following the recognised numeric run are intentionally ignored.
Since
0.1.0

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().

◆ internal_urlname_path_end()

size_t internal_urlname_path_end ( const char * url)
static

End offset of a URL's path, before any ?query/#fragment.

Finds the first query or fragment delimiter, otherwise uses string length.

Parameters
[in]urlNUL-terminated URL text.
Returns
Exclusive path-end offset.
Return values
otherA value from zero through strlen(url).
Precondition
url is non-NULL and NUL-terminated.
Query and fragment delimiters terminate the path for naming.
Postcondition
url is unchanged.
The return never exceeds the URL length.
Note
Thread-safe: reads only its argument.
Since
0.1.0

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().

◆ internal_urlname_path_start()

size_t internal_urlname_path_start ( const char * url,
size_t end )
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.

Parameters
[in]urlNUL-terminated URL containing at least end readable bytes.
[in]endExclusive end offset of the path scan.
Returns
Offset of the first path byte within url.
Return values
0url is relative or has no scheme separator before end.
otherThe first path slash, or end when an authority has no path.
Precondition
url is non-NULL and NUL-terminated.
end does not exceed the URL length.
Postcondition
The return value is at most end.
url is never modified.
Note
Query and fragment removal is handled by the caller when choosing end.
Since
0.1.0

Definition at line 120 of file mdl_urlname.c.

References memchr(), RA8_INTERNAL, strlen(), and strstr().

Referenced by mdl_urlname_chapter_parse().

◆ internal_urlname_to_lower_ascii()

char internal_urlname_to_lower_ascii ( char c)
static

Lower-case an ASCII byte.

Maps uppercase ASCII letters and preserves every other byte.

Parameters
[in]cCharacter to map.
Returns
Lower-case ASCII equivalent.
Return values
otherMapped or unchanged character.
Precondition
c is representable as char.
Locale-independent folding is required.
Postcondition
No state is modified.
Non-uppercase input is unchanged.
Note
Thread-safe: pure arithmetic.
Since
0.1.0

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().

◆ mdl_urlname_chapter_number()

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.

Parameters
[in]urlURL to parse (never NULL).
Returns
The parsed chapter number, or 0 when the URL holds no digits.
Return values
0No decimal digit appears in url.
Precondition
url is non-NULL and NUL-terminated.
The caller treats 0 as "unnumbered", not "chapter zero" specifically.
Postcondition
url is not modified.
The result is the integral truncation of mdl_urlname_chapter_value.
Note
Thread-safe: depends only on its argument.
Since
0.1.0

Definition at line 276 of file mdl_urlname.c.

References mdl_urlname_chapter_value().

◆ mdl_urlname_chapter_parse()

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.

Parameters
[in]urlAbsolute or relative URL to inspect.
[out]outParsed non-negative value on success.
Returns
Whether an explicitly marked chapter value was parsed.
Return values
trueAn explicit, bounded chapter value was found.
falseArguments were invalid, no marker/value was present, or the value exceeded the supported bound.
Precondition
A non-NULL url is NUL-terminated within the documented URL bound.
A non-NULL out addresses writable storage for one double.
Postcondition
On false, out is set to 0.0 when it is non-NULL.
url is never modified.
Note
Thread-safe: uses only caller-owned storage.
Since
0.1.0

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().

◆ mdl_urlname_chapter_text_parse()

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.

Parameters
[in]textNUL-terminated selector text to parse.
[out]outParsed non-negative value on success.
Returns
Whether all non-whitespace input formed one bounded chapter value.
Return values
trueThe complete trimmed input was accepted.
falseArguments were invalid or the text was not a strict value.
Precondition
A non-NULL text is NUL-terminated within the URL-name scan bound.
A non-NULL out addresses writable storage for one double.
Postcondition
On false, a non-NULL out is set to 0.0.
text is never modified.
Note
Thread-safe and allocation-free.
Since
0.1.0

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().

◆ mdl_urlname_chapter_value()

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.

Parameters
[in]urlURL to parse; may be NULL.
Returns
Parsed chapter value, or 0.0 for an unnumbered URL. New callers that must distinguish an absent number from chapter zero use mdl_urlname_chapter_parse.
Return values
0.0No explicit bounded chapter marker was found.
otherThe non-negative integral or decimal chapter value.
Precondition
A non-NULL url is NUL-terminated within the documented URL bound.
A NULL url is accepted and treated as unnumbered.
Postcondition
The return value is non-negative.
url is never modified.
Note
Thread-safe: uses only caller-owned storage.
Since
0.1.0

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().

◆ mdl_urlname_ext()

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.

Parameters
[in]urlURL whose last segment carries the extension (never NULL).
[out]outDestination buffer for the NUL-terminated extension.
[in]capCapacity of out in bytes (must be >= 5 for "jpeg").
Returns
Nothing.
Precondition
url and out are non-NULL; url is NUL-terminated.
cap is at least 5 so the longest accepted extension fits.
Postcondition
out is a NUL-terminated, lower-case, known extension (default jpg).
out contains no ?/# bytes from a query or fragment.
Note
Thread-safe: writes only caller-provided storage.
Since
0.1.0

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().

◆ mdl_urlname_last_segment()

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.

Parameters
[in]urlAbsolute URL to take the last segment of (never NULL).
[out]outDestination buffer for the sanitised segment (never NULL).
[in]capCapacity of out in bytes (must be >= 2).
Returns
Nothing.
Precondition
url and out are non-NULL; url is NUL-terminated.
cap is at least 2 so a one-character name plus a NUL fits.
Postcondition
out is NUL-terminated and holds no /, .-only, or .. result.
out is non-empty (a generated fallback when the segment was empty).
Note
Thread-safe: writes only caller-provided storage.
See also
mdl_sanitize_segment
Since
0.1.0

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().

◆ mdl_urlname_sniff_file()

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 )
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.

Parameters
[in,out]storageInitialized filesystem binding and exclusive file workspace.
[in]file_pathCanonical path beneath the bound filesystem root.
[in]content_typeHTTP Content-Type header string (may be NULL or empty).
[out]out_extDestination buffer for lower-case extension (e.g. "jpg"). May be NULL.
[in]ext_capCapacity of out_ext in bytes.
[out]out_mimeDestination buffer for exact MIME type (e.g. "image/jpeg"). May be NULL.
[in]mime_capCapacity of out_mime in bytes.
Returns
Canonical storage or classification status.
Return values
k_ra8_okFile magic or the HTTP content type identifies a supported image.
k_ra8_err_validation_failedClean EOF yielded no supported classification.
k_ra8_err_invalid_argThe storage binding or path is invalid.
k_ra8_err_invalid_sizeThe backend did not reach the bounded prefix in time.
otherAn open, read, or close error propagated unchanged.
Precondition
storage was initialized by mdl_storage_init and is exclusively owned.
file_path is a NUL-terminated canonical portable path.
Each non-NULL output points to writable storage of its corresponding capacity.
Postcondition
On success, each non-empty requested output receives a NUL-terminated canonical value, truncated when its capacity is too small.
On failure requested outputs are unchanged; any opened file is consumed.
Note
A recognised content type may supply the result when file magic is inconclusive.
Since
0.1.0

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().

◆ mdl_urlname_sniff_image_type()

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:

  • JPEG: FF D8 FF -> .jpg / image/jpeg
  • PNG: 89 50 4E 47 -> .png / image/png
  • WebP: RIFF....WEBP -> .webp / image/webp
  • GIF: GIF87a / GIF89a -> .gif / image/gif
  • BMP: BM -> .bmp / image/bmp
Parameters
[in]bufData buffer holding raw magic bytes (may be NULL if buf_len == 0).
[in]buf_lenLength of buf in bytes.
[in]content_typeHTTP Content-Type header string (may be NULL or empty).
[out]out_extDestination buffer for lower-case extension (e.g. "jpg"). May be NULL.
[in]ext_capCapacity of out_ext in bytes.
[out]out_mimeDestination buffer for exact MIME type (e.g. "image/jpeg"). May be NULL.
[in]mime_capCapacity of out_mime in bytes.
Returns
Whether a supported image type was recognised.
Return values
trueMagic bytes or the HTTP content type identify a supported image.
falseNeither input identifies a supported image.
Precondition
buf is readable for buf_len bytes when buf_len is non-zero.
Each non-NULL output points to writable storage of its corresponding capacity.
Postcondition
On true, each non-empty requested output receives a NUL-terminated canonical value, truncated when its capacity is too small.
On false, requested output buffers are left unchanged.
Note
Magic bytes take precedence over a conflicting HTTP content type.
Since
0.1.0

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().

Variable Documentation

◆ s_chapter_fraction_step

const double s_chapter_fraction_step = 0.1
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().