1 // Copyright 2018 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 "base/strings/string_util.h"
6 #include "base/strings/utf_string_conversions.h"
7
8 std::string output_std_string;
9 std::wstring output_std_wstring;
10 base::string16 output_string16;
11
12 // Entry point for LibFuzzer.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)13 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
14 base::StringPiece string_piece_input(reinterpret_cast<const char*>(data),
15 size);
16
17 base::UTF8ToWide(string_piece_input);
18 base::UTF8ToWide(reinterpret_cast<const char*>(data), size,
19 &output_std_wstring);
20 base::UTF8ToUTF16(string_piece_input);
21 base::UTF8ToUTF16(reinterpret_cast<const char*>(data), size,
22 &output_string16);
23
24 // Test for char16.
25 if (size % 2 == 0) {
26 base::StringPiece16 string_piece_input16(
27 reinterpret_cast<const base::char16*>(data), size / 2);
28 base::UTF16ToWide(output_string16);
29 base::UTF16ToWide(reinterpret_cast<const base::char16*>(data), size / 2,
30 &output_std_wstring);
31 base::UTF16ToUTF8(string_piece_input16);
32 base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(data), size / 2,
33 &output_std_string);
34 }
35
36 // Test for wchar_t.
37 size_t wchar_t_size = sizeof(wchar_t);
38 if (size % wchar_t_size == 0) {
39 base::WideToUTF8(output_std_wstring);
40 base::WideToUTF8(reinterpret_cast<const wchar_t*>(data),
41 size / wchar_t_size, &output_std_string);
42 base::WideToUTF16(output_std_wstring);
43 base::WideToUTF16(reinterpret_cast<const wchar_t*>(data),
44 size / wchar_t_size, &output_string16);
45 }
46
47 // Test for ASCII. This condition is needed to avoid hitting instant CHECK
48 // failures.
49 if (base::IsStringASCII(string_piece_input)) {
50 output_string16 = base::ASCIIToUTF16(string_piece_input);
51 base::StringPiece16 string_piece_input16(output_string16);
52 base::UTF16ToASCII(string_piece_input16);
53 }
54
55 return 0;
56 }
57