/src/xpdf-4.06/build/fuzz_pdfload.cc
Line | Count | Source |
1 | | /* Copyright 2020 Google Inc. |
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 <fuzzer/FuzzedDataProvider.h> |
16 | | |
17 | | #include <vector> |
18 | | #include <aconf.h> |
19 | | #include <stdio.h> |
20 | | #include <stdint.h> |
21 | | #include <stdlib.h> |
22 | | #include <stddef.h> |
23 | | #include <string.h> |
24 | | #include <png.h> |
25 | | |
26 | | #include "gmem.h" |
27 | | #include "gmempp.h" |
28 | | #include "parseargs.h" |
29 | | #include "GString.h" |
30 | | #include "gfile.h" |
31 | | #include "GlobalParams.h" |
32 | | #include "Object.h" |
33 | | #include "PDFDoc.h" |
34 | | #include "SplashBitmap.h" |
35 | | #include "Splash.h" |
36 | | #include "SplashOutputDev.h" |
37 | | #include "Stream.h" |
38 | | #include "config.h" |
39 | | #include "JBIG2Stream.h" |
40 | | |
41 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
42 | 14.8k | { |
43 | 14.8k | FuzzedDataProvider fdp (data, size); |
44 | 14.8k | double hdpi = fdp.ConsumeFloatingPoint<double>(); |
45 | 14.8k | double vdpi = fdp.ConsumeFloatingPoint<double>(); |
46 | 14.8k | int rotate = fdp.ConsumeIntegral<int>(); |
47 | 14.8k | bool useMediaBox = fdp.ConsumeBool(); |
48 | 14.8k | bool crop = fdp.ConsumeBool(); |
49 | 14.8k | bool printing = fdp.ConsumeBool(); |
50 | 14.8k | std::vector<char> payload = fdp.ConsumeRemainingBytes<char>(); |
51 | | |
52 | 14.8k | Object xpdf_obj; |
53 | 14.8k | xpdf_obj.initNull(); |
54 | 14.8k | BaseStream *stream = new MemStream(payload.data(), 0, payload.size(), &xpdf_obj); |
55 | | |
56 | 14.8k | Object info, xfa; |
57 | 14.8k | Object *acroForm; |
58 | 14.8k | globalParams = new GlobalParams(NULL); |
59 | 14.8k | globalParams->setErrQuiet(1); |
60 | 14.8k | globalParams->setupBaseFonts(NULL); |
61 | 14.8k | char yes[] = "yes"; |
62 | 14.8k | globalParams->setEnableFreeType(yes); // Yes, it's a string and not a bool. |
63 | 14.8k | globalParams->setErrQuiet(1); |
64 | | |
65 | 14.8k | PDFDoc *doc = NULL; |
66 | 14.8k | try { |
67 | 14.8k | PDFDoc doc(stream); |
68 | 14.8k | if (doc.isOk() == gTrue) |
69 | 10.2k | { |
70 | 10.2k | doc.getNumPages(); |
71 | 10.2k | doc.getOutline(); |
72 | 10.2k | doc.getStructTreeRoot(); |
73 | 10.2k | doc.getXRef(); |
74 | 10.2k | doc.okToPrint(gTrue); |
75 | 10.2k | doc.okToCopy(gTrue); |
76 | 10.2k | doc.okToChange(gTrue); |
77 | 10.2k | doc.okToAddNotes(gTrue); |
78 | 10.2k | doc.isLinearized(); |
79 | 10.2k | doc.getPDFVersion(); |
80 | | |
81 | 10.2k | GString *metadata; |
82 | 10.2k | if ((metadata = doc.readMetadata())) { |
83 | 906 | (void)metadata->getCString(); |
84 | 906 | } |
85 | 10.2k | delete metadata; |
86 | | |
87 | 10.2k | Object info; |
88 | 10.2k | doc.getDocInfo(&info); |
89 | 10.2k | if (info.isDict()) { |
90 | 2.48k | info.getDict(); |
91 | 2.48k | } |
92 | 10.2k | info.free(); |
93 | | |
94 | 10.2k | if ((acroForm = doc.getCatalog()->getAcroForm())->isDict()) { |
95 | 950 | acroForm->dictLookup("XFA", &xfa); |
96 | 950 | xfa.free(); |
97 | 950 | } |
98 | | |
99 | 242k | for (size_t i = 1; i <= doc.getNumPages(); i++) { |
100 | 232k | doc.getLinks(i); |
101 | 232k | auto page = doc.getCatalog()->getPage(i); |
102 | 232k | if (!page->isOk()) { |
103 | 0 | continue; |
104 | 0 | } |
105 | 232k | page->getResourceDict(); |
106 | 232k | page->getMetadata(); |
107 | 232k | page->getResourceDict(); |
108 | 232k | } |
109 | | |
110 | 10.2k | SplashColor paperColor = {0xff, 0xff, 0xff}; |
111 | 10.2k | SplashOutputDev *splashOut = new SplashOutputDev(splashModeRGB8, 1, gFalse, paperColor); |
112 | 10.2k | splashOut->setNoComposite(gTrue); |
113 | 10.2k | splashOut->startDoc(doc.getXRef()); |
114 | 242k | for (size_t i = 1; i <= doc.getNumPages(); ++i) { |
115 | 232k | doc.displayPage(splashOut, NULL, i, hdpi, vdpi, rotate, useMediaBox, crop, printing); |
116 | 232k | } |
117 | 10.2k | (void)splashOut->getBitmap(); |
118 | | |
119 | 10.2k | delete splashOut; |
120 | 10.2k | } |
121 | 14.8k | } catch (...) { |
122 | | |
123 | 70 | } |
124 | | |
125 | 14.8k | delete globalParams; |
126 | | |
127 | 14.8k | return 0; |
128 | 14.8k | } |
129 | | |