Coverage Report

Created: 2025-07-12 06:26

/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
125k
{
13
125k
    if (!item) {
14
0
        throw std::logic_error("Attempting to add an uninitialized object to a QPDF_Array.");
15
0
    }
16
125k
    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
125k
}
22
23
QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle>&& v, bool sparse)
24
14.5k
{
25
14.5k
    if (sparse) {
26
390
        sp = std::make_unique<Sparse>();
27
1.59M
        for (auto& item: v) {
28
1.59M
            if (item.raw_type_code() != ::ot_null || item.indirect()) {
29
1.53M
                sp->elements[sp->size] = std::move(item);
30
1.53M
            }
31
1.59M
            ++sp->size;
32
1.59M
        }
33
14.1k
    } else {
34
14.1k
        elements = std::move(v);
35
14.1k
    }
36
14.5k
}
37
38
QPDF_Array*
39
Array::array() const
40
8.19M
{
41
8.19M
    if (auto a = as<QPDF_Array>()) {
42
8.19M
        return a;
43
8.19M
    }
44
45
0
    throw std::runtime_error("Expected an array but found a non-array object");
46
0
    return nullptr; // unreachable
47
8.19M
}
48
49
Array::iterator
50
Array::begin()
51
52.4k
{
52
52.4k
    if (auto a = as<QPDF_Array>()) {
53
28.3k
        if (!a->sp) {
54
28.0k
            return a->elements.begin();
55
28.0k
        }
56
314
        if (!sp_elements) {
57
314
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
58
314
        }
59
314
        return sp_elements->begin();
60
28.3k
    }
61
24.0k
    return {};
62
52.4k
}
63
64
Array::iterator
65
Array::end()
66
52.4k
{
67
52.4k
    if (auto a = as<QPDF_Array>()) {
68
28.3k
        if (!a->sp) {
69
28.0k
            return a->elements.end();
70
28.0k
        }
71
314
        if (!sp_elements) {
72
0
            sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector());
73
0
        }
74
314
        return sp_elements->end();
75
28.3k
    }
76
24.0k
    return {};
