]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/fuzzer/unicode_string_codepage_create_fuzzer.cc
ed75f351c40d4f70c6be52e8bf1a92d9eb141dc2
[apple/icu.git] / icuSources / test / fuzzer / unicode_string_codepage_create_fuzzer.cc
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 <vector>
10
11 #include "fuzzer_utils.h"
12 #include "unicode/unistr.h"
13
14 // Taken from third_party/icu/source/data/mappings/convrtrs.txt file.
15 static const std::array<const char*, 45> kConverters = {
16 {
17 "UTF-8",
18 "utf-16be",
19 "utf-16le",
20 "UTF-32",
21 "UTF-32BE",
22 "UTF-32LE",
23 "ibm866-html",
24 "iso-8859-2-html",
25 "iso-8859-3-html",
26 "iso-8859-4-html",
27 "iso-8859-5-html",
28 "iso-8859-6-html",
29 "iso-8859-7-html",
30 "iso-8859-8-html",
31 "ISO-8859-8-I",
32 "iso-8859-10-html",
33 "iso-8859-13-html",
34 "iso-8859-14-html",
35 "iso-8859-15-html",
36 "iso-8859-16-html",
37 "koi8-r-html",
38 "koi8-u-html",
39 "macintosh-html",
40 "windows-874-html",
41 "windows-1250-html",
42 "windows-1251-html",
43 "windows-1252-html",
44 "windows-1253-html",
45 "windows-1254-html",
46 "windows-1255-html",
47 "windows-1256-html",
48 "windows-1257-html",
49 "windows-1258-html",
50 "x-mac-cyrillic-html",
51 "windows-936-2000",
52 "gb18030",
53 "big5-html",
54 "euc-jp-html",
55 "ISO_2022,locale=ja,version=0",
56 "shift_jis-html",
57 "euc-kr-html",
58 "ISO-2022-KR",
59 "ISO-2022-CN",
60 "ISO-2022-CN-EXT",
61 "HZ-GB-2312"
62 }
63 };
64
65 IcuEnvironment* env = new IcuEnvironment();
66
67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
68 if (size < 1) {
69 return 0;
70 }
71
72 // First byte is used for random converter selection.
73 uint8_t rnd = *data;
74 data++;
75 size--;
76
77 std::unique_ptr<char[]> fuzzbuff(new char[size]);
78 std::memcpy(fuzzbuff.get(), data, size);
79
80 icu::UnicodeString str(fuzzbuff.get(), size,
81 kConverters[rnd % kConverters.size()]);
82
83 return 0;
84 }