xref: /aosp_15_r20/external/pdfium/testing/fuzzers/pdf_font_fuzzer.cc (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2017 The PDFium Authors
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 "public/cpp/fpdf_scopers.h"
6 #include "public/fpdf_edit.h"
7 #include "public/fpdfview.h"
8 
9 static constexpr size_t kMaxFuzzBytes = 1024 * 1024 * 1024;  // 1 GB.
10 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)11 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
12   if (size < 2 || size > kMaxFuzzBytes)
13     return 0;
14 
15   ScopedFPDFDocument doc(FPDF_CreateNewDocument());
16   ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 612, 792));
17   int font_type = data[0];
18   FPDF_BOOL cid = data[1];
19   data += 2;
20   size -= 2;
21   ScopedFPDFFont font(FPDFText_LoadFont(
22       doc.get(), data, static_cast<uint32_t>(size), font_type, cid));
23   if (!font)
24     return 0;
25 
26   FPDF_PAGEOBJECT text_object =
27       FPDFPageObj_CreateTextObj(doc.get(), font.get(), 12.0f);
28   FPDFPage_InsertObject(page.get(), text_object);
29   FPDFPage_GenerateContent(page.get());
30   return 0;
31 }
32