/src/icu/icu4c/source/test/fuzzer/converter_fuzzer.cpp
Line | Count | Source |
1 | | // © 2019 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | |
4 | | #include <stddef.h> |
5 | | #include <stdint.h> |
6 | | |
7 | | #include <algorithm> |
8 | | #include <array> |
9 | | #include <cstring> |
10 | | #include <functional> |
11 | | #include <memory> |
12 | | #include <vector> |
13 | | |
14 | | #include "fuzzer_utils.h" |
15 | | #include "unicode/unistr.h" |
16 | | #include "unicode/ucnv.h" |
17 | | |
18 | | template <typename T> |
19 | | using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>; |
20 | | |
21 | 8.40k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
22 | 8.40k | UErrorCode status = U_ZERO_ERROR; |
23 | 8.40k | uint16_t rnd = 0; |
24 | | |
25 | 8.40k | if (size < 2) { |
26 | 7 | return 0; |
27 | 7 | } |
28 | | |
29 | 8.39k | rnd = *(reinterpret_cast<const uint16_t *>(data)); |
30 | 8.39k | data = data + 2; |
31 | 8.39k | size = size - 2; |
32 | | |
33 | 8.39k | size_t unistr_size = size/2; |
34 | 8.39k | std::unique_ptr<char16_t[]> fuzzbuff(new char16_t[unistr_size]); |
35 | 8.39k | std::memcpy(fuzzbuff.get(), data, unistr_size * 2); |
36 | | |
37 | 8.39k | icu::UnicodeString fuzzstr(false, fuzzbuff.get(), unistr_size); |
38 | | |
39 | 8.39k | const char* converter_name = |
40 | 8.39k | ucnv_getAvailableName(rnd % ucnv_countAvailable()); |
41 | 8.39k | deleted_unique_ptr<UConverter> converter(ucnv_open(converter_name, &status), |
42 | 8.39k | &ucnv_close); |
43 | 8.39k | if (U_FAILURE(status)) { |
44 | 0 | return 0; |
45 | 0 | } |
46 | | |
47 | 8.39k | static const size_t dest_buffer_size = 1024 * 1204; |
48 | 8.39k | const std::unique_ptr<char[]> dest_buffer(new char[dest_buffer_size]); |
49 | | |
50 | 8.39k | fuzzstr.extract(dest_buffer.get(), dest_buffer_size, converter.get(), status); |
51 | | |
52 | 8.39k | return 0; |
53 | 8.39k | } |