Coverage Report

Created: 2025-08-03 06:19

/src/qpdf/libqpdf/QPDF_Array.cc
Line
Count
Source (jump to first uncovered line)
1
#include <qpdf/QPDFObjectHandle_private.hh>
2
3
#include <qpdf/QTC.hh>
4
5
using namespace std::literals;
6
using namespace qpdf;
7
8
static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
9
10
inline void
11
Array::checkOwnership(QPDFObjectHandle const& item) const
12
0
{
13
0
    if (!item) {
14
0
        throw std::logic_error("Attempting to add an uninitialized object to a QPDF_Array.");
15
0
    }
16
0
    if (qpdf() && item.qpdf() && qpdf() != item.qpdf()) {
17
0
        throw std::logic_error(
18
0
            "Attempting to add an object from a different QPDF. Use "
19
0
            "QPDF::copyForeignObject to add objects from another file.");
20
0
    }
21
0
}
22
23
QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle>&& v, bool sparse)
24
619
{
25
619
    if (sparse) {
26
268
        sp = std::make_unique<Sparse>();
27
105k
        for (auto& item: v) {
28
105k
            if (item.raw_type_code() != ::ot_null || item.indirect()) {
29
53.6k
                sp->elements[sp->size] = std::move(item);
30
53.6k
            }
31
105k
            ++sp->size;
32
105k
        }
33
351
    } else {
34
351
        elements = std::move(v);
35
351
    }
36
619
}
37
38
QPDF_Array*
39
Array::array() const
40
566k
{
41
566k
    if (auto a = as<QPDF_Array>()) {
42
566k
        return a;
43
566k
    }
44
45
0
    throw std::runtime_error("Expected an array but found a non-array object");
46
0
    return nullptr; // unreachable
47
566k
}
48
49
Array::iterator
50
Array::begin()
51
275k
{
52
275k
    if (auto a = as<QPDF_Array>()) {
53
274k
        if (!a->sp) {
54
274k
            return a->elements.begin();
55
274k
        }
56
585
        if (!sp_elements) {
57
585
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
58
585
        }
59
585
        return sp_elements->begin();
60
274k
    }
61
287
    return {};
62
275k
}
63
64
Array::iterator
65
Array::end()
66
275k
{
67
275k
    if (auto a = as<QPDF_Array>()) {
68
274k
        if (!a->sp) {
69
274k
            return a->elements.end();
70
274k
        }
71
585
        if (!sp_elements) {
72
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
73
0
        }
74
585
        return sp_elements->end();
75
274k
    }
76
287
    return {};
77
275k
}
78
79
Array::const_iterator
80
Array::cbegin()
81
0
{
82
0
    if (auto a = as<QPDF_Array>()) {
83
0
        if (!a->sp) {
84
0
            return a->elements.cbegin();
85
0
        }
86
0
        if (!sp_elements) {
87
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
88
0
        }
89
0
        return sp_elements->cbegin();
90
0
    }
91
0
    return {};
92
0
}
93
94
Array::const_iterator
95
Array::cend()
96
0
{
97
0
    if (auto a = as<QPDF_Array>()) {
98
0
        if (!a->sp) {
99
0
            return a->elements.cend();
100
0
        }
101
0
        if (!sp_elements) {
102
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
103
0
        }
104
0
        return sp_elements->cend();
105
0
    }
106
0
    return {};
107
0
}
108
109
Array::const_reverse_iterator
110
Array::crbegin()
111
538k
{
112
538k
    if (auto a = as<QPDF_Array>()) {
113
25.4k
        if (!a->sp) {
114
25.3k
            return a->elements.crbegin();
115
25.3k
        }
116
22
        if (!sp_elements) {
117
22
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
118
22
        }
119
22
        return sp_elements->crbegin();
120
25.4k
    }
121
513k
    return {};
122
538k
}
123
124
Array::const_reverse_iterator
125
Array::crend()
126
538k
{
127
538k
    if (auto a = as<QPDF_Array>()) {
128
25.4k
        if (!a->sp) {
129
25.3k
            return a->elements.crend();
130
25.3k
        }
131
22
        if (!sp_elements) {
132
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
133
0
        }
134
22
        return sp_elements->crend();
135
25.4k
    }
136
513k
    return {};
137
538k
}
138
139
QPDFObjectHandle
140
Array::null() const
141
11.2k
{
142
11.2k
    return null_oh;
143
11.2k
}
144
145
int
146
Array::size() const
147
359k
{
148
359k
    auto a = array();
149
359k
    return a->sp ? a->sp->size : int(a->elements.size());
150
359k
}
151
152
std::pair<bool, QPDFObjectHandle>
153
Array::at(int n) const
154
188k
{
155
188k
    auto a = array();
156
188k
    if (n < 0 || n >= size()) {
157
59
        return {false, {}};
158
59
    }
159
188k
    if (!a->sp) {
160
158k
        return {true, a->elements[size_t(n)]};
161
158k
    }
162
29.7k
    auto const& iter = a->sp->elements.find(n);
163
29.7k
    return {true, iter == a->sp->elements.end() ? null() : iter->second};
164
188k
}
165
166
std::vector<QPDFObjectHandle>
167
Array::getAsVector() const
168
17.7k
{
169
17.7k
    auto a = array();
170
17.7k
    if (a->sp) {
171
687
        std::vector<QPDFObjectHandle> v;
172
687
        v.reserve(size_t(size()));
173
270k
        for (auto const& item: a->sp->elements) {
174
270k
            v.resize(size_t(item.first), null_oh);
175
270k
            v.emplace_back(item.second);
176
270k
        }
177
687
        v.resize(size_t(size()), null_oh);
178
687
        return v;
179
17.0k
    } else {
180
17.0k
        return a->elements;
181
17.0k
    }
182
17.7k
}
183
184
bool
185
Array::setAt(int at, QPDFObjectHandle const& oh)
186
0
{
187
0
    if (at < 0 || at >= size()) {
188
0
        return false;
189
0
    }
190
0
    auto a = array();
191
0
    checkOwnership(oh);
192
0
    if (a->sp) {
193
0
        a->sp->elements[at] = oh;
194
0
    } else {
195
0
        a->elements[size_t(at)] = oh;
196
0
    }
197
0
    return true;
198
0
}
199
200
void
201
Array::setFromVector(std::vector<QPDFObjectHandle> const& v)
202
0
{
203
0
    auto a = array();
204
0
    a->elements.resize(0);
205
0
    a->elements.reserve(v.size());
206
0
    for (auto const& item: v) {
207
0
        checkOwnership(item);
208
0
        a->elements.emplace_back(item);
209
0
    }
210
0
}
211
212
bool
213
Array::insert(int at, QPDFObjectHandle const& item)
214
0
{
215
0
    auto a = array();
216
0
    int sz = size();
217
0
    if (at < 0 || at > sz) {
218
        // As special case, also allow insert beyond the end
219
0
        return false;
220
0
    } else if (at == sz) {
221
0
        push_back(item);
222
0
    } else {
223
0
        checkOwnership(item);
224
0
        if (a->sp) {
225
0
            auto iter = a->sp->elements.crbegin();
226
0
            while (iter != a->sp->elements.crend()) {
227
0
                auto key = (iter++)->first;
228
0
                if (key >= at) {
229
0
                    auto nh = a->sp->elements.extract(key);
230
0
                    ++nh.key();
231
0
                    a->sp->elements.insert(std::move(nh));
232
0
                } else {
233
0
                    break;
234
0
                }
235
0
            }
236
0
            a->sp->elements[at] = item.getObj();
237
0
            ++a->sp->size;
238
0
        } else {
239
0
            a->elements.insert(a->elements.cbegin() + at, item.getObj());
240
0
        }
241
0
    }
242
0
    return true;
243
0
}
244
245
void
246
Array::push_back(QPDFObjectHandle const& item)
247
0
{
248
0
    auto a = array();
249
0
    checkOwnership(item);
250
0
    if (a->sp) {
251
0
        a->sp->elements[(a->sp->size)++] = item;
252
0
    } else {
253
0
        a->elements.emplace_back(item);
254
0
    }
255
0
}
256
257
bool
258
Array::erase(int at)
259
340
{
260
340
    auto a = array();
261
340
    if (at < 0 || at >= size()) {
262
12
        return false;
263
12
    }
264
328
    if (a->sp) {
265
45
        auto end = a->sp->elements.end();
266
45
        if (auto iter = a->sp->elements.lower_bound(at); iter != end) {
267
45
            if (iter->first == at) {
268
45
                iter++;
269
45
                a->sp->elements.erase(at);
270
45
            }
271
272
4.57k
            while (iter != end) {
273
4.52k
                auto nh = a->sp->elements.extract(iter++);
274
4.52k
                --nh.key();
275
4.52k
                a->sp->elements.insert(std::move(nh));
276
4.52k
            }
277
45
        }
278
45
        --(a->sp->size);
279
283
    } else {
280
283
        a->elements.erase(a->elements.cbegin() + at);
281
283
    }
282
328
    return true;
283
340
}
284
285
int
286
QPDFObjectHandle::getArrayNItems() const
287
161k
{
288
161k
    if (auto array = as_array(strict)) {
289
161k
        return array.size();
290
161k
    }
291
552
    typeWarning("array", "treating as empty");
292
552
    QTC::TC("qpdf", "QPDFObjectHandle array treating as empty");
293
552
    return 0;
294
161k
}
295
296
QPDFObjectHandle
297
QPDFObjectHandle::getArrayItem(int n) const
298
155k
{
299
155k
    if (auto array = as_array(strict)) {
300
155k
        if (auto const [success, oh] = array.at(n); success) {
301
155k
            return oh;
302
155k
        } else {
303
4
            objectWarning("returning null for out of bounds array access");
304
4
            QTC::TC("qpdf", "QPDFObjectHandle array bounds");
305
4
        }
306
155k
    } else {
307
83
        typeWarning("array", "returning null");
308
83
        QTC::TC("qpdf", "QPDFObjectHandle array null for non-array");
309
83
    }
310
87
    static auto constexpr msg = " -> null returned from invalid array access"sv;
311
87
    return QPDF_Null::create(obj, msg, "");
312
155k
}
313
314
bool
315
QPDFObjectHandle::isRectangle() const
316
23.3k
{
317
23.3k
    if (auto array = as_array(strict)) {
318
40.0k
        for (int i = 0; i < 4; ++i) {
319
32.3k
            if (auto item = array.at(i).second; !item.isNumber()) {
320
586
                return false;
321
586
            }
322
32.3k
        }
323
7.67k
        return array.size() == 4;
324
8.25k
    }
325
15.1k
    return false;
326
23.3k
}
327
328
bool
329
QPDFObjectHandle::isMatrix() const
330
0
{
331
0
    if (auto array = as_array(strict)) {
332
0
        for (int i = 0; i < 6; ++i) {
333
0
            if (auto item = array.at(i).second; !item.isNumber()) {
334
0
                return false;
335
0
            }
336
0
        }
337
0
        return array.size() == 6;
338
0
    }
339
0
    return false;
340
0
}
341
342
QPDFObjectHandle::Rectangle
343
QPDFObjectHandle::getArrayAsRectangle() const
344
0
{
345
0
    if (auto array = as_array(strict)) {
346
0
        if (array.size() != 4) {
347
0
            return {};
348
0
        }
349
0
        double items[4];
350
0
        for (int i = 0; i < 4; ++i) {
351
0
            if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {
352
0
                return {};
353
0
            }
354
0
        }
355
0
        return {
356
0
            std::min(items[0], items[2]),
357
0
            std::min(items[1], items[3]),
358
0
            std::max(items[0], items[2]),
359
0
            std::max(items[1], items[3])};
360
0
    }
361
0
    return {};
362
0
}
363
364
QPDFObjectHandle::Matrix
365
QPDFObjectHandle::getArrayAsMatrix() const
366
0
{
367
0
    if (auto array = as_array(strict)) {
368
0
        if (array.size() != 6) {
369
0
            return {};
370
0
        }
371
0
        double items[6];
372
0
        for (int i = 0; i < 6; ++i) {
373
0
            if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {
374
0
                return {};
375
0
            }
376
0
        }
377
0
        return {items[0], items[1], items[2], items[3], items[4], items[5]};
378
0
    }
379
0
    return {};
380
0
}
381
382
std::vector<QPDFObjectHandle>
383
QPDFObjectHandle::getArrayAsVector() const
384
17.1k
{
385
17.1k
    if (auto array = as_array(strict)) {
386
17.1k
        return array.getAsVector();
387
17.1k
    }
388
0
    typeWarning("array", "treating as empty");
389
0
    QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");
390
0
    return {};
391
17.1k
}
392
393
void
394
QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item)
395
0
{
396
0
    if (auto array = as_array(strict)) {
397
0
        if (!array.setAt(n, item)) {
398
0
            objectWarning("ignoring attempt to set out of bounds array item");
399
0
            QTC::TC("qpdf", "QPDFObjectHandle set array bounds");
400
0
        }
401
0
    } else {
402
0
        typeWarning("array", "ignoring attempt to set item");
403
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item");
404
0
    }
405
0
}
406
void
407
QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
408
0
{
409
0
    if (auto array = as_array(strict)) {
410
0
        array.setFromVector(items);
411
0
    } else {
412
0
        typeWarning("array", "ignoring attempt to replace items");
413
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring replace items");
414
0
    }
415
0
}
416
417
void
418
QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item)
419
0
{
420
0
    if (auto array = as_array(strict)) {
421
0
        if (!array.insert(at, item)) {
422
0
            objectWarning("ignoring attempt to insert out of bounds array item");
423
0
            QTC::TC("qpdf", "QPDFObjectHandle insert array bounds");
424
0
        }
425
0
    } else {
426
0
        typeWarning("array", "ignoring attempt to insert item");
427
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item");
428
0
    }
429
0
}
430
431
QPDFObjectHandle
432
QPDFObjectHandle::insertItemAndGetNew(int at, QPDFObjectHandle const& item)
433
0
{
434
0
    insertItem(at, item);
435
0
    return item;
436
0
}
437
438
void
439
QPDFObjectHandle::appendItem(QPDFObjectHandle const& item)
440
0
{
441
0
    if (auto array = as_array(strict)) {
442
0
        array.push_back(item);
443
0
    } else {
444
0
        typeWarning("array", "ignoring attempt to append item");
445
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring append item");
446
0
    }
447
0
}
448
449
QPDFObjectHandle
450
QPDFObjectHandle::appendItemAndGetNew(QPDFObjectHandle const& item)
451
0
{
452
0
    appendItem(item);
453
0
    return item;
454
0
}
455
456
void
457
QPDFObjectHandle::eraseItem(int at)
458
644
{
459
644
    if (auto array = as_array(strict)) {
460
340
        if (!array.erase(at)) {
461
12
            objectWarning("ignoring attempt to erase out of bounds array item");
462
12
            QTC::TC("qpdf", "QPDFObjectHandle erase array bounds");
463
12
        }
464
340
    } else {
465
304
        typeWarning("array", "ignoring attempt to erase item");
466
304
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item");
467
304
    }
468
644
}
469
470
QPDFObjectHandle
471
QPDFObjectHandle::eraseItemAndGetOld(int at)
472
0
{
473
0
    auto array = as_array(strict);
474
0
    auto result = (array && at < array.size() && at >= 0) ? array.at(at).second : newNull();
475
0
    eraseItem(at);
476
0
    return result;
477
0
}