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 <stdlib.h>
16 #include <string.h>
17 #include <stdint.h>
18 #include <exception>
19 #include "PDFDoc.h"
20 #include "GlobalParams.h"
21 #include "Zoox.h"
22 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)23 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
24 {
25     char filename[256];
26     sprintf(filename, "/tmp/libfuzzer.%d", getpid());
27     FILE *fp = fopen(filename, "wb");
28     if (!fp)
29         return 0;
30     fwrite(data, size, 1, fp);
31     fclose(fp);
32 
33     // Main fuzzing logic
34     Object info, xfa;
35     Object *acroForm;
36     globalParams = new GlobalParams(NULL);
37     globalParams->setErrQuiet(1);
38     globalParams->setupBaseFonts(NULL);
39 
40     PDFDoc *doc = NULL;
41     try {
42         doc = new PDFDoc(filename, NULL, NULL);
43         if (doc->isOk() == gTrue)
44         {
45             doc->getNumPages();
46             if ((acroForm = doc->getCatalog()->getAcroForm())->isDict()) {
47                 acroForm->dictLookup("XFA", &xfa);
48                 xfa.free();
49             }
50         }
51     } catch (...) {
52 
53     }
54 
55     // Cleanup
56     if (doc != NULL)
57         delete doc;
58     delete globalParams;
59 
60     // cleanup temporary file
61     unlink(filename);
62     return 0;
63 }
64 
65