blob: 9f616c0523d4cbfdb809350f966b0609a0faf6f7 [file] [log] [blame]
krasinf45d5292015-10-22 01:46:221// Copyright (c) 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
5#include <stdint.h>
6#include <string>
7
8#include "base/strings/string16.h"
9#include "base/strings/utf_string_conversions.h"
10#include "components/translate/core/language_detection/language_detection_util.h"
11
12// Entry point for LibFuzzer.
mmorozfa4495442015-11-26 14:07:4913extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, size_t size) {
krasinf45d5292015-10-22 01:46:2214 if (size == 0) {
15 return 0;
16 }
17 uint8_t ch = data[0];
18 int lang_len = ch & 0xF;
19 int html_lang_len = (ch >> 4) & 0xF;
zhaoqin58a44462016-01-29 20:38:3720 int text_len = static_cast<int>(size) - lang_len - html_lang_len;
krasinf45d5292015-10-22 01:46:2221 if ((text_len < 0) || (text_len % 2 != 0)) {
22 return 0;
23 }
24 std::string lang(reinterpret_cast<const char*>(data), lang_len);
25 std::string html_lang(reinterpret_cast<const char*>(data + lang_len),
26 html_lang_len);
27 base::string16 text(
28 reinterpret_cast<const base::char16*>(data + lang_len + html_lang_len),
29 text_len / 2);
30 std::string cld_lang;
31 bool is_cld_reliable;
32 translate::DeterminePageLanguage(lang, html_lang, text, &cld_lang,
33 &is_cld_reliable);
34 return 0;
35}