Merge to XFA: Kill remaining sprintfs
[pdfium.git] / fpdfsdk / src / fpdfview.cpp
index ae8f3cc..c0b39ed 100644 (file)
@@ -10,7 +10,8 @@
 #include "../include/fsdk_rendercontext.h"
 #include "../include/fpdf_progressive.h"
 #include "../include/fpdf_ext.h"
-#include "../../third_party/numerics/safe_conversions_impl.h"
+#include "../../../core/src/fxcrt/fx_safe_types.h"
+#include "../../third_party/base/numerics/safe_conversions_impl.h"
 #include "../include/fpdfformfill.h"
 #include "../include/fpdfxfa/fpdfxfa_doc.h"
 #include "../include/fpdfxfa/fpdfxfa_app.h"
@@ -128,7 +129,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;
@@ -211,40 +212,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
        g_pCodecModule = CCodec_ModuleMgr::Create();
        
        CFX_GEModule::Create();
@@ -254,42 +223,9 @@ 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 )
-       {
-               pModuleMgr->LoadEmbeddedGB1CMaps();
-               pModuleMgr->LoadEmbeddedJapan1CMaps();
-               pModuleMgr->LoadEmbeddedCNS1CMaps();
-               pModuleMgr->LoadEmbeddedKorea1CMaps();
-       }
-#endif 
-#endif
 
        CPDFXFA_App* pAppProvider = FPDFXFA_GetApp();
        pAppProvider->Initialize();
-
-#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
 }
 
 
@@ -307,11 +243,6 @@ DLLEXPORT void STDCALL FPDF_DestroyLibrary()
        CFX_GEModule::Destroy();
        g_pCodecModule->Destroy();
 #endif
-#ifndef _FXSDK_OPENSOURCE_
-       FXMEM_CollectAll(FXMEM_GetDefaultMgr());
-#else
-
-#endif
 }
 
 #ifndef _WIN32
@@ -371,16 +302,41 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BY
        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();
 }
 
 
@@ -398,7 +354,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;
@@ -492,7 +448,7 @@ DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document)
 DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document)
 {
     if (document == NULL) return -1;
-    CPDF_Document*pDoc = (CPDF_Document*)document;
+       CPDF_Document*pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
     CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
     CPDF_Dictionary* pDict = pParser->GetEncryptDict();
     if (pDict == NULL) return -1;
@@ -589,9 +545,10 @@ 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);
+                               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;
                        }
@@ -615,7 +572,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
@@ -633,7 +591,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);
@@ -669,7 +628,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);
@@ -864,7 +824,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 = ((CPDFXFA_Page*)page)->GetPDFPage();
        if (pPage == NULL) return;
 
@@ -970,7 +929,7 @@ DLLEXPORT FPDF_BOOL STDCALL FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT documen
 
 DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document)
 {
-    CPDF_Document* pDoc = (CPDF_Document*)document;
+       CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
     if (!pDoc) return 1;
     CPDF_ViewerPreferences viewRef(pDoc);
     return viewRef.NumCopies();
@@ -978,7 +937,7 @@ DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document)
 
 DLLEXPORT FPDF_PAGERANGE STDCALL FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document)
 {
-    CPDF_Document* pDoc = (CPDF_Document*)document;
+       CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
     if (!pDoc) return NULL;
     CPDF_ViewerPreferences viewRef(pDoc);
     return viewRef.PrintPageRange();
@@ -986,7 +945,7 @@ DLLEXPORT FPDF_PAGERANGE STDCALL FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT
 
 DLLEXPORT FPDF_DUPLEXTYPE STDCALL FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document)
 {
-    CPDF_Document* pDoc = (CPDF_Document*)document;
+       CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
     if (!pDoc) return DuplexUndefined;
     CPDF_ViewerPreferences viewRef(pDoc);
     CFX_ByteString duplex = viewRef.Duplex();
@@ -999,11 +958,27 @@ DLLEXPORT FPDF_DUPLEXTYPE STDCALL FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT documen
     return DuplexUndefined;
 }
 
+DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document)
+{
+    if (!document) return 0;
+       CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
+
+    CPDF_Dictionary* pRoot = pDoc->GetRoot();
+    if (!pRoot) return 0;
+
+    CPDF_NameTree nameTree(pDoc, FX_BSTRC("Dests"));
+    int count = nameTree.GetCount();
+    CPDF_Dictionary* pDest = pRoot->GetDict(FX_BSTRC("Dests"));
+    if (pDest)
+        count += pDest->GetCount();
+    return count;
+}
+
 DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,FPDF_BYTESTRING name)
 {
-       if (document == NULL)
+       if (!document)
                return NULL;
-       if (name == NULL || name[0] == 0) 
+       if (!name || name[0] == 0) 
                return NULL;
 
        CPDFXFA_Document* pDoc = (CPDFXFA_Document*)document;
@@ -1070,3 +1045,53 @@ FPDF_RESULT      FPDF_BStr_Clear(FPDF_BSTR* str)
        str->len = 0;
        return 0;
 }
+
+DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index, void* buffer, long& buflen)
+{
+    if (!buffer)
+        buflen = 0;
+    if (!document || index < 0) return NULL;
+       CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
+
+    CPDF_Dictionary* pRoot = pDoc->GetRoot();
+    if (!pRoot) return NULL;
+
+    CPDF_Object* pDestObj = NULL;
+    CFX_ByteString bsName;
+    CPDF_NameTree nameTree(pDoc, FX_BSTRC("Dests"));
+    int count = nameTree.GetCount();
+    if (index >= count) {
+        CPDF_Dictionary* pDest = pRoot->GetDict(FX_BSTRC("Dests"));
+        if (!pDest) return NULL;
+        if (index >= count + pDest->GetCount()) return NULL;
+        index -= count;
+        FX_POSITION pos = pDest->GetStartPos();
+        int i = 0;
+        while (pos) {
+            pDestObj = pDest->GetNextElement(pos, bsName);
+            if (!pDestObj) continue;
+            if (i == index) break;
+            i++;
+        }
+    } else {
+        pDestObj = nameTree.LookupValue(index, bsName);
+    }
+    if (!pDestObj) return NULL;
+    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) {
+        memcpy(buffer, utf16Name.c_str(), len);
+        buflen = len;
+    } else {
+        buflen = -1;
+    }
+    return (FPDF_DEST)pDestObj;
+}