Coverage Report

Created: 2026-01-09 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/QPDFTokenizer.cc
Line
Count
Source
1
#include <qpdf/QPDFTokenizer_private.hh>
2
3
// DO NOT USE ctype -- it is locale dependent for some things, and it's not worth the risk of
4
// including it in case it may accidentally be used.
5
6
#include <qpdf/InputSource_private.hh>
7
#include <qpdf/QIntC.hh>
8
#include <qpdf/QPDFExc.hh>
9
#include <qpdf/QPDFObjectHandle.hh>
10
#include <qpdf/QTC.hh>
11
#include <qpdf/QUtil.hh>
12
#include <qpdf/Util.hh>
13
14
#include <cstdlib>
15
#include <cstring>
16
#include <stdexcept>
17
18
using namespace qpdf;
19
20
using Token = QPDFTokenizer::Token;
21
using tt = QPDFTokenizer::token_type_e;
22
23
static inline bool
24
is_delimiter(char ch)
25
268M
{
26
268M
    return (
27
268M
        ch == ' ' || ch == '\n' || ch == '/' || ch == '(' || ch == ')' || ch == '{' || ch == '}' ||
28
247M
        ch == '<' || ch == '>' || ch == '[' || ch == ']' || ch == '%' || ch == '\t' || ch == '\r' ||
29
244M
        ch == '\v' || ch == '\f' || ch == 0);
30
268M
}
31
32
namespace
33
{
34
    class QPDFWordTokenFinder: public InputSource::Finder
35
    {
36
      public:
37
        QPDFWordTokenFinder(InputSource& is, std::string const& str) :
38
14.3k
            is(is),
39
14.3k
            str(str)
40
14.3k
        {
41
14.3k
        }
42
14.3k
        ~QPDFWordTokenFinder() override = default;
43
        bool check() override;
44
45
      private:
46
        InputSource& is;
47
        std::string str;
48
    };
49
} // namespace
50
51
bool
52
QPDFWordTokenFinder::check()
53
15.6k
{
54
    // Find a word token matching the given string, preceded by a delimiter, and followed by a
55
    // delimiter or EOF.
56
15.6k
    Tokenizer tokenizer;
57
15.6k
    tokenizer.nextToken(is, "finder", str.size() + 2);
58
15.6k
    qpdf_offset_t pos = is.tell();
59
15.6k
    if (tokenizer.getType() != tt::tt_word || tokenizer.getValue() != str) {
60
3.93k
        QTC::TC("qpdf", "QPDFTokenizer finder found wrong word");
61
3.93k
        return false;
62
3.93k
    }
63
11.7k
    qpdf_offset_t token_start = is.getLastOffset();
64
11.7k
    char next;
65
11.7k
    bool next_okay = false;
66
11.7k
    if (is.read(&next, 1) == 0) {
67
1
        QTC::TC("qpdf", "QPDFTokenizer inline image at EOF");
68
1
        next_okay = true;
69
11.7k
    } else {
70
11.7k
        next_okay = is_delimiter(next);
71
11.7k
    }
72
11.7k
    is.seek(pos, SEEK_SET);
73
11.7k
    if (!next_okay) {
74
0
        return false;
75
0
    }
76
11.7k
    if (token_start == 0) {
77
        // Can't actually happen...we never start the search at the beginning of the input.
78
0
        return false;
79
0
    }
80
11.7k
    return true;
81
11.7k
}
82
83
void
84
Tokenizer::reset()
85
137M
{
86
137M
    state = st_before_token;
87
137M
    type = tt::tt_bad;
88
137M
    val.clear();
89
137M
    raw_val.clear();
90
137M
    error_message = "";
91
137M
    before_token = true;
92
137M
    in_token = false;
93
137M
    char_to_unread = '\0';
94
137M
    inline_image_bytes = 0;
95
137M
    string_depth = 0;
96
137M
    bad = false;
97
137M
}
98
99
QPDFTokenizer::Token::Token(token_type_e type, std::string const& value) :
100
3.50k
    type(type),
101
3.50k
    value(value),
102
3.50k
    raw_value(value)
103
3.50k
{
104
3.50k
    if (type == tt_string) {
105
0
        raw_value = QPDFObjectHandle::newString(value).unparse();
106
3.50k
    } else if (type == tt_name) {
107
0
        raw_value = QPDFObjectHandle::newName(value).unparse();
108
0
    }
109
3.50k
}
110
111
QPDFTokenizer::QPDFTokenizer() :
112
2.70k
    m(std::make_unique<qpdf::Tokenizer>())
