Merge to XFA: Kill remaining sprintfs
[pdfium.git] / fpdfsdk / src / fpdfview.cpp
index 8a97c28..c0b39ed 100644 (file)
@@ -5,11 +5,96 @@
 // 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 "../../../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"
+#include "../include/fpdfxfa/fpdfxfa_page.h"
+#include "../include/fpdfxfa/fpdfxfa_util.h"
+
+CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS)
+{
+       m_pFS                   =       pFS;
+       m_nCurPos               =       0;
+}
+
+IFX_FileStream* CFPDF_FileStream::Retain()
+{
+       return this;
+}
+
+void CFPDF_FileStream::Release()
+{
+       if (m_pFS && m_pFS->Release)
+               m_pFS->Release(m_pFS->clientData);
+       delete this;
+}
+
+FX_FILESIZE CFPDF_FileStream::GetSize()
+{
+       if (m_pFS && m_pFS->GetSize)
+               return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
+       return 0;
+}
+
+FX_BOOL CFPDF_FileStream::IsEOF()
+{
+       return m_nCurPos >= GetSize();
+}
+
+FX_BOOL CFPDF_FileStream::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size)
+{
+       if (!buffer || !size || !m_pFS->ReadBlock) return FALSE;
+
+       if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, (FPDF_DWORD)size) == 0)
+       {
+               m_nCurPos = offset + size;
+               return TRUE;
+       }
+       return FALSE;
+}
+
+size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size)
+{
+       if (!buffer || !size || !m_pFS->ReadBlock) return 0;
+       
+       FX_FILESIZE nSize = GetSize();
+       if (m_nCurPos >= nSize) return 0;
+       FX_FILESIZE dwAvail = nSize - m_nCurPos;
+       if (dwAvail < (FX_FILESIZE)size) size = (size_t)dwAvail;
+       if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer, (FPDF_DWORD)size) == 0)
+       {
+               m_nCurPos += size;
+               return size;
+       }
+
+       return 0;
+}
+
+FX_BOOL CFPDF_FileStream::WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size)
+{
+       if (!m_pFS || !m_pFS->WriteBlock) return FALSE;
+
+       if(m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, (FPDF_DWORD)size) == 0)
+       {
+               m_nCurPos = offset + size;
+               return TRUE;
+       }
+       return FALSE;
+}
 
+FX_BOOL CFPDF_FileStream::Flush()
+{
+       if (!m_pFS || !m_pFS->Flush) return TRUE;
+
+       return m_pFS->Flush(m_pFS->clientData) == 0;
+}
 
 CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
 {
@@ -41,12 +126,15 @@ FX_BOOL CPDF_CustomAccess::GetBlock(FX_DWORD pos, FX_LPBYTE pBuf, FX_DWORD size)
 
 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 = pdfium::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
@@ -124,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();
@@ -167,44 +223,15 @@ 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
 
-#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
+       CPDFXFA_App* pAppProvider = FPDFXFA_GetApp();
+       pAppProvider->Initialize();
 }
 
 
 DLLEXPORT void STDCALL FPDF_DestroyLibrary()
 {
+       FPDFXFA_ReleaseApp();
 
 #if _FX_OS_ == _FX_LINUX_EMBEDDED_
        if (g_pFontMapper) delete g_pFontMapper;
@@ -216,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
@@ -265,25 +287,62 @@ 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();
+       CPDF_Document* pPDFDoc = pParser->GetDocument();
+       if (!pPDFDoc)
+               return NULL;
+
+       CPDFXFA_App* pProvider = FPDFXFA_GetApp();
+       CPDFXFA_Document* pDocument = FX_NEW CPDFXFA_Document(pPDFDoc, pProvider);
+       return pDocument;
+}
+
+DLLEXPORT FX_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document, int& docType)
+{
+       if (!document)
+               return FALSE;
+
+       CPDF_Document *pdfDoc = (static_cast<CPDFXFA_Document *>(document))->GetPDFDoc();
+       if (!pdfDoc)
+               return FALSE;
+
+       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: 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) {}
@@ -292,9 +351,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 = pdfium::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;
@@ -304,24 +370,25 @@ 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;
        }
