| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <stddef.h> | |
| 6 #include <stdint.h> | |
| 7 | |
| 8 #include <algorithm> | |
| 9 #include <array> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "third_party/icu/source/common/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 // Entry point for LibFuzzer. | |
| 66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | |
| 67 if (size < 2) | |
| 68 return 0; | |
| 69 | |
| 70 // Need null-terminated string. | |
| 71 std::vector<char> buffer(size, 0); | |
| 72 // First byte will be used for random decisions. | |
| 73 unsigned char selector = data[0]; | |
| 74 size -= 1; | |
| 75 std::copy(data + 1, data + size, buffer.data()); | |
| 76 | |
| 77 // Pointer to a part of fuzzer's data or to some valid codepage value. | |
| 78 char* codepage; | |
| 79 | |
| 80 if (selector & 1) { | |
| 81 // Use random codepage value provided by fuzzer (split data in two parts). | |
| 82 // Remove least significant bit because we already used it as input value. | |
| 83 size_t codepage_length = (selector >> 1) & 0xF; | |
| 84 if (size <= codepage_length) | |
| 85 size /= 2; | |
| 86 else | |
| 87 size -= codepage_length; | |
| 88 | |
| 89 codepage = buffer.data() + size; | |
| 90 } else { | |
| 91 // Use one of valid codepage values. | |
| 92 // Remove least significant bit because we already used it as input value. | |
| 93 size_t index = (selector >> 1) % kConverters.size(); | |
| 94 codepage = const_cast<char*>(kConverters[index]); | |
| 95 } | |
| 96 | |
| 97 icu::UnicodeString unicode_string(buffer.data(), | |
| 98 static_cast<int>(size), | |
| 99 codepage); | |
| 100 | |
| 101 return 0; | |
| 102 } | |
| OLD | NEW |