compact_enc_det_fuzzer.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2018 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. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <fuzzer/FuzzedDataProvider.h>
  7. #include <vector>
  8. #include "third_party/ced/src/compact_enc_det/compact_enc_det.h"
  9. namespace {
  10. constexpr size_t kMaxInputSize = 64 * 1024;
  11. }
  12. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  13. // Early out if there isn't enough data to extract options and pass data to
  14. // the library.
  15. if (size < 3 * sizeof(int32_t) + 1)
  16. return 0;
  17. // Limit the input size to avoid timing out on clusterfuzz.
  18. if (size > kMaxInputSize)
  19. return 0;
  20. FuzzedDataProvider data_provider(data, size);
  21. CompactEncDet::TextCorpusType corpus =
  22. static_cast<CompactEncDet::TextCorpusType>(
  23. data_provider.ConsumeIntegralInRange<int32_t>(
  24. 0, CompactEncDet::NUM_CORPA));
  25. Encoding encoding_hint = static_cast<Encoding>(
  26. data_provider.ConsumeIntegralInRange<int32_t>(0, NUM_ENCODINGS));
  27. Language langauge_hint = static_cast<Language>(
  28. data_provider.ConsumeIntegralInRange<int32_t>(0, NUM_LANGUAGES));
  29. bool ignore_7bit_mail_encodings = data_provider.ConsumeBool();
  30. std::vector<char> text = data_provider.ConsumeRemainingBytes<char>();
  31. int bytes_consumed = 0;
  32. bool is_reliable = false;
  33. CompactEncDet::DetectEncoding(
  34. text.data(), text.size(), nullptr /* url_hint */,
  35. nullptr /* http_charset_hint */, nullptr /* meta_charset_hint */,
  36. encoding_hint, langauge_hint, corpus, ignore_7bit_mail_encodings,
  37. &bytes_consumed, &is_reliable);
  38. return 0;
  39. }