• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 IcuEnvironment* env = new IcuEnvironment();
19 
20 template <typename T>
21 using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
22 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)23 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
24   UErrorCode status = U_ZERO_ERROR;
25   uint16_t rnd = 0;
26 
27   if (size < 2) {
28     return 0;
29   }
30 
31   rnd = *(reinterpret_cast<const uint16_t *>(data));
32   data = data + 2;
33   size = size - 2;
34 
35   size_t unistr_size = size/2;
36   std::unique_ptr<char16_t[]> fuzzbuff(new char16_t[unistr_size]);
37   std::memcpy(fuzzbuff.get(), data, unistr_size * 2);
38 
39   icu::UnicodeString fuzzstr(false, fuzzbuff.get(), unistr_size);
40 
41   const char* converter_name =
42       ucnv_getAvailableName(rnd % ucnv_countAvailable());
43   deleted_unique_ptr<UConverter> converter(ucnv_open(converter_name, &status),
44                                            &ucnv_close);
45   if (U_FAILURE(status)) {
46     return 0;
47   }
48 
49   static const size_t dest_buffer_size = 1024 * 1204;
50   static const std::unique_ptr<char[]> dest_buffer(new char[dest_buffer_size]);
51 
52   fuzzstr.extract(dest_buffer.get(), dest_buffer_size, converter.get(), status);
53 
54   return 0;
55 }
56