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