Fix -Wnon-virtual-dtor compiler warnings.
[pdfium.git] / fpdfsdk / src / fpdfview.cpp
index a5ba796..6537023 100644 (file)
@@ -5,48 +5,30 @@
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
 #include "../include/fsdk_define.h"
+#include "../include/fsdk_mgr.h"
 #include "../include/fpdfview.h"
 #include "../include/fsdk_rendercontext.h"
 #include "../include/fpdf_progressive.h"
 #include "../include/fpdf_ext.h"
-
+#include "../../third_party/numerics/safe_conversions_impl.h"
 
 CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
 {
-       m_FileAccess = *pFileAccess;
-       m_BufferOffset = (FX_DWORD)-1;
-}
-
-FX_BOOL CPDF_CustomAccess::GetByte(FX_DWORD pos, FX_BYTE& ch)
-{
-       if (pos >= m_FileAccess.m_FileLen) return FALSE;
-       if (m_BufferOffset == (FX_DWORD)-1 || pos < m_BufferOffset || pos >= m_BufferOffset + 512) {
-               // Need to read from file access
-               m_BufferOffset = pos;
-               int size = 512;
-               if (pos + 512 > m_FileAccess.m_FileLen)
-                       size = m_FileAccess.m_FileLen - pos;
-               if (!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, m_BufferOffset, m_Buffer, size))
-                       return FALSE;
-       }
-       ch = m_Buffer[pos - m_BufferOffset];
-       return TRUE;
-}
-
-FX_BOOL CPDF_CustomAccess::GetBlock(FX_DWORD pos, FX_LPBYTE pBuf, FX_DWORD size)
-{
-       if (pos + size > m_FileAccess.m_FileLen) return FALSE;
-       return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, pos, pBuf, size);
+       if (pFileAccess)
+               m_FileAccess = *pFileAccess;
 }
 
 FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size)
 {
-       //      m_FileAccess = *pFileAccess;
-       //      m_BufferOffset = (FX_DWORD)-1;
-       if (offset + size > m_FileAccess.m_FileLen) return FALSE;
-       return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,(FX_LPBYTE) buffer, size);
-
-       //      return FALSE;
+    if (offset < 0) {
+        return FALSE;
+    }
+    FX_SAFE_FILESIZE newPos = base::checked_cast<FX_FILESIZE, size_t>(size);
+    newPos += offset;
+    if (!newPos.IsValid() || newPos.ValueOrDie() > m_FileAccess.m_FileLen) {
+        return FALSE;
+    }
+    return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,(FX_LPBYTE) buffer, size);
 }
 
 //0 bit: FPDF_POLICY_MACHINETIME_ACCESS
@@ -96,9 +78,6 @@ FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy)
        CPDF_ModuleMgr* g_pModuleMgr = NULL;
 #else
        CCodec_ModuleMgr*       g_pCodecModule = NULL;
-#ifdef _FXSDK_OPENSOURCE_
-       FXMEM_FoxitMgr* g_pFoxitMgr = NULL;
-#endif
 #endif
 
 //extern CPDFSDK_FormFillApp* g_pFormFillApp;
@@ -127,43 +106,8 @@ public:
 CFontMapper* g_pFontMapper = NULL;
 #endif         // #if _FX_OS_ == _FX_LINUX_EMBEDDED_
 
