Coverage Report

Created: 2026-06-15 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/JSON.cc
Line
Count
Source
1
#include <qpdf/JSON.hh>
2
3
#include <qpdf/JSON_writer.hh>
4
5
#include <qpdf/InputSource_private.hh>
6
#include <qpdf/Pl_Base64.hh>
7
#include <qpdf/Pl_Concatenate.hh>
8
#include <qpdf/Pl_String.hh>
9
#include <qpdf/QTC.hh>
10
#include <qpdf/QUtil.hh>
11
#include <qpdf/Util.hh>
12
13
#include <cstring>
14
#include <stdexcept>
15
16
using namespace qpdf;
17
18
JSON::Members::Members(std::unique_ptr<JSON_value> value) :
19
4.28M
    value(std::move(value))
20
4.28M
{
21
4.28M
}
22
23
JSON::JSON(std::unique_ptr<JSON_value> value) :
24
4.28M
    m(new Members(std::move(value)))
25
4.28M
{
26
4.28M
}
27
28
void
29
JSON::writeClose(Pipeline* p, bool first, size_t depth, char const* delimiter)
30
0
{
31
0
    if (first) {
32
0
        *p << delimiter;
33
0
    } else {
34
0
        std::string s{"\n"};
35
0
        s.append(2 * depth, ' ');
36
0
        *p << s + delimiter;
37
0
    }
38
0
}
39
40
void
41
JSON::writeNext(Pipeline* p, bool& first, size_t depth)
42
0
{
43
0
    if (first) {
44
0
        first = false;
45
0
        std::string s{"\n"};
46
0
        s.append(2 * depth, ' ');
47
0
        *p << s;
48
0
    } else {
49
0
        std::string s{",\n"};
50
0
        s.append(2 * depth, ' ');
51
0
        *p << s;
52
0
    }
53
0
}
54
55
void
56
JSON::writeDictionaryOpen(Pipeline* p, bool& first, size_t depth)
57
0
{
58
0
    *p << "{";
59
0
    first = true;
60
0
}
61
62
void
63
JSON::writeArrayOpen(Pipeline* p, bool& first, size_t depth)
64
0
{
65
0
    *p << "[";
66
0
    first = true;
67
0
}
68
69
void
70
JSON::writeDictionaryClose(Pipeline* p, bool first, size_t depth)
71
0
{
72
0
    writeClose(p, first, depth, "}");
73
0
}
74
75
void
76
JSON::writeArrayClose(Pipeline* p, bool first, size_t depth)
77
0
{
78
0
    writeClose(p, first, depth, "]");
79
0
}
80
81
void
82
JSON::writeDictionaryKey(Pipeline* p, bool& first, std::string const& key, size_t depth)
83
0
{
84
0
    writeNext(p, first, depth);
85
0
    *p << std::string("\"") + key + "\": ";
86
0
}
87
88
void
89
JSON::writeDictionaryItem(
90
    Pipeline* p, bool& first, std::string const& key, JSON const& value, size_t depth)
91
0
{
92
0
    writeDictionaryKey(p, first, key, depth);
93
0
    value.write(p, depth);
94
0
}
95
96
void
97
JSON::writeArrayItem(Pipeline* p, bool& first, JSON const& element, size_t depth)
98
0
{
99
0
    writeNext(p, first, depth);
100
0
    element.write(p, depth);
101
0
}
102
103
void
104
JSON::JSON_dictionary::write(Pipeline* p, size_t depth) const
105
0
{
106
0
    bool first = true;
107
0
    writeDictionaryOpen(p, first, depth);
108
0
    for (auto const& iter: members) {
109
0
        writeDictionaryItem(p, first, iter.first, iter.second, 1 + depth);
110
0
    }
111
0
    writeDictionaryClose(p, first, depth);
112
0
}
113
114
void
115
JSON::JSON_array::write(Pipeline* p, size_t depth) const
116
0
{
117
0
    bool first = true;
118
0
    writeArrayOpen(p, first, depth);
119
0
    for (auto const& element: elements) {
120
0
        writeArrayItem(p, first, element, 1 + depth);
121
0
    }
122
0
    writeArrayClose(p, first, depth);
123
0
}
124
125
JSON::JSON_string::JSON_string(std::string const& utf8) :
126
1.52M
    JSON_value(vt_string),
127
1.52M
    utf8(utf8)
128
1.52M
{
129
1.52M
}
130
131
void
132
JSON::JSON_string::write(Pipeline* p, size_t) const
133
0
{
134
0
    *p << std::string("\"") + Writer::encode_string(utf8) + "\"";
135
0
}
136
137
JSON::JSON_number::JSON_number(long long value) :
138
0
    JSON_value(vt_number),
139
0
    encoded(std::to_string(value))
140
0
{
141
0
}
142
143
JSON::JSON_number::JSON_number(double value) :
144
0
    JSON_value(vt_number),
145
0
    encoded(QUtil::double_to_string(value, 6))
146
0
{
147
0
}
148
149
JSON::JSON_number::JSON_number(std::string const& value) :
150
2.55M
    JSON_value(vt_number),
151
2.55M
    encoded(value)
152
2.55M
{
153
2.55M
}
154
155
void
156
JSON::JSON_number::write(Pipeline* p, size_t) const
157
0
{
158
0
    *p << encoded;
159
0
}
160
161
JSON::JSON_bool::JSON_bool(bool val) :
162
14.9k
    JSON_value(vt_bool),
163
14.9k
    value(val)
164
14.9k
{
165
14.9k
}
166
167
void
168
JSON::JSON_bool::write(Pipeline* p, size_t) const
169
0
{
170
0
    *p << (value ? "true" : "false");
171
0
}
172
173
void
174
JSON::JSON_null::write(Pipeline* p, size_t) const
175
0
{
176
0
    *p << "null";
177
0
}
178
179
JSON::JSON_blob::JSON_blob(std::function<void(Pipeline*)> fn) :
180
0
    JSON_value(vt_blob),
181
0
    fn(fn)
