OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 The Chromium 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 // This fuzzer is simplified & cleaned up pdfium/samples/pdfium_test.cc |
| 6 |
| 7 #include <limits.h> |
| 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
| 10 #include <string.h> |
| 11 |
| 12 #include <list> |
| 13 #include <sstream> |
| 14 #include <string> |
| 15 #include <utility> |
| 16 #include <vector> |
| 17 |
| 18 #include "third_party/pdfium/public/fpdf_dataavail.h" |
| 19 #include "third_party/pdfium/public/fpdf_ext.h" |
| 20 #include "third_party/pdfium/public/fpdf_formfill.h" |
| 21 #include "third_party/pdfium/public/fpdf_text.h" |
| 22 #include "third_party/pdfium/public/fpdfview.h" |
| 23 #include "third_party/pdfium/testing/test_support.h" |
| 24 |
| 25 #include "v8/include/v8.h" |
| 26 |
| 27 static int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, |
| 28 int, int) { |
| 29 return 0; |
| 30 } |
| 31 |
| 32 static void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) { } |
| 33 |
| 34 static void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) { } |
| 35 |
| 36 |
| 37 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 38 return true; |
| 39 } |
| 40 |
| 41 static void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { } |
| 42 |
| 43 static bool RenderPage(const FPDF_DOCUMENT& doc, |
| 44 const FPDF_FORMHANDLE& form, |
| 45 const int page_index) { |
| 46 FPDF_PAGE page = FPDF_LoadPage(doc, page_index); |
| 47 if (!page) { |
| 48 return false; |
| 49 } |
| 50 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); |
| 51 FORM_OnAfterLoadPage(page, form); |
| 52 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
| 53 |
| 54 double scale = 1.0; |
| 55 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); |
| 56 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); |
| 57 |
| 58 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); |
| 59 if (!bitmap) { |
| 60 return false; |
| 61 } |
| 62 |
| 63 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 64 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
| 65 |
| 66 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
| 67 |
| 68 FPDFBitmap_Destroy(bitmap); |
| 69 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 70 FORM_OnBeforeClosePage(page, form); |
| 71 FPDFText_ClosePage(text_page); |
| 72 FPDF_ClosePage(page); |
| 73 return true; |
| 74 } |
| 75 |
| 76 static void RenderPdf(const char* pBuf, size_t len) { |
| 77 IPDF_JSPLATFORM platform_callbacks; |
| 78 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
| 79 platform_callbacks.version = 3; |
| 80 platform_callbacks.app_alert = ExampleAppAlert; |
| 81 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; |
| 82 |
| 83 FPDF_FORMFILLINFO form_callbacks; |
| 84 memset(&form_callbacks, '\0', sizeof(form_callbacks)); |
| 85 form_callbacks.version = 1; |
| 86 form_callbacks.m_pJsPlatform = &platform_callbacks; |
| 87 |
| 88 TestLoader loader(pBuf, len); |
| 89 FPDF_FILEACCESS file_access; |
| 90 memset(&file_access, '\0', sizeof(file_access)); |
| 91 file_access.m_FileLen = static_cast<unsigned long>(len); |
| 92 file_access.m_GetBlock = TestLoader::GetBlock; |
| 93 file_access.m_Param = &loader; |
| 94 |
| 95 FX_FILEAVAIL file_avail; |
| 96 memset(&file_avail, '\0', sizeof(file_avail)); |
| 97 file_avail.version = 1; |
| 98 file_avail.IsDataAvail = Is_Data_Avail; |
| 99 |
| 100 FX_DOWNLOADHINTS hints; |
| 101 memset(&hints, '\0', sizeof(hints)); |
| 102 hints.version = 1; |
| 103 hints.AddSegment = Add_Segment; |
| 104 |
| 105 FPDF_DOCUMENT doc; |
| 106 int nRet = PDF_DATA_NOTAVAIL; |
| 107 bool bIsLinearized = false; |
| 108 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); |
| 109 |
| 110 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) { |
| 111 doc = FPDFAvail_GetDocument(pdf_avail, nullptr); |
| 112 if (doc) { |
| 113 while (nRet == PDF_DATA_NOTAVAIL) { |
| 114 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints); |
| 115 } |
| 116 if (nRet == PDF_DATA_ERROR) { |
| 117 return; |
| 118 } |
| 119 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints); |
| 120 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) { |
| 121 return; |
| 122 } |
| 123 bIsLinearized = true; |
| 124 } |
| 125 } else { |
| 126 doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 127 } |
| 128 |
| 129 if (!doc) { |
| 130 FPDFAvail_Destroy(pdf_avail); |
| 131 return; |
| 132 } |
| 133 |
| 134 (void)FPDF_GetDocPermissions(doc); |
| 135 |
| 136 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); |
| 137 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); |
| 138 FPDF_SetFormFieldHighlightAlpha(form, 100); |
| 139 |
| 140 FORM_DoDocumentJSAction(form); |
| 141 FORM_DoDocumentOpenAction(form); |
| 142 |
| 143 int page_count = FPDF_GetPageCount(doc); |
| 144 |
| 145 for (int i = 0; i < page_count; ++i) { |
| 146 if (bIsLinearized) { |
| 147 nRet = PDF_DATA_NOTAVAIL; |
| 148 while (nRet == PDF_DATA_NOTAVAIL) { |
| 149 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); |
| 150 } |
| 151 if (nRet == PDF_DATA_ERROR) { |
| 152 return; |
| 153 } |
| 154 } |
| 155 RenderPage(doc, form, i); |
| 156 } |
| 157 |
| 158 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
| 159 FPDFDOC_ExitFormFillEnvironment(form); |
| 160 FPDF_CloseDocument(doc); |
| 161 FPDFAvail_Destroy(pdf_avail); |
| 162 } |
| 163 |
| 164 static v8::Platform* Init() { |
| 165 v8::Platform* platform; |
| 166 InitializeV8ForPDFium(&platform); |
| 167 |
| 168 FPDF_LIBRARY_CONFIG config; |
| 169 config.version = 2; |
| 170 config.m_pUserFontPaths = nullptr; |
| 171 config.m_pIsolate = nullptr; |
| 172 config.m_v8EmbedderSlot = 0; |
| 173 |
| 174 FPDF_InitLibraryWithConfig(&config); |
| 175 |
| 176 UNSUPPORT_INFO unsuppored_info; |
| 177 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
| 178 unsuppored_info.version = 1; |
| 179 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler; |
| 180 |
| 181 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 182 |
| 183 return platform; |
| 184 } |
| 185 |
| 186 static v8::Platform* platform = Init(); |
| 187 |
| 188 extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, size_t size) { |
| 189 RenderPdf(reinterpret_cast<const char*>(data), size); |
| 190 return 0; |
| 191 } |
OLD | NEW |