fuzzer_utils.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. #ifndef THIRD_PARTY_ICU_FUZZERS_FUZZER_UTILS_H_
  3. #define THIRD_PARTY_ICU_FUZZERS_FUZZER_UTILS_H_
  4. #include <assert.h>
  5. #include <algorithm>
  6. #include <random>
  7. #include "base/at_exit.h"
  8. #include "base/i18n/icu_util.h"
  9. #include "third_party/icu/source/common/unicode/locid.h"
  10. #include "third_party/icu/source/common/unicode/uchar.h"
  11. #include "third_party/icu/source/common/unicode/unistr.h"
  12. struct IcuEnvironment {
  13. IcuEnvironment() {
  14. base::i18n::InitializeICU();
  15. }
  16. };
  17. // Create RNG and seed it from data.
  18. std::mt19937_64 CreateRng(const uint8_t* data, size_t size) {
  19. std::mt19937_64 rng;
  20. std::string str = std::string(reinterpret_cast<const char*>(data), size);
  21. std::size_t data_hash = std::hash<std::string>()(str);
  22. rng.seed(data_hash);
  23. return rng;
  24. }
  25. const icu::Locale& GetRandomLocale(std::mt19937_64* rng) {
  26. int32_t num_locales = 0;
  27. const icu::Locale* locales = icu::Locale::getAvailableLocales(num_locales);
  28. assert(num_locales > 0);
  29. return locales[(*rng)() % num_locales];
  30. }
  31. icu::UnicodeString UnicodeStringFromUtf8(const uint8_t* data, size_t size) {
  32. return icu::UnicodeString::fromUTF8(
  33. icu::StringPiece(reinterpret_cast<const char*>(data), size));
  34. }
  35. icu::UnicodeString UnicodeStringFromUtf32(const uint8_t* data, size_t size) {
  36. std::vector<UChar32> uchars;
  37. uchars.resize(size * sizeof(uint8_t) / (sizeof(UChar32)));
  38. memcpy(uchars.data(), data, uchars.size() * sizeof(UChar32));
  39. for (size_t i = 0; i < uchars.size(); ++i) {
  40. // The valid range for UTF32 is [0, UCHAR_MAX_VALUE]
  41. // By % with (UCHAR_MAX_VALUE + 2) we make the output mostly valid with
  42. // a small percentage of (1 / UCHAR_MAX_VALUE) invalid data in UTF8.
  43. uchars[i] = uchars[i] % (UCHAR_MAX_VALUE + 2);
  44. }
  45. return icu::UnicodeString::fromUTF32(uchars.data(), uchars.size());
  46. }
  47. std::vector<char16_t> RandomChar16Array(size_t random_value,
  48. const uint8_t* data,
  49. size_t size) {
  50. std::vector<char16_t> arr;
  51. arr.resize(random_value % size * sizeof(uint8_t) / sizeof(char16_t));
  52. memcpy(arr.data(), data, arr.size() * sizeof(char16_t) / sizeof(uint8_t));
  53. return arr;
  54. }
  55. #endif // THIRD_PARTY_ICU_FUZZERS_FUZZER_UTILS_H_