182
0
{
183
0
}
184
185
void
186
JSON::JSON_blob::write(Pipeline* p, size_t) const
187
0
{
188
0
    *p << "\"";
189
0
    Pl_Concatenate cat("blob concatenate", p);
190
0
    Pl_Base64 base64("blob base64", &cat, Pl_Base64::a_encode);
191
0
    fn(&base64);
192
0
    base64.finish();
193
0
    *p << "\"";
194
0
}
195
196
void
197
JSON::write(Pipeline* p, size_t depth) const
198
0
{
199
0
    if (!m) {
200
0
        *p << "null";
201
0
    } else {
202
0
        m->value->write(p, depth);
203
0
    }
204
0
}
205
206
std::string
207
JSON::unparse() const
208
0
{
209
0
    if (!m) {
210
0
        return "null";
211
0
    }
212
0
    std::string s;
213
0
    Pl_String p("unparse", nullptr, s);
214
0
    write(&p, 0);
215
0
    return s;
216
0
}
217
218
std::string
219
JSON::Writer::encode_string(std::string const& str)
220
209k
{
221
209k
    static auto constexpr hexchars = "0123456789abcdef";
222
223
209k
    auto begin = str.cbegin();
224
209k
    auto end = str.cend();
225
209k
    auto iter = begin;
226
75.1M
    while (iter != end) {
227
74.9M
        auto c = static_cast<unsigned char>(*iter);
228
74.9M
        if ((c > 34 && c != '\\') || c == ' ' || c == 33) {
229
            // Optimistically check that no char in str requires escaping. Hopefully we can just
230
            // return the input str.
231
74.9M
            ++iter;
232
74.9M
        } else {
233
            // We found a char that requires escaping. Initialize result to the chars scanned so
234
            // far, append/replace the rest of str one char at a time, and return the result.
235
6.70k
            std::string result{begin, iter};
236
237
21.8M
            for (; iter != end; ++iter) {
238
21.8M
                auto ch = static_cast<unsigned char>(*iter);
239
21.8M
                if ((ch > 34 && ch != '\\') || ch == ' ' || ch == 33) {
240
                    // Check for most common case first.
241
21.8M
                    result += *iter;
242
21.8M
                } else {
243
62.6k
                    switch (ch) {
244
25.7k
                    case '\\':
245
25.7k
                        result += "\\\\";
246
25.7k
                        break;
247
1.49k
                    case '\"':
248
1.49k
                        result += "\\\"";
249
1.49k
                        break;
250
3.26k
                    case '\b':
251
3.26k
                        result += "\\b";
252
3.26k
                        break;
253
3.04k
                    case '\f':
254
3.04k
                        result += "\\f";
255
3.04k
                        break;
256
6.13k
                    case '\n':
257
6.13k
                        result += "\\n";
258
6.13k
                        break;
259
3.66k
                    case '\r':
260
3.66k
                        result += "\\r";
261
3.66k
                        break;
262
3.62k
                    case '\t':
263
3.62k
                        result += "\\t";
264
3.62k
                        break;
265
15.6k
                    default:
266
15.6k
                        result += ch < 16 ? "\\u000" : "\\u001";
267
15.6k
                        result += hexchars[ch % 16];
268
62.6k
                    }
269
62.6k
                }
270
21.8M
            }
271
6.70k
            return result;
272
6.70k
        }
273
74.9M
    }
274
203k
    return str;
275
209k
}
276
277
JSON
278
JSON::makeDictionary()
279
133k
{
280
133k
    return {std::make_unique<JSON_dictionary>()};
281
133k
}
282
283
JSON
284
JSON::addDictionaryMember(std::string const& key, JSON const& val)
285
209k
{
286
209k
    if (auto* obj = m ? dynamic_cast<JSON_dictionary*>(m->value.get()) : nullptr) {
287
209k
        return obj->members[Writer::encode_string(key)] = val.m ? val : makeNull();
288
209k
    } else {
289
0
        throw std::runtime_error("JSON::addDictionaryMember called on non-dictionary");
290
0
    }
291
209k
}
292
293
JSON
294
JSON::makeArray()
295
47.0k
{
296
47.0k
    return {std::make_unique<JSON_array>()};
297
47.0k
}
298
299
JSON
300
JSON::addArrayElement(JSON const& val)
301
1.92M
{
302
1.92M
    if (auto* arr = m ? dynamic_cast<JSON_array*>(m->value.get()) : nullptr) {
303
1.92M
        if (val.m) {
304
1.92M
            arr->elements.push_back(val);
305
1.92M
        } else {
306
0
            arr->elements.push_back(makeNull());
307
0
        }
308
1.92M
        return arr->elements.back();
309
1.92M
    }
310
0
    throw std::runtime_error("JSON::addArrayElement called on non-array");
311
0
    return {}; // unreachable
312
1.92M
}
313
314
JSON
315
JSON::makeString(std::string const& utf8)
316
1.52M
{
317
1.52M
    return {std::make_unique<JSON_string>(utf8)};
318
1.52M
}
319
320
JSON
321
JSON::makeInt(long long int value)
322
0
{
323
0
    return {std::make_unique<JSON_number>(value)};
324
0
}
325
326
JSON
327
JSON::makeReal(double value)
328
0
{
329
0
    return {std::make_unique<JSON_number>(value)};
330
0
}
331
332
JSON
333
JSON::makeNumber(std::string const& encoded)
334
2.55M
{
335
2.55M
    return {std::make_unique<JSON_number>(encoded)};
336
2.55M
}
337
338
JSON
339
JSON::makeBool(bool value)
340
14.9k
{
341
14.9k
    return {std::make_unique<JSON_bool>(value)};
342
14.9k
}
343
344
JSON
345
JSON::makeNull()
346
10.2k
{
347
10.2k
    return {std::make_unique<JSON_null>()};
348
10.2k
}
349
350
JSON
351
JSON::makeBlob(std::function<void(Pipeline*)> fn)
352
0
{
353
0
    return {std::make_unique<JSON_blob>(fn)};
354
0
}
355
356
bool
357
JSON::isArray() const
358
6.09M
{
359
6.09M
    return m ? m->value->type_code == vt_array : false;
360
6.09M
}
361
362
bool
363
JSON::isDictionary() const
364
6.43M
{
365
6.43M
    return m && m->value->type_code == vt_dictionary;
366
6.43M
}
367
368
bool
369
JSON::getString(std::string& utf8) const
370
763k
{
371
763k
    if (m && m->value->type_code == vt_string) {
372
760k
        auto v = dynamic_cast<JSON_string const*>(m->value.get());
373
760k
        utf8 = v->utf8;
374
760k
        return true;
375
760k
    }
376
3.24k
    return false;
377
763k
}
378
379
bool
380
JSON::getNumber(std::string& value) const
381
1.86M
{
382
1.86M
    if (m && m->value->type_code == vt_number) {
383
1.11M
        auto v = dynamic_cast<JSON_number const*>(m->value.get());
384
1.11M
        value = v->encoded;
385
1.11M
        return true;
386
1.11M
    }
387
751k
    return false;
388
1.86M
}
389
390
bool
391
JSON::getBool(bool& value) const
392
1.87M
{
393
1.87M
    if (m && m->value->type_code == vt_bool) {
394
6.54k
        auto v = dynamic_cast<JSON_bool const*>(m->value.get());
395
6.54k
        value = v->value;
396
6.54k
        return true;
397
6.54k
    }
398
1.86M
    return false;
399
1.87M
}
400
401
bool
402
JSON::isNull() const
403
1.87M
{
404
1.87M
    return m && m->value->type_code == vt_null;
405
1.87M
}
406
407
JSON
408
JSON::getDictItem(std::string const& key) const
409
0
{
410
0
    if (auto v = m ? dynamic_cast<JSON_dictionary const*>(m->value.get()) : nullptr) {
411
0
        if (auto it = v->members.find(key); it != v->members.end()) {
412
0
            return it->second;
413
0
        }
414
0
    }
415
0
    return makeNull();
416
0
}
417
418
bool
419
JSON::forEachDictItem(std::function<void(std::string const& key, JSON value)> fn) const
420
0
{
421
0
    if (auto v = m ? dynamic_cast<JSON_dictionary const*>(m->value.get()) : nullptr) {
422
0
        for (auto const& [key, value]: v->members) {
423
0
            fn(key, value);
424
0
        }
425
0
        return true;
426
0
    }
427
0
    return false;
428
0
}
429
430
bool
431
JSON::forEachArrayItem(std::function<void(JSON value)> fn) const
432
0
{
433
0
    if (auto v = m ? dynamic_cast<JSON_array const*>(m->value.get()) : nullptr) {
434
0
        for (auto const& i: v->elements) {
435
0
            fn(JSON(i));
436
0
        }
437
0
        return true;
438
0
    }
439
0
    return false;
440
0
}
441
442
bool
443
JSON::checkSchema(JSON schema, std::list<std::string>& errors)
444
0
{
445
0
    if (!m || !schema.m) {
446
0
        return false;
447
0
    }
448
0
    checkSchemaInternal(m->value.get(), schema.m->value.get(), 0, errors, "");
449
0
    return errors.empty();
450
0
}
451
452
bool
453
JSON::checkSchema(JSON schema, unsigned long flags, std::list<std::string>& errors)
454
0
{
455
0
    if (!m || !schema.m) {
456
0
        return false;
457
0
    }
458
0
    checkSchemaInternal(m->value.get(), schema.m->value.get(), flags, errors, "");
459
0
    return errors.empty();
460
0
}
461
462
void
463
JSON::checkSchemaInternal(
464
    JSON_value* this_v,
465
    JSON_value* sch_v,
466
    unsigned long flags,
467
    std::list<std::string>& errors,
468
    std::string prefix)
