blob: 80a112d32bf659346a1bf497d2f94230f8b43099 [file] [log] [blame]
Oliver Change67d2182016-02-16 11:42:07 -08001// Copyright 2016 The 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 <cstddef>
6#include <cstdint>
Oliver Change67d2182016-02-16 11:42:07 -08007#include <memory>
8
Dan Sinclair3b71d262017-04-19 08:58:54 -04009#include "core/fxcrt/cfx_seekablestreamproxy.h"
npm43c8a6a2016-09-30 08:37:51 -070010#include "core/fxcrt/fx_basic.h"
thestig62114cf2016-11-08 12:59:30 -080011#include "core/fxcrt/fx_safe_types.h"
npm43c8a6a2016-09-30 08:37:51 -070012#include "core/fxcrt/fx_system.h"
Dan Sinclair0d86ecb2017-04-19 09:19:57 -040013#include "core/fxcrt/xml/cfx_xmldoc.h"
14#include "core/fxcrt/xml/cfx_xmlnode.h"
15#include "core/fxcrt/xml/cfx_xmlparser.h"
thestig62114cf2016-11-08 12:59:30 -080016#include "third_party/base/ptr_util.h"
Oliver Change67d2182016-02-16 11:42:07 -080017
18namespace {
19
Dan Sinclair0d86ecb2017-04-19 09:19:57 -040020CFX_XMLNode* XFA_FDEExtension_GetDocumentNode(
21 CFX_XMLDoc* pXMLDoc,
tsepez304bb912016-11-03 06:10:26 -070022 bool bVerifyWellFormness = false) {
Oliver Change67d2182016-02-16 11:42:07 -080023 if (!pXMLDoc) {
24 return nullptr;
25 }
Dan Sinclair0d86ecb2017-04-19 09:19:57 -040026 CFX_XMLNode* pXMLFakeRoot = pXMLDoc->GetRoot();
27 for (CFX_XMLNode* pXMLNode =
28 pXMLFakeRoot->GetNodeItem(CFX_XMLNode::FirstChild);
29 pXMLNode; pXMLNode = pXMLNode->GetNodeItem(CFX_XMLNode::NextSibling)) {
30 if (pXMLNode->GetType() == FX_XMLNODE_Element) {
Oliver Change67d2182016-02-16 11:42:07 -080031 if (bVerifyWellFormness) {
Dan Sinclair0d86ecb2017-04-19 09:19:57 -040032 for (CFX_XMLNode* pNextNode =
33 pXMLNode->GetNodeItem(CFX_XMLNode::NextSibling);
Oliver Change67d2182016-02-16 11:42:07 -080034 pNextNode;
Dan Sinclair0d86ecb2017-04-19 09:19:57 -040035 pNextNode = pNextNode->GetNodeItem(CFX_XMLNode::NextSibling)) {
36 if (pNextNode->GetType() == FX_XMLNODE_Element) {
tsepez919e48d2016-11-01 14:40:44 -070037 return nullptr;
Oliver Change67d2182016-02-16 11:42:07 -080038 }
39 }
40 }
41 return pXMLNode;
42 }
43 }
44 return nullptr;
45}
46
47} // namespace
48
49extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
thestig62114cf2016-11-08 12:59:30 -080050 FX_SAFE_STRSIZE safe_size = size;
51 if (!safe_size.IsValid())
Oliver Change67d2182016-02-16 11:42:07 -080052 return 0;
53
Dan Sinclair3b71d262017-04-19 08:58:54 -040054 CFX_RetainPtr<CFX_SeekableStreamProxy> stream =
55 pdfium::MakeRetain<CFX_SeekableStreamProxy>(const_cast<uint8_t*>(data),
56 size);
Dan Sinclair0d86ecb2017-04-19 09:19:57 -040057 auto doc = pdfium::MakeUnique<CFX_XMLDoc>();
58 if (!doc->LoadXML(pdfium::MakeUnique<CFX_XMLParser>(doc->GetRoot(), stream)))
Oliver Change67d2182016-02-16 11:42:07 -080059 return 0;
60
Dan Sinclairdf673c22017-05-04 12:09:52 -040061 if (doc->DoLoad() < 100)
Oliver Change67d2182016-02-16 11:42:07 -080062 return 0;
63
64 (void)XFA_FDEExtension_GetDocumentNode(doc.get());
65 return 0;
66}