Coverage Report

Created: 2025-12-05 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/QPDF_Dictionary.cc
Line
Count
Source
1
#include <qpdf/QPDFObjectHandle_private.hh>
2
3
#include <qpdf/QPDFObject_private.hh>
4
#include <qpdf/QTC.hh>
5
#include <qpdf/Util.hh>
6
7
using namespace std::literals;
8
using namespace qpdf;
9
10
QPDF_Dictionary*
11
BaseDictionary::dict() const
12
130k
{
13
130k
    if (auto d = as<QPDF_Dictionary>()) {
14
130k
        return d;
15
130k
    }
16
0
    throw std::runtime_error("Expected a dictionary but found a non-dictionary object");
17
0
    return nullptr; // unreachable
18
130k
}
19
20
QPDFObjectHandle const&
21
BaseHandle::operator[](std::string const& key) const
22
14.1M
{
23
14.1M
    if (auto d = as<QPDF_Dictionary>()) {
24
13.8M
        auto it = d->items.find(key);
25
13.8M
        if (it != d->items.end()) {
26
8.09M
            return it->second;
27
8.09M
        }
28
13.8M
    }
29
6.03M
    static const QPDFObjectHandle null_obj;
30
6.03M
    return null_obj;
31
14.1M
}
32
33
/// Retrieves a reference to the QPDFObjectHandle associated with the given key in the
34
/// dictionary object contained within this instance.
35
///
36
/// If the current object is not of dictionary type, a `std::runtime_error` is thrown.
37
/// According to the PDF specification, missing keys in the dictionary are treated as
38
/// keys with a `null` value. This behavior is reflected in this function's implementation,
39
/// where a missing key will still return a reference to a newly inserted null value entry.
40
///
41
/// @param key The key for which the corresponding value in the dictionary is retrieved.
42
/// @return A reference to the QPDFObjectHandle associated with the specified key.
43
/// @throws std::runtime_error if the current object is not a dictionary.
44
QPDFObjectHandle&
45
BaseHandle::at(std::string const& key) const
46
0
{
47
0
    auto d = as<QPDF_Dictionary>();
48
0
    if (!d) {
49
0
        throw std::runtime_error("Expected a dictionary but found a non-dictionary object");
50
0
    }
51
0
    return d->items[key];
52
0
}
53
54
/// @brief Checks if the specified key exists in the object.
55
///
56
/// This method determines whether the given key is present in the object by verifying if the
57
/// associated value is non-null.
58
///
59
/// @param key The key to look for in the object.
60
/// @return True if the key exists and its associated value is non-null. Otherwise, returns false.
61
bool
62
BaseHandle::contains(std::string const& key) const
63
1.46M
{
64
1.46M
    return !(*this)[key].null();
65
1.46M
}
66
67
/// @brief Retrieves the value associated with the given key from  dictionary.
68
///
69
/// This method attempts to find the value corresponding to the specified key for objects that can
70
/// be interpreted as dictionaries.
71
///
72
/// - If the object is a dictionary and the specified key exists, it returns a reference
73
///   to the associated value.
74
/// - If the object is not a dictionary or the specified key does not exist, it returns
75
///   a reference to a static uninitialized object handle.
76
///
77
/// @note Modifying the uninitialized object returned when the key is not found is strictly
78
/// prohibited.
79
///
80
/// @param key The key whose associated value should be retrieved.
81
/// @return A reference to the associated value if the key is found or a reference to a static
82
/// uninitialized object if the key is not found.
83
QPDFObjectHandle&
84
BaseHandle::find(std::string const& key) const
85
0
{
86
0
    static const QPDFObjectHandle null_obj;
87
0
    qpdf_invariant(!null_obj);
88
0
    if (auto d = as<QPDF_Dictionary>()) {
89
0
        auto it = d->items.find(key);
90
0
        if (it != d->items.end()) {
91
0
            return it->second;
92
0
        }
93
0
    }
94
0
    return const_cast<QPDFObjectHandle&>(null_obj);
95
0
}
96
97
std::set<std::string>
98
BaseDictionary::getKeys()
99
130k
{
100
130k
    std::set<std::string> result;
101
508k
    for (auto& iter: dict()->items) {
102
508k
        if (!iter.second.null()) {
103
447k
            result.insert(iter.first);
104
447k
        }
105
508k
    }
106
130k
    return result;
107
130k
}
108
109
std::map<std::string, QPDFObjectHandle> const&
110
BaseDictionary::getAsMap() const
111
0
{
112
0
    return dict()->items;
113
0
}
114
115
size_t
116
BaseHandle::erase(const std::string& key)
117
2.68M
{
118
    // no-op if key does not exist
119
2.68M
    if (auto d = as<QPDF_Dictionary>()) {
120
2.68M
        return d->items.erase(key);
121
2.68M
    }
122
0
    return 0;
123
2.68M
}
124
125
bool
126
BaseHandle::replace(std::string const& key, QPDFObjectHandle value)
127
1.12M
{
128
1.12M
    if (auto d = as<QPDF_Dictionary>()) {
129
1.12M
        if (value.null() && !value.indirect()) {
130
            // The PDF spec doesn't distinguish between keys with null values and missing keys.
131
            // Allow indirect nulls which are equivalent to a dangling reference, which is permitted
132
            // by the spec.
133
96.2k
            d->items.erase(key);
134
1.02M
        } else {
135
            // add or replace value
136
1.02M
            d->items[key] = value;
137
1.02M
        }
138
1.12M
        return true;
139
1.12M
    }
140
0
    return false;
141
1.12M
}
142
143
void
144
BaseDictionary::replace(std::string const& key, QPDFObjectHandle value)
145
1.12M
{
146
1.12M
    if (!BaseHandle::replace(key, value)) {
147
0
        (void)dict();
148
0
    }
149
1.12M
}
150
151
Dictionary::Dictionary(std::map<std::string, QPDFObjectHandle>&& dict) :
152
96.9k
    BaseDictionary(std::move(dict))
