Merge to XFA: Kill remaining sprintfs
[pdfium.git] / fpdfsdk / src / fpdfview.cpp
index 4114dac..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;
@@ -301,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;
+
+       CPDF_Document *pdfDoc = (static_cast<CPDFXFA_Document *>(document))->GetPDFDoc();
+       if (!pdfDoc)
                return FALSE;
 
-       int iDocType = DOCTYPE_PDF;
-       FX_BOOL hasXFAField = FPDF_HasXFAField(((CPDFXFA_Document*)document)->GetPDFDoc(), iDocType);
-       if (!hasXFAField)
+       CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
+       if (!pRoot)
                return FALSE;
-       return ((CPDFXFA_Document*)document)->LoadXFADoc();
+
+       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();
 }
 
 
@@ -328,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;
@@ -546,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
@@ -564,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);
@@ -600,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);
@@ -1048,8 +1077,10 @@ 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();
@@ -1058,6 +1089,7 @@ DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index,
         buflen = len;
     } else if (buflen >= len) {
         memcpy(buffer, utf16Name.c_str(), len);
+        buflen = len;
     } else {
         buflen = -1;
     }