Andrew Top | c2b4089 | 2017-01-19 14:03:49 -0800 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | |
| 3 | // Fuzzer for ucasemap. |
| 4 | |
| 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | #include <memory> |
| 8 | #include "third_party/icu/fuzzers/fuzzer_utils.h" |
| 9 | #include "third_party/icu/source/common/unicode/ucasemap.h" |
| 10 | |
| 11 | IcuEnvironment* env = new IcuEnvironment(); |
| 12 | |
| 13 | template<typename T> |
| 14 | using deleted_unique_ptr = std::unique_ptr<T,std::function<void(T*)>>; |
| 15 | |
| 16 | |
| 17 | // Entry point for LibFuzzer. |
| 18 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 19 | UErrorCode status = U_ZERO_ERROR; |
| 20 | |
| 21 | auto rng = CreateRng(data, size); |
| 22 | const icu::Locale& locale = GetRandomLocale(&rng); |
| 23 | uint32_t open_flags = static_cast<uint32_t>(rng()); |
| 24 | |
| 25 | deleted_unique_ptr<UCaseMap> csm( |
| 26 | ucasemap_open(locale.getName(), open_flags, &status), |
| 27 | [](UCaseMap* map) { ucasemap_close(map); }); |
| 28 | |
| 29 | if (U_FAILURE(status)) |
| 30 | return 0; |
| 31 | |
| 32 | int32_t dst_size = size * 2; |
| 33 | std::unique_ptr<char[]> dst(new char[dst_size]); |
| 34 | auto src = reinterpret_cast<const char*>(data); |
| 35 | |
| 36 | switch (rng() % 4) { |
| 37 | case 0: ucasemap_utf8ToLower(csm.get(), dst.get(), dst_size, src, size, |
| 38 | &status); |
| 39 | break; |
| 40 | case 1: ucasemap_utf8ToUpper(csm.get(), dst.get(), dst_size, src, size, |
| 41 | &status); |
| 42 | break; |
| 43 | case 2: ucasemap_utf8ToTitle(csm.get(), dst.get(), dst_size, src, size, |
| 44 | &status); |
| 45 | break; |
| 46 | case 3: ucasemap_utf8FoldCase(csm.get(), dst.get(), dst_size, src, size, |
| 47 | &status); |
| 48 | break; |
| 49 | } |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |