]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/fuzzer/converter_fuzzer.cc
ICU-64232.0.1.tar.gz
[apple/icu.git] / icuSources / test / fuzzer / converter_fuzzer.cc
CommitLineData
3d1f044b
A
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 <memory>
10#include <vector>
11
12#include "fuzzer_utils.h"
13#include "unicode/unistr.h"
14#include "unicode/ucnv.h"
15
16IcuEnvironment* env = new IcuEnvironment();
17
18template <typename T>
19using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
20
21extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
22 UErrorCode status = U_ZERO_ERROR;
23 uint16_t rnd = 0;
24
25 if (size < 2) {
26 return 0;
27 }
28
29 rnd = *(reinterpret_cast<const uint16_t *>(data));
30 data = data + 2;
31 size = size - 2;
32
33 size_t unistr_size = size/2;
34 std::unique_ptr<char16_t[]> fuzzbuff(new char16_t[unistr_size]);
35 std::memcpy(fuzzbuff.get(), data, unistr_size * 2);
36
37 icu::UnicodeString fuzzstr(false, fuzzbuff.get(), unistr_size);
38
39 const char* converter_name =
40 ucnv_getAvailableName(rnd % ucnv_countAvailable());
41 deleted_unique_ptr<UConverter> converter(ucnv_open(converter_name, &status),
42 &ucnv_close);
43 if (U_FAILURE(status)) {
44 return 0;
45 }
46
47 static const size_t dest_buffer_size = 1024 * 1204;
48 static const std::unique_ptr<char[]> dest_buffer(new char[dest_buffer_size]);
49
50 fuzzstr.extract(dest_buffer.get(), dest_buffer_size, converter.get(), status);
51
52 return 0;
53}