153
96.9k
{
154
96.9k
}
155
156
Dictionary::Dictionary(std::shared_ptr<QPDFObject> const& obj) :
157
4.15M
    BaseDictionary(obj)
158
4.15M
{
159
4.15M
}
160
161
Dictionary
162
Dictionary::empty()
163
40.7k
{
164
40.7k
    return Dictionary(std::map<std::string, QPDFObjectHandle>());
165
40.7k
}
166
167
void
168
QPDFObjectHandle::checkOwnership(QPDFObjectHandle const& item) const
169
1.07M
{
170
1.07M
    auto qpdf = getOwningQPDF();
171
1.07M
    auto item_qpdf = item.getOwningQPDF();
172
1.07M
    if (qpdf && item_qpdf && qpdf != item_qpdf) {
173
0
        throw std::logic_error(
174
0
            "Attempting to add an object from a different QPDF. Use "
175
0
            "QPDF::copyForeignObject to add objects from another file.");
176
0
    }
177
1.07M
}
178
179
bool
180
QPDFObjectHandle::hasKey(std::string const& key) const
181
1.15M
{
182
1.15M
    if (Dictionary dict = *this) {
183
1.15M
        return dict.contains(key);
184
1.15M
    } else {
185
1.89k
        typeWarning("dictionary", "returning false for a key containment request");
186
1.89k
        QTC::TC("qpdf", "QPDFObjectHandle dictionary false for hasKey");
187
1.89k
        return false;
188
1.89k
    }
189
1.15M
}
190
191
QPDFObjectHandle
192
QPDFObjectHandle::getKey(std::string const& key) const
193
4.95M
{
194
4.95M
    if (auto result = get(key)) {
195
2.78M
        return result;
196
2.78M
    }
197
2.17M
    if (isDictionary()) {
198
2.16M
        static auto constexpr msg = " -> dictionary key $VD"sv;
199
2.16M
        return QPDF_Null::create(obj, msg, key);
200
2.16M
    }
201
4.09k
    typeWarning("dictionary", "returning null for attempted key retrieval");
202
4.09k
    static auto constexpr msg = " -> null returned from getting key $VD from non-Dictionary"sv;
203
4.09k
    return QPDF_Null::create(obj, msg, "");
204
2.17M
}
205
206
QPDFObjectHandle
207
QPDFObjectHandle::getKeyIfDict(std::string const& key) const
208
40.3k
{
209
40.3k
    return isNull() ? newNull() : getKey(key);
210
40.3k
}
211
212
std::set<std::string>
213
QPDFObjectHandle::getKeys() const
214
131k
{
215
131k
    if (auto dict = as_dictionary(strict)) {
216
130k
        return dict.getKeys();
217
130k
    }
218
1.28k
    typeWarning("dictionary", "treating as empty");
219
1.28k
    QTC::TC("qpdf", "QPDFObjectHandle dictionary empty set for getKeys");
220
1.28k
    return {};
221
131k
}
222
223
std::map<std::string, QPDFObjectHandle>
224
QPDFObjectHandle::getDictAsMap() const
225
0
{
226
0
    if (auto dict = as_dictionary(strict)) {
227
0
        return dict.getAsMap();
228
0
    }
229
0
    typeWarning("dictionary", "treating as empty");
230
0
    QTC::TC("qpdf", "QPDFObjectHandle dictionary empty map for asMap");
231
0
    return {};
232
0
}
233
234
void
235
QPDFObjectHandle::replaceKey(std::string const& key, QPDFObjectHandle const& value)
236
1.13M
{
237
1.13M
    if (auto dict = as_dictionary(strict)) {
238
1.07M
        checkOwnership(value);
239
1.07M
        dict.replace(key, value);
240
1.07M
        return;
241
1.07M
    }
242
62.7k
    typeWarning("dictionary", "ignoring key replacement request");
243
62.7k
}
244
245
QPDFObjectHandle
246
QPDFObjectHandle::replaceKeyAndGetNew(std::string const& key, QPDFObjectHandle const& value)
247
51.4k
{
248
51.4k
    replaceKey(key, value);
249
51.4k
    return value;
250
51.4k
}
251
252
QPDFObjectHandle
253
QPDFObjectHandle::replaceKeyAndGetOld(std::string const& key, QPDFObjectHandle const& value)
254
0
{
255
0
    QPDFObjectHandle old = removeKeyAndGetOld(key);
256
0
    replaceKey(key, value);
257
0
    return old;
258
0
}
259
260
void
261
QPDFObjectHandle::removeKey(std::string const& key)
262
766k
{
263
766k
    if (erase(key) || isDictionary()) {
264
766k
        return;
265
766k
    }
266
0
    typeWarning("dictionary", "ignoring key removal request");
267
0
}
268
269
QPDFObjectHandle
270
QPDFObjectHandle::removeKeyAndGetOld(std::string const& key)
271
0
{
272
0
    auto result = get(key);
273
0
    erase(key);
274
0
    return result ? result : newNull();
275
0
}