aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDF_Name.cc
blob: 458b1428dc9a98ff7ece376a8ba9b639ac96254b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <qpdf/QPDF_Name.hh>

#include <qpdf/JSON_writer.hh>
#include <qpdf/QUtil.hh>

QPDF_Name::QPDF_Name(std::string const& name) :
    QPDFValue(::ot_name, "name"),
    name(name)
{
}

std::shared_ptr<QPDFObject>
QPDF_Name::create(std::string const& name)
{
    return do_create(new QPDF_Name(name));
}

std::shared_ptr<QPDFObject>
QPDF_Name::copy(bool shallow)
{
    return create(name);
}

std::string
QPDF_Name::normalizeName(std::string const& name)
{
    if (name.empty()) {
        return name;
    }
    std::string result;
    result += name.at(0);
    for (size_t i = 1; i < name.length(); ++i) {
        char ch = name.at(i);
        // Don't use locale/ctype here; follow PDF spec guidelines.
        if (ch == '\0') {
            // QPDFTokenizer embeds a null character to encode an invalid #.
            result += "#";
        } else if (
            ch < 33 || ch == '#' || ch == '/' || ch == '(' || ch == ')' || ch == '{' || ch == '}' ||
            ch == '<' || ch == '>' || ch == '[' || ch == ']' || ch == '%' || ch > 126) {
            result += QUtil::hex_encode_char(ch);
        } else {
            result += ch;
        }
    }
    return result;
}

std::string
QPDF_Name::unparse()
{
    return normalizeName(this->name);
}

JSON
QPDF_Name::getJSON(int json_version)
{
    if (json_version == 1) {
        return JSON::makeString(normalizeName(this->name));
    } else {
        bool has_8bit_chars;
        bool is_valid_utf8;
        bool is_utf16;
        QUtil::analyze_encoding(this->name, has_8bit_chars, is_valid_utf8, is_utf16);
        if (!has_8bit_chars || is_valid_utf8) {
            return JSON::makeString(this->name);
        } else {
            return JSON::makeString("n:" + normalizeName(this->name));
        }
    }
}

void
QPDF_Name::writeJSON(int json_version, JSON::Writer& p)
{
    if (json_version == 1) {
        p << "\"" << JSON::Writer::encode_string(normalizeName(name)) << "\"";
    } else {
        bool has_8bit_chars;
        bool is_valid_utf8;
        bool is_utf16;
        QUtil::analyze_encoding(this->name, has_8bit_chars, is_valid_utf8, is_utf16);
        if (!has_8bit_chars || is_valid_utf8) {
            p << "\"" << JSON::Writer::encode_string(name) << "\"";
        } else {
            p << "\"n:" << JSON::Writer::encode_string(normalizeName(name)) << "\"";
        }
    }
}