xref: /aosp_15_r20/external/cronet/third_party/ced/compact_enc_det_fuzzer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
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 <stddef.h>
6 #include <stdint.h>
7 
8 #include <fuzzer/FuzzedDataProvider.h>
9 
10 #include <vector>
11 
12 #include "third_party/ced/src/compact_enc_det/compact_enc_det.h"
13 
14 namespace {
15 constexpr size_t kMaxInputSize = 64 * 1024;
16 }
17 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)18 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
19   // Early out if there isn't enough data to extract options and pass data to
20   // the library.
21   if (size < 3 * sizeof(int32_t) + 1)
22     return 0;
23 
24   // Limit the input size to avoid timing out on clusterfuzz.
25   if (size > kMaxInputSize)
26     return 0;
27 
28   FuzzedDataProvider data_provider(data, size);
29 
30   CompactEncDet::TextCorpusType corpus =
31       static_cast<CompactEncDet::TextCorpusType>(
32           data_provider.ConsumeIntegralInRange<int32_t>(
33               0, CompactEncDet::NUM_CORPA));
34   Encoding encoding_hint = static_cast<Encoding>(
35       data_provider.ConsumeIntegralInRange<int32_t>(0, NUM_ENCODINGS));
36   Language langauge_hint = static_cast<Language>(
37       data_provider.ConsumeIntegralInRange<int32_t>(0, NUM_LANGUAGES));
38   bool ignore_7bit_mail_encodings = data_provider.ConsumeBool();
39 
40   std::vector<char> text = data_provider.ConsumeRemainingBytes<char>();
41   int bytes_consumed = 0;
42   bool is_reliable = false;
43   CompactEncDet::DetectEncoding(
44       text.data(), text.size(), nullptr /* url_hint */,
45       nullptr /* http_charset_hint */, nullptr /* meta_charset_hint */,
46       encoding_hint, langauge_hint, corpus, ignore_7bit_mail_encodings,
47       &bytes_consumed, &is_reliable);
48 
49   return 0;
50 }
51