Coverage Report

Created: 2026-01-09 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/QPDF_Array.cc
Line
Count
Source
1
#include <qpdf/QPDFObjectHandle_private.hh>
2
3
#include <qpdf/QTC.hh>
4
5
#include <array>
6
#include <utility>
7
8
using namespace std::literals;
9
using namespace qpdf;
10
11
static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
12
13
static size_t
14
to_s(int n)
15
0
{
16
0
    return static_cast<size_t>(n);
17
0
}
18
19
static int
20
to_i(size_t n)
21
0
{
22
0
    return static_cast<int>(n);
23
0
}
24
25
inline void
26
Array::checkOwnership(QPDFObjectHandle const& item) const
27
1.74M
{
28
1.74M
    if (!item) {
29
0
        throw std::logic_error("Attempting to add an uninitialized object to a QPDF_Array.");
30
0
    }
31
1.74M
    if (qpdf() && item.qpdf() && qpdf() != item.qpdf()) {
32
0
        throw std::logic_error(
33
0
            "Attempting to add an object from a different QPDF. Use "
34
0
            "QPDF::copyForeignObject to add objects from another file.");
35
0
    }
36
1.74M
}
37
38
QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle>&& v, bool sparse)
39
0
{
40
0
    if (sparse) {
41
0
        sp = std::make_unique<Sparse>();
42
0
        for (auto& item: v) {
43
0
            if (item.raw_type_code() != ::ot_null || item.indirect()) {
44
0
                sp->elements[sp->size] = std::move(item);
45
0
            }
46
0
            ++sp->size;
47
0
        }
48
0
    } else {
49
0
        elements = std::move(v);
50
0
    }
51
0
}
52
53
QPDF_Array*
54
Array::array() const
55
1.74M
{
56
1.74M
    if (auto a = as<QPDF_Array>()) {
57
1.74M
        return a;
58
1.74M
    }
59
60
0
    throw std::runtime_error("Expected an array but found a non-array object");
61
0
    return nullptr; // unreachable
62
1.74M
}
63
64
Array::Array(std::vector<QPDFObjectHandle> const& items) :
65
0
    BaseHandle(QPDFObject::create<QPDF_Array>(items))
66
0
{
67
0
}
68
69
Array::Array(std::vector<QPDFObjectHandle>&& items) :
70
0
    BaseHandle(QPDFObject::create<QPDF_Array>(std::move(items)))
