aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Berkenbilt <jberkenbilt@users.noreply.github.com>2024-02-03 19:09:31 +0100
committerGitHub <noreply@github.com>2024-02-03 19:09:31 +0100
commit532cc58d7e1ad73d2e724abb0bb21af90e791d30 (patch)
treea92ca391f3941a99f26b390159eca2899712316f
parentaea885aab292abfb583232f0069ee47acbd94bbd (diff)
parent8ff20b00897acecc6908c98fab72db1c57692abb (diff)
downloadqpdf-532cc58d7e1ad73d2e724abb0bb21af90e791d30.tar.zst
Merge pull request #1138 from m-holger/j_key
Allow "n:/pdf-syntax" JSON syntax for dictionary keys
-rw-r--r--libqpdf/QPDF_Dictionary.cc17
-rw-r--r--libqpdf/QPDF_json.cc4
-rw-r--r--qpdf/qtest/qpdf/weird-tokens-alt.json3
-rw-r--r--qpdf/qtest/qpdf/weird-tokens.json3
-rw-r--r--qpdf/qtest/qpdf/weird-tokens.pdf13
5 files changed, 28 insertions, 12 deletions
diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc
index 942e6d9e..f7e32fc9 100644
--- a/libqpdf/QPDF_Dictionary.cc
+++ b/libqpdf/QPDF_Dictionary.cc
@@ -3,6 +3,7 @@
#include <qpdf/QPDFObject_private.hh>
#include <qpdf/QPDF_Name.hh>
#include <qpdf/QPDF_Null.hh>
+#include <qpdf/QUtil.hh>
using namespace std::literals;
@@ -72,9 +73,19 @@ QPDF_Dictionary::getJSON(int json_version)
JSON j = JSON::makeDictionary();
for (auto& iter: this->items) {
if (!iter.second.isNull()) {
- std::string key =
- (json_version == 1 ? QPDF_Name::normalizeName(iter.first) : iter.first);
- j.addDictionaryMember(key, iter.second.getJSON(json_version));
+ if (json_version == 1) {
+ j.addDictionaryMember(
+ QPDF_Name::normalizeName(iter.first), iter.second.getJSON(json_version));
+ } else {
+ bool has_8bit_chars;
+ bool is_valid_utf8;
+ bool is_utf16;
+ QUtil::analyze_encoding(iter.first, has_8bit_chars, is_valid_utf8, is_utf16);
+ std::string key = !has_8bit_chars || is_valid_utf8
+ ? iter.first
+ : "n:" + QPDF_Name::normalizeName(iter.first);
+ j.addDictionaryMember(key, iter.second.getJSON(json_version));
+ }
}
}
return j;
diff --git a/libqpdf/QPDF_json.cc b/libqpdf/QPDF_json.cc
index 7951b1e4..33211f14 100644
--- a/libqpdf/QPDF_json.cc
+++ b/libqpdf/QPDF_json.cc
@@ -666,7 +666,9 @@ QPDF::JSONReactor::dictionaryItem(std::string const& key, JSON const& value)
if (dict.isStream()) {
dict = dict.getDict();
}
- dict.replaceKey(key, makeObject(value));
+ dict.replaceKey(
+ is_pdf_name(key) ? QPDFObjectHandle::parse(key.substr(2)).getName() : key,
+ makeObject(value));
}
} else {
throw std::logic_error("QPDF_json: unknown state " + std::to_string(state));
diff --git a/qpdf/qtest/qpdf/weird-tokens-alt.json b/qpdf/qtest/qpdf/weird-tokens-alt.json
index adeb5bda..607bdd55 100644
--- a/qpdf/qtest/qpdf/weird-tokens-alt.json
+++ b/qpdf/qtest/qpdf/weird-tokens-alt.json
@@ -26,7 +26,8 @@
1e12
],
"/Pages": "2 0 R",
- "/Type": "/Catalog"
+ "/Type": "/Catalog",
+ "n:/WeirdKey+#ba#da#cc#e5": 42
}
},
"obj:2 0 R": {
diff --git a/qpdf/qtest/qpdf/weird-tokens.json b/qpdf/qtest/qpdf/weird-tokens.json
index bc685732..6aca6a5a 100644
--- a/qpdf/qtest/qpdf/weird-tokens.json
+++ b/qpdf/qtest/qpdf/weird-tokens.json
@@ -26,7 +26,8 @@
1000000000000
],
"/Pages": "2 0 R",
- "/Type": "/Catalog"
+ "/Type": "/Catalog",
+ "n:/WeirdKey+#ba#da#cc#e5": 42
}
},
"obj:2 0 R": {
diff --git a/qpdf/qtest/qpdf/weird-tokens.pdf b/qpdf/qtest/qpdf/weird-tokens.pdf
index fc1a71f1..27415a46 100644
--- a/qpdf/qtest/qpdf/weird-tokens.pdf
+++ b/qpdf/qtest/qpdf/weird-tokens.pdf
@@ -21,6 +21,7 @@
]
/Pages 2 0 R
/Type /Catalog
+ /WeirdKey+#ba#da#cc#e5 42
>>
endobj
@@ -85,16 +86,16 @@ xref
0 7
0000000000 65535 f
0000000025 00000 n
-0000000361 00000 n
-0000000443 00000 n
-0000000639 00000 n
-0000000738 00000 n
-0000000757 00000 n
+0000000389 00000 n
+0000000471 00000 n
+0000000667 00000 n
+0000000766 00000 n
+0000000785 00000 n
trailer <<
/Root 1 0 R
/Size 7
/ID [<42841c13bbf709d79a200fa1691836f8><728c020f464c3cf7e02c12605fa7d88b>]
>>
startxref
-863
+891
%%EOF