Coverage Report

Created: 2025-10-12 07:06

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
311
{
16
311
    return static_cast<size_t>(n);
17
311
}
18
19
static int
20
to_i(size_t n)
21
72.6k
{
22
72.6k
    return static_cast<int>(n);
23
72.6k
}
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
881
{
40
881
    if (sparse) {
41
449
        sp = std::make_unique<Sparse>();
42
4.63M
        for (auto& item: v) {
43
4.63M
            if (item.raw_type_code() != ::ot_null || item.indirect()) {
44
4.54M
                sp->elements[sp->size] = std::move(item);
45
4.54M
            }
46
4.63M
            ++sp->size;
47
4.63M
        }
48
449
    } else {
49
432
        elements = std::move(v);
50
432
    }
51
881
}
52
53
QPDF_Array*
54
Array::array() const
55
13.2k
{
56
13.2k
    if (auto a = as<QPDF_Array>()) {
57
13.2k
        return a;
58
13.2k
    }
59
60
0
    throw std::runtime_error("Expected an array but found a non-array object");
61
0
    return nullptr; // unreachable
62
13.2k
}
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
219k
{
83
219k
    if (auto a = as<QPDF_Array>()) {
84
202k
        if (!a->sp) {
85
201k
            return a->elements.begin();
86
201k
        }
87
664
        if (!sp_elements) {
88
664
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
89
664
        }
90
664
        return sp_elements->begin();
91
202k
    }
92
17.8k
    return {};
93
219k
}
94
95
Array::iterator
96
Array::end()
97
219k
{
98
219k
    if (auto a = as<QPDF_Array>()) {
99
202k
        if (!a->sp) {
100
201k
            return a->elements.end();
101
201k
        }
102
664
        if (!sp_elements) {
103
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
104
0
        }
105
664
        return sp_elements->end();
106
202k
    }
107
17.8k
    return {};
108
219k
}
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
5.02M
{
143
5.02M
    if (auto a = as<QPDF_Array>()) {
144
18.1k
        if (!a->sp) {
145
18.0k
            return a->elements.crbegin();
146
18.0k
        }
147
44
        if (!sp_elements) {
148
44
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
149
44
        }
150
44
        return sp_elements->crbegin();
151
18.1k
    }
152
5.00M
    return {};
153
5.02M
}
154
155
Array::const_reverse_iterator
156
Array::crend()
157
5.02M
{
158
5.02M
    if (auto a = as<QPDF_Array>()) {
159
18.1k
        if (!a->sp) {
160
18.0k
            return a->elements.crend();
161
18.0k
        }
162
44
        if (!sp_elements) {
163
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
164
0
        }
165
44
        return sp_elements->crend();
166
18.1k
    }
167
5.00M
    return {};
168
5.02M
}
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
44.4k
{
179
44.4k
    if (auto a = as<QPDF_Array>()) {
180
27.3k
        return a->sp ? a->sp->size : a->elements.size();
181
27.3k
    }
182
17.0k
    return 0;
183
44.4k
}
184
185
QPDFObjectHandle const&
186
Array::operator[](size_t n) const
187
98.0k
{
188
98.0k
    static const QPDFObjectHandle null_obj;
189
98.0k
    auto a = as<QPDF_Array>();
190
98.0k
    if (!a) {
191
0
        return null_obj;
192
0
    }
193
98.0k
    if (a->sp) {
194
20.4k
        auto const& iter = a->sp->elements.find(n);
195
20.4k
        return iter == a->sp->elements.end() ? null_obj : iter->second;
196
20.4k
    }
197
77.5k
    return n >= a->elements.size() ? null_obj : a->elements[n];
198
98.0k
}
199
200
QPDFObjectHandle const&
201
Array::operator[](int n) const
202
98.0k
{
203
98.0k
    static const QPDFObjectHandle null_obj;
204
98.0k
    if (n < 0) {
205
0
        return null_obj;
206
0
    }
207
98.0k
    return (*this)[static_cast<size_t>(n)];
208
98.0k
}
209
210
QPDFObjectHandle
211
Array::get(size_t n) const
212
116
{
213
116
    if (n >= size()) {
214
0
        return {};
215
0
    }
216
116
    auto a = array();
217
116
    if (!a->sp) {
218
116
        return a->elements[n];
219
116
    }
220
0
    auto const& iter = a->sp->elements.find(n);
221
0
    return iter == a->sp->elements.end() ? null() : iter->second;
222
116
}
223
224
QPDFObjectHandle
225
Array::get(int n) const
226
116
{
227
116
    if (n < 0) {
228
0
        return {};
229
0
    }
230
116
    return get(to_s(n));
231
116
}
232
233
std::vector<QPDFObjectHandle>
234
Array::getAsVector() const
235
12.9k
{
236
12.9k
    auto a = array();
237
12.9k
    if (a->sp) {
238
756
        std::vector<QPDFObjectHandle> v;
239
756
        v.reserve(size());
240
14.0M
        for (auto const& item: a->sp->elements) {
241
14.0M
            v.resize(item.first, null_oh);
242
14.0M
            v.emplace_back(item.second);
243
14.0M
        }
244
756
        v.resize(size(), null_oh);
245
756
        return v;
246
12.1k
    } else {
247
12.1k
        return a->elements;
248
12.1k
    }
249
12.9k
}
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
0
{
334
0
    auto a = array();
335
0
    checkOwnership(item);
336
0
    if (a->sp) {
337
0
        a->sp->elements[(a->sp->size)++] = item;
338
0
    } else {
339
0
        a->elements.emplace_back(item);
340
0
    }
341
0
}
342
343
bool
344
Array::erase(size_t at)
345
195
{
346
195
    auto a = array();
347
195
    if (at >= size()) {
348
12
        return false;
349
12
    }
350
183
    if (!a->sp) {
351
144
        a->elements.erase(a->elements.cbegin() + to_i(at));
352
144
        return true;
353
144
    }
354
39
    auto end = a->sp->elements.end();
355
39
    if (auto iter = a->sp->elements.lower_bound(at); iter != end) {
356
39
        if (iter->first == at) {
357
39
            iter++;
358
39
            a->sp->elements.erase(at);
359
39
        }
360
361
13.3k
        while (iter != end) {
362
13.3k
            auto nh = a->sp->elements.extract(iter++);
363
13.3k
            --nh.key();
364
13.3k
            a->sp->elements.insert(std::move(nh));
365
13.3k
        }
366
39
    }
367
39
    --(a->sp->size);
368
39
    return true;
369
183
}
370
371
bool
372
Array::erase(int at_i)
373
195
{
374
195
    if (at_i < 0) {
375
0
        return false;
376
0
    }
377
195
    return erase(to_s(at_i));
378
195
}
379
380
int
381
QPDFObjectHandle::getArrayNItems() const
382
73.6k
{
383
73.6k
    auto s = size();
384
73.6k
    if (s > 1 || isArray()) {
385
72.5k
        return to_i(s);
386
72.5k
    }
387
1.09k
    typeWarning("array", "treating as empty");
388
1.09k
    return 0;
389
73.6k
}
390
391
QPDFObjectHandle
392
QPDFObjectHandle::getArrayItem(int n) const
393
98.0k
{
394
98.0k
    if (auto array = Array(*this)) {
395
98.0k
        if (auto result = array[n]) {
396
79.9k
            return result;
397
79.9k
        }
398
18.0k
        if (n >= 0 && std::cmp_less(n, array.size())) {
399
            // sparse array null
400
18.0k
            return newNull();
401
18.0k
        }
402
3
        objectWarning("returning null for out of bounds array access");
403
30
    } else {
404
30
        typeWarning("array", "returning null");
405
30
    }
406
33
    static auto constexpr msg = " -> null returned from invalid array access"sv;
407
33
    return QPDF_Null::create(obj, msg, "");
408
98.0k
}
409
410
bool
411
QPDFObjectHandle::isRectangle() const
412
24.4k
{
413
24.4k
    Array array(*this);
414
29.2k
    for (auto const& oh: array) {
415
29.2k
        if (!oh.isNumber()) {
416
326
            return false;
417
326
        }
418
29.2k
    }
419
24.1k
    return array.size() == 4;
420
24.4k
}
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.1k
{
473
12.1k
    if (auto array = as_array(strict)) {
474
12.1k
        return array.getAsVector();
475
12.1k
    }
476
0
    typeWarning("array", "treating as empty");
477
0
    QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");
478
0
    return {};
479
12.1k
}
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
276
{
545
276
    if (auto array = as_array(strict)) {
546
195
        if (!array.erase(at)) {
547
12
            objectWarning("ignoring attempt to erase out of bounds array item");
548
12
            QTC::TC("qpdf", "QPDFObjectHandle erase array bounds");
549
12
        }
550
195
    } else {
551
81
        typeWarning("array", "ignoring attempt to erase item");
552
81
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item");
553
81
    }
554
276
}
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
107k
{
567
107k
    switch (resolved_type_code()) {
568
81.2k
    case ::ot_array:
569
81.2k
        return as<QPDF_Array>()->size();
570
0
    case ::ot_uninitialized:
571
0
    case ::ot_reserved:
572
24.3k
    case ::ot_null:
573
24.3k
    case ::ot_destroyed:
574
24.3k
    case ::ot_unresolved:
575
24.3k
    case ::ot_reference:
576
24.3k
        return 0;
577
6
    case ::ot_boolean:
578
67
    case ::ot_integer:
579
79
    case ::ot_real:
580
112
    case ::ot_string:
581
1.13k
    case ::ot_name:
582
2.04k
    case ::ot_dictionary:
583
2.05k
    case ::ot_stream:
584
2.05k
    case ::ot_inlineimage:
585
2.05k
    case ::ot_operator:
586
2.05k
        return 1;
587
0
    default:
588
0
        throw std::logic_error("Unexpected type code in size"); // unreachable
589
0
        return 0;                                               // unreachable
590
107k
    }
591
107k
}
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
}