-       return pParser->GetDocument();
+       CPDF_Document * pDoc = NULL;
+       pDoc = pParser?pParser->GetDocument():NULL;
+       CheckUnSupportError(pDoc, err_code);
+       CPDF_Document* pPDFDoc = pParser->GetDocument();
+       if (!pPDFDoc)
+               return NULL;
+
+       CPDFXFA_App* pProvider = FPDFXFA_GetApp();
+       CPDFXFA_Document* pDocument = FX_NEW CPDFXFA_Document(pPDFDoc, pProvider);
+       //pDocument->LoadXFADoc();
+
+       return pDocument;
 }
 
 DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess, FPDF_BYTESTRING password)
@@ -329,31 +396,35 @@ 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;
        }
-       return pParser->GetDocument();
+       CPDF_Document * pDoc = NULL;
+       pDoc = pParser?pParser->GetDocument():NULL;
+       CheckUnSupportError(pDoc, err_code);
+       CPDF_Document* pPDFDoc = pParser->GetDocument();
+       if (!pPDFDoc)
+               return NULL;
+
+       CPDFXFA_App* pProvider = FPDFXFA_GetApp();
+       CPDFXFA_Document* pDocument = FX_NEW CPDFXFA_Document(pPDFDoc, pProvider);
+       //pDocument->LoadXFADoc();
+
+       return pDocument;
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc, int* fileVersion)
 {
        if(!doc||!fileVersion) return FALSE;
        *fileVersion = 0;
-       CPDF_Document* pDoc = (CPDF_Document*)doc;
-       CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
+       CPDFXFA_Document* pDoc = (CPDFXFA_Document*)doc;
+       CPDF_Document* pPDFDoc = pDoc->GetPDFDoc();
+       if (!pPDFDoc) return (FX_DWORD)-1;
+       CPDF_Parser* pParser =  (CPDF_Parser*)pPDFDoc->GetParser();
+
        if(!pParser)
                return FALSE;
        *fileVersion = pParser->GetFileVersion();
@@ -364,55 +435,57 @@ DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc, int* fileVers
 DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document)
 {
        if (document == NULL) return 0;
-       CPDF_Document*pDoc = (CPDF_Document*)document;
-       CPDF_Parser* pParser =  (CPDF_Parser*)pDoc->GetParser();
+       CPDFXFA_Document*pDoc = (CPDFXFA_Document*)document;
+       CPDF_Document* pPDFDoc = pDoc->GetPDFDoc();
+       if (!pPDFDoc) return (FX_DWORD)-1;
+       CPDF_Parser* pParser =  (CPDF_Parser*)pPDFDoc->GetParser();
        CPDF_Dictionary* pDict = pParser->GetEncryptDict();
        if (pDict == NULL) return (FX_DWORD)-1;
 
        return pDict->GetInteger("P");
 }
 
+DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document)
+{
+    if (document == NULL) return -1;
+       CPDF_Document*pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
+    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;
-       return ((CPDF_Document*)document)->GetPageCount();
+       CPDFXFA_Document* pDoc = (CPDFXFA_Document*)document;
+       return pDoc->GetPageCount();
+//     return ((CPDF_Document*)document)->GetPageCount();
 }
 
 DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document, int page_index)
 {
        if (document == NULL) return NULL;
-       if (page_index < 0 || page_index >= FPDF_GetPageCount(document)) return NULL;
+       CPDFXFA_Document* pDoc = (CPDFXFA_Document*)document;
+       if (page_index < 0 || page_index >= pDoc->GetPageCount()) return NULL;
 //     CPDF_Parser* pParser = (CPDF_Parser*)document;
-       CPDF_Document* pDoc = (CPDF_Document*)document;
-       if (pDoc == NULL) return NULL;
-       CPDF_Dictionary* pDict = pDoc->GetPage(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;
-       }
-       
-//     CheckUnSupportError(pDoc, 0);
-
-       return pPage;
+       return pDoc->GetPage(page_index);
 }
 
 DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page)
 {
        if (!page)
                return 0.0;
-       return ((CPDF_Page*)page)->GetPageWidth();
+       return ((CPDFXFA_Page*)page)->GetPageWidth();
+//     return ((CPDF_Page*)page)->GetPageWidth();
 }
 
 DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page)
 {
        if (!page) return 0.0;
-       return ((CPDF_Page*)page)->GetPageHeight();
+//     return ((CPDF_Page*)page)->GetPageHeight();
+       return ((CPDFXFA_Page*)page)->GetPageHeight();
 }
 
 void DropContext(void* data)
