Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
checkers.cpp
Go to the documentation of this file.
1#include "ada/checkers-inl.h"
2#include "ada/checkers.h"
3#include "ada/unicode-inl.h"
4#include "ada/common_defs.h"
5
6#include <algorithm>
7#include <array>
8#include <cstddef>
9#include <cstdint>
10#include <string_view>
11
12namespace ada::checkers {
13
14ada_really_inline constexpr bool is_ipv4(std::string_view view) noexcept {
15 // The string is not empty and does not contain upper case ASCII characters.
16 if (!last_label_may_be_a_number(view)) {
17 return false;
18 }
19 // If the address ends with a dot, we need to prune it (special case).
20 if (view.ends_with('.')) {
21 view.remove_suffix(1);
22 }
23 // From the last character, find the last dot.
24 size_t last_dot = view.rfind('.');
25 if (last_dot != std::string_view::npos) {
26 // We have at least one dot.
27 view.remove_prefix(last_dot + 1);
28 }
32 if (std::ranges::all_of(view, ada::checkers::is_digit)) {
33 return true;
34 }
35 // It could be hex (0x), but not if there is a single character.
36 if (view.size() == 1) {
37 return false;
38 }
39 // It must start with 0x.
40 if (!view.starts_with("0x")) {
41 return false;
42 }
43 // We must allow "0x".
44 if (view.size() == 2) {
45 return true;
46 }
47 // We have 0x followed by some characters, we need to check that they are
48 // hexadecimals.
49 view.remove_prefix(2);
50 return std::ranges::all_of(view, ada::unicode::is_lowercase_hex);
51}
52
53// for use with path_signature, we include all characters that need percent
54// encoding.
55static constexpr std::array<uint8_t, 256> path_signature_table =
56 []() consteval {
57 std::array<uint8_t, 256> result{};
58 for (size_t i = 0; i < 256; i++) {
59 if (i <= 0x20 || i == 0x22 || i == 0x23 || i == 0x3c || i == 0x3e ||
60 i == 0x3f || i == 0x5e || i == 0x60 || i == 0x7b || i == 0x7d ||
61 i > 0x7e) {
62 result[i] = 1;
63 } else if (i == 0x25) {
64 result[i] = 8;
65 } else if (i == 0x2e) {
66 result[i] = 4;
67 } else if (i == 0x5c) {
68 result[i] = 2;
69 } else {
70 result[i] = 0;
71 }
72 }
73 return result;
74 }();
75
76ada_really_inline constexpr uint8_t path_signature(
77 std::string_view input) noexcept {
78 // The path percent-encode set is the query percent-encode set and U+003F (?),
79 // U+0060 (`), U+007B ({), and U+007D (}). The query percent-encode set is the
80 // C0 control percent-encode set and U+0020 SPACE, U+0022 ("), U+0023 (#),
81 // U+003C (<), and U+003E (>). The C0 control percent-encode set are the C0
82 // controls and all code points greater than U+007E (~).
83 size_t i = 0;
84 uint8_t accumulator{};
85 for (; i + 7 < input.size(); i += 8) {
86 accumulator |= uint8_t(path_signature_table[uint8_t(input[i])] |
87 path_signature_table[uint8_t(input[i + 1])] |
88 path_signature_table[uint8_t(input[i + 2])] |
89 path_signature_table[uint8_t(input[i + 3])] |
90 path_signature_table[uint8_t(input[i + 4])] |
91 path_signature_table[uint8_t(input[i + 5])] |
92 path_signature_table[uint8_t(input[i + 6])] |
93 path_signature_table[uint8_t(input[i + 7])]);
94 }
95 for (; i < input.size(); i++) {
96 accumulator |= uint8_t(path_signature_table[uint8_t(input[i])]);
97 }
98 return accumulator;
99}
100
101ada_really_inline constexpr bool verify_dns_length(
102 std::string_view input) noexcept {
103 if (input.empty()) return false;
104 if (input.back() == '.') {
105 if (input.size() > 254) return false;
106 } else if (input.size() > 253)
107 return false;
108
109 size_t start = 0;
110 while (start < input.size()) {
111 auto dot_location = input.find('.', start);
112 // If not found, it's likely the end of the domain
113 if (dot_location == std::string_view::npos) dot_location = input.size();
114
115 auto label_size = dot_location - start;
116 if (label_size > 63 || label_size == 0) return false;
117
118 start = dot_location + 1;
119 }
120
121 return true;
122}
123
124} // namespace ada::checkers
Definitions for URL specific checkers used within Ada.
Declarations for URL specific checkers used within Ada.
Cross-platform compiler macros and common definitions.
#define ada_really_inline
Definition common_defs.h:85
Includes the definitions for validation functions.
constexpr bool last_label_may_be_a_number(std::string_view view) noexcept
static constexpr std::array< uint8_t, 256 > path_signature_table
Definition checkers.cpp:55
constexpr bool is_digit(char x) noexcept
tl::expected< result_type, ada::errors > result
Definitions for unicode operations.