From 8cf93825a1329feca248df567c543e399a0aad50 Mon Sep 17 00:00:00 2001
From: Sebastião Guerra <sebastiao.luiz.guerra@gmail.com>
Date: Tue, 17 Nov 2026 06:12:35 -0300
Subject: kpdf: find installed CID-to-Unicode maps
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Search XDG_DATA_DIRS for installed Poppler CID-to-Unicode maps, using /usr/local/share and /usr/share when the variable is unset or empty. Explicit xpdfrc mappings retain precedence, including when an explicit mapping is invalid or missing.

Signed-off-by: Sebastião Guerra <sebastiao.luiz.guerra@gmail.com>
---
 kpdf/xpdf/xpdf/GlobalParams.cpp | 64 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 62 insertions(+), 2 deletions(-)

diff --git a/kpdf/xpdf/xpdf/GlobalParams.cpp b/kpdf/xpdf/xpdf/GlobalParams.cpp
index 7fc5a2a2..4c70cb79 100644
--- a/kpdf/xpdf/xpdf/GlobalParams.cpp
+++ b/kpdf/xpdf/xpdf/GlobalParams.cpp
@@ -28,6 +28,7 @@
 #include <X11/Xft/XftCompat.h>
 #include <stdio.h>
 #include <ctype.h>
+#include <stdlib.h>
 #include <sys/stat.h>
 #ifdef ENABLE_PLUGINS
 #  ifndef WIN32
@@ -2545,11 +2546,70 @@ GBool GlobalParams::getErrQuiet() {
 CharCodeToUnicode *GlobalParams::getCIDToUnicode(GString *collection) {
   GString *fileName;
   CharCodeToUnicode *ctu;
+#ifndef WIN32
+  static const char defaultDataDirs[] = "/usr/local/share:/usr/share";
+  const char *dataDirs;
+  const char *dirStart;
+  const char *dirEnd;
+  const char *dirSeparator;
+  GBool safeCollection;
+  int j;
+  struct stat fileInfo;
+#endif
 
   lockGlobalParams;
   if (!(ctu = cidToUnicodeCache->getCharCodeToUnicode(collection))) {
-    if ((fileName = (GString *)cidToUnicodes->lookup(collection)) &&
-	(ctu = CharCodeToUnicode::parseCIDToUnicode(fileName, collection))) {
+    fileName = (GString *)cidToUnicodes->lookup(collection);
+    if (fileName) {
+      // An explicit xpdfrc entry takes precedence, even if it is invalid.
+      ctu = CharCodeToUnicode::parseCIDToUnicode(fileName, collection);
+    } else {
+#ifndef WIN32
+      // Xpdf normally gets these paths from xpdfrc.  Check standard data
+      // directories for poppler-data when none was configured.
+      // The collection comes from the PDF, so constrain it before using it
+      // as a filename under these directories.
+      safeCollection = collection->getLength() > 6 &&
+                       collection->getLength() <= 128 &&
+                       !strncmp(collection->getCString(), "Adobe-", 6);
+
+      if (safeCollection) {
+        for (j = 0; j < collection->getLength(); ++j) {
+          char c = collection->getCString()[j];
+          if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
+                (c >= '0' && c <= '9') || c == '-')) {
+            safeCollection = gFalse;
+            break;
+          }
+        }
+      }
+      if (safeCollection) {
+        dataDirs = getenv("XDG_DATA_DIRS");
+        if (!dataDirs || !dataDirs[0]) {
+          dataDirs = defaultDataDirs;
+        }
+        dirStart = dataDirs;
+        while (!ctu) {
+          dirSeparator = strchr(dirStart, ':');
+          dirEnd = dirSeparator ? dirSeparator : dirStart + strlen(dirStart);
+          // XDG_DATA_DIRS contains absolute directory names.  Ignore empty
+          // and relative entries instead of resolving them against cwd.
+          if (dirEnd > dirStart && dirStart[0] == '/') {
+            GString candidate(dirStart, (int)(dirEnd - dirStart));
+            candidate.append("/poppler/cidToUnicode/")->append(collection->getCString());
+            if (!stat(candidate.getCString(), &fileInfo) && S_ISREG(fileInfo.st_mode)) {
+              ctu = CharCodeToUnicode::parseCIDToUnicode(&candidate, collection);
+            }
+          }
+          if (!dirSeparator) {
+            break;
+          }
+          dirStart = dirSeparator + 1;
+        }
+      }
+#endif
+    }
+    if (ctu) {
       cidToUnicodeCache->add(ctu);
     }
   }
-- 
cgit v1.2.3