469
0
{
470
0
    auto error = [&errors, prefix](std::string const& msg) {
471
0
        if (prefix.empty()) {
472
0
            errors.emplace_back("top-level object" + msg);
473
0
        } else {
474
0
            errors.emplace_back("json key \"" + prefix + "\"" + msg);
475
0
        }
476
0
    };
477
478
0
    if (auto* sch_dict = dynamic_cast<JSON_dictionary*>(sch_v)) {
479
0
        auto* this_dict = dynamic_cast<JSON_dictionary*>(this_v);
480
0
        if (!this_dict) {
481
0
            error(" is supposed to be a dictionary");
482
0
            return;
483
0
        }
484
0
        auto const& members = sch_dict->members;
485
0
        if (members.size() == 1) {
486
0
            auto const& pattern_key = members.begin()->first;
487
0
            if (pattern_key.starts_with('<') && pattern_key.ends_with('>')) {
488
0
                auto pattern_schema = sch_dict->members[pattern_key].m->value.get();
489
0
                for (auto const& [key, val]: this_dict->members) {
490
0
                    checkSchemaInternal(
491
0
                        val.m->value.get(), pattern_schema, flags, errors, prefix + "." + key);
492
0
                }
493
0
                return;
494
0
            }
495
0
        }
496
497
0
        for (auto& [key, val]: sch_dict->members) {
498
0
            if (this_dict->members.contains(key)) {
499
0
                checkSchemaInternal(
500
0
                    this_dict->members[key].m->value.get(),
501
0
                    val.m->value.get(),
502
0
                    flags,
503
0
                    errors,
504
0
                    prefix + "." + key);
505
0
            } else {
506
0
                if (flags & f_optional) {
507
0
                    QTC::TC("libtests", "JSON optional key");
508
0
                } else {
509
0
                    error(": key \"" + key + "\" is present in schema but missing in object");
510
0
                }
511
0
            }
512
0
        }
513
0
        for (auto const& item: this_dict->members) {
514
0
            if (!sch_dict->members.contains(item.first)) {
515
0
                error(
516
0
                    ": key \"" + item.first + "\" is not present in schema but appears in object");
517
0
            }
518
0
        }
519
0
        return;
520
0
    }
521
522
0
    if (auto* sch_arr = dynamic_cast<JSON_array*>(sch_v)) {
523
0
        auto* this_arr = dynamic_cast<JSON_array*>(this_v);
524
0
        auto n_elements = sch_arr->elements.size();
525
0
        if (n_elements == 1) {
526
            // A single-element array in the schema allows a single element in the object or a
527
            // variable-length array, each of whose items must conform to the single element of the
528
            // schema array. This doesn't apply to arrays of arrays -- we fall back to the behavior
529
            // of allowing a single item only when the object is not an array.
530
0
            if (this_arr) {
531
0
                int i = 0;
532
0
                for (auto const& element: this_arr->elements) {
533
0
                    checkSchemaInternal(
534
0
                        element.m->value.get(),
535
0
                        sch_arr->elements.at(0).m->value.get(),
536
0
                        flags,
537
0
                        errors,
538
0
                        prefix + "." + std::to_string(i));
539
0
                    ++i;
540
0
                }
541
0
            } else {
542
0
                checkSchemaInternal(
543
0
                    this_v, sch_arr->elements.at(0).m->value.get(), flags, errors, prefix);
544
0
            }
545
0
        } else if (!this_arr || this_arr->elements.size() != n_elements) {
546
0
            error(" is supposed to be an array of length " + std::to_string(n_elements));
547
0
            return;
548
0
        } else {
549
            // A multi-element array in the schema must correspond to an element of the same length
550
            // in the object. Each element in the object is validated against the corresponding
551
            // element in the schema.
552
0
            size_t i = 0;
553
0
            for (auto const& element: this_arr->elements) {
554
0
                checkSchemaInternal(
555
0
                    element.m->value.get(),
556
0
                    sch_arr->elements.at(i).m->value.get(),
557
0
                    flags,
558
0
                    errors,
559
0
                    prefix + "." + std::to_string(i));
560
0
                ++i;
561
0
            }
562
0
        }
563
0
        return;
564
0
    }
565
566
0
    if (!dynamic_cast<JSON_string*>(sch_v)) {
567
0
        error(" schema value is not dictionary, array, or string");
568
0
    }
569
0
}
570
571
namespace
572
{
573
    class JSONParser
574
    {
575
      public:
576
        JSONParser(InputSource& is, JSON::Reactor* reactor) :
577
15.3k
            is(is),
578
15.3k
            reactor(reactor),
579
15.3k
            p(buf)
580
15.3k
        {
581
15.3k
        }
582
583
        JSON parse();
584
585
      private:
586
        enum parser_state_e {
587
            ps_top,
588
            ps_dict_begin,
589
            ps_dict_after_key,
590
            ps_dict_after_colon,
591
            ps_dict_after_item,
592
            ps_dict_after_comma,
593
            ps_array_begin,
594
            ps_array_after_item,
595
            ps_array_after_comma,
596
            ps_done,
597
        };
598
599
        enum lex_state_e {
600
            ls_top,
601
            ls_number,
602
            ls_number_minus,
603
            ls_number_leading_zero,
604
            ls_number_before_point,
605
            ls_number_point,
606
            ls_number_after_point,
607
            ls_number_e,
608
            ls_number_e_sign,
609
            ls_alpha,
610
            ls_string,
611
            ls_after_string,
612
            ls_backslash,
613
            ls_u4,
614
            ls_begin_array,
615
            ls_end_array,
616
            ls_begin_dict,
617
            ls_end_dict,
618
            ls_colon,
619
            ls_comma,
620
        };
621
622
        struct StackFrame
623
        {
624
            StackFrame(parser_state_e state, JSON& item) :
625
180k
                state(state),
626
180k
                item(item)
627
180k
            {
628
180k
            }
629
630
            parser_state_e state;
631
            JSON item;
632
        };
633
634
        void getToken();
635
        void handleToken();
636
        void tokenError();
637
        static void handle_u_code(
638
            unsigned long codepoint,
639
            qpdf_offset_t offset,
640
            unsigned long& high_surrogate,
641
            qpdf_offset_t& high_offset,
642
            std::string& result);
643
        inline void append();
644
        inline void append(lex_state_e);
645
        inline void ignore();
646
        inline void ignore(lex_state_e);
647
648
        InputSource& is;
649
        JSON::Reactor* reactor;
650
        lex_state_e lex_state{ls_top};
651
        char buf[16384];
652
        size_t bytes{0};
653
        char const* p;
654
        qpdf_offset_t u_count{0};
655
        unsigned long u_value{0};
656
        qpdf_offset_t offset{0};
657
        bool done{false};
658
        std::string token;
659
        qpdf_offset_t token_start{0};
660
        parser_state_e parser_state{ps_top};
661
        std::vector<StackFrame> stack;
662
        std::string dict_key;
663
        qpdf_offset_t dict_key_offset{0};
664
    };
665
} // namespace
666
667
void
668
JSONParser::handle_u_code(
669
    unsigned long codepoint,
670
    qpdf_offset_t offset,
671
    unsigned long& high_surrogate,
672
    qpdf_offset_t& high_offset,
673
    std::string& result)
