language_detection_fuzzer.cc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <string>
  7. #include "base/strings/utf_string_conversions.h"
  8. #include "components/translate/core/language_detection/language_detection_util.h"
  9. // Entry point for LibFuzzer.
  10. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  11. if (size == 0) {
  12. return 0;
  13. }
  14. uint8_t ch = data[0];
  15. int lang_len = ch & 0xF;
  16. int html_lang_len = (ch >> 4) & 0xF;
  17. int text_len = static_cast<int>(size) - lang_len - html_lang_len;
  18. if ((text_len < 0) || (text_len % sizeof(char16_t) != 0)) {
  19. return 0;
  20. }
  21. std::string lang(reinterpret_cast<const char*>(data), lang_len);
  22. std::string html_lang(reinterpret_cast<const char*>(data + lang_len),
  23. html_lang_len);
  24. std::u16string text(
  25. reinterpret_cast<const char16_t*>(data + lang_len + html_lang_len),
  26. text_len / sizeof(char16_t));
  27. std::string model_detected_language;
  28. bool is_model_reliable;
  29. float model_reliability_score;
  30. translate::DeterminePageLanguage(lang, html_lang, text,
  31. &model_detected_language, &is_model_reliable,
  32. model_reliability_score);
  33. return 0;
  34. }