-DLLEXPORT void STDCALL FPDF_InitLibrary(FX_LPVOID hInstance)
+DLLEXPORT void STDCALL FPDF_InitLibrary()
 {
-#ifdef API5
-       CPDF_ModuleMgr::Create();
-       g_pModuleMgr = CPDF_ModuleMgr::Get();
-        #if _FX_OS_ == _FX_WIN32_MOBILE_ || _FX_OS_ == _FX_LINUX_EMBEDDED_
-               g_pModuleMgr->InitEmbedded();
-        #ifdef _GB1_CMAPS_
-               g_pModuleMgr->LoadEmbeddedGB1CMaps();
-        #endif
-        #ifdef _GB1_CMAPS_4_
-               g_pModuleMgr->LoadEmbeddedGB1CMaps_4();
-        #endif
-        #ifdef _CNS1_CMAPS_
-               g_pModuleMgr->LoadEmbeddedCNS1CMaps();
-        #endif
-        #ifdef _JAPAN1_CMAPS_
-               g_pModuleMgr->LoadEmbeddedJapan1CMaps();
-        #endif
-        #ifdef _JAPAN1_CMAPS_6_
-               g_pModuleMgr->LoadEmbeddedJapan1CMaps_6();
-        #endif
-        #ifdef _KOREA1_CMAPS_
-               g_pModuleMgr->LoadEmbeddedKorea1CMaps();
-        #endif
-        #ifdef _JPX_DECODER_
-               g_pModuleMgr->InitJpxModule();
-               g_pModuleMgr->InitJbig2Module();
-        //     g_pModuleMgr->InitIccModule();
-        #endif
-        #else
-               g_pModuleMgr->InitDesktop();
-        #endif
-#else
-#ifdef _FXSDK_OPENSOURCE_
-       g_pFoxitMgr = FXMEM_CreateMemoryMgr(1024 * 1024 * 32, TRUE);
-#endif
        g_pCodecModule = CCodec_ModuleMgr::Create();
        
        CFX_GEModule::Create();
@@ -173,7 +117,6 @@ DLLEXPORT void STDCALL FPDF_InitLibrary(FX_LPVOID hInstance)
        CPDF_ModuleMgr::Get()->SetCodecModule(g_pCodecModule);
        CPDF_ModuleMgr::Get()->InitPageModule();
        CPDF_ModuleMgr::Get()->InitRenderModule();
-#ifdef FOXIT_CHROME_BUILD
        CPDF_ModuleMgr * pModuleMgr = CPDF_ModuleMgr::Get();
        if ( pModuleMgr )
        {
@@ -182,30 +125,6 @@ DLLEXPORT void STDCALL FPDF_InitLibrary(FX_LPVOID hInstance)
                pModuleMgr->LoadEmbeddedCNS1CMaps();
                pModuleMgr->LoadEmbeddedKorea1CMaps();
        }
-#endif 
-#endif
-
-#ifdef _WIN32
-       // Get module path
-       TCHAR app_path[MAX_PATH];
-       ::GetModuleFileName((HINSTANCE)hInstance, app_path, MAX_PATH);
-       size_t len = _tcslen(app_path);
-       for (size_t i = len; i >= 0; i --)
-               if (app_path[i] == '\\') {
-                       app_path[i] = 0;
-                       break;
-               }
-               
-#ifdef _UNICODE
-               #ifndef _FXSDK_OPENSOURCE_
-               CPDF_ModuleMgr::Get()->SetModulePath(NULL, CFX_ByteString::FromUnicode(app_path));
-               #endif
-#else
-#ifndef _FXSDK_OPENSOURCE_
-               CPDF_ModuleMgr::Get()->SetModulePath(NULL, app_path);
-#endif
-#endif
-#endif
 }
 
 
@@ -222,11 +141,6 @@ DLLEXPORT void STDCALL FPDF_DestroyLibrary()
        CFX_GEModule::Destroy();
        g_pCodecModule->Destroy();
 #endif
-#ifndef _FXSDK_OPENSOURCE_
-       FXMEM_CollectAll(FXMEM_GetDefaultMgr());
-#else
-       FXMEM_DestroyFoxitMgr(g_pFoxitMgr);
-#endif
 }
 
 #ifndef _WIN32
@@ -271,17 +185,11 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BY
 {
        CPDF_Parser* pParser = FX_NEW CPDF_Parser;
        pParser->SetPassword(password);
-       try {
-               FX_DWORD err_code = pParser->StartParse((FX_LPCSTR)file_path);
-               if (err_code) {
-                       delete pParser;
-                       ProcessParseError(err_code);
-                       return NULL;
-               }
-       }
-       catch (...) {
+
+       FX_DWORD err_code = pParser->StartParse((FX_LPCSTR)file_path);
+       if (err_code) {
                delete pParser;
-               SetLastError(FPDF_ERR_UNKNOWN);
+               ProcessParseError(err_code);
                return NULL;
        }
        return pParser->GetDocument();
@@ -289,7 +197,7 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BY
 
 extern void CheckUnSupportError(CPDF_Document * pDoc, FX_DWORD err_code);
 
-class CMemFile: public IFX_FileRead, public CFX_Object
+class CMemFile FX_FINAL: public IFX_FileRead, public CFX_Object
 {
 public:
        CMemFile(FX_BYTE* pBuf, FX_FILESIZE size):m_pBuf(pBuf),m_size(size) {}
@@ -298,9 +206,16 @@ public:
        virtual FX_FILESIZE             GetSize() {return m_size;}
        virtual FX_BOOL                 ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) 
        {
-               if(offset+size > (FX_DWORD)m_size) return FALSE;
-               FXSYS_memcpy(buffer, m_pBuf+offset, size);
-               return TRUE;
+            if (offset < 0) {
+                return FALSE;
+            }
+            FX_SAFE_FILESIZE newPos = base::checked_cast<FX_FILESIZE, size_t>(size);
+            newPos += offset;
+            if (!newPos.IsValid() || newPos.ValueOrDie() > (FX_DWORD)m_size) {
+                return FALSE;
+            }
+           FXSYS_memcpy(buffer, m_pBuf+offset, size);
+           return TRUE;
        }
 private:
        FX_BYTE* m_pBuf;
@@ -310,23 +225,16 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf, int s
 {
        CPDF_Parser* pParser = FX_NEW CPDF_Parser;
        pParser->SetPassword(password);
-       try {
-               CMemFile* pMemFile = FX_NEW CMemFile((FX_BYTE*)data_buf, size);
-               FX_DWORD err_code = pParser->StartParse(pMemFile);
-               if (err_code) {
-                       delete pParser;
-                       ProcessParseError(err_code);
-                       return NULL;
-               }
-               CPDF_Document * pDoc = NULL;
-               pDoc = pParser?pParser->GetDocument():NULL;
-               CheckUnSupportError(pDoc, err_code);
-       }
-       catch (...) {
+       CMemFile* pMemFile = FX_NEW CMemFile((FX_BYTE*)data_buf, size);
+       FX_DWORD err_code = pParser->StartParse(pMemFile);
+       if (err_code) {
                delete pParser;
-               SetLastError(FPDF_ERR_UNKNOWN);
+               ProcessParseError(err_code);
                return NULL;
        }
+       CPDF_Document * pDoc = NULL;
+       pDoc = pParser?pParser->GetDocument():NULL;
+       CheckUnSupportError(pDoc, err_code);
        return pParser->GetDocument();
 }
 
@@ -335,22 +243,15 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAc
        CPDF_Parser* pParser = FX_NEW CPDF_Parser;
        pParser->SetPassword(password);
        CPDF_CustomAccess* pFile = FX_NEW CPDF_CustomAccess(pFileAccess);
-       try {
-               FX_DWORD err_code = pParser->StartParse(pFile);
-               if (err_code) {
-                       delete pParser;
-                       ProcessParseError(err_code);
-                       return NULL;
-               }
-               CPDF_Document * pDoc = NULL;
-               pDoc = pParser?pParser->GetDocument():NULL;
-               CheckUnSupportError(pDoc, err_code);
-       }
-       catch (...) {
+       FX_DWORD err_code = pParser->StartParse(pFile);
+       if (err_code) {
                delete pParser;
-               SetLastError(FPDF_ERR_UNKNOWN);
+               ProcessParseError(err_code);
                return NULL;
        }
+       CPDF_Document * pDoc = NULL;
+       pDoc = pParser?pParser->GetDocument():NULL;
+       CheckUnSupportError(pDoc, err_code);
        return pParser->GetDocument();
 }
 
@@ -378,6 +279,17 @@ DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document)
        return pDict->GetInteger("P");
 }
 
+DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document)
+{
+    if (document == NULL) return -1;
+    CPDF_Document*pDoc = (CPDF_Document*)document;
+    CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
+    CPDF_Dictionary* pDict = pParser->GetEncryptDict();
+    if (pDict == NULL) return -1;
+
+    return pDict->GetInteger("R");
+}
+
 DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document)
 {
        if (document == NULL) return 0;
@@ -395,13 +307,7 @@ DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document, int page_index
        if (pDict == NULL) return NULL;
        CPDF_Page* pPage = FX_NEW CPDF_Page;
        pPage->Load(pDoc, pDict);
-       try {
-               pPage->ParseContent();
-       }
-       catch (...) {
-               delete pPage;
-               return NULL;
-       }
+       pPage->ParseContent();
        
 //     CheckUnSupportError(pDoc, 0);
 
@@ -464,15 +370,10 @@ DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc, FPDF_PAGE page, int start_x, int
 #endif
        }
        else
-       pContext->m_pDevice = FX_NEW CFX_WindowsDevice(dc);
-       if (flags & FPDF_NO_CATCH)
-               Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);
-       else {
-               try {
-                       Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);
-               } catch (...) {
-               }
-       }
+           pContext->m_pDevice = FX_NEW CFX_WindowsDevice(dc);
+
+       Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);
+
        if (bBackgroundAlphaNeeded) 
        {
                if (pBitmap)
@@ -482,10 +383,11 @@ DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc, FPDF_PAGE page, int start_x, int
                        if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER)
                        {
                                CFX_DIBitmap* pDst = FX_NEW CFX_DIBitmap;
-                               pDst->Create(pBitmap->GetWidth(), pBitmap->GetHeight(),FXDIB_Rgb32);
-                               FXSYS_memcpy(pDst->GetBuffer(), pBitmap->GetBuffer(), pBitmap->GetPitch()*pBitmap->GetHeight());
-//                             WinDC.SetDIBits(pDst,0,0);
-                               WinDC.StretchDIBits(pDst,0,0,size_x*2,size_y*2);
+                               int pitch = pBitmap->GetPitch();
+                               pDst->Create(size_x, size_y, FXDIB_Rgb32);
+                               FXSYS_memset(pDst->GetBuffer(), -1, pitch*size_y);
+                               pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0, FXDIB_BLEND_NORMAL, NULL, FALSE, NULL);
+                               WinDC.StretchDIBits(pDst,0,0,size_x,size_y);
                                delete pDst;
                        }
                        else
@@ -551,14 +453,7 @@ DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc, FPDF_PAGE page, int start_x, int
 #endif
 
        // output to bitmap device
-       if (flags & FPDF_NO_CATCH)
-               Func_RenderPage(pContext, page, start_x - rect.left, start_y - rect.top, size_x, size_y, rotate, flags);
-       else {
-               try {
-                       Func_RenderPage(pContext, page, start_x - rect.left, start_y - rect.top, size_x, size_y, rotate, flags);
-               } catch (...) {
-               }
-       }
+       Func_RenderPage(pContext, page, start_x - rect.left, start_y - rect.top, size_x, size_y, rotate, flags);
 
 #ifdef DEBUG_TRACE
        CPDF_ModuleMgr::Get()->ReportError(999, "Finished PDF rendering");
@@ -626,14 +521,8 @@ DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap, FPDF_PAGE page,
        else
                ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
 #endif
-       if (flags & FPDF_NO_CATCH)
-               Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);
-       else {
-               try {
-                       Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);
-               } catch (...) {
-               }
-       }
+
+       Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);
 
        delete pContext;
        pPage->RemovePrivateData((void*)1);
@@ -642,6 +531,11 @@ DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap, FPDF_PAGE page,
 DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page)
 {
        if (!page) return;
+        CPDFSDK_PageView* pPageView = (CPDFSDK_PageView*)(((CPDF_Page*)page))->GetPrivateData((FX_LPVOID)page);
+        if (pPageView && pPageView->IsLocked()) {
+            pPageView->TakeOverPage();
+            return;
+        }
        delete (CPDF_Page*)page;
 
 }
@@ -731,8 +625,7 @@ DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width, int height, int for
        return pBitmap;
 }
 
-DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap, int left, int top, int width, int height, 
-                                                                       int red, int green, int blue, int alpha)
+DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap, int left, int top, int width, int height, FPDF_DWORD color)
 {
        if (bitmap == NULL) return;
 #ifdef _SKIA_SUPPORT_
@@ -741,9 +634,9 @@ DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap, int left, int top
        CFX_FxgeDevice device;
 #endif
        device.Attach((CFX_DIBitmap*)bitmap);
-       if (!((CFX_DIBitmap*)bitmap)->HasAlpha()) alpha = 255;
+       if (!((CFX_DIBitmap*)bitmap)->HasAlpha()) color |= 0xFF000000;
        FX_RECT rect(left, top, left+width, top+height);
-       device.FillRect(&rect, FXARGB_MAKE(alpha, red, green, blue));
+       device.FillRect(&rect, color);
 }
 
 DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap)
@@ -779,7 +672,6 @@ DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap)
 void FPDF_RenderPage_Retail(CRenderContext* pContext, FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,
                                                int rotate, int flags,FX_BOOL bNeedToRestore, IFSDK_PAUSE_Adapter * pause )
 {
-//#ifdef _LICENSED_BUILD_
        CPDF_Page* pPage = (CPDF_Page*)page;
        if (pPage == NULL) return;
 
@@ -866,6 +758,37 @@ DLLEXPORT FPDF_BOOL STDCALL FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT documen
        return viewRef.PrintScaling();
 }
 
+DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document)
+{
+    CPDF_Document* pDoc = (CPDF_Document*)document;
+    if (!pDoc) return 1;
+    CPDF_ViewerPreferences viewRef(pDoc);
+    return viewRef.NumCopies();
+}
+
+DLLEXPORT FPDF_PAGERANGE STDCALL FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document)
+{
+    CPDF_Document* pDoc = (CPDF_Document*)document;
+    if (!pDoc) return NULL;
+    CPDF_ViewerPreferences viewRef(pDoc);
+    return viewRef.PrintPageRange();
+}
+
+DLLEXPORT FPDF_DUPLEXTYPE STDCALL FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document)
+{
+    CPDF_Document* pDoc = (CPDF_Document*)document;
+    if (!pDoc) return DuplexUndefined;
+    CPDF_ViewerPreferences viewRef(pDoc);
+    CFX_ByteString duplex = viewRef.Duplex();
+    if (FX_BSTRC("Simplex") == duplex)
+        return Simplex;
+    if (FX_BSTRC("DuplexFlipShortEdge") == duplex)
+        return DuplexFlipShortEdge;
+    if (FX_BSTRC("DuplexFlipLongEdge") == duplex)
+        return DuplexFlipLongEdge;
+    return DuplexUndefined;
+}
+
 DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,FPDF_BYTESTRING name)
 {
        if (document == NULL)