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