113
2.70k
{
114
2.70k
}
115
116
2.70k
QPDFTokenizer::~QPDFTokenizer() = default;
117
118
Tokenizer::Tokenizer()
119
56.5k
{
120
56.5k
    reset();
121
56.5k
}
122
123
void
124
QPDFTokenizer::allowEOF()
125
2.70k
{
126
2.70k
    m->allowEOF();
127
2.70k
}
128
129
void
130
Tokenizer::allowEOF()
131
29.0k
{
132
29.0k
    allow_eof = true;
133
29.0k
}
134
135
void
136
QPDFTokenizer::includeIgnorable()
137
2.70k
{
138
2.70k
    m->includeIgnorable();
139
2.70k
}
140
141
void
142
Tokenizer::includeIgnorable()
143
2.70k
{
144
2.70k
    include_ignorable = true;
145
2.70k
}
146
147
bool
148
Tokenizer::isSpace(char ch)
149
156M
{
150
156M
    return (ch == '\0' || util::is_space(ch));
151
156M
}
152
153
bool
154
Tokenizer::isDelimiter(char ch)
155
268M
{
156
268M
    return is_delimiter(ch);
157
268M
}
158
159
void
160
QPDFTokenizer::presentCharacter(char ch)
161
0
{
162
0
    m->presentCharacter(ch);
163
0
}
164
165
void
166
Tokenizer::presentCharacter(char ch)
167
21.7k
{
168
21.7k
    handleCharacter(ch);
169
170
21.7k
    if (in_token) {
171
0
        raw_val += ch;
172
0
    }
173
21.7k
}
174
175
void
176
Tokenizer::handleCharacter(char ch)
177
990M
{
178
    // In some cases, functions called below may call a second handler. This happens whenever you
179
    // have to use a character from the next token to detect the end of the current token.
180
181
990M
    switch (state) {
182
0
    case st_top:
183
0
        inTop(ch);
184
0
        return;
185
186
69.9M
    case st_in_space:
187
69.9M
        inSpace(ch);
188
69.9M
        return;
189
190
113M
    case st_in_comment:
191
113M
        inComment(ch);
192
113M
        return;
193
194
890k
    case st_lt:
195
890k
        inLt(ch);
196
890k
        return;
197
198
579k
    case st_gt:
199
579k
        inGt(ch);
200
579k
        return;
201
202
374M
    case st_in_string:
203
374M
        inString(ch);
204
374M
        return;
205
206
84.8M
    case st_name:
207
84.8M
        inName(ch);
208
84.8M
        return;
209
210
15.1M
    case st_number:
211
15.1M
        inNumber(ch);
212
15.1M
        return;
213
214
447k
    case st_real:
215
447k
        inReal(ch);
216
447k
        return;
217
218
1.72M
    case st_string_after_cr:
219
1.72M
        inStringAfterCR(ch);
220
1.72M
        return;
221
222
763k
    case st_string_escape:
223
763k
        inStringEscape(ch);
224
763k
        return;
225
226
43.1k
    case st_char_code:
227
43.1k
        inCharCode(ch);
228
43.1k
        return;
229
230
177M
    case st_literal:
231
177M
        inLiteral(ch);
232
177M
        return;
233
234
62.1M
    case st_inline_image:
235
62.1M
        inInlineImage(ch);
236
62.1M
        return;
237
238
378k
    case st_in_hexstring:
239
378k
        inHexstring(ch);
240
378k
        return;
241
242
312k
    case st_in_hexstring_2nd:
243
312k
        inHexstring2nd(ch);
244
312k
        return;
245
246
445k
    case st_name_hex1:
247
445k
        inNameHex1(ch);
248
445k
        return;
249
250
46.5k
    case st_name_hex2:
251
46.5k
        inNameHex2(ch);
252
46.5k
        return;
253
254
1.24M
    case st_sign:
255
1.24M
        inSign(ch);
256
1.24M
        return;
257
258
207k
    case st_decimal:
259
207k
        inDecimal(ch);
260
207k
        return;
261
262
85.5M
    case (st_before_token):
263
85.5M
        inBeforeToken(ch);
264
85.5M
        return;
265
266
0
    case (st_token_ready):
267
0
        inTokenReady(ch);
268
0
        return;
269
270
0
    default:
271
0
        throw std::logic_error("INTERNAL ERROR: invalid state while reading token");
272
990M
    }
273
990M
}
274
275
void
276
Tokenizer::inTokenReady(char ch)
277
0
{
278
0
    throw std::logic_error(
279
0
        "INTERNAL ERROR: QPDF tokenizer presented character while token is waiting");
280
0
}
281
282
void
283
Tokenizer::inBeforeToken(char ch)
284
85.5M
{
285
    // Note: we specifically do not use ctype here.  It is locale-dependent.
286
85.5M
    if (isSpace(ch)) {
287
33.3M
        before_token = !include_ignorable;
288
33.3M
        in_token = include_ignorable;
289
33.3M
        if (include_ignorable) {
290
25.8M
            state = st_in_space;
291
25.8M
        }
292
52.1M
    } else if (ch == '%') {
293
124k
        before_token = !include_ignorable;
294
124k
        in_token = include_ignorable;
295
124k
        state = st_in_comment;
296
52.0M
    } else {
297
52.0M
        before_token = false;
298
52.0M
        in_token = true;
299
52.0M
        inTop(ch);
300
52.0M
    }
301
85.5M
}
302
303
void
304
Tokenizer::inTop(char ch)
305
52.0M
{
306
52.0M
    switch (ch) {
307
128k
    case '(':
308
128k
        string_depth = 1;
309
128k
        state = st_in_string;
310
128k
        return;
311
312
891k
    case '<':
313
891k
        state = st_lt;
314
891k
        return;
315
316
580k
    case '>':
317
580k
        state = st_gt;
318
580k
        return;
319
320
1.18M
    case (')'):
321
1.18M
        type = tt::tt_bad;
322
1.18M
        QTC::TC("qpdf", "QPDFTokenizer bad )");
323
1.18M
        error_message = "unexpected )";
324
1.18M
        state = st_token_ready;
325
1.18M
        return;
326
327
604k
    case '[':
328
604k
        type = tt::tt_array_open;
329
604k
        state = st_token_ready;
330
604k
        return;
331
332
513k
    case ']':
333
513k
        type = tt::tt_array_close;
334
513k
        state = st_token_ready;
335
513k
        return;
336
337
375k
    case '{':
338
375k
        type = tt::tt_brace_open;
339
375k
        state = st_token_ready;
340
375k
        return;
341
342
276k
    case '}':
343
276k
        type = tt::tt_brace_close;
344
276k
        state = st_token_ready;
345
276k
        return;
346
347
16.2M
    case '/':
348
16.2M
        state = st_name;
349
16.2M
        val += ch;
350
16.2M
        return;
351
352
1.41M
    case '0':
353
1.99M
    case '1':
354
2.30M
    case '2':
355
2.59M
    case '3':
356
2.79M
    case '4':
357
3.13M
    case '5':
358
3.31M
    case '6':
359
3.54M
    case '7':
360
3.67M
    case '8':
361
3.89M
    case '9':
362
3.89M
        state = st_number;
363
3.89M
        return;
364
365
794k
    case '+':
366
1.24M
    case '-':
367
1.24M
        state = st_sign;
368
1.24M
        return;
369
370
206k
    case '.':
371
206k
        state = st_decimal;
372
206k
        return;
373
374
25.9M
    default:
375
25.9M
        state = st_literal;
376
25.9M
        return;
377
52.0M
    }
378
52.0M
}
379
380
void
381
Tokenizer::inSpace(char ch)
382
69.9M
{
383
    // We only enter this state if include_ignorable is true.
384
69.9M
    if (!isSpace(ch)) {
385
25.8M
        type = tt::tt_space;
386
25.8M
        in_token = false;
387
25.8M
        char_to_unread = ch;
388
25.8M
        state = st_token_ready;
389
25.8M
    }
390
69.9M
}
391
392
void
393
Tokenizer::inComment(char ch)
394
113M
{
395
113M
    if ((ch == '\r') || (ch == '\n')) {
396
122k
        if (include_ignorable) {
397
98.1k
            type = tt::tt_comment;
398
98.1k
            in_token = false;
399
98.1k
            char_to_unread = ch;
400
98.1k
            state = st_token_ready;
401
98.1k
        } else {
402
24.8k
            state = st_before_token;
403
24.8k
        }
404
122k
    }
405
113M
}
406
407
void
408
Tokenizer::inString(char ch)
409
376M
{
410
376M
    switch (ch) {
411
763k
    case '\\':
412
763k
        state = st_string_escape;
413
763k
        return;
414
415
784k
    case '(':
416
784k
        val += ch;
417
784k
        ++string_depth;
418
784k
        return;
419
420
805k
    case ')':
421
805k
        if (--string_depth == 0) {
422
118k
            type = tt::tt_string;
423
118k
            state = st_token_ready;
424
118k
            return;
425
118k
        }
426
427
686k
        val += ch;
428
686k
        return;
429
430
1.72M
    case '\r':
431
        // CR by itself is converted to LF
432
1.72M
        val += '\n';
433
1.72M
        state = st_string_after_cr;
434
1.72M
        return;
435
436
950k
    case '\n':
437
950k
        val += ch;
438
950k
        return;
439
440
371M
    default:
441
371M
        val += ch;
442
371M
        return;
443
376M
    }
444
376M
}
445
446
void
447
Tokenizer::inName(char ch)
448
85.3M
{
449
85.3M
    if (isDelimiter(ch)) {
450
        // A C-locale whitespace character or delimiter terminates token.  It is important to unread
451
        // the whitespace character even though it is ignored since it may be the newline after a
452
        // stream keyword.  Removing it here could make the stream-reading code break on some files,
453
        // though not on any files in the test suite as of this
454
        // writing.
455
456
16.2M
        type = bad ? tt::tt_bad : tt::tt_name;
457
16.2M
        in_token = false;
458
16.2M
        char_to_unread = ch;
459
16.2M
        state = st_token_ready;
460
69.1M
    } else if (ch == '#') {
461
445k
        char_code = 0;
462
445k
        state = st_name_hex1;
463
68.6M
    } else {
464
68.6M
        val += ch;
465
68.6M
    }
466
85.3M
}
467
468
void
469
Tokenizer::inNameHex1(char ch)
470
445k
{
471
445k
    hex_char = ch;
472
473
445k
    if (char hval = util::hex_decode_char(ch); hval < '\20') {
474
46.5k
        char_code = int(hval) << 4;
475
46.5k
        state = st_name_hex2;
476
398k
    } else {
477
398k
        QTC::TC("qpdf", "QPDFTokenizer bad name 1");
478
398k
        error_message = "name with stray # will not work with PDF >= 1.2";
479
        // Use null to encode a bad # -- this is reversed in QPDF_Name::normalizeName.
480
398k
        val += '\0';
481
398k
        state = st_name;
482
398k
        inName(ch);
483
398k
    }
484
445k
}
485
486
void
487
Tokenizer::inNameHex2(char ch)
488
46.5k
{
489
46.5k
    if (char hval = util::hex_decode_char(ch); hval < '\20') {
490
21.8k
        char_code |= int(hval);
491
24.6k
    } else {
492
24.6k
        QTC::TC("qpdf", "QPDFTokenizer bad name 2");
493
24.6k
        error_message = "name with stray # will not work with PDF >= 1.2";
494
        // Use null to encode a bad # -- this is reversed in QPDF_Name::normalizeName.
495
24.6k
        val += '\0';
496
24.6k
        val += hex_char;
497
24.6k
        state = st_name;
498
24.6k
        inName(ch);
499
24.6k
        return;
500
24.6k
    }
501
21.8k
    if (char_code == 0) {
502
1.35k
        QTC::TC("qpdf", "QPDFTokenizer null in name");
503
1.35k
        error_message = "null character not allowed in name token";
504
1.35k
        val += "#00";
505
1.35k
        state = st_name;
506
1.35k
        bad = true;
507
20.5k
    } else {
508
20.5k
        val += char(char_code);
509
20.5k
        state = st_name;
510
20.5k
    }
511
21.8k
}
512
513
void
514
Tokenizer::inSign(char ch)
515
1.24M
{
516
1.24M
    if (util::is_digit(ch)) {
517
230k
        state = st_number;
518
1.01M
    } else if (ch == '.') {
519
475
        state = st_decimal;
520
1.01M
    } else {
521
1.01M
        state = st_literal;
522
1.01M
        inLiteral(ch);
523
1.01M
    }
524
1.24M
}
525
526
void
527
Tokenizer::inDecimal(char ch)
528
207k
{
529
207k
    if (util::is_digit(ch)) {
530
34.6k
        state = st_real;
531
172k
    } else {
532
172k
        state = st_literal;
533
172k
        inLiteral(ch);
534
172k
    }
535
207k
}
536
537
void
538
Tokenizer::inNumber(char ch)
539
15.1M
{
540
15.1M
    if (util::is_digit(ch)) {
541
11.0M
    } else if (ch == '.') {
542
111k
        state = st_real;
543
3.97M
    } else if (isDelimiter(ch)) {
544
2.80M
        type = tt::tt_integer;
545
2.80M
        state = st_token_ready;
546
2.80M
        in_token = false;
547
2.80M
        char_to_unread = ch;
548
2.80M
    } else {
549
1.17M
        state = st_literal;
550
1.17M
    }
551
15.1M
}
552
553
void
554
Tokenizer::inReal(char ch)
555
447k
{
556
447k
    if (util::is_digit(ch)) {
557
301k
    } else if (isDelimiter(ch)) {
558
138k
        type = tt::tt_real;
559
138k
        state = st_token_ready;
560
138k
        in_token = false;
561
138k
        char_to_unread = ch;
562
138k
    } else {
563
6.89k
        state = st_literal;
564
6.89k
    }
565
447k
}
566
void
567
Tokenizer::inStringEscape(char ch)
568
763k
{
569
763k
    state = st_in_string;
570
763k
    switch (ch) {
571
5.68k
    case '0':
572
6.75k
    case '1':
573
13.3k
    case '2':
574
19.4k
    case '3':
575
26.0k
    case '4':
576
27.0k
    case '5':
577
29.9k
    case '6':
578
32.8k
    case '7':
579
32.8k
        state = st_char_code;
580
32.8k
        char_code = 0;
581
32.8k
        digit_count = 0;
582
32.8k
        inCharCode(ch);
583
32.8k
        return;
584
585
5.43k
    case 'n':
586
5.43k
        val += '\n';
587
5.43k
        return;
588
589
27.6k
    case 'r':
590
27.6k
        val += '\r';
591
27.6k
        return;
592
593
4.37k
    case 't':
594
4.37k
        val += '\t';
595
4.37k
        return;
596
597
5.13k
    case 'b':
598
5.13k
        val += '\b';
599
5.13k
        return;
600
601
45.2k
    case 'f':
602
45.2k
        val += '\f';
603
45.2k
        return;
604
605
979
    case '\n':
606
979
        return;
607
608
2.14k
    case '\r':
609
2.14k
        state = st_string_after_cr;
610
2.14k
        return;
611
612
639k
    default:
613
        // PDF spec says backslash is ignored before anything else
614
639k
        val += ch;
615
639k
        return;
616
763k
    }
617
763k
}
618
619
void
620
Tokenizer::inStringAfterCR(char ch)
621
1.72M
{
622
1.72M
    state = st_in_string;
623
1.72M
    if (ch != '\n') {
624
1.69M
        inString(ch);
625
1.69M
    }
626
1.72M
}
627
628
void
629
Tokenizer::inLt(char ch)
630
890k
{
631
890k
    if (ch == '<') {
632
406k
        type = tt::tt_dict_open;
633
406k
        state = st_token_ready;
634
406k
        return;
635
406k
    }
636
637
484k
    state = st_in_hexstring;
638
484k
    inHexstring(ch);
639
484k
}
640
641
void
642
Tokenizer::inGt(char ch)
643
579k
{
644
579k
    if (ch == '>') {
645
284k
        type = tt::tt_dict_close;
646
284k
        state = st_token_ready;
647
295k
    } else {
648
295k
        type = tt::tt_bad;
649
295k
        QTC::TC("qpdf", "QPDFTokenizer bad >");
650
295k
        error_message = "unexpected >";
651
295k
        in_token = false;
652
295k
        char_to_unread = ch;
653
295k
        state = st_token_ready;
654
295k
    }
655
579k
}
656
657
void
658
Tokenizer::inLiteral(char ch)
659
178M
{
660
178M
    if (isDelimiter(ch)) {
661
        // A C-locale whitespace character or delimiter terminates token.  It is important to unread
662
        // the whitespace character even though it is ignored since it may be the newline after a
663
        // stream keyword.  Removing it here could make the stream-reading code break on some files,
664
        // though not on any files in the test suite as of this writing.
665
666
28.1M
        in_token = false;
667
28.1M
        char_to_unread = ch;
668
28.1M
        state = st_token_ready;
669
28.1M
        type = (raw_val == "true") || (raw_val == "false")
670
28.1M
            ? tt::tt_bool
671
28.1M
            : (raw_val == "null" ? tt::tt_null : tt::tt_word);
672
28.1M
    }
673
178M
}
674
675
void
676
Tokenizer::inHexstring(char ch)
677
863k
{
678
863k
    if (char hval = util::hex_decode_char(ch); hval < '\20') {
679
293k
        char_code = int(hval) << 4;
680
293k
        state = st_in_hexstring_2nd;
681
682
570k
    } else if (ch == '>') {
683
63.1k
        type = tt::tt_string;
684
63.1k
        state = st_token_ready;
685
686
506k
    } else if (isSpace(ch)) {
687
        // ignore
688
689
336k
    } else {
690
336k
        type = tt::tt_bad;
691
336k
        QTC::TC("qpdf", "QPDFTokenizer bad hexstring character");
692
336k
        error_message = std::string("invalid character (") + ch + ") in hexstring";
693
336k
        state = st_token_ready;
694
336k
    }
695
863k
}
696
697
void
698
Tokenizer::inHexstring2nd(char ch)
699
312k
{
700
312k
    if (char hval = util::hex_decode_char(ch); hval < '\20') {
701
208k
        val += char(char_code) | hval;
702
208k
        state = st_in_hexstring;
703
704
208k
    } else if (ch == '>') {
705
        // PDF spec says odd hexstrings have implicit trailing 0.
706
5.68k
        val += char(char_code);
707
5.68k
        type = tt::tt_string;
708
5.68k
        state = st_token_ready;
709
710
98.7k
    } else if (isSpace(ch)) {
711
        // ignore
712
713
78.9k
    } else {
714
78.9k
        type = tt::tt_bad;
715
78.9k
        QTC::TC("qpdf", "QPDFTokenizer bad hexstring 2nd character");
716
78.9k
        error_message = std::string("invalid character (") + ch + ") in hexstring";
717
78.9k
        state = st_token_ready;
718
78.9k
    }
719
312k
}
720
721
void
722
Tokenizer::inCharCode(char ch)
723
76.0k
{
724
76.0k
    bool handled = false;
725
76.0k
    if (('0' <= ch) && (ch <= '7')) {
726
53.1k
        char_code = 8 * char_code + (int(ch) - int('0'));
727
53.1k
        if (++(digit_count) < 3) {
728
43.3k
            return;
729
43.3k
        }
730
9.87k
        handled = true;
731
9.87k
    }
732
    // We've accumulated \ddd or we have \d or \dd followed by other than an octal digit. The PDF
733
    // Spec says to ignore high-order overflow.
734
32.6k
    val += char(char_code % 256);
735
32.6k
    state = st_in_string;
736
32.6k
    if (!handled) {
737
22.8k
        inString(ch);
738
22.8k
    }
739
32.6k
}
740
741
void
742
Tokenizer::inInlineImage(char ch)
743
62.1M
{
744
62.1M
    if ((raw_val.length() + 1) == inline_image_bytes) {
745
3.40k
        QTC::TC("qpdf", "QPDFTokenizer found EI by byte count");
746
3.40k
        type = tt::tt_inline_image;
747
3.40k
        inline_image_bytes = 0;
748
3.40k
        state = st_token_ready;
749
3.40k
    }
750
62.1M
}
751
752
void
753
QPDFTokenizer::presentEOF()
754
0
{
755
0
    m->presentEOF();
756
0
}
757
758
void
759
Tokenizer::presentEOF()
760
50.3k
{
761
50.3k
    switch (state) {
762
2.66k
    case st_name:
763
2.99k
    case st_name_hex1:
764
3.27k
    case st_name_hex2:
765
9.78k
    case st_number:
766
10.1k
    case st_real:
767
10.3k
    case st_sign:
768
10.6k
    case st_decimal:
769
21.7k
    case st_literal:
770
21.7k
        QTC::TC("qpdf", "QPDFTokenizer EOF reading appendable token");
771
        // Push any delimiter to the state machine to finish off the final token.
772
21.7k
        presentCharacter('\f');
773
21.7k
        in_token = true;
774
21.7k
        break;
775
776
0
    case st_top:
777
21.8k
    case st_before_token:
778
21.8k
        type = tt::tt_eof;
779
21.8k
        break;
780
781
1.07k
    case st_in_space:
782
1.07k
        type = include_ignorable ? tt::tt_space : tt::tt_eof;
783
1.07k
        break;
784
785
1.15k
    case st_in_comment:
786
1.15k
        type = include_ignorable ? tt::tt_comment : tt::tt_bad;
787
1.15k
        break;
788
789
0
    case st_token_ready:
790
0
        break;
791
792
4.45k
    default:
793
4.45k
        QTC::TC("qpdf", "QPDFTokenizer EOF reading token");
794
4.45k
        type = tt::tt_bad;
795
4.45k
        error_message = "EOF while reading token";
796
50.3k
    }
797
50.3k
    state = st_token_ready;
798
50.3k
}
799
800
void
801
QPDFTokenizer::expectInlineImage(std::shared_ptr<InputSource> input)
802
0
{
803
0
    m->expectInlineImage(*input);
804
0
}
805
806
void
807
QPDFTokenizer::expectInlineImage(InputSource& input)
808
3.50k
{
809
3.50k
    m->expectInlineImage(input);
810
3.50k
}
811
812
void
813
Tokenizer::expectInlineImage(InputSource& input)
814
3.50k
{
815
3.50k
    if (state == st_token_ready) {
816
0
        reset();
817
3.50k
    } else if (state != st_before_token) {
818
0
        throw std::logic_error(
819
0
            "QPDFTokenizer::expectInlineImage called when tokenizer is in improper state");
820
0
    }
821
3.50k
    findEI(input);
822
3.50k
    before_token = false;
823
3.50k
    in_token = true;
824
3.50k
    state = st_inline_image;
825
3.50k
}
826
827
void
828
Tokenizer::findEI(InputSource& input)
829
3.50k
{
830
3.50k
    qpdf_offset_t last_offset = input.getLastOffset();
831
3.50k
    qpdf_offset_t pos = input.tell();
832
833
    // Use QPDFWordTokenFinder to find EI surrounded by delimiters. Then read the next several
834
    // tokens or up to EOF. If we find any suspicious-looking or tokens, this is probably still part
835
    // of the image data, so keep looking for EI. Stop at the first EI that passes. If we get to the
836
    // end without finding one, return the last EI we found. Store the number of bytes expected in
837
    // the inline image including the EI and use that to break out of inline image, falling back to
838
    // the old method if needed.
839
840
3.50k
    bool okay = false;
841
3.50k
    bool first_try = true;
842
15.2k
    while (!okay) {
843
14.3k
        QPDFWordTokenFinder f(input, "EI");
844
14.3k
        if (!input.findFirst("EI", input.tell(), 0, f)) {
845
2.65k
            break;
846
2.65k
        }
847
11.7k
        inline_image_bytes = QIntC::to_size(input.tell() - pos - 2);
848
849
11.7k
        Tokenizer check;
850
11.7k
        bool found_bad = false;
851
        // Look at the next 10 tokens or up to EOF. The next inline image's image data would look
852
        // like bad tokens, but there will always be at least 10 tokens between one inline image's
853
        // EI and the next valid one's ID since width, height, bits per pixel, and color space are
854
        // all required as well as a BI and ID. If we get 10 good tokens in a row or hit EOF, we can
855
        // be pretty sure we've found the actual EI.
856
38.2k
        for (int i = 0; i < 10; ++i) {
857
37.4k
            check.nextToken(input, "checker");
858
37.4k
            auto typ = check.getType();
859
37.4k
            if (typ == tt::tt_eof) {
860
0
                okay = true;
861
37.4k
            } else if (typ == tt::tt_bad) {
862
4.41k
                found_bad = true;
863
32.9k
            } else if (typ == tt::tt_word) {
864
                // The qpdf tokenizer lumps alphabetic and otherwise uncategorized characters into
865
                // "words". We recognize strings of alphabetic characters as potential valid
866
                // operators for purposes of telling whether we're in valid content or not. It's not
867
                // perfect, but it should work more reliably than what we used to do, which was
868
                // already good enough for the vast majority of files.
869
17.7k
                bool found_alpha = false;
870
17.7k
                bool found_non_printable = false;
871
17.7k
                bool found_other = false;
872
34.1k
                for (char ch: check.getValue()) {
873
34.1k
                    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch == '*')) {
874
                        // Treat '*' as alpha since there are valid PDF operators that contain *
875
                        // along with alphabetic characters.
876
17.2k
                        found_alpha = true;
877
17.2k
                    } else if (static_cast<signed char>(ch) < 32 && !isSpace(ch)) {
878
                        // Compare ch as a signed char so characters outside of 7-bit will be < 0.
879
5.53k
                        found_non_printable = true;
880
5.53k
                        break;
881
11.3k
                    } else {
882
11.3k
                        found_other = true;
883
11.3k
                    }
884
34.1k
                }
885
17.7k
                if (found_non_printable || (found_alpha && found_other)) {
886
6.47k
                    found_bad = true;
887
6.47k
                }
888
17.7k
            }
889
37.4k
            if (okay || found_bad) {
890
10.8k
                break;
891
10.8k
            }
892
37.4k
        }
