blob: 1545f76aff923ab628661e7cf296b63d318db56d [file] [log] [blame]
mmoroze87d66d52016-02-05 19:10:571// Copyright 2015 The Chromium Authors. All rights reserved.
krasinf45d5292015-10-22 01:46:222// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
mmoroze87d66d52016-02-05 19:10:575#include <stddef.h>
krasinf45d5292015-10-22 01:46:226#include <stdint.h>
mmoroze87d66d52016-02-05 19:10:577
krasinf45d5292015-10-22 01:46:228#include <string>
9
10#include "base/strings/string16.h"
11#include "base/strings/utf_string_conversions.h"
12#include "components/translate/core/language_detection/language_detection_util.h"
13
14// Entry point for LibFuzzer.
mmoroze87d66d52016-02-05 19:10:5715extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
krasinf45d5292015-10-22 01:46:2216 if (size == 0) {
17 return 0;
18 }
19 uint8_t ch = data[0];
20 int lang_len = ch & 0xF;
21 int html_lang_len = (ch >> 4) & 0xF;
zhaoqin58a44462016-01-29 20:38:3722 int text_len = static_cast<int>(size) - lang_len - html_lang_len;
krasinf45d5292015-10-22 01:46:2223 if ((text_len < 0) || (text_len % 2 != 0)) {
24 return 0;
25 }
26 std::string lang(reinterpret_cast<const char*>(data), lang_len);
27 std::string html_lang(reinterpret_cast<const char*>(data + lang_len),
28 html_lang_len);
29 base::string16 text(
30 reinterpret_cast<const base::char16*>(data + lang_len + html_lang_len),
31 text_len / 2);
32 std::string cld_lang;
33 bool is_cld_reliable;
34 translate::DeterminePageLanguage(lang, html_lang, text, &cld_lang,
35 &is_cld_reliable);
36 return 0;
37}