@@ -434,7 +507,8 @@ DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc, FPDF_PAGE page, int start_x, int
                                                int rotate, int flags)
 {
        if (page==NULL) return;
-       CPDF_Page* pPage = (CPDF_Page*)page;
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+       if (!pPage) return;
 
        CRenderContext* pContext = FX_NEW CRenderContext;
        pPage->SetPrivateData((void*)1, pContext, DropContext);
@@ -458,15 +532,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)
@@ -476,10 +545,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
@@ -502,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
@@ -520,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);
@@ -545,14 +617,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");
@@ -563,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);
@@ -600,8 +666,8 @@ DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap, FPDF_PAGE page,
                                                int size_x, int size_y, int rotate, int flags)
 {
        if (bitmap == NULL || page == NULL) return;
-       CPDF_Page* pPage = (CPDF_Page*)page;
-
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+       if (!pPage) return;
 
        CRenderContext* pContext = FX_NEW CRenderContext;
        pPage->SetPrivateData((void*)1, pContext, DropContext);
@@ -620,14 +686,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);
@@ -636,7 +696,14 @@ DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap, FPDF_PAGE page,
 DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page)
 {
        if (!page) return;
-       delete (CPDF_Page*)page;
+       CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
+       
+       pPage->Release();
+//     CPDFXFA_Document* pDoc = pPage->GetDocument();
+//     if (pDoc) {
+//             pDoc->RemovePage(pPage);
+//     }
+//     delete (CPDFXFA_Page*)page;
 
 }
 
@@ -644,14 +711,16 @@ DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document)
 {
        if (!document)
                return;
-       CPDF_Document* pDoc = (CPDF_Document*)document; 
-       CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
-       if (pParser == NULL) 
-       {
-               delete pDoc;
-               return;
-       }
-       delete pParser;
+       CPDFXFA_Document* pDoc = (CPDFXFA_Document*)document;   
+       delete pDoc;
+
+//     CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
+//     if (pParser == NULL) 
+//     {
+//             delete pDoc;
+//             return;
+//     }
+//     delete pParser;
 //     delete pDoc;
 }
 
@@ -664,34 +733,17 @@ DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page, int start_x, int start_
                                                int rotate, int device_x, int device_y, double* page_x, double* page_y)
 {
        if (page == NULL || page_x == NULL || page_y == NULL) return;
-       CPDF_Page* pPage = (CPDF_Page*)page;
-
-       CPDF_Matrix page2device;
-       pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate);
-       CPDF_Matrix device2page;
-       device2page.SetReverse(page2device);
+       CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
 
-       FX_FLOAT page_x_f, page_y_f;
-       device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f, page_y_f);
-
-       *page_x = (page_x_f);
-       *page_y = (page_y_f);
+       pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x, device_y, page_x, page_y);
 }
 
 DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,
                                                int rotate, double page_x, double page_y, int* device_x, int* device_y)
 {
        if (page == NULL || device_x == NULL || device_y == NULL) return;
-       CPDF_Page* pPage = (CPDF_Page*)page;
-
-       CPDF_Matrix page2device;
-       pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate);
-
-       FX_FLOAT device_x_f, device_y_f;
-       page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f, device_y_f);
-
-       *device_x = FXSYS_round(device_x_f);
-       *device_y = FXSYS_round(device_y_f);
+       CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
+       pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y, device_x, device_y);
 }
 
 DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width, int height, int alpha)
@@ -772,8 +824,7 @@ 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;
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
        if (pPage == NULL) return;
 
        if (!pContext->m_pOptions)