893
11.7k
        if (!found_bad) {
894
853
            okay = true;
895
853
        }
896
11.7k
        if (!okay) {
897
10.8k
            first_try = false;
898
10.8k
        }
899
11.7k
    }
900
3.50k
    if (okay && (!first_try)) {
901
56
        QTC::TC("qpdf", "QPDFTokenizer found EI after more than one try");
902
56
    }
903
904
3.50k
    input.seek(pos, SEEK_SET);
905
3.50k
    input.setLastOffset(last_offset);
906
3.50k
}
907
908
bool
909
QPDFTokenizer::getToken(Token& token, bool& unread_char, char& ch)
910
0
{
911
0
    return m->getToken(token, unread_char, ch);
912
0
}
913
914
bool
915
Tokenizer::getToken(Token& token, bool& unread_char, char& ch)
916
59.6M
{
917
59.6M
    bool ready = (state == st_token_ready);
918
59.6M
    unread_char = !in_token && !before_token;
919
59.6M
    ch = char_to_unread;
920
59.6M
    if (ready) {
921
59.6M
        token = (!(type == tt::tt_name || type == tt::tt_string))
922
59.6M
            ? Token(type, raw_val, raw_val, error_message)
923
59.6M
            : Token(type, val, raw_val, error_message);
924
925
59.6M
        reset();
926
59.6M
    }
927
59.6M
    return ready;
928
59.6M
}
929
930
bool
931
QPDFTokenizer::betweenTokens()
932
0
{
933
0
    return m->betweenTokens();
934
0
}
935
936
bool
937
Tokenizer::betweenTokens()
938
0
{
939
0
    return before_token;
940
0
}
941
942
QPDFTokenizer::Token
943
QPDFTokenizer::readToken(
944
    InputSource& input, std::string const& context, bool allow_bad, size_t max_len)
