]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/fuzzer/ucasemap_fuzzer.cc
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / test / fuzzer / ucasemap_fuzzer.cc
1 // © 2019 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 // Fuzzer for ucasemap.
5
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <memory>
9 #include "fuzzer_utils.h"
10 #include "unicode/ucasemap.h"
11
12 IcuEnvironment* env = new IcuEnvironment();
13
14 template<typename T>
15 using deleted_unique_ptr = std::unique_ptr<T,std::function<void(T*)>>;
16
17 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
18 UErrorCode status = U_ZERO_ERROR;
19 uint8_t rnd8 = 0;
20 uint16_t rnd16 = 0;
21 uint32_t rnd32 = 0;
22
23 if (size < 7) {
24 return 0;
25 }
26 // Extract one, two, and four bytes from fuzzer data for random selection
27 // purposes.
28 rnd8 = *data;
29 data++;
30 rnd16 = *(reinterpret_cast<const uint16_t *>(data));
31 data = data + 2;
32 rnd32 = *(reinterpret_cast<const uint32_t *>(data));
33 data = data + 4;
34 size = size - 7;
35
36 std::unique_ptr<char[]> fuzzbuff(new char[size]);
37 std::memcpy(fuzzbuff.get(), data, size);
38
39 const icu::Locale& locale = GetRandomLocale(rnd16);
40 uint32_t open_flags = rnd32;
41
42 deleted_unique_ptr<UCaseMap> csm(
43 ucasemap_open(locale.getName(), open_flags, &status),
44 [](UCaseMap* map) { ucasemap_close(map); });
45
46 if (U_FAILURE(status)) {
47 return 0;
48 }
49
50 int32_t dst_size = size * 2;
51 std::unique_ptr<char[]> dst(new char[dst_size]);
52 auto src = reinterpret_cast<const char*>(fuzzbuff.get());
53
54 switch (rnd8 % 4) {
55 case 0: ucasemap_utf8ToLower(csm.get(), dst.get(), dst_size, src, size,
56 &status);
57 break;
58 case 1: ucasemap_utf8ToUpper(csm.get(), dst.get(), dst_size, src, size,
59 &status);
60 break;
61 case 2: ucasemap_utf8ToTitle(csm.get(), dst.get(), dst_size, src, size,
62 &status);
63 break;
64 case 3: ucasemap_utf8FoldCase(csm.get(), dst.get(), dst_size, src, size,
65 &status);
66 break;
67 }
68
69 return 0;
70 }