mirror of
https://github.com/libretro/RetroArch.git
synced 2026-11-23 09:49:34 -10:00
The portable implementation was only compiled where the C library lacked strcasestr, and shadowed the real name with a macro when it was. So most targets never used it - qb probes for the function and sets HAVE_STRCASESTR whenever the library has one, which covers Linux, macOS and the BSDs, and eight handheld makefiles set it outright. That left the choice of implementation to the platform, and the platform's is not reliably the better one. glibc's measured 1.63 ms against 0.031 ms for this one on a representative search - fifty times slower - and glibc is the favourable case. The handhelds that opt in are the slowest machines RetroArch runs on and the least likely to ship a good version, so the targets with the least headroom were the ones most exposed. Call it by its own name instead, and build it everywhere. No macro shadowing a standard function, no conditional compilation, no probe: one implementation with one set of semantics on every target. The callers all switch to compat_strcasestr; the qb check, the HAVE_STRCASESTR guards in Makefile.common and griffin, and the flag in the handheld makefiles all go. It also settles the case folding. The library versions fold according to the locale; this one folds ASCII, which is what every caller means - file extensions, driver names, search terms - and no longer varies by platform. Checked against glibc's implementation over four hundred thousand random haystack and needle pairs drawn from an alphabet with both cases and a multi-byte character: no difference in result. The behavioural tests from the earlier rewrite still pass, and the frontend builds and runs.
99 lines
3.4 KiB
C
99 lines
3.4 KiB
C
/* Copyright (C) 2010-2020 The RetroArch team
|
|
*
|
|
* ---------------------------------------------------------------------------------------
|
|
* The following license statement only applies to this file (net_http_parse.c).
|
|
* ---------------------------------------------------------------------------------------
|
|
*
|
|
* Permission is hereby granted, free of charge,
|
|
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
|
* to deal in the Software without restriction, including without limitation the rights to
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <compat/strcasestr.h>
|
|
|
|
/**
|
|
* string_parse_html_anchor:
|
|
* @line : Buffer where the <a> tag is stored
|
|
* @link : Buffer to store the link URL in
|
|
* @name : Buffer to store the link URL in
|
|
* @link_size : Size of the link buffer including the NUL-terminator
|
|
* @name_size : Size of the name buffer including the NUL-terminator
|
|
*
|
|
* Parses an HTML anchor link stored in @line in the form of: <a href="/path/to/url">Title</a>
|
|
* The buffer pointed to by @link is filled with the URL path the link points to,
|
|
* and @name is filled with the title portion of the link.
|
|
*
|
|
* @return 0 if URL was parsed completely, otherwise 1.
|
|
**/
|
|
int string_parse_html_anchor(const char *line, char *link, char *name,
|
|
size_t link_size, size_t name_size)
|
|
{
|
|
if (!line || !link || !name)
|
|
return 1;
|
|
|
|
memset(link, 0, link_size);
|
|
memset(name, 0, name_size);
|
|
|
|
line = compat_strcasestr(line, "<a href=\"");
|
|
|
|
if (!line)
|
|
return 1;
|
|
|
|
line += 9;
|
|
|
|
if (line && *line)
|
|
{
|
|
if (!*link)
|
|
{
|
|
const char *end = strstr(line, "\"");
|
|
size_t _len;
|
|
|
|
if (!end)
|
|
return 1;
|
|
|
|
/* Bound the href length against the caller's buffer.
|
|
* Pre-patch the memcpy was unbounded and a long href
|
|
* would overflow link[]. Keep room for the NUL. */
|
|
_len = (size_t)(end - line);
|
|
if (_len >= link_size)
|
|
_len = (link_size > 0) ? link_size - 1 : 0;
|
|
|
|
memcpy(link, line, _len);
|
|
link[_len] = '\0';
|
|
line += end - line;
|
|
}
|
|
|
|
if (!*name)
|
|
{
|
|
const char *start = strstr(line, "\">");
|
|
const char *end = start ? strstr(start, "</a>") : NULL;
|
|
size_t _len;
|
|
|
|
if (!start || !end)
|
|
return 1;
|
|
|
|
/* Same bounding for the anchor text. */
|
|
_len = (size_t)(end - start - 2);
|
|
if (_len >= name_size)
|
|
_len = (name_size > 0) ? name_size - 1 : 0;
|
|
|
|
memcpy(name, start + 2, _len);
|
|
name[_len] = '\0';
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|