@@ -836,32 +887,49 @@ void FPDF_RenderPage_Retail(CRenderContext* pContext, FPDF_PAGE page, int start_
 
 DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document, int page_index, double* width, double* height)
 {
-       CPDF_Document* pDoc = (CPDF_Document*)document;
-       if(pDoc == NULL)
+//     CPDF_Document* pDoc = (CPDF_Document*)document;
+//     if(pDoc == NULL)
+//             return FALSE;
+// 
+//     CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
+//     if (pDict == NULL) return FALSE;
+// 
+//     CPDF_Page page;
+//     page.Load(pDoc, pDict);
+//     *width = page.GetPageWidth();
+//     *height = page.GetPageHeight();
+
+       CPDFXFA_Document* pDoc = (CPDFXFA_Document*)document;
+       if (pDoc == NULL)
                return FALSE;
 
-       CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
-       if (pDict == NULL) return FALSE;
+       int count = pDoc->GetPageCount();
+       if (page_index < 0 || page_index >= count)
+               return FALSE;
+
+       CPDFXFA_Page* pPage = pDoc->GetPage(page_index);
+       if (!pPage)
+               return FALSE;
 
-       CPDF_Page page;
-       page.Load(pDoc, pDict);
-       *width = page.GetPageWidth();
-       *height = page.GetPageHeight();
+       *width = pPage->GetPageWidth();
+       *height = pPage->GetPageHeight();
 
        return TRUE;
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document)
 {
-       CPDF_Document* pDoc = (CPDF_Document*)document;
+       CPDFXFA_Document* pDoc = (CPDFXFA_Document*)document;
        if (!pDoc) return TRUE;
-       CPDF_ViewerPreferences viewRef(pDoc);
+       CPDF_Document* pPDFDoc = pDoc->GetPDFDoc();
+       if (!pPDFDoc) return TRUE;
+       CPDF_ViewerPreferences viewRef(pPDFDoc);
        return viewRef.PrintScaling();
 }
 
 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();
@@ -869,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();
@@ -877,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();
@@ -890,14 +958,140 @@ 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 || name[0] == 0) 
                return NULL;
-       if (name == NULL || name[0] == 0) 
+
+       CPDFXFA_Document* pDoc = (CPDFXFA_Document*)document;
+       CPDF_Document* pPDFDoc = pDoc->GetPDFDoc();
+       if (!pPDFDoc) 
                return NULL;
+       CPDF_NameTree name_tree(pPDFDoc, FX_BSTRC("Dests"));
+       return name_tree.LookupNamedDest(pPDFDoc, name);
+}
+
+FPDF_RESULT    FPDF_BStr_Init(FPDF_BSTR* str)
+{
+       if (!str)
+               return -1;
+
+       FXSYS_memset32(str, 0, sizeof(FPDF_BSTR));
+       return 0;
+}
+
+FPDF_RESULT FPDF_BStr_Set(FPDF_BSTR* str, FPDF_LPCSTR bstr, int length)
+{
+       if (!str) return -1;
+       if (!bstr || !length)
+               return -1;
+       if (length == -1)
+               length = FXSYS_strlen(bstr);
+
+       if (length == 0)
+       {
+               if (str->str)
+               {
+                       FX_Free(str->str);
+                       str->str = NULL;
+               }
+               str->len = 0;
+               return 0;
+       }
+
+       if (str->str && str->len < length)
+               str->str = FX_Realloc(char, str->str, length+1);
+       else if (!str->str)
+               str->str = FX_Alloc(char, length+1);
+
+       str->str[length] = 0;
+       if (str->str == NULL)
+               return -1;
 
-       CPDF_Document* pDoc = (CPDF_Document*)document;
-       CPDF_NameTree name_tree(pDoc, FX_BSTRC("Dests"));
-       return name_tree.LookupNamedDest(pDoc, name);
+       FXSYS_memcpy(str->str, bstr, length);
+       str->len = length;
+
+       return 0;
+}
+
+FPDF_RESULT    FPDF_BStr_Clear(FPDF_BSTR* str)
+{
+       if(!str)
+               return -1;
+
+       if (str->str)
+       {
+               FX_Free(str->str);
+               str->str = NULL;
+       }
+       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;
 }