2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Name.hh>
|
|
|
|
|
2024-02-09 13:09:08 +00:00
|
|
|
#include <qpdf/JSON_writer.hh>
|
2013-01-25 13:59:55 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2008-05-04 16:02:53 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF_Name::QPDF_Name(std::string const& name) :
|
2022-08-02 21:57:33 +00:00
|
|
|
QPDFValue(::ot_name, "name"),
|
2008-04-29 12:55:25 +00:00
|
|
|
name(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 16:45:04 +00:00
|
|
|
QPDF_Name::create(std::string const& name)
|
|
|
|
{
|
|
|
|
return do_create(new QPDF_Name(name));
|
|
|
|
}
|
|
|
|
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-11-14 17:54:12 +00:00
|
|
|
QPDF_Name::copy(bool shallow)
|
2022-06-16 16:45:04 +00:00
|
|
|
{
|
|
|
|
return create(name);
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_Name::normalizeName(std::string const& name)
|
|
|
|
{
|
2013-10-05 09:52:42 +00:00
|
|
|
if (name.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return name;
|
2013-10-05 09:52:42 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string result;
|
2013-10-05 23:42:39 +00:00
|
|
|
result += name.at(0);
|
2019-08-19 01:26:19 +00:00
|
|
|
for (size_t i = 1; i < name.length(); ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
char ch = name.at(i);
|
|
|
|
// Don't use locale/ctype here; follow PDF spec guidelines.
|
2019-08-19 01:26:19 +00:00
|
|
|
if (ch == '\0') {
|
|
|
|
// QPDFTokenizer embeds a null character to encode an invalid #.
|
|
|
|
result += "#";
|
2023-01-30 15:56:29 +00:00
|
|
|
} else if (
|
2023-04-04 14:54:54 +00:00
|
|
|
ch < 33 || ch == '#' || ch == '/' || ch == '(' || ch == ')' || ch == '{' || ch == '}' ||
|
|
|
|
ch == '<' || ch == '>' || ch == '[' || ch == ']' || ch == '%' || ch > 126) {
|
2023-01-04 11:17:01 +00:00
|
|
|
result += QUtil::hex_encode_char(ch);
|
2022-02-08 14:18:08 +00:00
|
|
|
} else {
|
|
|
|
result += ch;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF_Name::unparse()
|
|
|
|
{
|
|
|
|
return normalizeName(this->name);
|
|
|
|
}
|
|
|
|
|
2024-02-10 12:03:28 +00:00
|
|
|
std::pair<bool, bool>
|
|
|
|
QPDF_Name::analyzeJSONEncoding(const std::string& name)
|
|
|
|
{
|
2024-02-17 14:58:48 +00:00
|
|
|
int tail = 0; // Number of continuation characters expected.
|
2024-02-10 12:03:28 +00:00
|
|
|
bool tail2 = false; // Potential overlong 3 octet utf-8.
|
|
|
|
bool tail3 = false; // potential overlong 4 octet
|
|
|
|
bool needs_escaping = false;
|
2024-03-03 18:40:59 +00:00
|
|
|
for (auto const& it: name) {
|
|
|
|
auto c = static_cast<unsigned char>(it);
|
2024-02-10 12:03:28 +00:00
|
|
|
if (tail) {
|
|
|
|
if ((c & 0xc0) != 0x80) {
|
|
|
|
return {false, false};
|
|
|
|
}
|
|
|
|
if (tail2) {
|
|
|
|
if ((c & 0xe0) == 0x80) {
|
|
|
|
return {false, false};
|
|
|
|
}
|
|
|
|
tail2 = false;
|
|
|
|
} else if (tail3) {
|
|
|
|
if ((c & 0xf0) == 0x80) {
|
|
|
|
return {false, false};
|
|
|
|
}
|
|
|
|
tail3 = false;
|
|
|
|
}
|
|
|
|
tail--;
|
|
|
|
} else if (c < 0x80) {
|
|
|
|
if (!needs_escaping) {
|
|
|
|
needs_escaping = !((c > 34 && c != '\\') || c == ' ' || c == 33);
|
|
|
|
}
|
|
|
|
} else if ((c & 0xe0) == 0xc0) {
|
|
|
|
if ((c & 0xfe) == 0xc0) {
|
|
|
|
return {false, false};
|
|
|
|
}
|
|
|
|
tail = 1;
|
|
|
|
} else if ((c & 0xf0) == 0xe0) {
|
|
|
|
tail2 = (c == 0xe0);
|
|
|
|
tail = 2;
|
|
|
|
} else if ((c & 0xf8) == 0xf0) {
|
|
|
|
tail3 = (c == 0xf0);
|
|
|
|
tail = 3;
|
|
|
|
} else {
|
|
|
|
return {false, false};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {tail == 0, !needs_escaping};
|
|
|
|
}
|
|
|
|
|
2024-02-09 13:09:08 +00:00
|
|
|
void
|
|
|
|
QPDF_Name::writeJSON(int json_version, JSON::Writer& p)
|
|
|
|
{
|
2024-02-17 14:58:48 +00:00
|
|
|
// For performance reasons this code is duplicated in QPDF_Dictionary::writeJSON. When updating
|
|
|
|
// this method make sure QPDF_Dictionary is also update.
|
2024-02-09 13:09:08 +00:00
|
|
|
if (json_version == 1) {
|
|
|
|
p << "\"" << JSON::Writer::encode_string(normalizeName(name)) << "\"";
|
|
|
|
} else {
|
2024-02-10 12:03:28 +00:00
|
|
|
if (auto res = analyzeJSONEncoding(name); res.first) {
|
|
|
|
if (res.second) {
|
|
|
|
p << "\"" << name << "\"";
|
|
|
|
} else {
|
|
|
|
p << "\"" << JSON::Writer::encode_string(name) << "\"";
|
|
|
|
}
|
2024-02-09 13:09:08 +00:00
|
|
|
} else {
|
|
|
|
p << "\"n:" << JSON::Writer::encode_string(normalizeName(name)) << "\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|