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

Bounded image/anchor URL scanning and relative-URL resolution. More...

#include "mdl_extract.h"
#include <stdio.h>
#include <string.h>
#include "ra8_attributes.h"
#include "mdl_extract_internal.h"
Include dependency graph for mdl_extract.c:

Go to the source code of this file.

Enumerations

enum  mdl_scan_limits_t : uint16_t { k_authority_max = 512 }
 Local scan/resolve limits. More...

Functions

static bool internal_authority_of (const char *base, char *auth, size_t cap)
 Extract "scheme://host" from base into auth (NUL-terminated).
static bool internal_resolve_scheme_rel (const char *base, const char *raw, char *out, size_t out_cap)
 Resolve a scheme-relative ("//host/...") URL against base.
static bool internal_resolve_root_rel (const char *auth, const char *raw, char *out, size_t out_cap)
 Resolve a root-relative ("/path") URL against authority auth.
static bool internal_resolve_path_rel (const char *base, const char *auth, const char *raw, char *out, size_t out_cap)
 Resolve a path-relative URL against base (authority auth).
bool mdl_extract_resolve_url (const char *base, const char *raw, char *out, size_t out_cap)
 Resolve raw (possibly relative) against base into out.
static bool internal_already_have (const mdl_url_list_t *list, const char *url)
 True if url is already present in list.
static ra8_err_t internal_emit_tag_url (const char *base_url, const char *attr1, const char *attr2, const char *keep, const char *tag, size_t tag_len, mdl_url_list_t *out)
 Read attr1 (or attr2) from one tag, resolve+filter, append.
static ra8_err_t internal_scan_tags (const char *html, size_t html_len, const char *base_url, const char *tagname, const char *attr1, const char *attr2, const char *keep, mdl_url_list_t *out)
 Shared scanner for <img>/<a>: emit each tag's resolved URL.
ra8_err_t mdl_extract_images (const char *html, size_t html_len, const char *base_url, const char *prefer_attr, const char *url_contains, mdl_url_list_t *out)
 Scan html for <img> image URLs, resolved to absolute form.
ra8_err_t mdl_extract_anchors (const char *html, size_t html_len, const char *base_url, const char *href_contains, mdl_url_list_t *out)
 Scan html for <a href> links, resolved to absolute form.

Detailed Description

Bounded image/anchor URL scanning and relative-URL resolution.

Resolves absolute and relative URL forms, then scans bounded image or anchor tags into caller-owned URL lists. Titled-anchor extraction is isolated in mdl_extract_hits.c.

Definition in file mdl_extract.c.

Enumeration Type Documentation

◆ mdl_scan_limits_t

enum mdl_scan_limits_t : uint16_t

Local scan/resolve limits.

Enumerator
k_authority_max 

Max bytes of "scheme://host" prefix.

Definition at line 18 of file mdl_extract.c.

Function Documentation

◆ internal_already_have()

bool internal_already_have ( const mdl_url_list_t * list,
const char * url )
static

True if url is already present in list.

Scans caller-supplied markup and URLs without allocation. Only fully resolved values that fit bounded result storage are appended.

Parameters
[in]listExisting bounded URL list.
[in]urlNUL-terminated URL input.
Returns
True when url already occurs in list.
Return values
trueThe documented predicate holds or the requested operation completed.
falseThe predicate does not hold or validation rejected the operation.
Precondition
Every required pointer is non-null and remains valid for the call.
Lengths and capacities describe complete referenced objects without overflow.
Postcondition
Documented outputs and the return value describe the same outcome.
A rejected or failed operation is never reported as successful.
Note
Thread safety follows ownership of the supplied context; no synchronization is added.
Since
Version 0.1.0

Definition at line 208 of file mdl_extract.c.

References mdl_url_list_t::count, RA8_INTERNAL, strcmp(), and mdl_url_list_t::urls.

Referenced by internal_emit_tag_url().

◆ internal_authority_of()

bool internal_authority_of ( const char * base,
char * auth,
size_t cap )
static

Extract "scheme://host" from base into auth (NUL-terminated).

Scans caller-supplied markup and URLs without allocation. Only fully resolved values that fit bounded result storage are appended.

Parameters
[in]baseNUL-terminated base URL or path.
[in,out]authBounded authority string.
[in]capDestination capacity including any terminator.
Returns
True when a complete authority fits in auth; otherwise false.
Return values
trueThe documented predicate holds or the requested operation completed.
falseThe predicate does not hold or validation rejected the operation.
Precondition
Every required pointer is non-null and remains valid for the call.
Lengths and capacities describe complete referenced objects without overflow.
Postcondition
Documented outputs and the return value describe the same outcome.
A rejected or failed operation is never reported as successful.
Note
Thread safety follows ownership of the supplied context; no synchronization is added.
Since
Version 0.1.0

Definition at line 40 of file mdl_extract.c.

References memcpy(), RA8_INTERNAL, strchr(), strlen(), and strstr().

Referenced by mdl_extract_resolve_url().

◆ internal_emit_tag_url()

