Merge to XFA: Use stdint.h types throughout PDFium.
[pdfium.git] / fpdfsdk / src / fpdfview.cpp
index ae8f3cc..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,144 +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(FX_LPVOID hInstance)
-{
-#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();
-       CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
-       
-       CPDF_ModuleMgr::Create();
-       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
+    CPDF_ModuleMgr::Create();
+    CPDF_ModuleMgr::Get()->SetCodecModule(g_pCodecModule);
+    CPDF_ModuleMgr::Get()->InitPageModule();
+    CPDF_ModuleMgr::Get()->InitRenderModule();
 
-       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
+    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
-#ifndef _FXSDK_OPENSOURCE_
-       FXMEM_CollectAll(FXMEM_GetDefaultMgr());
-#else
-
-#endif
+    CPDFXFA_App::ReleaseInstance();
+    CPDF_ModuleMgr::Destroy();
+    CFX_GEModule::Destroy();
+    g_pCodecModule->Destroy();
 }
 
 #ifndef _WIN32
@@ -367,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;
+
+       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;
 
-       int iDocType = DOCTYPE_PDF;
-       FX_BOOL hasXFAField = FPDF_HasXFAField(((CPDFXFA_Document*)document)->GetPDFDoc(), iDocType);
-       if (!hasXFAField)
+       CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
+       if (!pXFA)
                return FALSE;
-       return ((CPDFXFA_Document*)document)->LoadXFADoc();
+
+       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;}
@@ -398,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;
@@ -407,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;
@@ -428,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;
 }
 
@@ -453,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;
 }
 
@@ -492,7 +401,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 +498,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 +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
@@ -633,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);
@@ -669,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);
@@ -788,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)
@@ -864,7 +779,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 +884,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 +892,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 +900,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 +913,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 +1000,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;
+}