Coverage Report

Created: 2022-03-21 07:20

/src/xpdf-4.03/build/fuzz_pdfload.cc
Line
Count
Source (jump to first uncovered line)
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
40
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
41
36
{
42
36
    FuzzedDataProvider fdp (data, size);
43
36
    double hdpi = fdp.ConsumeFloatingPoint<double>();
44
36
    double vdpi = fdp.ConsumeFloatingPoint<double>();
45
36
    int rotate = fdp.ConsumeIntegral<int>();
46
36
    bool useMediaBox = fdp.ConsumeBool();
47
36
    bool crop = fdp.ConsumeBool();
48
36
    bool printing = fdp.ConsumeBool();
49
36
    std::vector<char> payload = fdp.ConsumeRemainingBytes<char>();
50
51
36
    Object xpdf_obj;
52
36
    xpdf_obj.initNull();
53
36
    BaseStream *stream = new MemStream(payload.data(), 0, payload.size(), &xpdf_obj);
54
55
36
    Object info, xfa;
56
36
    Object *acroForm;
57
36
    globalParams = new GlobalParams(NULL);
58
36
    globalParams->setErrQuiet(1);
59
36
    globalParams->setupBaseFonts(NULL);
60
36
    char yes[] = "yes";
61
36
    globalParams->setEnableFreeType(yes);  // Yes, it's a string and not a bool.
62
36
    globalParams->setErrQuiet(1);
63
64
36
    PDFDoc *doc = NULL;
65
36
    try {
66
36
      PDFDoc doc(stream);
67
36
        if (doc.isOk() == gTrue)
68
0
        {
69
0
            doc.getNumPages();
70
0
            doc.getOutline();
71
0
            doc.getStructTreeRoot();
72
0
            doc.getXRef();
73
0
            doc.okToPrint(gTrue);
74
0
            doc.okToCopy(gTrue);
75
0
            doc.okToChange(gTrue);
76
0
            doc.okToAddNotes(gTrue);
77
0
            doc.isLinearized();
78
0
            doc.getPDFVersion();
79
80
0
            GString *metadata;
81
0
            if ((metadata = doc.readMetadata())) {
82
0
              (void)metadata->getCString();
83
0
            }
84
0
            delete metadata;
85
86
0
            Object info;
87
0
            doc.getDocInfo(&info);
88
0
            if (info.isDict()) {
89
0
              info.getDict();
90
0
            }
91
0
            info.free();
92
93
0
            if ((acroForm = doc.getCatalog()->getAcroForm())->isDict()) {
94
0
                acroForm->dictLookup("XFA", &xfa);
95
0
                xfa.free();
96
0
            }
97
98
0
            for (size_t i = 0; i < doc.getNumPages(); i++) {
99
0
              doc.getLinks(i);
100
0
              auto page = doc.getCatalog()->getPage(i);
101
0
              if (!page->isOk()) {
102
0
                continue;
103
0
              }
104
0
              page->getResourceDict();
105
0
              page->getMetadata();
106
0
              page->getResourceDict();
107
0
            }
108
109
0
            SplashColor paperColor = {0xff, 0xff, 0xff};
110
0
            SplashOutputDev *splashOut = new SplashOutputDev(splashModeRGB8, 1, gFalse, paperColor);
111
0
            splashOut->setNoComposite(gTrue);
112
0
            splashOut->startDoc(doc.getXRef());
113
0
            for (size_t i = 0; i <= doc.getNumPages(); ++i) {
114
0
              doc.displayPage(splashOut, i, hdpi, vdpi, rotate, useMediaBox, crop, printing);
115
0
            }
116
0
            (void)splashOut->getBitmap();
117
118
0
            delete splashOut;
119
0
        }
120
36
    } catch (...) {
121
122
1
    }
123
124
36
    delete globalParams;
125
126
36
    return 0;
127
36
}
128