ra8_err_t internal_emit_tag_url ( const char * base_url,
const char * attr1,
const char * attr2,
const char * keep,
const char * tag,
size_t tag_len,
mdl_url_list_t * out )
static

Read attr1 (or attr2) from one tag, resolve+filter, append.

Return values
k_ra8_okAppended, filtered out, or attr absent (all non-fatal).
k_ra8_err_no_memList already at k_mdl_max_urls.

Scans caller-supplied markup and URLs without allocation. Only fully resolved values that fit bounded result storage are appended.

Parameters
[in]base_urlNUL-terminated base URL.
[in]attr1Preferred attribute name.
[in]attr2Fallback attribute name, if any.
[in]keepOptional substring filter.
[in]tagReadable tag byte span.
[in]tag_lenReadable tag length.
[out]outCaller-owned result storage.
Returns
Operation status.
Precondition
Every required pointer is non-null and remains valid for the call.
Lengths and capacities describe complete referenced objects without overflow.
Postcondition
Documented outputs and the return value describe the same outcome.
A rejected or failed operation is never reported as successful.
Note
Thread safety follows ownership of the supplied context; no synchronization is added.
Since
Version 0.1.0

Definition at line 240 of file mdl_extract.c.

References abs(), mdl_url_list_t::count, internal_already_have(), internal_contains_ok(), internal_copy_fits(), internal_find_attr_value(), k_mdl_max_urls, k_mdl_url_max, k_ra8_err_no_mem, k_ra8_ok, mdl_extract_resolve_url(), RA8_INTERNAL, and mdl_url_list_t::urls.

Referenced by internal_scan_tags().

◆ internal_resolve_path_rel()

bool internal_resolve_path_rel ( const char * base,
const char * auth,
const char * raw,
char * out,
size_t out_cap )
static

Resolve a path-relative URL against base (authority auth).

Scans caller-supplied markup and URLs without allocation. Only fully resolved values that fit bounded result storage are appended.

Parameters
[in]baseNUL-terminated base URL or path.
[in]authBounded authority string.
[in]rawUntrusted source text to validate or resolve.
[out]outCaller-owned result storage.
[in]out_capDestination capacity including any terminator.
Returns
True when the path-relative URL resolves completely into out.
Return values
trueThe documented predicate holds or the requested operation completed.
falseThe predicate does not hold or validation rejected the operation.
Precondition
Every required pointer is non-null and remains valid for the call.
Lengths and capacities describe complete referenced objects without overflow.
Postcondition
Documented outputs and the return value describe the same outcome.
A rejected or failed operation is never reported as successful.
Note
Thread safety follows ownership of the supplied context; no synchronization is added.
Since
Version 0.1.0

Definition at line 134 of file mdl_extract.c.

References memcpy(), RA8_INTERNAL, and strlen().

Referenced by mdl_extract_resolve_url().

◆ internal_resolve_root_rel()

bool internal_resolve_root_rel ( const char * auth,
const char * raw,
char * out,
size_t out_cap )
static

Resolve a root-relative ("/path") URL against authority auth.

Scans caller-supplied markup and URLs without allocation. Only fully resolved values that fit bounded result storage are appended.

Parameters
[in]authBounded authority string.
[in]rawUntrusted source text to validate or resolve.
[out]outCaller-owned result storage.
[in]out_capDestination capacity including any terminator.
Returns
True when the root-relative URL resolves completely into out.
Return values
trueThe documented predicate holds or the requested operation completed.
falseThe predicate does not hold or validation rejected the operation.
Precondition
Every required pointer is non-null and remains valid for the call.
Lengths and capacities describe complete referenced objects without overflow.
Postcondition
Documented outputs and the return value describe the same outcome.
A rejected or failed operation is never reported as successful.
Note
Thread safety follows ownership of the supplied context; no synchronization is added.
Since
Version 0.1.0

Definition at line 106 of file mdl_extract.c.

Referenced by mdl_extract_resolve_url().

◆ internal_resolve_scheme_rel()

bool internal_resolve_scheme_rel ( const char * base,
const char * raw,
char * out,
size_t out_cap )
static

Resolve a scheme-relative ("//host/...") URL against base.

Scans caller-supplied markup and URLs without allocation. Only fully resolved values that fit bounded result storage are appended.

Parameters
[in]baseNUL-terminated base URL or path.
[in]rawUntrusted source text to validate or resolve.
[out]outCaller-owned result storage.
[in]out_capDestination capacity including any terminator.
Returns
True when the scheme-relative URL resolves completely into out.
Return values
trueThe documented predicate holds or the requested operation completed.
falseThe predicate does not hold or validation rejected the operation.
Precondition
Every required pointer is non-null and remains valid for the call.
Lengths and capacities describe complete referenced objects without overflow.
Postcondition
Documented outputs and the return value describe the same outcome.
A rejected or failed operation is never reported as successful.
Note
Thread safety follows ownership of the supplied context; no synchronization is added.
Since
Version 0.1.0

Definition at line 75 of file mdl_extract.c.

References memcpy(), strlen(), and strstr().

Referenced by mdl_extract_resolve_url().

◆ internal_scan_tags()

