Coverage Report

Created: 2025-08-26 07:09

/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
#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
301
{
16
301
    return static_cast<size_t>(n);
17
301
}
18
19
static int
20
to_i(size_t n)
21
69.8k
{
22
69.8k
    return static_cast<int>(n);
23
69.8k
}
24
25
inline void
26
Array::checkOwnership(QPDFObjectHandle const& item) const
27
0
{
28
0
    if (!item) {
29
0
        throw std::logic_error("Attempting to add an uninitialized object to a QPDF_Array.");
30
0
    }
31
0
    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
0
}
37
38
QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle>&& v, bool sparse)
39
698
{
40
698
    if (sparse) {
41
460
        sp = std::make_unique<Sparse>();
42
7.24M
        for (auto& item: v) {
43
7.24M
            if (item.raw_type_code() != ::ot_null || item.indirect()) {
44
7.11M
                sp->elements[sp->size] = std::move(item);
45
7.11M
            }
46
7.24M
            ++sp->size;
47
7.24M
        }
48
460
    } else {
49
238
        elements = std::move(v);
50
238
    }
51
698
}
52
53
QPDF_Array*
54
Array::array() const
55
13.4k
{
56
13.4k
    if (auto a = as<QPDF_Array>()) {
57
13.4k
        return a;
58
13.4k
    }
59
60
0
    throw std::runtime_error("Expected an array but found a non-array object");
61
0
    return nullptr; // unreachable
62
13.4k
}
63
64
Array::Array(bool empty) :
65
0
    BaseHandle(empty ? QPDFObject::create<QPDF_Array>() : nullptr)
66
0
{
67
0
}
68
69
Array::Array(std::vector<QPDFObjectHandle> const& items) :
70
0
    BaseHandle(QPDFObject::create<QPDF_Array>(items))
71
0
{
72
0
}
73
74
Array::Array(std::vector<QPDFObjectHandle>&& items) :
75
0
    BaseHandle(QPDFObject::create<QPDF_Array>(std::move(items)))
