From 8ff20b00897acecc6908c98fab72db1c57692abb Mon Sep 17 00:00:00 2001 From: m-holger Date: Mon, 29 Jan 2024 13:22:58 +0000 Subject: [PATCH] Allow "n:/pdf-syntax" JSON syntax for dictionary keys --- libqpdf/QPDF_Dictionary.cc | 17 ++++++++++++++--- libqpdf/QPDF_json.cc | 4 +++- qpdf/qtest/qpdf/weird-tokens-alt.json | 3 ++- qpdf/qtest/qpdf/weird-tokens.json | 3 ++- qpdf/qtest/qpdf/weird-tokens.pdf | 13 +++++++------ 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 #include #include +#include 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