Ada 3.4.4
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
idna.cc
Go to the documentation of this file.
1#include <fuzzer/FuzzedDataProvider.h>
2
3#include <memory>
4#include <string>
5
6#include "ada.cpp"
7#include "ada.h"
8
9extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
10 FuzzedDataProvider fdp(data, size);
11 std::string source = fdp.ConsumeRandomLengthString(256);
12 std::string source2 = fdp.ConsumeRandomLengthString(64);
13
17 std::string ascii_result = ada::idna::to_ascii(source);
18 std::string unicode_result = ada::idna::to_unicode(source);
19
20 // Avoid dead-code elimination
21 volatile size_t length = 0;
22 length += ascii_result.size();
23 length += unicode_result.size();
24
29 if (!ascii_result.empty()) {
30 std::string roundtrip = ada::idna::to_unicode(ascii_result);
31 length += roundtrip.size();
32 }
33
37 {
38 std::u32string utf32_out;
39 // punycode_to_utf32: source can be any string (it's a punycode label)
40 bool punycode_ok = ada::idna::punycode_to_utf32(source, utf32_out);
41 length += utf32_out.size();
42
43 // verify_punycode: checks if source is valid punycode
44 volatile bool is_valid_punycode = ada::idna::verify_punycode(source);
45 (void)is_valid_punycode;
46
47 // utf32_to_punycode: round-trip if punycode_to_utf32 succeeded
48 if (punycode_ok && !utf32_out.empty()) {
49 std::string punycode_back;
50 volatile bool encode_ok =
51 ada::idna::utf32_to_punycode(utf32_out, punycode_back);
52 length += punycode_back.size();
53 (void)encode_ok;
54 }
55 }
56
60 {
61 // UTF-8 to UTF-32 conversion
62 size_t utf32_len =
63 ada::idna::utf32_length_from_utf8(source.data(), source.size());
64 if (utf32_len > 0 && utf32_len < 1024) {
65 std::vector<char32_t> utf32_buf(utf32_len + 1, 0);
66 size_t actual = ada::idna::utf8_to_utf32(source.data(), source.size(),
67 utf32_buf.data());
68 length += actual;
69
70 // UTF-32 to UTF-8 round-trip
71 if (actual > 0) {
72 size_t utf8_len =
73 ada::idna::utf8_length_from_utf32(utf32_buf.data(), actual);
74 if (utf8_len > 0 && utf8_len < 4096) {
75 std::string utf8_back(utf8_len, '\0');
76 size_t written = ada::idna::utf32_to_utf8(utf32_buf.data(), actual,
77 utf8_back.data());
78 length += written;
79 }
80 }
81 }
82 }
83
87 {
88 // is_label_valid requires a UTF-32 string
89 std::u32string utf32_label;
90 bool ok = ada::idna::punycode_to_utf32(source2, utf32_label);
91 if (ok && !utf32_label.empty()) {
92 volatile bool label_valid = ada::idna::is_label_valid(utf32_label);
93 (void)label_valid;
94 }
95
96 // Also test is_label_valid with direct ASCII-to-UTF32 conversion
97 std::u32string ascii_label(source2.begin(), source2.end());
98 if (!ascii_label.empty()) {
99 volatile bool ascii_label_valid = ada::idna::is_label_valid(ascii_label);
100 (void)ascii_label_valid;
101 }
102 }
103
107 {
108 // ASCII mapping: just lowercases ASCII characters
109 std::string ascii_copy = source;
110 ada::idna::ascii_map(ascii_copy.data(), ascii_copy.size());
111 length += ascii_copy.size();
112
113 // Unicode mapping: maps UTF-32 characters according to IDNA
114 size_t utf32_len =
115 ada::idna::utf32_length_from_utf8(source.data(), source.size());
116 if (utf32_len > 0 && utf32_len < 256) {
117 std::u32string utf32_input(utf32_len, 0);
118 size_t actual = ada::idna::utf8_to_utf32(source.data(), source.size(),
119 utf32_input.data());
120 if (actual > 0) {
121 utf32_input.resize(actual);
122 std::u32string mapped = ada::idna::map(utf32_input);
123 length += mapped.size();
124 }
125 }
126 }
127
131 {
132 volatile bool has_forbidden =
134 (void)has_forbidden;
135
136 // is_ascii checks
137 volatile bool is_ascii_str =
138 ada::idna::is_ascii(std::string_view(source.data(), source.size()));
139 (void)is_ascii_str;
140 }
141
145 {
146 size_t utf32_len =
147 ada::idna::utf32_length_from_utf8(source.data(), source.size());
148 if (utf32_len > 0 && utf32_len < 256) {
149 std::u32string utf32_input(utf32_len, 0);
150 size_t actual = ada::idna::utf8_to_utf32(source.data(), source.size(),
151 utf32_input.data());
152 if (actual > 0) {
153 utf32_input.resize(actual);
154 ada::idna::normalize(utf32_input);
155 length += utf32_input.size();
156 }
157 }
158 }
159
160 return 0;
161}
Main header for the Ada URL parser library.
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition idna.cc:9
void ascii_map(char *input, size_t length)
bool punycode_to_utf32(std::string_view input, std::u32string &out)
size_t utf32_length_from_utf8(const char *buf, size_t len)
Definition ada_idna.cpp:122
size_t utf32_to_utf8(const char32_t *buf, size_t len, char *utf8_output)
Definition ada_idna.cpp:131
bool constexpr is_ascii(std::u32string_view view)
void normalize(std::u32string &input)
bool utf32_to_punycode(std::u32string_view input, std::string &out)
std::string to_ascii(std::string_view ut8_string)
std::string to_unicode(std::string_view input)
size_t utf8_length_from_utf32(const char32_t *buf, size_t len)
Definition ada_idna.cpp:109
bool is_label_valid(std::u32string_view label)
bool contains_forbidden_domain_code_point(std::string_view ascii_string)
std::u32string map(std::u32string_view input)
size_t utf8_to_utf32(const char *buf, size_t len, char32_t *utf32_output)
Definition ada_idna.cpp:12
bool verify_punycode(std::string_view input)