From 5c12d11614a325317ceaa7c0567070b3e4188275 Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Tue, 26 Mar 2019 12:43:59 +0100
Subject: [PATCH] Add support for Poppler 0.75.0 (fixes #1388)

---
 gdal/frmts/pdf/pdfio.h       |  5 ---
 gdal/frmts/pdf/pdfobject.cpp | 59 ++++++++----------------------------
 2 files changed, 12 insertions(+), 52 deletions(-)

diff --git a/frmts/pdf/pdfio.h b/frmts/pdf/pdfio.h
index 7ae15c88aa3..ec13b8fe94c 100644
--- a/frmts/pdf/pdfio.h
+++ b/frmts/pdf/pdfio.h
@@ -93,11 +93,6 @@ class VSIPDFFileStream final: public BaseStream
         virtual void       close() override;
 
     private:
-        /* getChars/hasGetChars added in poppler 0.15.0
-         * POPPLER_BASE_STREAM_HAS_TWO_ARGS true from poppler 0.16,
-         * This test will be wrong for poppler 0.15 or 0.16,
-         * but will still compile correctly.
-         */
         virtual GBool hasGetChars() override;
         virtual int getChars(int nChars, Guchar *buffer) override;
 
diff --git a/frmts/pdf/pdfobject.cpp b/frmts/pdf/pdfobject.cpp
--- a/frmts/pdf/pdfobject.cpp
+++ b/frmts/pdf/pdfobject.cpp
@@ -1195,7 +1195,7 @@ GDALPDFObject* GDALPDFDictionaryPoppler::Get(const char* pszKey)
         return oIter->second;
 
 #if POPPLER_MAJOR_VERSION >= 1 || POPPLER_MINOR_VERSION >= 58
-    Object o = m_poDict->lookupNF(((char*)pszKey));
+    auto&& o(m_poDict->lookupNF(((char*)pszKey)));
     if (!o.isNull())
     {
         int nRefNum = 0;
@@ -1204,7 +1204,7 @@ GDALPDFObject* GDALPDFDictionaryPoppler::Get(const char* pszKey)
         {
             nRefNum = o.getRefNum();
             nRefGen = o.getRefGen();
-            Object o2 = m_poDict->lookup((char*)pszKey);
+            Object o2(m_poDict->lookup((char*)pszKey));
             if( !o2.isNull() )
             {
                 GDALPDFObjectPoppler* poObj = new GDALPDFObjectPoppler(new Object(std::move(o2)), TRUE);
@@ -1215,7 +1215,7 @@ GDALPDFObject* GDALPDFDictionaryPoppler::Get(const char* pszKey)
         }
         else
         {
-            GDALPDFObjectPoppler* poObj = new GDALPDFObjectPoppler(new Object(std::move(o)), TRUE);
+            GDALPDFObjectPoppler* poObj = new GDALPDFObjectPoppler(new Object(std::move(o.copy())), TRUE);
             poObj->SetRefNumAndGen(nRefNum, nRefGen);
             m_map[pszKey] = poObj;
             return poObj;
@@ -1329,7 +1329,7 @@ GDALPDFObject* GDALPDFArrayPoppler::Get(int nIndex)
         return m_v[nIndex];
 
 #if POPPLER_MAJOR_VERSION >= 1 || POPPLER_MINOR_VERSION >= 58
-    Object o = m_poArray->getNF(nIndex);
+    auto&& o(m_poArray->getNF(nIndex));
     if( !o.isNull() )
     {
         int nRefNum = 0;
@@ -1338,7 +1338,7 @@ GDALPDFObject* GDALPDFArrayPoppler::Get(int nIndex)
         {
             nRefNum = o.getRefNum();
             nRefGen = o.getRefGen();
-            Object o2 = m_poArray->get(nIndex);
+            Object o2(m_poArray->get(nIndex));
             if( !o2.isNull() )
             {
                 GDALPDFObjectPoppler* poObj = new GDALPDFObjectPoppler(new Object(std::move(o2)), TRUE);
@@ -1349,7 +1349,7 @@ GDALPDFObject* GDALPDFArrayPoppler::Get(int nIndex)
         }
         else
         {
-            GDALPDFObjectPoppler* poObj = new GDALPDFObjectPoppler(new Object(std::move(o)), TRUE);
+            GDALPDFObjectPoppler* poObj = new GDALPDFObjectPoppler(new Object(std::move(o.copy())), TRUE);
             poObj->SetRefNumAndGen(nRefNum, nRefGen);
             m_v[nIndex] = poObj;
             return poObj;
@@ -1416,8 +1416,6 @@ int GDALPDFStreamPoppler::GetLength()
 
 char* GDALPDFStreamPoppler::GetBytes()
 {
-    /* fillGooString() available in poppler >= 0.16.0 */
-#ifdef POPPLER_BASE_STREAM_HAS_TWO_ARGS
     GooString* gstr = new GooString();
     m_poStream->fillGooString(gstr);
 
@@ -1427,7 +1425,12 @@ char* GDALPDFStreamPoppler::GetBytes()
         char* pszContent = (char*) VSIMalloc(m_nLength + 1);
         if (pszContent)
         {
-            memcpy(pszContent, gstr->getCString(), m_nLength);
+#if (POPPLER_MAJOR_VERSION >= 1 || POPPLER_MINOR_VERSION >= 72)
+            const char* srcStr = gstr->c_str();
+#else
+            const char* srcStr = gstr->getCString();
+#endif
+            memcpy(pszContent, srcStr, m_nLength);
             pszContent[m_nLength] = '\0';
         }
         delete gstr;
@@ -1438,41 +1441,6 @@ char* GDALPDFStreamPoppler::GetBytes()
         delete gstr;
         return nullptr;
     }
-#else
-    int i;
-    int nLengthAlloc = 0;
-    char* pszContent = nullptr;
-    if( m_nLength >= 0 )
-    {
-        pszContent = (char*) VSIMalloc(m_nLength + 1);
-        if (!pszContent)
-            return nullptr;
-        nLengthAlloc = m_nLength;
-    }
-    m_poStream->reset();
-    for(i = 0; ; ++i )
-    {
-        int nVal = m_poStream->getChar();
-        if (nVal == EOF)
-            break;
-        if( i >= nLengthAlloc )
-        {
-            nLengthAlloc = 32 + nLengthAlloc + nLengthAlloc / 3;
-            char* pszContentNew = (char*) VSIRealloc(pszContent, nLengthAlloc + 1);
-            if( pszContentNew == nullptr )
-            {
-                CPLFree(pszContent);
-                m_nLength = 0;
-                return nullptr;
-            }
-            pszContent = pszContentNew;
-        }
-        pszContent[i] = (GByte)nVal;
-    }
-    m_nLength = i;
-    pszContent[i] = '\0';
-    return pszContent;
-#endif
 }
 
 #endif // HAVE_POPPLER