674
51.9k
{
675
51.9k
    if ((codepoint & 0xFC00) == 0xD800) {
676
        // high surrogate
677
2.83k
        qpdf_offset_t new_high_offset = offset;
678
2.83k
        if (high_offset) {
679
35
            QTC::TC("libtests", "JSON 16 high high");
680
35
            throw std::runtime_error(
681
35
                "JSON: offset " + std::to_string(new_high_offset) +
682
35
                ": UTF-16 high surrogate found after previous high surrogate at offset " +
683
35
                std::to_string(high_offset));
684
35
        }
685
2.80k
        high_offset = new_high_offset;
686
2.80k
        high_surrogate = codepoint;
687
49.1k
    } else if ((codepoint & 0xFC00) == 0xDC00) {
688
        // low surrogate
689
2.74k
        if (offset != (high_offset + 6)) {
690
19
            QTC::TC("libtests", "JSON 16 low not after high");
691
19
            throw std::runtime_error(
692
19
                "JSON: offset " + std::to_string(offset) +
693
19
                ": UTF-16 low surrogate found not immediately after high surrogate");
694
19
        }
695
2.72k
        high_offset = 0;
696
2.72k
        codepoint = 0x10000U + ((high_surrogate & 0x3FFU) << 10U) + (codepoint & 0x3FF);
697
2.72k
        result += QUtil::toUTF8(codepoint);
698
46.4k
    } else {
699
46.4k
        result += QUtil::toUTF8(codepoint);
700
46.4k
    }
701
51.9k
}
702
703
void
704
JSONParser::tokenError()
705
1.71k
{
706
1.71k
    if (done) {
707
1.03k
        QTC::TC("libtests", "JSON parse ls premature end of input");
708
1.03k
        throw std::runtime_error("JSON: premature end of input");
709
1.03k
    }
710
711
678
    if (lex_state == ls_u4) {
712
82
        QTC::TC("libtests", "JSON parse bad hex after u");
713
82
        throw std::runtime_error(
714
82
            "JSON: offset " + std::to_string(offset - u_count - 1) +
715
82
            ": \\u must be followed by four hex digits");
716
596
    } else if (lex_state == ls_alpha) {
717
234
        QTC::TC("libtests", "JSON parse keyword bad character");
718
234
        throw std::runtime_error(
719
234
            "JSON: offset " + std::to_string(offset) + ": keyword: unexpected character " +
720
234
            std::string(p, 1));
721
362
    } else if (lex_state == ls_string) {
722
27
        QTC::TC("libtests", "JSON parse control char in string");
723
27
        throw std::runtime_error(
724
27
            "JSON: offset " + std::to_string(offset) +
725
27
            ": control character in string (missing \"?)");
726
335
    } else if (lex_state == ls_backslash) {
727
37
        QTC::TC("libtests", "JSON parse backslash bad character");
728
37
        throw std::runtime_error(
729
37
            "JSON: offset " + std::to_string(offset) +
730
37
            ": invalid character after backslash: " + std::string(p, 1));
731
37
    }
732
733
298
    if (*p == '.') {
734
17
        if (lex_state == ls_number || lex_state == ls_number_e || lex_state == ls_number_e_sign) {
735
9
            QTC::TC("libtests", "JSON parse point after e");
736
9
            throw std::runtime_error(
737
9
                "JSON: offset " + std::to_string(offset) +
738
9
                ": numeric literal: decimal point after e");
739
9
        } else {
740
8
            QTC::TC("libtests", "JSON parse duplicate point");
741
8
            throw std::runtime_error(
742
8
                "JSON: offset " + std::to_string(offset) +
743
8
                ": numeric literal: decimal point already seen");
744
8
        }
745
281
    } else if (*p == 'e' || *p == 'E') {
746
18
        QTC::TC("libtests", "JSON parse duplicate e");
747
18
        throw std::runtime_error(
748
18
            "JSON: offset " + std::to_string(offset) + ": numeric literal: e already seen");
749
263
    } else if ((*p == '+') || (*p == '-')) {
750
25
        QTC::TC("libtests", "JSON parse unexpected sign");
751
25
        throw std::runtime_error(
752
25
            "JSON: offset " + std::to_string(offset) + ": numeric literal: unexpected sign");
753
238
    } else if (util::is_space(*p) || strchr("{}[]:,", *p)) {
754
53
        QTC::TC("libtests", "JSON parse incomplete number");
755
53
        throw std::runtime_error(
756
53
            "JSON: offset " + std::to_string(offset) + ": numeric literal: incomplete number");
757
758
185
    } else {
759
185
        QTC::TC("libtests", "JSON parse numeric bad character");
760
185
        throw std::runtime_error(
761
185
            "JSON: offset " + std::to_string(offset) + ": numeric literal: unexpected character " +
762
185
            std::string(p, 1));
763
185
    }
764
0
    throw std::logic_error("JSON::tokenError : unhandled error");
765
298
}
766
767
// Append current character to token and advance to next input character.
768
inline void
769
JSONParser::append()
770
311M
{
771
311M
    token += *p;
772
311M
    ++p;
773
311M
    ++offset;
774
311M
}
775
776
// Append current character to token, advance to next input character and transition to 'next' lexer
777
// state.
778
inline void
779
JSONParser::append(lex_state_e next)
780
2.61M
{
781
2.61M
    lex_state = next;
782
2.61M
    token += *p;
783
2.61M
    ++p;
784
2.61M
    ++offset;
785
2.61M
}
786
787
// Advance to next input character without appending the current character to token.
788
inline void
789
JSONParser::ignore()
790
6.51M
{
791
6.51M
    ++p;
792
6.51M
    ++offset;
793
6.51M
}
794
795
// Advance to next input character without appending the current character to token and transition
796
// to 'next' lexer state.
797
inline void
798
JSONParser::ignore(lex_state_e next)
799
8.83M
{
800
8.83M
    lex_state = next;
801
8.83M
    ++p;
802
8.83M
    ++offset;
803
8.83M
}
804
805
void
806
JSONParser::getToken()
807
9.32M
{
808
9.32M
    token.clear();
809
810
    // Keep track of UTF-16 surrogate pairs.
811
9.32M
    unsigned long high_surrogate = 0;
812
9.32M
    qpdf_offset_t high_offset = 0;
813
814
331M
    while (true) {
815
331M
        if (p == (buf + bytes)) {
816
46.8k
            p = buf;
817
46.8k
            bytes = is.read(buf, sizeof(buf));
818
46.8k
            if (bytes == 0) {
819
12.2k
                done = true;
820
12.2k
                break;
821
12.2k
            }
822
46.8k
        }
823
824
331M
        if ((*p < 32 && *p >= 0)) {
825
321k
            if (*p == '\t' || *p == '\n' || *p == '\r') {
826
                // Legal white space not permitted in strings. This will always end the current
827
                // token (unless we are still before the start of the token).
828
321k
                if (lex_state == ls_top) {
829
303k
                    ignore();
830
303k
                } else {
831
17.7k
                    break;
832
17.7k
                }
833
834
321k
            } else {
835
144
                QTC::TC("libtests", "JSON parse null character");
836
144
                throw std::runtime_error(
837
144
                    "JSON: control or null character at offset " + std::to_string(offset));
838
144
            }
839
331M
        } else if (*p == ',') {
840
8.05M
            if (lex_state == ls_top) {
841
4.11M
                ignore(ls_comma);
842
4.11M
                return;
843
4.11M
            } else if (lex_state == ls_string) {
844
1.38M
                append();
845
2.55M
            } else {
846
2.55M
                break;
847
2.55M
            }
848
323M
        } else if (*p == ':') {
849
603k
            if (lex_state == ls_top) {
850
419k
                ignore(ls_colon);
851
419k
                return;
852
419k
            } else if (lex_state == ls_string) {
853
183k
                append();
854
183k
            } else {
855
91
                break;
856
91
            }
857
322M
        } else if (*p == ' ') {
858
121M
            if (lex_state == ls_top) {
859
5.85M
                ignore();
860
115M
            } else if (lex_state == ls_string) {
861
115M
                append();
862
115M
            } else {
863
326
                break;
864
326
            }
865
201M
        } else if (*p == '{') {
866
208k
            if (lex_state == ls_top) {
867
133k
                token_start = offset;
868
133k
                ignore(ls_begin_dict);
869
133k
                return;
870
133k
            } else if (lex_state == ls_string) {
871
75.1k
                append();
872
75.1k
            } else {
873
77
                break;
874
77
            }
875
200M
        } else if (*p == '}') {
876
74.9k
            if (lex_state == ls_top) {
877
64.7k
                ignore(ls_end_dict);
878
64.7k
                return;
879
64.7k
            } else if (lex_state == ls_string) {
880
4.03k
                append();
881
6.09k
            } else {
882
6.09k
                break;
883
6.09k
            }
884
200M
        } else if (*p == '[') {
885
152k
            if (lex_state == ls_top) {
886
47.0k
                token_start = offset;
887
47.0k
                ignore(ls_begin_array);
888
47.0k
                return;
889
105k
            } else if (lex_state == ls_string) {
890
105k
                append();
891
105k
            } else {
892
27
                break;
893
27
            }
894
200M
        } else if (*p == ']') {
895
68.8k
            if (lex_state == ls_top) {
896
6.18k
                ignore(ls_end_array);
897
6.18k
                return;
898
62.6k
            } else if (lex_state == ls_string) {
899
61.9k
                append();
900
61.9k
            } else {
901
734
                break;
902
734
            }
903
200M
        } else {
904
200M
            switch (lex_state) {
905
4.52M
            case ls_top:
906
4.52M
                token_start = offset;
907
4.52M
                if (*p == '"') {
908
1.94M
                    ignore(ls_string);
909
2.58M
                } else if ((*p >= 'a') && (*p <= 'z')) {
910
25.8k
                    append(ls_alpha);
911
2.55M
                } else if (*p == '-') {
912
2.22k
                    append(ls_number_minus);
913
2.55M
                } else if ((*p >= '1') && (*p <= '9')) {
914
2.21M
                    append(ls_number_before_point);
915
2.21M
                } else if (*p == '0') {
916
342k
                    append(ls_number_leading_zero);
917
342k
                } else {
918
422
                    QTC::TC("libtests", "JSON parse bad character");
919
422
                    throw std::runtime_error(
920
422
                        "JSON: offset " + std::to_string(offset) + ": unexpected character " +
921
422
                        std::string(p, 1));
922
422
                }
923
4.52M
                break;
924
925
4.52M
            case ls_number_minus:
926
2.18k
                if ((*p >= '1') && (*p <= '9')) {
927
1.36k
                    append(ls_number_before_point);
928
1.36k
                } else if (*p == '0') {
929
786
                    append(ls_number_leading_zero);
930
786
                } else {
931
27
                    QTC::TC("libtests", "JSON parse number minus no digits");
932
27
                    throw std::runtime_error(
933
27
                        "JSON: offset " + std::to_string(offset) +
934
27
                        ": numeric literal: no digit after minus sign");
935
27
                }
936
2.15k
                break;
937
938
2.74k
            case ls_number_leading_zero:
939
2.74k
                if (*p == '.') {
940
514
                    append(ls_number_point);
941
2.23k
                } else if (*p == 'e' || *p == 'E') {
942
2.16k
                    append(ls_number_e);
943
2.16k
                } else {
944
67
                    QTC::TC("libtests", "JSON parse leading zero");
945
67
                    throw std::runtime_error(
946
67
                        "JSON: offset " + std::to_string(offset) + ": number with leading zero");
947
67
                }
948
2.68k
                break;
949
950
387k
            case ls_number_before_point:
951
387k
                if ((*p >= '0') && (*p <= '9')) {
952
372k
                    append();
953
372k
                } else if (*p == '.') {
954
1.47k
                    append(ls_number_point);
955
12.7k
                } else if (*p == 'e' || *p == 'E') {
956
12.7k
                    append(ls_number_e);
957
12.7k
                } else {
958
89
                    tokenError();
959
89
                }
960
387k
                break;
961
962
1.95k
            case ls_number_point:
963
1.95k
                if ((*p >= '0') && (*p <= '9')) {
964
1.93k
                    append(ls_number_after_point);
965
1.93k
                } else {
966
28
                    tokenError();
967
28
                }
968
1.95k
                break;
969
970
2.52M
            case ls_number_after_point:
971
2.52M
                if ((*p >= '0') && (*p <= '9')) {
972
2.52M
                    append();
973
2.52M
                } else if (*p == 'e' || *p == 'E') {
974
650
                    append(ls_number_e);
975
650
                } else {
976
29
                    tokenError();
977
29
                }
978
2.52M
                break;
979
980
15.4k
            case ls_number_e:
981
15.4k
                if ((*p >= '0') && (*p <= '9')) {
982
13.0k
                    append(ls_number);
983
13.0k
                } else if ((*p == '+') || (*p == '-')) {
984
2.36k
                    append(ls_number_e_sign);
985
2.36k
                } else {
986
37
                    tokenError();
987
37
                }
988
15.4k
                break;
989
990
2.34k
            case ls_number_e_sign:
991
2.34k
                if ((*p >= '0') && (*p <= '9')) {
992
2.32k
                    append(ls_number);
993
2.32k
                } else {
994
25
                    tokenError();
995
25
                }
996
2.34k
                break;
997
998
4.23M
            case ls_number:
999
                // We only get here after we have seen an exponent.
1000
4.23M
                if ((*p >= '0') && (*p <= '9')) {
1001
4.23M
                    append();
1002
4.23M
                } else {
1003
37
                    tokenError();
1004
37
                }
1005
4.23M
                break;
1006
1007
1.20M
            case ls_alpha:
1008
1.20M
                if ((*p >= 'a') && (*p <= 'z')) {
1009
1.20M
                    append();
1010
1.20M
                } else {
1011
234
                    tokenError();
1012
234
                }
1013
1.20M
                break;
1014
1015
187M
            case ls_string:
1016
187M
                if (*p == '"') {
1017
1.94M
                    if (high_offset) {
1018
33
                        QTC::TC("libtests", "JSON 16 dangling high");
1019
33
                        throw std::runtime_error(
1020
33
                            "JSON: offset " + std::to_string(high_offset) +
1021
33
                            ": UTF-16 high surrogate not followed by low surrogate");
1022
33
                    }
1023
1.94M
                    ignore(ls_after_string);
1024
1.94M
                    return;
1025
185M
                } else if (*p == '\\') {
1026
147k
                    ignore(ls_backslash);
1027
185M
                } else {
1028
185M
                    append();
1029
185M
                }
1030
185M
                break;
1031
1032
185M
            case ls_backslash:
1033
147k
                lex_state = ls_string;
1034
147k
                switch (*p) {
1035
51.8k
                case '\\':
1036
55.9k
                case '\"':
1037
56.3k
                case '/':
1038
                    // \/ is allowed in json input, but so is /, so we don't map / to \/ in output.
1039
56.3k
                    token += *p;
1040
56.3k
                    break;
1041
6.69k
                case 'b':
1042
6.69k
                    token += '\b';
1043
6.69k
                    break;
1044
6.61k
                case 'f':
1045
6.61k
                    token += '\f';
1046
6.61k
                    break;
1047
13.8k
                case 'n':
1048
13.8k
                    token += '\n';
1049
13.8k
                    break;
1050
7.57k
                case 'r':
1051
7.57k
                    token += '\r';
1052
7.57k
                    break;
1053
3.98k
                case 't':
1054
3.98k
                    token += '\t';
1055
3.98k
                    break;
1056
52.1k
                case 'u':
1057
52.1k
                    lex_state = ls_u4;
1058
52.1k
                    u_count = 0;
1059
52.1k
                    u_value = 0;
1060
52.1k
                    break;
1061
25
                default:
1062
25
                    lex_state = ls_backslash;
1063
25
                    tokenError();
1064
147k
                }
1065
147k
                ignore();
1066
147k
                break;
1067
1068
208k
            case ls_u4:
1069
208k
                using ui = unsigned int;
1070
208k
                if (ui val = ui(util::hex_decode_char(*p)); val < 16) {
1071
208k
                    u_value = 16 * u_value + val;
1072
208k
                } else {
1073
74
                    tokenError();
1074
74
                }
1075
208k
                if (++u_count == 4) {
1076
51.9k
                    handle_u_code(u_value, offset - 5, high_surrogate, high_offset, token);
1077
51.9k
                    lex_state = ls_string;
1078
51.9k
                }
1079
208k
                ignore();
1080
208k
                break;
1081
1082
0
            default:
1083
0
                throw std::logic_error("JSONParser::getToken : trying to handle delimiter state");
1084
200M
            }
1085
200M
        }
1086
331M
    }
1087
1088
    // We only get here if on end of input or if the last character was a control character or other
1089
    // delimiter.
1090
1091
2.58M
    if (!token.empty()) {
1092
2.58M
        switch (lex_state) {
1093
0
        case ls_top:
1094
            // Can't happen
1095
0
            throw std::logic_error("tok_start set in ls_top while parsing");
1096
0
            break;
1097
1098
340k
        case ls_number_leading_zero:
1099
2.53M
        case ls_number_before_point:
1100
2.53M
        case ls_number_after_point:
1101
2.53M
            lex_state = ls_number;
1102
2.53M
            break;
1103
1104
15.3k
        case ls_number:
1105
40.9k
        case ls_alpha:
1106
            // terminal state
1107
40.9k
            break;
1108
1109
1.13k
        default:
1110
1.13k
            tokenError();
1111
2.58M
        }
1112
2.58M
    }
1113
2.58M
}
1114
1115
void
1116
JSONParser::handleToken()
1117
9.32M
{
1118
9.32M
    if (lex_state == ls_top) {
1119
7.45k
        return;
1120
7.45k
    }
1121
1122
9.31M
    if (parser_state == ps_done) {
1123
66
        QTC::TC("libtests", "JSON parse junk after object");
1124
66
        throw std::runtime_error(
1125
66
            "JSON: offset " + std::to_string(offset) +
1126
66
            ": material follows end of object: " + token);
1127
66
    }
1128
1129
9.31M
    const static JSON null_item = JSON::makeNull();
1130
9.31M
    JSON item;
1131
9.31M
    auto tos = stack.empty() ? null_item : stack.back().item;
1132
9.31M
    auto ls = lex_state;
1133
9.31M
    lex_state = ls_top;
1134
1135
9.31M
    switch (ls) {
1136
133k
    case ls_begin_dict:
1137
133k
        item = JSON::makeDictionary();
1138
133k
        break;
1139
1140
47.0k
    case ls_begin_array:
1141
47.0k
        item = JSON::makeArray();
1142
47.0k
        break;
1143
1144
419k
    case ls_colon:
1145
419k
        if (parser_state != ps_dict_after_key) {
1146
251
            QTC::TC("libtests", "JSON parse unexpected :");
1147
251
            throw std::runtime_error(
1148
251
                "JSON: offset " + std::to_string(offset) + ": unexpected colon");
1149
251
        }
1150
419k
        parser_state = ps_dict_after_colon;
1151
419k
        return;
1152
1153
4.11M
    case ls_comma:
1154
4.11M
        if (!((parser_state == ps_dict_after_item) || (parser_state == ps_array_after_item))) {
1155
105
            QTC::TC("libtests", "JSON parse unexpected ,");
1156
105
            throw std::runtime_error(
1157
105
                "JSON: offset " + std::to_string(offset) + ": unexpected comma");
1158
105
        }
1159
4.11M
        if (parser_state == ps_dict_after_item) {
1160
307k
            parser_state = ps_dict_after_comma;
1161
3.81M
        } else if (parser_state == ps_array_after_item) {
1162
3.81M
            parser_state = ps_array_after_comma;
1163
3.81M
        } else {
1164
0
            throw std::logic_error("JSONParser::handleToken: unexpected parser state for comma");
1165
0
        }
1166
4.11M
        return;
1167
1168
4.11M
    case ls_end_array:
1169
6.18k
        if (!(parser_state == ps_array_begin || parser_state == ps_array_after_item)) {
1170
28
            QTC::TC("libtests", "JSON parse unexpected ]");
1171
28
            throw std::runtime_error(
1172
28
                "JSON: offset " + std::to_string(offset) + ": unexpected array end delimiter");
1173
28
        }
1174
6.15k
        parser_state = stack.back().state;
1175
6.15k
        tos.setEnd(offset);
1176
6.15k
        if (reactor) {
1177
2.96k
            reactor->containerEnd(tos);
1178
2.96k
        }
1179
6.15k
        if (parser_state != ps_done) {
1180
6.14k
            stack.pop_back();
1181
6.14k
        }
1182
6.15k
        return;
1183
1184
64.7k
    case ls_end_dict:
1185
64.7k
        if (!((parser_state == ps_dict_begin) || (parser_state == ps_dict_after_item))) {
1186
16
            QTC::TC("libtests", "JSON parse unexpected }");
1187
16
            throw std::runtime_error(
1188
16
                "JSON: offset " + std::to_string(offset) + ": unexpected dictionary end delimiter");
1189
16
        }
1190
64.7k
        parser_state = stack.back().state;
1191
64.7k
        tos.setEnd(offset);
1192
64.7k
        if (reactor) {
1193
32.3k
            reactor->containerEnd(tos);
1194
32.3k
        }
1195
64.7k
        if (parser_state != ps_done) {
1196
64.6k
            stack.pop_back();
1197
64.6k
        }
1198
64.7k
        return;
1199
1200
2.55M
    case ls_number:
1201
2.55M
        item = JSON::makeNumber(token);
1202
2.55M
        break;
1203
1204
25.6k
    case ls_alpha:
1205
25.6k
        if (token == "true") {
1206
12.0k
            item = JSON::makeBool(true);
1207
13.5k
        } else if (token == "false") {
1208
2.96k
            item = JSON::makeBool(false);
1209
10.6k
        } else if (token == "null") {
1210
10.2k
            item = JSON::makeNull();
1211
10.2k
        } else {
1212
411
            QTC::TC("libtests", "JSON parse invalid keyword");
1213
411
            throw std::runtime_error(
1214
411
                "JSON: offset " + std::to_string(offset) + ": invalid keyword " + token);
1215
411
        }
1216
25.2k
        break;
1217
1218
1.94M
    case ls_after_string:
1219
1.94M
        if (parser_state == ps_dict_begin || parser_state == ps_dict_after_comma) {
1220
420k
            dict_key = token;
1221
420k
            dict_key_offset = token_start;
1222
420k
            parser_state = ps_dict_after_key;
1223
420k
            return;
1224
1.52M
        } else {
1225
1.52M
            item = JSON::makeString(token);
1226
1.52M
        }
1227
1.52M
        break;
1228
1229
1.52M
    default:
1230
280
        throw std::runtime_error(
1231
280
            "JSON: offset " + std::to_string(offset) + ": premature end of input");
1232
0
        break;
1233
9.31M
    }
1234
1235
4.28M
    item.setStart(token_start);
1236
4.28M
    item.setEnd(offset);
1237
1238
4.28M
    switch (parser_state) {
1239
26
    case ps_dict_begin:
1240
68
    case ps_dict_after_comma:
1241
68
        QTC::TC("libtests", "JSON parse string as dict key");
1242
68
        throw std::runtime_error(
1243
68
            "JSON: offset " + std::to_string(offset) + ": expect string as dictionary key");
1244
0
        break;
1245
1246
419k
    case ps_dict_after_colon:
1247
419k
        if (!reactor || !reactor->dictionaryItem(dict_key, item)) {
1248
209k
            tos.addDictionaryMember(dict_key, item);
1249
209k
        }
1250
419k
        parser_state = ps_dict_after_item;
1251
419k
        break;
1252
1253
42.4k
    case ps_array_begin:
1254
3.85M
    case ps_array_after_comma:
1255
3.85M
        if (!reactor || !reactor->arrayItem(item)) {
1256
1.92M
            tos.addArrayElement(item);
1257
1.92M
        }
1258
3.85M
        parser_state = ps_array_after_item;
1259
3.85M
        break;
1260
1261
14.3k
    case ps_top:
1262
14.3k
        if (!(item.isDictionary() || item.isArray())) {
1263
224
            stack.emplace_back(ps_done, item);
1264
224
            parser_state = ps_done;
1265
224
            return;
1266
224
        }
1267
14.0k
        parser_state = ps_done;
1268
14.0k
        break;
1269
1270
60
    case ps_dict_after_key:
1271
60
        QTC::TC("libtests", "JSON parse expected colon");
1272
60
        throw std::runtime_error("JSON: offset " + std::to_string(offset) + ": expected ':'");
1273
0
        break;
1274
1275
122
    case ps_dict_after_item:
1276
122
        QTC::TC("libtests", "JSON parse expected , or }");
1277
122
        throw std::runtime_error(
1278
122
            "JSON: offset " + std::to_string(offset) + ": expected ',' or '}'");
1279
0
        break;
1280
1281
105
    case ps_array_after_item:
1282
105
        QTC::TC("libtests", "JSON parse expected, or ]");
1283
105
        throw std::runtime_error(
1284
105
            "JSON: offset " + std::to_string(offset) + ": expected ',' or ']'");
1285
0
        break;
1286
1287
0
    case ps_done:
1288
0
        throw std::logic_error("JSONParser::handleToken: unexpected parser state");
1289
4.28M
    }
1290
1291
4.28M
    if (item.isDictionary() || item.isArray()) {
1292
180k
        stack.emplace_back(parser_state, item);
1293
        // Calling container start method is postponed until after adding the containers to their
1294
        // parent containers, if any. This makes it much easier to keep track of the current nesting
1295
        // level.
1296
180k
        if (item.isDictionary()) {
1297
133k
            if (reactor) {
1298
66.5k
                reactor->dictionaryStart();
1299
66.5k
            }
1300
133k
            parser_state = ps_dict_begin;
1301
133k
        } else if (item.isArray()) {
1302
47.0k
            if (reactor) {
1303
22.2k
                reactor->arrayStart();
1304
22.2k
            }
1305
47.0k
            parser_state = ps_array_begin;
1306
47.0k
        }
1307
1308
180k
        if (stack.size() > 500) {
1309
3
            throw std::runtime_error(
1310
3
                "JSON: offset " + std::to_string(offset) + ": maximum object depth exceeded");
1311
3
        }
1312
180k
    }
1313
4.28M
}
1314
1315
JSON
1316
JSONParser::parse()
1317
15.3k
{
1318
9.34M
    while (!done) {
1319
9.32M
        getToken();
1320
9.32M
        handleToken();
1321
9.32M
    }
1322
15.3k
    if (parser_state != ps_done) {
1323
10.2k
        QTC::TC("libtests", "JSON parse premature EOF");
1324
10.2k
        throw std::runtime_error("JSON: premature end of input");
1325
10.2k
    }
1326
5.07k
    auto const& tos = stack.back().item;
1327
5.07k
    if (reactor && !(tos.isArray() || tos.isDictionary())) {
1328
64
        reactor->topLevelScalar();
1329
64
    }
1330
5.07k
    return tos;
1331
15.3k
}
1332
1333
JSON
1334
JSON::parse(InputSource& is, Reactor* reactor)
1335
7.67k
{
1336
7.67k
    JSONParser jp(is, reactor);
1337
7.67k
    return jp.parse();
1338
7.67k
}
1339
1340
JSON
1341
JSON::parse(std::string const& s)
1342
7.67k
{
1343
7.67k
    is::OffsetBuffer bis("json input", s);
1344
7.67k
    JSONParser jp(bis, nullptr);
1345
7.67k
    return jp.parse();
1346
7.67k
}
1347
1348
void
1349
JSON::setStart(qpdf_offset_t start)
1350
4.28M
{
1351
4.28M
    if (m) {
1352
4.28M
        m->start = start;
1353
4.28M
    }
1354
4.28M
}
1355
1356
void
1357
JSON::setEnd(qpdf_offset_t end)
1358
4.35M
{
1359
4.35M
    if (m) {
1360
4.35M
        m->end = end;
1361
4.35M
    }
1362
4.35M
}
1363
1364
qpdf_offset_t
1365
JSON::getStart() const
1366
1.28M
{
1367
1.28M
    return m ? m->start : 0;
1368
1.28M
}
1369
1370
qpdf_offset_t
1371
JSON::getEnd() const
1372
6.09k
{
1373
6.09k
    return m ? m->end : 0;
1374
6.09k
}