1 // Copyright 2019 PDFium 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 "xfa/fgas/crt/cfgas_stringformatter.h"
6
7 #include <stdint.h>
8
9 #include "core/fxcrt/fx_memory.h"
10 #include "core/fxcrt/fx_string.h"
11 #include "third_party/base/ptr_util.h"
12 #include "xfa/fxfa/parser/cxfa_localemgr.h"
13
14 namespace {
15
16 const wchar_t* const kLocales[] = {L"en", L"fr", L"jp", L"zh"};
17 const FX_DATETIMETYPE kTypes[] = {FX_DATETIMETYPE_Date, FX_DATETIMETYPE_Time,
18 FX_DATETIMETYPE_DateTime,
19 FX_DATETIMETYPE_TimeDate};
20
21 } // namespace
22
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)23 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
24 if (size < 5 || size > 128) // Big strings are unlikely to help.
25 return 0;
26
27 // Static for speed.
28 static std::vector<std::unique_ptr<CXFA_LocaleMgr>> mgrs;
29 if (mgrs.empty()) {
30 for (const auto* locale : kLocales)
31 mgrs.push_back(pdfium::MakeUnique<CXFA_LocaleMgr>(nullptr, locale));
32 }
33
34 uint8_t test_selector = data[0] % 10;
35 uint8_t locale_selector = data[1] % FX_ArraySize(kLocales);
36 uint8_t type_selector = data[2] % FX_ArraySize(kTypes);
37 data += 3;
38 size -= 3;
39
40 size_t pattern_len = size / 2;
41 size_t value_len = size - pattern_len;
42 WideString pattern =
43 WideString::FromLatin1(ByteStringView(data, pattern_len));
44 WideString value =
45 WideString::FromLatin1(ByteStringView(data + pattern_len, value_len));
46
47 auto fmt = pdfium::MakeUnique<CFGAS_StringFormatter>(
48 mgrs[locale_selector].get(), pattern);
49
50 WideString result;
51 CFX_DateTime dt;
52 switch (test_selector) {
53 case 0:
54 fmt->FormatText(value, &result);
55 break;
56 case 1:
57 fmt->FormatNum(value, &result);
58 break;
59 case 2:
60 fmt->FormatDateTime(value, kTypes[type_selector], &result);
61 break;
62 case 3:
63 fmt->FormatNull(&result);
64 break;
65 case 4:
66 fmt->FormatZero(&result);
67 break;
68 case 5:
69 fmt->ParseText(value, &result);
70 break;
71 case 6:
72 fmt->ParseNum(value, &result);
73 break;
74 case 7:
75 fmt->ParseDateTime(value, kTypes[type_selector], &dt);
76 break;
77 case 8:
78 fmt->ParseNull(value);
79 break;
80 case 9:
81 fmt->ParseZero(value);
82 break;
83 }
84 return 0;
85 }
86