945
57.1M
{
946
57.1M
    return m->readToken(input, context, allow_bad, max_len);
947
57.1M
}
948
949
QPDFTokenizer::Token
950
QPDFTokenizer::readToken(
951
    std::shared_ptr<InputSource> input, std::string const& context, bool allow_bad, size_t max_len)
952
0
{
953
0
    return m->readToken(*input, context, allow_bad, max_len);
954
0
}
955
956
QPDFTokenizer::Token
957
Tokenizer::readToken(InputSource& input, std::string const& context, bool allow_bad, size_t max_len)
958
59.6M
{
959
59.6M
    nextToken(input, context, max_len);
960
961
59.6M
    Token token;
962
59.6M
    bool unread_char;
963
59.6M
    char char_to_unread;
964
59.6M
    getToken(token, unread_char, char_to_unread);
965
966
59.6M
    if (token.getType() == tt::tt_bad) {
967
2.07M
        if (allow_bad) {
968
2.07M
            QTC::TC("qpdf", "QPDFTokenizer allowing bad token");
969
2.07M
        } else {
970
0
            throw QPDFExc(
971
0
                qpdf_e_damaged_pdf,
972
0
                input.getName(),
973
0
                context.empty() ? "offset " + std::to_string(input.getLastOffset()) : context,
974
0
                input.getLastOffset(),
975
0
                token.getErrorMessage());
976
0
        }
977
2.07M
    }
978
59.6M
    return token;
979
59.6M
}
980
981
bool
982
Tokenizer::nextToken(InputSource& input, std::string const& context, size_t max_len)
983
77.9M
{
984
77.9M
    if (state != st_inline_image) {
985
77.9M
        reset();
986
77.9M
    }
987
77.9M
    qpdf_offset_t offset = input.fastTell();
988
989
1.06G
    while (state != st_token_ready) {
990
990M
        char ch;
991
990M
        if (!input.fastRead(ch)) {
992
50.3k
            presentEOF();
993
994
50.3k
            if ((type == tt::tt_eof) && (!allow_eof)) {
995
                // Nothing in the qpdf library calls readToken without allowEOF anymore, so this
996
                // case is not exercised.
997
1.28k
                type = tt::tt_bad;
998
1.28k
                error_message = "unexpected EOF";
999
1.28k
                offset = input.getLastOffset();
1000
1.28k
            }
1001
990M
        } else {
1002
990M
            handleCharacter(ch);
1003
990M
            if (before_token) {
1004
16.4M
                ++offset;
1005
16.4M
            }
1006
990M
            if (in_token) {
1007
900M
                raw_val += ch;
1008
900M
            }
1009
990M
            if (max_len && (raw_val.length() >= max_len) && (state != st_token_ready)) {
1010
                // terminate this token now
1011
211k
                QTC::TC("qpdf", "QPDFTokenizer block long token");
1012
211k
                type = tt::tt_bad;
1013
211k
                state = st_token_ready;
1014
211k
                error_message = "exceeded allowable length while reading token";
1015
211k
            }
1016
990M
        }
1017
990M
    }
1018
1019
77.9M
    input.fastUnread(!in_token && !before_token);
1020
1021
77.9M
    if (type != tt::tt_eof) {
1022
77.9M
        input.setLastOffset(offset);
1023
77.9M
    }
1024
1025
77.9M
    return error_message.empty();
1026
77.9M
}