aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFFileSpecObjectHelper.cc
blob: eada4613f1e5d3bb4a78f9b2577a39af77c54989 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <qpdf/QPDFFileSpecObjectHelper.hh>

#include <qpdf/QPDF.hh>
#include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh>

#include <string>
#include <vector>

QPDFFileSpecObjectHelper::QPDFFileSpecObjectHelper(QPDFObjectHandle oh) :
    QPDFObjectHelper(oh)
{
    if (!oh.isDictionary()) {
        oh.warnIfPossible("Embedded file object is not a dictionary");
        return;
    }
    if (!oh.isDictionaryOfType("/Filespec")) {
        oh.warnIfPossible("Embedded file object's type is not /Filespec");
    }
}

static std::vector<std::string> name_keys = {"/UF", "/F", "/Unix", "/DOS", "/Mac"};

std::string
QPDFFileSpecObjectHelper::getDescription()
{
    std::string result;
    auto desc = this->oh.getKey("/Desc");
    if (desc.isString()) {
        result = desc.getUTF8Value();
    }
    return result;
}

std::string
QPDFFileSpecObjectHelper::getFilename()
{
    for (auto const& i: name_keys) {
        auto k = this->oh.getKey(i);
        if (k.isString()) {
            return k.getUTF8Value();
        }
    }
    return "";
}

std::map<std::string, std::string>
QPDFFileSpecObjectHelper::getFilenames()
{
    std::map<std::string, std::string> result;
    for (auto const& i: name_keys) {
        auto k = this->oh.getKey(i);
        if (k.isString()) {
            result[i] = k.getUTF8Value();
        }
    }
    return result;
}

QPDFObjectHandle
QPDFFileSpecObjectHelper::getEmbeddedFileStream(std::string const& key)
{
    auto ef = this->oh.getKey("/EF");
    if (!ef.isDictionary()) {
        return QPDFObjectHandle::newNull();
    }
    if (!key.empty()) {
        return ef.getKey(key);
    }
    for (auto const& i: name_keys) {
        auto k = ef.getKey(i);
        if (k.isStream()) {
            return k;
        }
    }
    return QPDFObjectHandle::newNull();
}

QPDFObjectHandle
QPDFFileSpecObjectHelper::getEmbeddedFileStreams()
{
    return this->oh.getKey("/EF");
}

QPDFFileSpecObjectHelper
QPDFFileSpecObjectHelper::createFileSpec(
    QPDF& qpdf, std::string const& filename, std::string const& fullpath)
{
    return createFileSpec(
        qpdf,
        filename,
        QPDFEFStreamObjectHelper::createEFStream(qpdf, QUtil::file_provider(fullpath)));
}

QPDFFileSpecObjectHelper
QPDFFileSpecObjectHelper::createFileSpec(
    QPDF& qpdf, std::string const& filename, QPDFEFStreamObjectHelper efsoh)
{
    auto oh = qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
    oh.replaceKey("/Type", QPDFObjectHandle::newName("/Filespec"));
    QPDFFileSpecObjectHelper result(oh);
    result.setFilename(filename);
    auto ef = QPDFObjectHandle::newDictionary();
    ef.replaceKey("/F", efsoh.getObjectHandle());
    ef.replaceKey("/UF", efsoh.getObjectHandle());
    oh.replaceKey("/EF", ef);
    return result;
}

QPDFFileSpecObjectHelper&
QPDFFileSpecObjectHelper::setDescription(std::string const& desc)
{
    this->oh.replaceKey("/Desc", QPDFObjectHandle::newUnicodeString(desc));
    return *this;
}

QPDFFileSpecObjectHelper&
QPDFFileSpecObjectHelper::setFilename(
    std::string const& unicode_name, std::string const& compat_name)
{
    auto uf = QPDFObjectHandle::newUnicodeString(unicode_name);
    this->oh.replaceKey("/UF", uf);
    if (compat_name.empty()) {
        QTC::TC("qpdf", "QPDFFileSpecObjectHelper empty compat_name");
        this->oh.replaceKey("/F", uf);
    } else {
        QTC::TC("qpdf", "QPDFFileSpecObjectHelper non-empty compat_name");
        this->oh.replaceKey("/F", QPDFObjectHandle::newString(compat_name));
    }
    return *this;
}