xref: /aosp_15_r20/external/libprotobuf-mutator/examples/expat/expat_example.cc (revision fd525a9c096e28cf6f8d8719388df0568a611e7b)
1*fd525a9cSAndroid Build Coastguard Worker // Copyright 2017 Google Inc. All rights reserved.
2*fd525a9cSAndroid Build Coastguard Worker //
3*fd525a9cSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*fd525a9cSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*fd525a9cSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*fd525a9cSAndroid Build Coastguard Worker //
7*fd525a9cSAndroid Build Coastguard Worker //     http://www.apache.org/licenses/LICENSE-2.0
8*fd525a9cSAndroid Build Coastguard Worker //
9*fd525a9cSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*fd525a9cSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*fd525a9cSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*fd525a9cSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*fd525a9cSAndroid Build Coastguard Worker // limitations under the License.
14*fd525a9cSAndroid Build Coastguard Worker 
15*fd525a9cSAndroid Build Coastguard Worker #include <vector>
16*fd525a9cSAndroid Build Coastguard Worker 
17*fd525a9cSAndroid Build Coastguard Worker #include "expat.h"  // NOLINT
18*fd525a9cSAndroid Build Coastguard Worker 
19*fd525a9cSAndroid Build Coastguard Worker #include "examples/xml/xml.pb.h"
20*fd525a9cSAndroid Build Coastguard Worker #include "examples/xml/xml_writer.h"
21*fd525a9cSAndroid Build Coastguard Worker #include "src/libfuzzer/libfuzzer_macro.h"
22*fd525a9cSAndroid Build Coastguard Worker 
23*fd525a9cSAndroid Build Coastguard Worker namespace {
24*fd525a9cSAndroid Build Coastguard Worker protobuf_mutator::protobuf::LogSilencer log_silincer;
25*fd525a9cSAndroid Build Coastguard Worker std::vector<const char*> kEncodings = {{"UTF-16", "UTF-8", "ISO-8859-1",
26*fd525a9cSAndroid Build Coastguard Worker                                         "US-ASCII", "UTF-16BE", "UTF-16LE",
27*fd525a9cSAndroid Build Coastguard Worker                                         "INVALIDENCODING"}};
28*fd525a9cSAndroid Build Coastguard Worker }
29*fd525a9cSAndroid Build Coastguard Worker 
DEFINE_PROTO_FUZZER(const protobuf_mutator::xml::Input & message)30*fd525a9cSAndroid Build Coastguard Worker DEFINE_PROTO_FUZZER(const protobuf_mutator::xml::Input& message) {
31*fd525a9cSAndroid Build Coastguard Worker   std::string xml = MessageToXml(message.document());
32*fd525a9cSAndroid Build Coastguard Worker   int options = message.options();
33*fd525a9cSAndroid Build Coastguard Worker 
34*fd525a9cSAndroid Build Coastguard Worker   int use_ns = options % 2;
35*fd525a9cSAndroid Build Coastguard Worker   options /= 2;
36*fd525a9cSAndroid Build Coastguard Worker   auto enc = kEncodings[options % kEncodings.size()];
37*fd525a9cSAndroid Build Coastguard Worker   XML_Parser parser =
38*fd525a9cSAndroid Build Coastguard Worker       use_ns ? XML_ParserCreateNS(enc, '\n') : XML_ParserCreate(enc);
39*fd525a9cSAndroid Build Coastguard Worker   XML_Parse(parser, xml.data(), xml.size(), true);
40*fd525a9cSAndroid Build Coastguard Worker   XML_ParserFree(parser);
41*fd525a9cSAndroid Build Coastguard Worker }
42