71
0
{
72
0
}
73
74
Array
75
Array::empty()
76
0
{
77
0
    return Array(std::vector<QPDFObjectHandle>());
78
0
}
79
80
Array::iterator
81
Array::begin()
82
0
{
83
0
    if (auto a = as<QPDF_Array>()) {
84
0
        if (!a->sp) {
85
0
            return a->elements.begin();
86
0
        }
87
0
        if (!sp_elements) {
88
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
89
0
        }
90
0
        return sp_elements->begin();
91
0
    }
92
0
    return {};
93
0
}
94
95
Array::iterator
96
Array::end()
97
0
{
98
0
    if (auto a = as<QPDF_Array>()) {
99
0
        if (!a->sp) {
100
0
            return a->elements.end();
101
0
        }
102
0
        if (!sp_elements) {
103
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
104
0
        }
105
0
        return sp_elements->end();
106
0
    }
107
0
    return {};
108
0
}
109
110
Array::const_iterator
111
Array::cbegin()
112
0
{
113
0
    if (auto a = as<QPDF_Array>()) {
114
0
        if (!a->sp) {
115
0
            return a->elements.cbegin();
116
0
        }
117
0
        if (!sp_elements) {
118
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
119
0
        }
120
0
        return sp_elements->cbegin();
121
0
    }
122
0
    return {};
123
0
}
124
125
Array::const_iterator
126
Array::cend()
127
0
{
128
0
    if (auto a = as<QPDF_Array>()) {
129
0
        if (!a->sp) {
130
0
            return a->elements.cend();
131
0
        }
132
0
        if (!sp_elements) {
133
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
134
0
        }
135
0
        return sp_elements->cend();
136
0
    }
137
0
    return {};
138
0
}
139
140
Array::const_reverse_iterator
141
Array::crbegin()
142
0
{
143
0
    if (auto a = as<QPDF_Array>()) {
144
0
        if (!a->sp) {
145
0
            return a->elements.crbegin();
146
0
        }
147
0
        if (!sp_elements) {
148
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
149
0
        }
150
0
        return sp_elements->crbegin();
151
0
    }
152
0
    return {};
153
0
}
154
155
Array::const_reverse_iterator
156
Array::crend()
157
0
{
158
0
    if (auto a = as<QPDF_Array>()) {
159
0
        if (!a->sp) {
160
0
            return a->elements.crend();
161
0
        }
162
0
        if (!sp_elements) {
163
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
164
0
        }
165
0
        return sp_elements->crend();
166
0
    }
167
0
    return {};
168
0
}
169
170
QPDFObjectHandle
171
Array::null() const
172
0
{
173
0
    return null_oh;
174
0
}
175
176
size_t
177
Array::size() const
178
0
{
179
0
    if (auto a = as<QPDF_Array>()) {
180
0
        return a->sp ? a->sp->size : a->elements.size();
181
0
    }
182
0
    return 0;
183
0
}
184
185
QPDFObjectHandle const&
186
Array::operator[](size_t n) const
187
0
{
188
0
    static const QPDFObjectHandle null_obj;
189
0
    auto a = as<QPDF_Array>();
190
0
    if (!a) {
191
0
        return null_obj;
192
0
    }
193
0
    if (a->sp) {
194
0
        auto const& iter = a->sp->elements.find(n);
195
0
        return iter == a->sp->elements.end() ? null_obj : iter->second;
196
0
    }
197
0
    return n >= a->elements.size() ? null_obj : a->elements[n];
198
0
}
199
200
QPDFObjectHandle const&
201
Array::operator[](int n) const
202
0
{
203
0
    static const QPDFObjectHandle null_obj;
204
0
    if (n < 0) {
205
0
        return null_obj;
206
0
    }
207
0
    return (*this)[static_cast<size_t>(n)];
208
0
}
209
210
QPDFObjectHandle
211
Array::get(size_t n) const
212
0
{
213
0
    if (n >= size()) {
214
0
        return {};
215
0
    }
216
0
    auto a = array();
217
0
    if (!a->sp) {
218
0
        return a->elements[n];
219
0
    }
220
0
    auto const& iter = a->sp->elements.find(n);
221
0
    return iter == a->sp->elements.end() ? null() : iter->second;
222
0
}
223
224
QPDFObjectHandle
225
Array::get(int n) const
226
0
{
227
0
    if (n < 0) {
228
0
        return {};
229
0
    }
230
0
    return get(to_s(n));
231
0
}
232
233
std::vector<QPDFObjectHandle>
234
Array::getAsVector() const
235
0
{
236
0
    auto a = array();
237
0
    if (a->sp) {
238
0
        std::vector<QPDFObjectHandle> v;
239
0
        v.reserve(size());
240
0
        for (auto const& item: a->sp->elements) {
241
0
            v.resize(item.first, null_oh);
242
0
            v.emplace_back(item.second);
243
0
        }
244
0
        v.resize(size(), null_oh);
245
0
        return v;
246
0
    } else {
247
0
        return a->elements;
248
0
    }
249
0
}
250
251
bool
252
Array::set(size_t at, QPDFObjectHandle const& oh)
253
0
{
254
0
    if (at >= size()) {
255
0
        return false;
256
0
    }
257
0
    auto a = array();
258
0
    checkOwnership(oh);
259
0
    if (a->sp) {
260
0
        a->sp->elements[at] = oh;
261
0
    } else {
262
0
        a->elements[at] = oh;
263
0
    }
264
0
    return true;
265
0
}
266
267
bool
268
Array::set(int at, QPDFObjectHandle const& oh)
269
0
{
270
0
    if (at < 0) {
271
0
        return false;
272
0
    }
273
0
    return set(to_s(at), oh);
274
0
}
275
276
void
277
Array::setFromVector(std::vector<QPDFObjectHandle> const& v)
278
0
{
279
0
    auto a = array();
280
0
    a->elements.resize(0);
281
0
    a->elements.reserve(v.size());
282
0
    for (auto const& item: v) {
283
0
        checkOwnership(item);
284
0
        a->elements.emplace_back(item);
285
0
    }
286
0
}
287
288
bool
289
Array::insert(size_t at, QPDFObjectHandle const& item)
290
0
{
291
0
    auto a = array();
292
0
    size_t sz = size();
293
0
    if (at > sz) {
294
0
        return false;
295
0
    }
296
0
    checkOwnership(item);
297
0
    if (at == sz) {
298
        // As special case, also allow insert beyond the end
299
0
        push_back(item);
300
0
        return true;
301
0
    }
302
0
    if (!a->sp) {
303
0
        a->elements.insert(a->elements.cbegin() + to_i(at), item);
304
0
        return true;
305
0
    }
306
0
    auto iter = a->sp->elements.crbegin();
307
0
    while (iter != a->sp->elements.crend()) {
308
0
        auto key = (iter++)->first;
309
0
        if (key >= at) {
310
0
            auto nh = a->sp->elements.extract(key);
311
0
            ++nh.key();
312
0
            a->sp->elements.insert(std::move(nh));
313
0
        } else {
314
0
            break;
315
0
        }
316
0
    }
317
0
    a->sp->elements[at] = item;
318
0
    ++a->sp->size;
319
0
    return true;
320
0
}
321
322
bool
323
Array::insert(int at_i, QPDFObjectHandle const& item)
324
0
{
325
0
    if (at_i < 0) {
326
0
        return false;
327
0
    }
328
0
    return insert(to_s(at_i), item);
329
0
}
330
331
void
332
Array::push_back(QPDFObjectHandle const& item)
333
1.74M
{
334
1.74M
    auto a = array();
335
1.74M
    checkOwnership(item);
336
1.74M
    if (a->sp) {
337
0
        a->sp->elements[(a->sp->size)++] = item;
338
1.74M
    } else {
339
1.74M
        a->elements.emplace_back(item);
340
1.74M
    }
341
1.74M
}
342
343
bool
344
Array::erase(size_t at)
345
0
{
346
0
    auto a = array();
347
0
    if (at >= size()) {
348
0
        return false;
349
0
    }
350
0
    if (!a->sp) {
351
0
        a->elements.erase(a->elements.cbegin() + to_i(at));
352
0
        return true;
353
0
    }
354
0
    auto end = a->sp->elements.end();
355
0
    if (auto iter = a->sp->elements.lower_bound(at); iter != end) {
356
0
        if (iter->first == at) {
357
0
            iter++;
358
0
            a->sp->elements.erase(at);
359
0
        }
360
361
0
        while (iter != end) {
362
0
            auto nh = a->sp->elements.extract(iter++);
363
0
            --nh.key();
364
0
            a->sp->elements.insert(std::move(nh));
365
0
        }
366
0
    }
367
0
    --(a->sp->size);
368
0
    return true;
369
0
}
370
371
bool
372
Array::erase(int at_i)
373
0
{
374
0
    if (at_i < 0) {
375
0
        return false;
376
0
    }
377
0
    return erase(to_s(at_i));
378
0
}
379
380
int
381
QPDFObjectHandle::getArrayNItems() const
382
0
{
383
0
    auto s = size();
384
0
    if (s > 1 || isArray()) {
385
0
        return to_i(s);
386
0
    }
387
0
    typeWarning("array", "treating as empty");
388
0
    return 0;
389
0
}
390
391
QPDFObjectHandle
392
QPDFObjectHandle::getArrayItem(int n) const
393
0
{
394
0
    if (auto array = Array(*this)) {
395
0
        if (auto result = array[n]) {
396
0
            return result;
397
0
        }
398
0
        if (n >= 0 && std::cmp_less(n, array.size())) {
399
            // sparse array null
400
0
            return newNull();
401
0
        }
402
0
        objectWarning("returning null for out of bounds array access");
403
0
    } else {
404
0
        typeWarning("array", "returning null");
405
0
    }
406
0
    static auto constexpr msg = " -> null returned from invalid array access"sv;
407
0
    return QPDF_Null::create(obj, msg, "");
408
0
}
409
410
bool
411
QPDFObjectHandle::isRectangle() const
412
0
{
413
0
    Array array(*this);
414
0
    for (auto const& oh: array) {
415
0
        if (!oh.isNumber()) {
416
0
            return false;
417
0
        }
418
0
    }
419
0
    return array.size() == 4;
420
0
}
421
422
bool
423
QPDFObjectHandle::isMatrix() const
424
0
{
425
0
    Array array(*this);
426
0
    for (auto const& oh: array) {
427
0
        if (!oh.isNumber()) {
428
0
            return false;
429
0
        }
430
0
    }
431
0
    return array.size() == 6;
432
0
}
433
434
QPDFObjectHandle::Rectangle
435
QPDFObjectHandle::getArrayAsRectangle() const
436
0
{
437
0
    Array array(*this);
438
0
    if (array.size() != 4) {
439
0
        return {};
440
0
    }
441
0
    std::array<double, 4> items;
442
0
    for (size_t i = 0; i < 4; ++i) {
443
0
        if (!array[i].getValueAsNumber(items[i])) {
444
0
            return {};
445
0
        }
446
0
    }
447
0
    return {
448
0
        std::min(items[0], items[2]),
449
0
        std::min(items[1], items[3]),
450
0
        std::max(items[0], items[2]),
451
0
        std::max(items[1], items[3])};
452
0
}
453
454
QPDFObjectHandle::Matrix
455
QPDFObjectHandle::getArrayAsMatrix() const
456
0
{
457
0
    Array array(*this);
458
0
    if (array.size() != 6) {
459
0
        return {};
460
0
    }
461
0
    std::array<double, 6> items;
462
0
    for (size_t i = 0; i < 6; ++i) {
463
0
        if (!array[i].getValueAsNumber(items[i])) {
464
0
            return {};
465
0
        }
466
0
    }
467
0
    return {items[0], items[1], items[2], items[3], items[4], items[5]};
468
0
}
469
470
std::vector<QPDFObjectHandle>
471
QPDFObjectHandle::getArrayAsVector() const
472
0
{
473
0
    if (auto array = as_array(strict)) {
474
0
        return array.getAsVector();
475
0
    }
476
0
    typeWarning("array", "treating as empty");
477
0
    QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");
478
0
    return {};
479
0
}
480
481
void
482
QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item)
483
0
{
484
0
    if (auto array = as_array(strict)) {
485
0
        if (!array.set(n, item)) {
486
0
            objectWarning("ignoring attempt to set out of bounds array item");
487
0
        }
488
0
    } else {
489
0
        typeWarning("array", "ignoring attempt to set item");
490
0
    }
491
0
}
492
void
493
QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
494
0
{
495
0
    if (auto array = as_array(strict)) {
496
0
        array.setFromVector(items);
497
0
    } else {
498
0
        typeWarning("array", "ignoring attempt to replace items");
499
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring replace items");
500
0
    }
501
0
}
502
503
void
504
QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item)
505
0
{
506
0
    if (auto array = as_array(strict)) {
507
0
        if (!array.insert(at, item)) {
508
0
            objectWarning("ignoring attempt to insert out of bounds array item");
509
0
            QTC::TC("qpdf", "QPDFObjectHandle insert array bounds");
510
0
        }
511
0
    } else {
512
0
        typeWarning("array", "ignoring attempt to insert item");
513
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item");
514
0
    }
515
0
}
516
517
QPDFObjectHandle
518
QPDFObjectHandle::insertItemAndGetNew(int at, QPDFObjectHandle const& item)
519
0
{
520
0
    insertItem(at, item);
521
0
    return item;
522
0
}
523
524
void
525
QPDFObjectHandle::appendItem(QPDFObjectHandle const& item)
526
1.74M
{
527
1.74M
    if (auto array = as_array(strict)) {
528
1.74M
        array.push_back(item);
529
1.74M
    } else {
530
0
        typeWarning("array", "ignoring attempt to append item");
531
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring append item");
532
0
    }
533
1.74M
}
534
535
QPDFObjectHandle
536
QPDFObjectHandle::appendItemAndGetNew(QPDFObjectHandle const& item)
537
0
{
538
0
    appendItem(item);
539
0
    return item;
540
0
}
541
542
void
543
QPDFObjectHandle::eraseItem(int at)
544
0
{
545
0
    if (auto array = as_array(strict)) {
546
0
        if (!array.erase(at)) {
547
0
            objectWarning("ignoring attempt to erase out of bounds array item");
548
0
            QTC::TC("qpdf", "QPDFObjectHandle erase array bounds");
549
0
        }
550
0
    } else {
551
0
        typeWarning("array", "ignoring attempt to erase item");
552
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item");
553
0
    }
554
0
}
555
556
QPDFObjectHandle
557
QPDFObjectHandle::eraseItemAndGetOld(int at)
558
0
{
559
0
    auto result = Array(*this)[at];
560
0
    eraseItem(at);
561
0
    return result ? result : newNull();
562
0
}
563
564
size_t
565
BaseHandle::size() const
566
0
{
567
0
    switch (resolved_type_code()) {
568
0
    case ::ot_array:
569
0
        return as<QPDF_Array>()->size();
570
0
    case ::ot_uninitialized:
571
0
    case ::ot_reserved:
572
0
    case ::ot_null:
573
0
    case ::ot_destroyed:
574
0
    case ::ot_unresolved:
575
0
    case ::ot_reference:
576
0
        return 0;
577
0
    case ::ot_boolean:
578
0
    case ::ot_integer:
579
0
    case ::ot_real:
580
0
    case ::ot_string:
581
0
    case ::ot_name:
582
0
    case ::ot_dictionary:
583
0
    case ::ot_stream:
584
0
    case ::ot_inlineimage:
585
0
    case ::ot_operator:
586
0
        return 1;
587
0
    default:
588
0
        throw std::logic_error("Unexpected type code in size"); // unreachable
589
0
        return 0;                                               // unreachable
590
0
    }
591
0
}
592
593
QPDFObjectHandle
594
BaseHandle::operator[](size_t n) const
595
0
{
596
0
    if (resolved_type_code() == ::ot_array) {
597
0
        return Array(obj)[n];
598
0
    }
599
0
    if (n < size()) {
600
0
        return *this;
601
0
    }
602
0
    return {};
603
0
}
604
605
QPDFObjectHandle
606
BaseHandle::operator[](int n) const
607
0
{
608
0
    if (n < 0) {
609
0
        return {};
610
0
    }
611
0
    return (*this)[static_cast<size_t>(n)];
612
0
}