From 3f7ff0540e7913ddc2c2d4f60e144cc0c7700e9e Mon Sep 17 00:00:00 2001 From: Artem Strygin Date: Thu, 27 Jul 2017 22:16:13 +0300 Subject: Unify FPDF_LoadXXXDocument functions. Change-Id: I31f3f926fa0004e30714b2a579974155c363e065 Reviewed-on: https://pdfium-review.googlesource.com/9390 Commit-Queue: Lei Zhang Reviewed-by: Lei Zhang --- fpdfsdk/fpdfview.cpp | 69 ++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp index c51ee90fe0..355fcb3f8f 100644 --- a/fpdfsdk/fpdfview.cpp +++ b/fpdfsdk/fpdfview.cpp @@ -272,6 +272,26 @@ bool CFPDF_FileStream::Flush() { } #endif // PDF_ENABLE_XFA +FPDF_DOCUMENT LoadDocumentImpl( + const CFX_RetainPtr& pFileAccess, + FPDF_BYTESTRING password) { + if (!pFileAccess) + return nullptr; + + auto pParser = pdfium::MakeUnique(); + pParser->SetPassword(password); + + auto pDocument = pdfium::MakeUnique(std::move(pParser)); + CPDF_Parser::Error error = + pDocument->GetParser()->StartParse(pFileAccess, pDocument.get()); + if (error != CPDF_Parser::SUCCESS) { + ProcessParseError(error); + return nullptr; + } + CheckUnSupportError(pDocument.get(), error); + return FPDFDocumentFromCPDFDocument(pDocument.release()); +} + } // namespace UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) { @@ -492,23 +512,9 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BYTESTRING password) { // NOTE: the creation of the file needs to be by the embedder on the // other side of this API. - CFX_RetainPtr pFileAccess = - IFX_SeekableReadStream::CreateFromFilename((const char*)file_path); - if (!pFileAccess) - return nullptr; - - auto pParser = pdfium::MakeUnique(); - pParser->SetPassword(password); - - auto pDocument = pdfium::MakeUnique(std::move(pParser)); - CPDF_Parser::Error error = - pDocument->GetParser()->StartParse(pFileAccess, pDocument.get()); - if (error != CPDF_Parser::SUCCESS) { - ProcessParseError(error); - return nullptr; - } - CheckUnSupportError(pDocument.get(), error); - return FPDFDocumentFromCPDFDocument(pDocument.release()); + return LoadDocumentImpl( + IFX_SeekableReadStream::CreateFromFilename((const char*)file_path), + password); } #ifdef PDF_ENABLE_XFA @@ -574,37 +580,14 @@ class CMemFile final : public IFX_SeekableReadStream { DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password) { - CFX_RetainPtr pMemFile = CMemFile::Create((uint8_t*)data_buf, size); - auto pParser = pdfium::MakeUnique(); - pParser->SetPassword(password); - - auto pDocument = pdfium::MakeUnique(std::move(pParser)); - CPDF_Parser::Error error = - pDocument->GetParser()->StartParse(pMemFile, pDocument.get()); - if (error != CPDF_Parser::SUCCESS) { - ProcessParseError(error); - return nullptr; - } - CheckUnSupportError(pDocument.get(), error); - return FPDFDocumentFromCPDFDocument(pDocument.release()); + return LoadDocumentImpl(CMemFile::Create((uint8_t*)data_buf, size), password); } DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess, FPDF_BYTESTRING password) { - auto pFile = pdfium::MakeRetain(pFileAccess); - auto pParser = pdfium::MakeUnique(); - pParser->SetPassword(password); - - auto pDocument = pdfium::MakeUnique(std::move(pParser)); - CPDF_Parser::Error error = - pDocument->GetParser()->StartParse(pFile, pDocument.get()); - if (error != CPDF_Parser::SUCCESS) { - ProcessParseError(error); - return nullptr; - } - CheckUnSupportError(pDocument.get(), error); - return FPDFDocumentFromCPDFDocument(pDocument.release()); + return LoadDocumentImpl(pdfium::MakeRetain(pFileAccess), + password); } DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc, -- cgit v1.2.3