76
0
{
77
0
}
78
79
Array::iterator
80
Array::begin()
81
232k
{
82
232k
    if (auto a = as<QPDF_Array>()) {
83
215k
        if (!a->sp) {
84
215k
            return a->elements.begin();
85
215k
        }
86
679
        if (!sp_elements) {
87
679
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
88
679
        }
89
679
        return sp_elements->begin();
90
215k
    }
91
16.9k
    return {};
92
232k
}
93
94
Array::iterator
95
Array::end()
96
232k
{
97
232k
    if (auto a = as<QPDF_Array>()) {
98
215k
        if (!a->sp) {
99
215k
            return a->elements.end();
100
215k
        }
101
679
        if (!sp_elements) {
102
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
103
0
        }
104
679
        return sp_elements->end();
105
215k
    }
106
16.9k
    return {};
107
232k
}
108
109
Array::const_iterator
110
Array::cbegin()
111
0
{
112
0
    if (auto a = as<QPDF_Array>()) {
113
0
        if (!a->sp) {
114
0
            return a->elements.cbegin();
115
0
        }
116
0
        if (!sp_elements) {
117
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
118
0
        }
119
0
        return sp_elements->cbegin();
120
0
    }
121
0
    return {};
122
0
}
123
124
Array::const_iterator
125
Array::cend()
126
0
{
127
0
    if (auto a = as<QPDF_Array>()) {
128
0
        if (!a->sp) {
129
0
            return a->elements.cend();
130
0
        }
131
0
        if (!sp_elements) {
132
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
133
0
        }
134
0
        return sp_elements->cend();
135
0
    }
136
0
    return {};
137
0
}
138
139
Array::const_reverse_iterator
140
Array::crbegin()
141
7.60M
{
142
7.60M
    if (auto a = as<QPDF_Array>()) {
143
19.2k
        if (!a->sp) {
144
19.1k
            return a->elements.crbegin();
145
19.1k
        }
146
68
        if (!sp_elements) {
147
68
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
148
68
        }
149
68
        return sp_elements->crbegin();
150
19.2k
    }
151
7.58M
    return {};
152
7.60M
}
153
154
Array::const_reverse_iterator
155
Array::crend()
156
7.60M
{
157
7.60M
    if (auto a = as<QPDF_Array>()) {
158
19.2k
        if (!a->sp) {
159
19.1k
            return a->elements.crend();
160
19.1k
        }
161
68
        if (!sp_elements) {
162
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
163
0
        }
164
68
        return sp_elements->crend();
165
19.2k
    }
166
7.58M
    return {};
167
7.60M
}
168
169
QPDFObjectHandle
170
Array::null() const
171
0
{
172
0
    return null_oh;
173
0
}
174
175
size_t
176
Array::size() const
177
45.7k
{
178
45.7k
    if (auto a = as<QPDF_Array>()) {
179
29.4k
        return a->sp ? a->sp->size : a->elements.size();
180
29.4k
    }
181
16.2k
    return 0;
182
45.7k
}
183
184
QPDFObjectHandle const&
185
Array::operator[](size_t n) const
186
103k
{
187
103k
    static const QPDFObjectHandle null_obj;
188
103k
    auto a = as<QPDF_Array>();
189
103k
    if (!a) {
190
0
        return null_obj;
191
0
    }
192
103k
    if (a->sp) {
193
22.6k
        auto const& iter = a->sp->elements.find(n);
194
22.6k
        return iter == a->sp->elements.end() ? null_obj : iter->second;
195
22.6k
    }
196
81.0k
    return n >= a->elements.size() ? null_obj : a->elements[n];
197
103k
}
198
199
QPDFObjectHandle const&
200
Array::operator[](int n) const
201
103k
{
202
103k
    static const QPDFObjectHandle null_obj;
203
103k
    if (n < 0) {
204
0
        return null_obj;
205
0
    }
206
103k
    return (*this)[static_cast<size_t>(n)];
207
103k
}
208
209
QPDFObjectHandle
210
Array::get(size_t n) const
211
127
{
212
127
    if (n >= size()) {
213
0
        return {};
214
0
    }
215
127
    auto a = array();
216
127
    if (!a->sp) {
217
127
        return a->elements[n];
218
127
    }
219
0
    auto const& iter = a->sp->elements.find(n);
220
0
    return iter == a->sp->elements.end() ? null() : iter->second;
221
127
}
222
223
QPDFObjectHandle
224
Array::get(int n) const
225
127
{
226
127
    if (n < 0) {
227
0
        return {};
228
0
    }
229
127
    return get(to_s(n));
230
127
}
231
232
std::vector<QPDFObjectHandle>
233
Array::getAsVector() const
234
13.1k
{
235
13.1k
    auto a = array();
236
13.1k
    if (a->sp) {
237
802
        std::vector<QPDFObjectHandle> v;
238
802
        v.reserve(size());
239
21.8M
        for (auto const& item: a->sp->elements) {
240
21.8M
            v.resize(item.first, null_oh);
241
21.8M
            v.emplace_back(item.second);
242
21.8M
        }
243
802
        v.resize(size(), null_oh);
244
802
        return v;
245
12.3k
    } else {
246
12.3k
        return a->elements;
247
12.3k
    }
248
13.1k
}
249
250
bool
251
Array::set(size_t at, QPDFObjectHandle const& oh)
252
0
{
253
0
    if (at >= size()) {
254
0
        return false;
255
0
    }
256
0
    auto a = array();
257
0
    checkOwnership(oh);
258
0
    if (a->sp) {
259
0
        a->sp->elements[at] = oh;
260
0
    } else {
261
0
        a->elements[at] = oh;
262
0
    }
263
0
    return true;
264
0
}
265
266
bool
267
Array::set(int at, QPDFObjectHandle const& oh)
268
0
{
269
0
    if (at < 0) {
270
0
        return false;
271
0
    }
272
0
    return set(to_s(at), oh);
273
0
}
274
275
void
276
Array::setFromVector(std::vector<QPDFObjectHandle> const& v)
277
0
{
278
0
    auto a = array();
279
0
    a->elements.resize(0);
280
0
    a->elements.reserve(v.size());
281
0
    for (auto const& item: v) {
282
0
        checkOwnership(item);
283
0
        a->elements.emplace_back(item);
284
0
    }
285
0
}
286
287
bool
288
Array::insert(size_t at, QPDFObjectHandle const& item)
289
0
{
290
0
    auto a = array();
291
0
    size_t sz = size();
292
0
    if (at > sz) {
293
0
        return false;
294
0
    }
295
0
    checkOwnership(item);
296
0
    if (at == sz) {
297
        // As special case, also allow insert beyond the end
298
0
        push_back(item);
299
0
        return true;
300
0
    }
301
0
    if (!a->sp) {
302
0
        a->elements.insert(a->elements.cbegin() + to_i(at), item);
303
0
        return true;
304
0
    }
305
0
    auto iter = a->sp->elements.crbegin();
306
0
    while (iter != a->sp->elements.crend()) {
307
0
        auto key = (iter++)->first;
308
0
        if (key >= at) {
309
0
            auto nh = a->sp->elements.extract(key);
310
0
            ++nh.key();
311
0
            a->sp->elements.insert(std::move(nh));
312
0
        } else {
313
0
            break;
314
0
        }
315
0
    }
316
0
    a->sp->elements[at] = item;
317
0
    ++a->sp->size;
318
0
    return true;
319
0
}
320
321
bool
322
Array::insert(int at_i, QPDFObjectHandle const& item)
323
0
{
324
0
    if (at_i < 0) {
325
0
        return false;
326
0
    }
327
0
    return insert(to_s(at_i), item);
328
0
}
329
330
void
331
Array::push_back(QPDFObjectHandle const& item)
332
0
{
333
0
    auto a = array();
334
0
    checkOwnership(item);
335
0
    if (a->sp) {
336
0
        a->sp->elements[(a->sp->size)++] = item;
337
0
    } else {
338
0
        a->elements.emplace_back(item);
339
0
    }
340
0
}
341
342
bool
343
Array::erase(size_t at)
344
174
{
345
174
    auto a = array();
346
174
    if (at >= size()) {
347
6
        return false;
348
6
    }
349
168
    if (!a->sp) {
350
121
        a->elements.erase(a->elements.cbegin() + to_i(at));
351
121
        return true;
352
121
    }
353
47
    auto end = a->sp->elements.end();
354
47
    if (auto iter = a->sp->elements.lower_bound(at); iter != end) {
355
47
        if (iter->first == at) {
356
47
            iter++;
357
47
            a->sp->elements.erase(at);
358
47
        }
359
360
19.4k
        while (iter != end) {
361
19.3k
            auto nh = a->sp->elements.extract(iter++);
362
19.3k
            --nh.key();
363
19.3k
            a->sp->elements.insert(std::move(nh));
364
19.3k
        }
365
47
    }
366
47
    --(a->sp->size);
367
47
    return true;
368
168
}
369
370
bool
371
Array::erase(int at_i)
372
174
{
373
174
    if (at_i < 0) {
374
0
        return false;
375
0
    }
376
174
    return erase(to_s(at_i));
377
174
}
378
379
int
380
QPDFObjectHandle::getArrayNItems() const
381
70.6k
{
382
70.6k
    auto s = size();
383
70.6k
    if (s > 1 || isArray()) {
384
69.7k
        return to_i(s);
385
69.7k
    }
386
882
    typeWarning("array", "treating as empty");
387
882
    return 0;
388
70.6k
}
389
390
QPDFObjectHandle
391
QPDFObjectHandle::getArrayItem(int n) const
392
103k
{
393
103k
    if (auto array = Array(*this)) {
394
103k
        if (auto result = array[n]) {
395
82.8k
            return result;
396
82.8k
        }
397
20.8k
        if (n >= 0 && std::cmp_less(n, array.size())) {
398
            // sparse array null
399
20.8k
            return newNull();
400
20.8k
        }
401
1
        objectWarning("returning null for out of bounds array access");
402
403
24
    } else {
404
24
        typeWarning("array", "returning null");
405
24
    }
406
25
    static auto constexpr msg = " -> null returned from invalid array access"sv;
407
25
    return QPDF_Null::create(obj, msg, "");
408
103k
}
409
410
bool
411
QPDFObjectHandle::isRectangle() const
412
23.1k
{
413
23.1k
    Array array(*this);
414
27.0k
    for (auto const& oh: array) {
415
27.0k
        if (!oh.isNumber()) {
416
361
            return false;
417
361
        }
418
27.0k
    }
419
22.7k
    return array.size() == 4;
420
23.1k
}
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
12.3k
{
473
12.3k
    if (auto array = as_array(strict)) {
474
12.3k
        return array.getAsVector();
475
12.3k
    }
476
0
    typeWarning("array", "treating as empty");
477
0
    QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");
478
0
    return {};
479
12.3k
}
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
0
{
527
0
    if (auto array = as_array(strict)) {
528
0
        array.push_back(item);
529
0
    } else {
530
0
        typeWarning("array", "ignoring attempt to append item");
531
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring append item");
532
0
    }
533
0
}
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
264
{
545
264
    if (auto array = as_array(strict)) {
546
174
        if (!array.erase(at)) {
547
6
            objectWarning("ignoring attempt to erase out of bounds array item");
548
6
            QTC::TC("qpdf", "QPDFObjectHandle erase array bounds");
549
6
        }
550
174
    } else {
551
90
        typeWarning("array", "ignoring attempt to erase item");
552
90
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item");
553
90
    }
554
264
}
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
105k
{
567
105k
    switch (resolved_type_code()) {
568
78.4k
    case ::ot_array:
569
78.4k
        return as<QPDF_Array>()->size();
570
0
    case ::ot_uninitialized:
571
0
    case ::ot_reserved:
572
26.1k
    case ::ot_null:
573
26.1k
    case ::ot_destroyed:
574
26.1k
    case ::ot_unresolved:
575
26.1k
    case ::ot_reference:
576
26.1k
        return 0;
577
3
    case ::ot_boolean:
578
29
    case ::ot_integer:
579
35
    case ::ot_real:
580
53
    case ::ot_string:
581
938
    case ::ot_name:
582
1.30k
    case ::ot_dictionary:
583
1.31k
    case ::ot_stream:
584
1.31k
    case ::ot_inlineimage:
585
1.31k
    case ::ot_operator:
586
1.31k
        return 1;
587
0
    default:
588
0
        throw std::logic_error("Unexpected type code in size"); // unreachable
589
0
        return 0;                                               // unreachable
590
105k
    }
591
105k
}
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
}