mmoroz | e87d66d5 | 2016-02-05 19:10:57 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
krasin | f45d529 | 2015-10-22 01:46:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
mmoroz | e87d66d5 | 2016-02-05 19:10:57 | [diff] [blame] | 5 | #include <stddef.h> |
krasin | f45d529 | 2015-10-22 01:46:22 | [diff] [blame] | 6 | #include <stdint.h> |
mmoroz | e87d66d5 | 2016-02-05 19:10:57 | [diff] [blame] | 7 | |
krasin | f45d529 | 2015-10-22 01:46:22 | [diff] [blame] | 8 | #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. |
mmoroz | e87d66d5 | 2016-02-05 19:10:57 | [diff] [blame] | 15 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
krasin | f45d529 | 2015-10-22 01:46:22 | [diff] [blame] | 16 | 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; |
zhaoqin | 58a4446 | 2016-01-29 20:38:37 | [diff] [blame] | 22 | int text_len = static_cast<int>(size) - lang_len - html_lang_len; |
krasin | f45d529 | 2015-10-22 01:46:22 | [diff] [blame] | 23 | 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 | } |