aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorm-holger <m-holger@kubitscheck.org>2024-01-29 14:22:58 +0100
committerm-holger <m-holger@kubitscheck.org>2024-01-29 14:22:58 +0100
commit8ff20b00897acecc6908c98fab72db1c57692abb (patch)
tree43861ce541d68eee3bb7cc9f7cd85de86a22e386
parent4660e23262fd3606d0b9ef7db9bcc315783b16e3 (diff)
downloadqpdf-8ff20b00897acecc6908c98fab72db1c57692abb.tar.zst
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