77
52.4k
}
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
0
{
112
0
    if (auto a = as<QPDF_Array>()) {
113
0
        if (!a->sp) {
114
0
            return a->elements.crbegin();
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->crbegin();
120
0
    }
121
0
    return {};
122
0
}
123
124
Array::const_reverse_iterator
125
Array::crend()
126
0
{
127
0
    if (auto a = as<QPDF_Array>()) {
128
0
        if (!a->sp) {
129
0
            return a->elements.crend();
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->crend();
135
0
    }
136
0
    return {};
137
0
}
138
139
QPDFObjectHandle
140
Array::null() const
141
62.5k
{
142
62.5k
    return null_oh;
143
62.5k
}
144
145
int
146
Array::size() const
147
4.91M
{
148
4.91M
    auto a = array();
149
4.91M
    return a->sp ? a->sp->size : int(a->elements.size());
150
4.91M
}
151
152
std::pair<bool, QPDFObjectHandle>
153
Array::at(int n) const
154
3.09M
{
155
3.09M
    auto a = array();
156
3.09M
    if (n < 0 || n >= size()) {
157
1.40k
        return {false, {}};
158
1.40k
    }
159
3.09M
    if (!a->sp) {
160
3.00M
        return {true, a->elements[size_t(n)]};
161
3.00M
    }
162
93.0k
    auto const& iter = a->sp->elements.find(n);
163
93.0k
    return {true, iter == a->sp->elements.end() ? null() : iter->second};
164
3.09M
}
165
166
std::vector<QPDFObjectHandle>
167
Array::getAsVector() const
168
7.16k
{
169
7.16k
    auto a = array();
170
7.16k
    if (a->sp) {
171
329
        std::vector<QPDFObjectHandle> v;
172
329
        v.reserve(size_t(size()));
173
14.2k
        for (auto const& item: a->sp->elements) {
174
14.2k
            v.resize(size_t(item.first), null_oh);
175
14.2k
            v.emplace_back(item.second);
176
14.2k
        }
177
329
        v.resize(size_t(size()), null_oh);
178
329
        return v;
179
6.83k
    } else {
180
6.83k
        return a->elements;
181
6.83k
    }
182
7.16k
}
183
184
bool
185
Array::setAt(int at, QPDFObjectHandle const& oh)
186
7.29k
{
187
7.29k
    if (at < 0 || at >= size()) {
188
0
        return false;
189
0
    }
190
7.29k
    auto a = array();
191
7.29k
    checkOwnership(oh);
192
7.29k
    if (a->sp) {
193
6
        a->sp->elements[at] = oh;
194
7.28k
    } else {
195
7.28k
        a->elements[size_t(at)] = oh;
196
7.28k
    }
197
7.29k
    return true;
198
7.29k
}
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
52.5k
{
215
52.5k
    auto a = array();
216
52.5k
    int sz = size();
217
52.5k
    if (at < 0 || at > sz) {
218
        // As special case, also allow insert beyond the end
219
0
        return false;
220
52.5k
    } else if (at == sz) {
221
16.2k
        push_back(item);
222
36.3k
    } else {
223
36.3k
        checkOwnership(item);
224
36.3k
        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
36.3k
        } else {
239
36.3k
            a->elements.insert(a->elements.cbegin() + at, item.getObj());
240
36.3k
        }
241
36.3k
    }
242
52.5k
    return true;
243
52.5k
}
244
245
void
246
Array::push_back(QPDFObjectHandle const& item)
247
81.4k
{
248
81.4k
    auto a = array();
249
81.4k
    checkOwnership(item);
250
81.4k
    if (a->sp) {
251
375
        a->sp->elements[(a->sp->size)++] = item;
252
81.0k
    } else {
253
81.0k
        a->elements.emplace_back(item);
254
81.0k
    }
255
81.4k
}
256
257
bool
258
Array::erase(int at)
259
25.6k
{
260
25.6k
    auto a = array();
261
25.6k
    if (at < 0 || at >= size()) {
262
0
        return false;
263
0
    }
264
25.6k
    if (a->sp) {
265
0
        auto end = a->sp->elements.end();
266
0
        if (auto iter = a->sp->elements.lower_bound(at); iter != end) {
267
0
            if (iter->first == at) {
268
0
                iter++;
269
0
                a->sp->elements.erase(at);
270
0
            }
271
272
0
            while (iter != end) {
273
0
                auto nh = a->sp->elements.extract(iter++);
274
0
                --nh.key();
275
0
                a->sp->elements.insert(std::move(nh));
276
0
            }
277
0
        }
278
0
        --(a->sp->size);
279
25.6k
    } else {
280
25.6k
        a->elements.erase(a->elements.cbegin() + at);
281
25.6k
    }
282
25.6k
    return true;
283
25.6k
}
284
285
int
286
QPDFObjectHandle::getArrayNItems() const
287
1.54M
{
288
1.54M
    if (auto array = as_array(strict)) {
289
1.54M
        return array.size();
290
1.54M
    }
291
105
    typeWarning("array", "treating as empty");
292
105
    QTC::TC("qpdf", "QPDFObjectHandle array treating as empty");
293
105
    return 0;
294
1.54M
}
295
296
QPDFObjectHandle
297
QPDFObjectHandle::getArrayItem(int n) const
298
2.36M
{
299
2.36M
    if (auto array = as_array(strict)) {
300
2.36M
        if (auto const [success, oh] = array.at(n); success) {
301
2.36M
            return oh;
302
2.36M
        } else {
303
0
            objectWarning("returning null for out of bounds array access");
304
0
            QTC::TC("qpdf", "QPDFObjectHandle array bounds");
305
0
        }
306
2.36M
    } else {
307
0
        typeWarning("array", "returning null");
308
0
        QTC::TC("qpdf", "QPDFObjectHandle array null for non-array");
309
0
    }
310
0
    static auto constexpr msg = " -> null returned from invalid array access"sv;
311
0
    return QPDF_Null::create(obj, msg, "");
312
2.36M
}
313
314
bool
315
QPDFObjectHandle::isRectangle() const
316
156k
{
317
156k
    if (auto array = as_array(strict)) {
318
561k
        for (int i = 0; i < 4; ++i) {
319
451k
            if (auto item = array.at(i).second; !item.isNumber()) {
320
5.10k
                return false;
321
5.10k
            }
322
451k
        }
323
109k
        return array.size() == 4;
324
115k
    }
325
41.6k
    return false;
326
156k
}
327
328
bool
329
QPDFObjectHandle::isMatrix() const
330
11.6k
{
331
11.6k
    if (auto array = as_array(strict)) {
332
4.77k
        for (int i = 0; i < 6; ++i) {
333
4.38k
            if (auto item = array.at(i).second; !item.isNumber()) {
334
451
                return false;
335
451
            }
336
4.38k
        }
337
392
        return array.size() == 6;
338
843
    }
339
10.8k
    return false;
340
11.6k
}
341
342
QPDFObjectHandle::Rectangle
343
QPDFObjectHandle::getArrayAsRectangle() const
344
68.9k
{
345
68.9k
    if (auto array = as_array(strict)) {
346
67.6k
        if (array.size() != 4) {
347
474
            return {};
348
474
        }
349
67.1k
        double items[4];
350
334k
        for (int i = 0; i < 4; ++i) {
351
267k
            if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {
352
366
                return {};
353
366
            }
354
267k
        }
355
66.7k
        return {
356
66.7k
            std::min(items[0], items[2]),
357
66.7k
            std::min(items[1], items[3]),
358
66.7k
            std::max(items[0], items[2]),
359
66.7k
            std::max(items[1], items[3])};
360
67.1k
    }
361
1.30k
    return {};
362
68.9k
}
363
364
QPDFObjectHandle::Matrix
365
QPDFObjectHandle::getArrayAsMatrix() const
366
392
{
367
392
    if (auto array = as_array(strict)) {
368
392
        if (array.size() != 6) {
369
0
            return {};
370
0
        }
371
392
        double items[6];
372
2.74k
        for (int i = 0; i < 6; ++i) {
373
2.35k
            if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {
374
0
                return {};
375
0
            }
376
2.35k
        }
377
392
        return {items[0], items[1], items[2], items[3], items[4], items[5]};
378
392
    }
379
0
    return {};
380
392
}
381
382
std::vector<QPDFObjectHandle>
383
QPDFObjectHandle::getArrayAsVector() const
384
6.85k
{
385
6.85k
    if (auto array = as_array(strict)) {
386
6.85k
        return array.getAsVector();
387
6.85k
    }
388
0
    typeWarning("array", "treating as empty");
389
0
    QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");
390
0
    return {};
391
6.85k
}
392
393
void
394
QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item)
395
7.29k
{
396
7.29k
    if (auto array = as_array(strict)) {
397
7.29k
        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
7.29k
    } else {
402
0
        typeWarning("array", "ignoring attempt to set item");
403
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item");
404
0
    }
405
7.29k
}
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
52.5k
{
420
52.5k
    if (auto array = as_array(strict)) {
421
52.5k
        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
52.5k
    } else {
426
0
        typeWarning("array", "ignoring attempt to insert item");
427
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item");
428
0
    }
429
52.5k
}
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
65.1k
{
441
65.1k
    if (auto array = as_array(strict)) {
442
65.1k
        array.push_back(item);
443
65.1k
    } else {
444
0
        typeWarning("array", "ignoring attempt to append item");
445
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring append item");
446
0
    }
447
65.1k
}
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
25.6k
{
459
25.6k
    if (auto array = as_array(strict)) {
460
25.6k
        if (!array.erase(at)) {
461
0
            objectWarning("ignoring attempt to erase out of bounds array item");
462
0
            QTC::TC("qpdf", "QPDFObjectHandle erase array bounds");
463
0
        }
464
25.6k
    } else {
465
0
        typeWarning("array", "ignoring attempt to erase item");
466
0
        QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item");
467
0
    }
468
25.6k
}
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
}