/src/uriparser/fuzz/FuzzingUtils.h
Line | Count | Source |
1 | | // Copyright 2025 Mikhail Khachaiants <mkhachaiants@gmail.com> |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef URI_FUZZING_UTILS_H |
16 | | #define URI_FUZZING_UTILS_H 1 |
17 | | |
18 | | #include "uriparser/Uri.h" |
19 | | #include <fuzzer/FuzzedDataProvider.h> |
20 | | #include <string> |
21 | | |
22 | | |
23 | | |
24 | | using UriString = std::basic_string<URI_CHAR>; |
25 | | |
26 | | |
27 | | |
28 | 21.7k | inline UriString tryConsumeBytesAsString(FuzzedDataProvider & fdp, size_t chars) { |
29 | 21.7k | UriString str(chars, 0); |
30 | 21.7k | const size_t bytes = fdp.ConsumeData(str.data(), chars * sizeof(URI_CHAR)); |
31 | | // FuzzedDataProvider may provide less data than requested if the input is |
32 | | // insufficiently long. We need to adjust the string length accordingly. |
33 | 21.7k | str.resize(bytes / sizeof(URI_CHAR)); |
34 | 21.7k | return str; |
35 | 21.7k | } |
36 | | |
37 | | |
38 | | |
39 | 10.8k | inline UriString consumeRandomLengthString(FuzzedDataProvider & fdp) { |
40 | 10.8k | const size_t max_chars = fdp.remaining_bytes() / sizeof(URI_CHAR); |
41 | 10.8k | const size_t chars = fdp.ConsumeIntegralInRange<size_t>(0, max_chars); |
42 | 10.8k | return tryConsumeBytesAsString(fdp, chars); |
43 | 10.8k | } |
44 | | |
45 | | |
46 | | |
47 | 10.8k | inline UriString consumeRemainingBytesAsString(FuzzedDataProvider & fdp) { |
48 | 10.8k | const size_t chars = fdp.remaining_bytes() / sizeof(URI_CHAR); |
49 | 10.8k | return tryConsumeBytesAsString(fdp, chars); |
50 | 10.8k | } |
51 | | |
52 | | #endif /* URI_FUZZING_UTILS_H */ |