ra8_err_t internal_scan_tags ( const char * html,
size_t html_len,
const char * base_url,
const char * tagname,
const char * attr1,
const char * attr2,
const char * keep,
mdl_url_list_t * out )
static

Shared scanner for <img>/<a>: emit each tag's resolved URL.

Scans caller-supplied markup and URLs without allocation. Only fully resolved values that fit bounded result storage are appended.

Parameters
[in]htmlReadable markup bytes.
[in]html_lenReadable markup length.
[in]base_urlNUL-terminated base URL.
[in]tagnameTag-name prefix to scan for.
[in]attr1Preferred attribute name.
[in]attr2Fallback attribute name, if any.
[in]keepOptional substring filter.
[out]outCaller-owned result storage.
Returns
Operation status.
Return values
k_ra8_okThe operation completed successfully.
otherThe originating validation, storage, stream, or network error.
Precondition
Every required pointer is non-null and remains valid for the call.
Lengths and capacities describe complete referenced objects without overflow.
Postcondition
Documented outputs and the return value describe the same outcome.
A rejected or failed operation is never reported as successful.
Note
Thread safety follows ownership of the supplied context; no synchronization is added.
Since
Version 0.1.0

Definition at line 292 of file mdl_extract.c.

References mdl_url_list_t::count, internal_emit_tag_url(), internal_find_ci(), internal_is_name_end(), k_ra8_err_invalid_arg, k_ra8_err_no_mem, k_ra8_ok, memchr(), RA8_INTERNAL, and strlen().

Referenced by mdl_extract_anchors(), and mdl_extract_images().

◆ mdl_extract_anchors()

ra8_err_t mdl_extract_anchors ( const char * html,
size_t html_len,
const char * base_url,
const char * href_contains,
mdl_url_list_t * out )

Scan html for <a href> links, resolved to absolute form.

Parameters
[in]htmlHTML bytes (need not be NUL-terminated).
[in]html_lenLength of html in bytes.
[in]base_urlAbsolute URL of the page.
[in]href_containsIf non-NULL and non-empty, keep only hrefs whose absolute URL contains this substring.
[out]outList to fill; out->count is reset first.
Return values
k_ra8_okScan complete (count may be 0).
k_ra8_err_invalid_argNULL argument.
k_ra8_err_no_memReached k_mdl_max_urls; remainder skipped.

Uses caller-owned fixed-capacity results and performs no allocation. Inputs stay borrowed and appended values are complete and terminated.

Returns
Operation status.
Precondition
Every required pointer is non-null and remains valid for the call.
Lengths and capacities describe complete referenced objects without overflow.
Postcondition
Documented outputs and the return value describe the same outcome.
A rejected or failed operation is never reported as successful.
Note
Thread safety follows ownership of the supplied context; no synchronization is added.
Since
Version 0.1.0

Definition at line 342 of file mdl_extract.c.

References internal_scan_tags().

Referenced by priv_mdl_app_prepare_chapters().

◆ mdl_extract_images()

ra8_err_t mdl_extract_images ( const char * html,
size_t html_len,
const char * base_url,
const char * prefer_attr,
const char * url_contains,
mdl_url_list_t * out )

Scan html for <img> image URLs, resolved to absolute form.

Parameters
[in]htmlHTML bytes (need not be NUL-terminated).
[in]html_lenLength of html in bytes.
[in]base_urlAbsolute URL of the page (for relative resolution).
[in]prefer_attr"data-src" or "src"; the other is tried as fallback.
[in]url_containsIf non-NULL and non-empty, keep only URLs that contain this substring (drops loaders/ads/nav icons).
[out]outList to fill; out->count is reset first.
Return values
k_ra8_okScan complete (count may be 0).
k_ra8_err_invalid_argNULL argument.
k_ra8_err_no_memReached k_mdl_max_urls; remainder skipped.
Note
Duplicate URLs are dropped. Values longer than k_mdl_url_max are skipped rather than truncated.

Uses caller-owned fixed-capacity results and performs no allocation. Inputs stay borrowed and appended values are complete and terminated.

Returns
Operation status.
Precondition
Every required pointer is non-null and remains valid for the call.
Lengths and capacities describe complete referenced objects without overflow.
Postcondition
Documented outputs and the return value describe the same outcome.
A rejected or failed operation is never reported as successful.
Since
Version 0.1.0

Definition at line 330 of file mdl_extract.c.

References internal_scan_tags(), and strcmp().

Referenced by internal_extract_page_images(), and internal_mdl_fetch_chapter_html().

◆ mdl_extract_resolve_url()

bool mdl_extract_resolve_url ( const char * base,
const char * raw,
char * out,
size_t out_cap )

Resolve raw (possibly relative) against base into out.

Resolve a possibly relative HTTP(S) URL against an absolute base URL.

Definition at line 164 of file mdl_extract.c.

References internal_authority_of(), internal_copy_fits(), internal_resolve_path_rel(), internal_resolve_root_rel(), internal_resolve_scheme_rel(), k_authority_max, and strncmp().

Referenced by internal_emit_hit(), internal_emit_tag_url(), and internal_extract_cover().