From 525147a1f6d6cd736a407d1e189ac25d2f4726e8 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 3 May 2018 17:19:53 +0000 Subject: Use strict types in FPDF API, try #3 Rather than messing with actual inheritence, add type-checking wrappers and just blatantly cast to incomplete types. Along the way, this points out places where we would downcast without checking, which I fix. Change-Id: Ieb303eb46ad8522dfe082454f1f10f247ffd52d5 Reviewed-on: https://pdfium-review.googlesource.com/32030 Reviewed-by: dsinclair Commit-Queue: Tom Sepez --- fpdfsdk/fpdf_text.cpp | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'fpdfsdk/fpdf_text.cpp') diff --git a/fpdfsdk/fpdf_text.cpp b/fpdfsdk/fpdf_text.cpp index c06885a1a7..edca0669ab 100644 --- a/fpdfsdk/fpdf_text.cpp +++ b/fpdfsdk/fpdf_text.cpp @@ -7,6 +7,7 @@ #include "public/fpdf_text.h" #include +#include #include #include "core/fpdfapi/page/cpdf_page.h" @@ -16,6 +17,7 @@ #include "core/fpdftext/cpdf_textpagefind.h" #include "fpdfsdk/cpdfsdk_helpers.h" #include "third_party/base/numerics/safe_conversions.h" +#include "third_party/base/ptr_util.h" #include "third_party/base/stl_util.h" #ifdef PDF_ENABLE_XFA @@ -31,18 +33,6 @@ namespace { constexpr size_t kBytesPerCharacter = sizeof(unsigned short); -CPDF_TextPage* CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE text_page) { - return static_cast(text_page); -} - -CPDF_TextPageFind* CPDFTextPageFindFromFPDFSchHandle(FPDF_SCHHANDLE handle) { - return static_cast(handle); -} - -CPDF_LinkExtract* CPDFLinkExtractFromFPDFPageLink(FPDF_PAGELINK link) { - return static_cast(link); -} - } // namespace FPDF_EXPORT FPDF_TEXTPAGE FPDF_CALLCONV FPDFText_LoadPage(FPDF_PAGE page) { @@ -62,7 +52,7 @@ FPDF_EXPORT FPDF_TEXTPAGE FPDF_CALLCONV FPDFText_LoadPage(FPDF_PAGE page) { pPDFPage, viewRef.IsDirectionR2L() ? FPDFText_Direction::Right : FPDFText_Direction::Left); textpage->ParseTextPage(); - return textpage; + return FPDFTextPageFromCPDFTextPage(textpage); } FPDF_EXPORT void FPDF_CALLCONV FPDFText_ClosePage(FPDF_TEXTPAGE text_page) { @@ -269,7 +259,7 @@ FPDFText_FindStart(FPDF_TEXTPAGE text_page, textpageFind->FindFirst( WideString::FromUTF16LE(findwhat, len), flags, start_index >= 0 ? Optional(start_index) : Optional()); - return textpageFind; + return FPDFSchHandleFromCPDFTextPageFind(textpageFind); } FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_FindNext(FPDF_SCHHANDLE handle) { @@ -309,9 +299,9 @@ FPDF_EXPORT void FPDF_CALLCONV FPDFText_FindClose(FPDF_SCHHANDLE handle) { if (!handle) return; - CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); - delete textpageFind; - handle = nullptr; + // Take ownership back from caller and destroy. + std::unique_ptr textpageFind( + CPDFTextPageFindFromFPDFSchHandle(handle)); } // web link @@ -320,10 +310,12 @@ FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page) { if (!text_page) return nullptr; - CPDF_LinkExtract* pageLink = - new CPDF_LinkExtract(CPDFTextPageFromFPDFTextPage(text_page)); + CPDF_TextPage* pPage = CPDFTextPageFromFPDFTextPage(text_page); + auto pageLink = pdfium::MakeUnique(pPage); pageLink->ExtractLinks(); - return pageLink; + + // Caller takes ownership. + return FPDFPageLinkFromCPDFLinkExtract(pageLink.release()); } FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountWebLinks(FPDF_PAGELINK link_page) { -- cgit v1.2.3