| 1 | /* Copyright (C) 2010-2020 The RetroArch team |
| 2 | * |
| 3 | * --------------------------------------------------------------------------------------- |
| 4 | * The following license statement only applies to this file (net_http_parse.c). |
| 5 | * --------------------------------------------------------------------------------------- |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, |
| 8 | * to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation the rights to |
| 10 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 11 | * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <compat/strcasestr.h> |
| 25 | |
| 26 | /** |
| 27 | * string_parse_html_anchor: |
| 28 | * @line : Buffer where the <a> tag is stored |
| 29 | * @link : Buffer to store the link URL in |
| 30 | * @name : Buffer to store the link URL in |
| 31 | * @link_size : Size of the link buffer including the NUL-terminator |
| 32 | * @name_size : Size of the name buffer including the NUL-terminator |
| 33 | * |
| 34 | * Parses an HTML anchor link stored in @line in the form of: <a href="/path/to/url">Title</a> |
| 35 | * The buffer pointed to by @link is filled with the URL path the link points to, |
| 36 | * and @name is filled with the title portion of the link. |
| 37 | * |
| 38 | * @return 0 if URL was parsed completely, otherwise 1. |
| 39 | **/ |
| 40 | int string_parse_html_anchor(const char *line, char *link, char *name, |
| 41 | size_t link_size, size_t name_size) |
| 42 | { |
| 43 | if (!line || !link || !name) |
| 44 | return 1; |
| 45 | |
| 46 | memset(link, 0, link_size); |
| 47 | memset(name, 0, name_size); |
| 48 | |
| 49 | line = strcasestr(line, "<a href=\""); |
| 50 | |
| 51 | if (!line) |
| 52 | return 1; |
| 53 | |
| 54 | line += 9; |
| 55 | |
| 56 | if (line && *line) |
| 57 | { |
| 58 | if (!*link) |
| 59 | { |
| 60 | const char *end = strstr(line, "\""); |
| 61 | |
| 62 | if (!end) |
| 63 | return 1; |
| 64 | |
| 65 | memcpy(link, line, end - line); |
| 66 | |
| 67 | *(link + (end - line)) = '\0'; |
| 68 | line += end - line; |
| 69 | } |
| 70 | |
| 71 | if (!*name) |
| 72 | { |
| 73 | const char *start = strstr(line, "\">"); |
| 74 | const char *end = start ? strstr(start, "</a>") : NULL; |
| 75 | |
| 76 | if (!start || !end) |
| 77 | return 1; |
| 78 | |
| 79 | memcpy(name, start + 2, end - start - 2); |
| 80 | |
| 81 | *(name + (end - start - 2)) = '\0'; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |