Merge to XFA: Use stdint.h types throughout PDFium.
[pdfium.git] / fpdfsdk / src / fpdfview.cpp
index 4114dac..fa6bb2e 100644 (file)
@@ -4,14 +4,17 @@
  
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
+
+#include "../../core/include/fxcrt/fx_safe_types.h"
+#include "../../public/fpdf_ext.h"
+#include "../../public/fpdf_formfill.h"
+#include "../../public/fpdf_progressive.h"
+#include "../../public/fpdfview.h"
+#include "../../third_party/base/nonstd_unique_ptr.h"
+#include "../../third_party/base/numerics/safe_conversions_impl.h"
 #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"
-#include "../include/fpdfformfill.h"
 #include "../include/fpdfxfa/fpdfxfa_doc.h"
 #include "../include/fpdfxfa/fpdfxfa_app.h"
 #include "../include/fpdfxfa/fpdfxfa_page.h"
@@ -101,7 +104,7 @@ CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
        m_BufferOffset = (FX_DWORD)-1;
 }
 
-FX_BOOL CPDF_CustomAccess::GetByte(FX_DWORD pos, FX_BYTE& ch)
+FX_BOOL CPDF_CustomAccess::GetByte(FX_DWORD pos, uint8_t& ch)
 {
        if (pos >= m_FileAccess.m_FileLen) return FALSE;
        if (m_BufferOffset == (FX_DWORD)-1 || pos < m_BufferOffset || pos >= m_BufferOffset + 512) {
@@ -128,7 +131,7 @@ FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer, FX_FILESIZE offset, size_t si
     if (offset < 0) {
         return FALSE;
     }
-    FX_SAFE_FILESIZE newPos = base::checked_cast<FX_FILESIZE, size_t>(size);
+    FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
     newPos += offset;
     if (!newPos.IsValid() || newPos.ValueOrDie() > m_FileAccess.m_FileLen) {
         return FALSE;
@@ -174,74 +177,29 @@ FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy)
        return FALSE;
 }
 
+CCodec_ModuleMgr*      g_pCodecModule = NULL;
 
-#ifndef _T
-#define _T(x) x
-#endif
-
-#ifdef API5
-       CPDF_ModuleMgr* g_pModuleMgr = NULL;
-#else
-       CCodec_ModuleMgr*       g_pCodecModule = NULL;
-#endif
-
-//extern CPDFSDK_FormFillApp* g_pFormFillApp;
-
-#if _FX_OS_ == _FX_LINUX_EMBEDDED_
-class CFontMapper : public IPDF_FontMapper
+DLLEXPORT void STDCALL FPDF_InitLibrary()
 {
-public:
-       CFontMapper();
-       virtual ~CFontMapper();
-
-       virtual FT_Face FindSubstFont(
-                                                       CPDF_Document* pDoc,                            // [IN] The PDF document
-                                                       const CFX_ByteString& face_name,        // [IN] Original name
-                                                       FX_BOOL bTrueType,                                      // [IN] TrueType or Type1
-                                                       FX_DWORD flags,                                         // [IN] PDF font flags (see PDF Reference section 5.7.1)
-                                                       int font_weight,                                        // [IN] original font weight. 0 for not specified
-                                                       int CharsetCP,                                          // [IN] code page for charset (see Win32 GetACP())
-                                                       FX_BOOL bVertical,
-                                                       CPDF_SubstFont* pSubstFont                      // [OUT] Subst font data
-                                               );
-
-       FT_Face m_SysFace;
-};
+    g_pCodecModule = CCodec_ModuleMgr::Create();
 
-CFontMapper* g_pFontMapper = NULL;
-#endif         // #if _FX_OS_ == _FX_LINUX_EMBEDDED_
+    CFX_GEModule::Create();
+    CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
 
-DLLEXPORT void STDCALL FPDF_InitLibrary()
-{
-       g_pCodecModule = CCodec_ModuleMgr::Create();
-       
-       CFX_GEModule::Create();
-       CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
-       
-       CPDF_ModuleMgr::Create();
-       CPDF_ModuleMgr::Get()->SetCodecModule(g_pCodecModule);
-       CPDF_ModuleMgr::Get()->InitPageModule();
-       CPDF_ModuleMgr::Get()->InitRenderModule();
+    CPDF_ModuleMgr::Create();
+    CPDF_ModuleMgr::Get()->SetCodecModule(g_pCodecModule);
+    CPDF_ModuleMgr::Get()->InitPageModule();
+    CPDF_ModuleMgr::Get()->InitRenderModule();
 
-       CPDFXFA_App* pAppProvider = FPDFXFA_GetApp();
-       pAppProvider->Initialize();
+    CPDFXFA_App::GetInstance()->Initialize();
 }
 
-
 DLLEXPORT void STDCALL FPDF_DestroyLibrary()
 {
-       FPDFXFA_ReleaseApp();
-
-#if _FX_OS_ == _FX_LINUX_EMBEDDED_
-       if (g_pFontMapper) delete g_pFontMapper;
-#endif
-#ifdef API5
-       g_pModuleMgr->Destroy();
-#else
-       CPDF_ModuleMgr::Destroy();
-       CFX_GEModule::Destroy();
-       g_pCodecModule->Destroy();
-#endif
+    CPDFXFA_App::ReleaseInstance();
+    CPDF_ModuleMgr::Destroy();
+    CFX_GEModule::Destroy();
+    g_pCodecModule->Destroy();
 }
 
 #ifndef _WIN32
@@ -297,29 +255,54 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BY
        if (!pPDFDoc)
                return NULL;
 
-       CPDFXFA_App* pProvider = FPDFXFA_GetApp();
+       CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
        CPDFXFA_Document* pDocument = FX_NEW CPDFXFA_Document(pPDFDoc, pProvider);
        return pDocument;
 }
-DLLEXPORT  FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document)
+
+DLLEXPORT FX_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document, int* docType)
 {
-       if (!document||!((CPDFXFA_Document*)document)->GetPDFDoc()) 
+       if (!document)
                return FALSE;
 
-       int iDocType = DOCTYPE_PDF;
-       FX_BOOL hasXFAField = FPDF_HasXFAField(((CPDFXFA_Document*)document)->GetPDFDoc(), iDocType);
-       if (!hasXFAField)
+       CPDF_Document *pdfDoc = (static_cast<CPDFXFA_Document *>(document))->GetPDFDoc();
+       if (!pdfDoc)
                return FALSE;
-       return ((CPDFXFA_Document*)document)->LoadXFADoc();
+
+       CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
+       if (!pRoot)
+               return FALSE;
+
+       CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");
+       if (!pAcroForm)
+               return FALSE;
+
+       CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
+       if (!pXFA)
+               return FALSE;
+
+       FX_BOOL bDynamicXFA = pRoot->GetBoolean("NeedsRendering", FALSE);
+
+       if (bDynamicXFA)
+               *docType = DOCTYPE_DYNIMIC_XFA;
+       else
+               *docType = DOCTYPE_STATIC_XFA;
+
+       return TRUE;
+}
+
+DLLEXPORT  FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document)
+{
+       return document && (static_cast<CPDFXFA_Document *>(document))->LoadXFADoc();
 }
 
 
 extern void CheckUnSupportError(CPDF_Document * pDoc, FX_DWORD err_code);
 
-class CMemFile FX_FINAL: public IFX_FileRead, public CFX_Object
+class CMemFile final : public IFX_FileRead
 {
 public:
-       CMemFile(FX_BYTE* pBuf, FX_FILESIZE size):m_pBuf(pBuf),m_size(size) {}
+       CMemFile(uint8_t* pBuf, FX_FILESIZE size):m_pBuf(pBuf),m_size(size) {}
 
        virtual void                    Release() {delete this;}
        virtual FX_FILESIZE             GetSize() {return m_size;}
@@ -328,7 +311,7 @@ public:
             if (offset < 0) {
                 return FALSE;
             }
-            FX_SAFE_FILESIZE newPos = base::checked_cast<FX_FILESIZE, size_t>(size);
+            FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
             newPos += offset;
             if (!newPos.IsValid() || newPos.ValueOrDie() > (FX_DWORD)m_size) {
                 return FALSE;
@@ -337,14 +320,14 @@ public:
            return TRUE;
        }
 private:
-       FX_BYTE* m_pBuf;
+       uint8_t* m_pBuf;
        FX_FILESIZE m_size;
 };
 DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password)
 {
        CPDF_Parser* pParser = FX_NEW CPDF_Parser;
        pParser->SetPassword(password);
-       CMemFile* pMemFile = FX_NEW CMemFile((FX_BYTE*)data_buf, size);
+       CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
        FX_DWORD err_code = pParser->StartParse(pMemFile);
        if (err_code) {
                delete pParser;
@@ -358,10 +341,8 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf, int s
        if (!pPDFDoc)
                return NULL;
 
-       CPDFXFA_App* pProvider = FPDFXFA_GetApp();
+       CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
        CPDFXFA_Document* pDocument = FX_NEW CPDFXFA_Document(pPDFDoc, pProvider);
-       //pDocument->LoadXFADoc();
-
        return pDocument;
 }
 
@@ -383,10 +364,8 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAc
        if (!pPDFDoc)
                return NULL;
 
-       CPDFXFA_App* pProvider = FPDFXFA_GetApp();
+       CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
        CPDFXFA_Document* pDocument = FX_NEW CPDFXFA_Document(pPDFDoc, pProvider);
-       //pDocument->LoadXFADoc();
-
        return pDocument;
 }
 
@@ -546,7 +525,8 @@ DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc, FPDF_PAGE page, int start_x, int
 #ifdef DEBUG_TRACE
        {
                char str[128];
-               sprintf(str, "Rendering DIB %d x %d", width, height);
+               memset(str, 0, sizeof(str));
+               FXSYS_snprintf(str, sizeof(str) - 1, "Rendering DIB %d x %d", width, height);
                CPDF_ModuleMgr::Get()->ReportError(999, str);
        }
 #endif
@@ -564,7 +544,8 @@ DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc, FPDF_PAGE page, int start_x, int
        if (pContext->m_hBitmap == NULL) {
 #if defined(DEBUG) || defined(_DEBUG)
                char str[128];
-               sprintf(str, "Error CreateDIBSection: %d x %d, error code = %d", width, height, GetLastError());
+               memset(str, 0, sizeof(str));
+               FXSYS_snprintf(str, sizeof(str) - 1, "Error CreateDIBSection: %d x %d, error code = %d", width, height, GetLastError());
                CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
 #else
                CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
@@ -600,7 +581,8 @@ DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc, FPDF_PAGE page, int start_x, int
        if (hMemDC == NULL) {
 #if defined(DEBUG) || defined(_DEBUG)
                char str[128];
-               sprintf(str, "Error CreateCompatibleDC. Error code = %d", GetLastError());
+               memset(str, 0, sizeof(str));
+               FXSYS_snprintf(str, sizeof(str) - 1, "Error CreateCompatibleDC. Error code = %d", GetLastError());
                CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
 #else
                CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
@@ -719,9 +701,11 @@ DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page, int start_x, int start_
 
 DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width, int height, int alpha)
 {
-       CFX_DIBitmap* pBitmap = FX_NEW CFX_DIBitmap;
-       pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32);
-       return pBitmap;
+    nonstd::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
+    if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
+        return NULL;
+    }
+    return pBitmap.release();
 }
 
 DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width, int height, int format, void* first_scan, int stride)
@@ -1017,10 +1001,10 @@ FPDF_RESULT     FPDF_BStr_Clear(FPDF_BSTR* str)
        return 0;
 }
 
-DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index, void* buffer, long& buflen)
+DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index, void* buffer, long* buflen)
 {
     if (!buffer)
-        buflen = 0;
+        *buflen = 0;
     if (!document || index < 0) return NULL;
        CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
 
@@ -1048,18 +1032,21 @@ DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index,
         pDestObj = nameTree.LookupValue(index, bsName);
     }
     if (!pDestObj) return NULL;
-    if (pDestObj->GetType() == PDFOBJ_DICTIONARY)
+    if (pDestObj->GetType() == PDFOBJ_DICTIONARY) {
         pDestObj = ((CPDF_Dictionary*)pDestObj)->GetArray(FX_BSTRC("D"));
+        if (!pDestObj) return NULL;
+    }
     if (pDestObj->GetType() != PDFOBJ_ARRAY) return NULL;
     CFX_WideString wsName = PDF_DecodeText(bsName);
     CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
     unsigned int len = utf16Name.GetLength();
     if (!buffer) {
-        buflen = len;
-    } else if (buflen >= len) {
+        *buflen = len;
+    } else if (*buflen >= len) {
         memcpy(buffer, utf16Name.c_str(), len);
+        *buflen = len;
     } else {
-        buflen = -1;
+        *buflen = -1;
     }
     return (FPDF_DEST)pDestObj;
 }