Coverage Report

Created: 2026-01-25 06:25

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
225M
{
26
225M
    return (
27
225M
        ch == ' ' || ch == '\n' || ch == '/' || ch == '(' || ch == ')' || ch == '{' || ch == '}' ||
28
214M
        ch == '<' || ch == '>' || ch == '[' || ch == ']' || ch == '%' || ch == '\t' || ch == '\r' ||
29
213M
        ch == '\v' || ch == '\f' || ch == 0);
30
225M
}
31
32
namespace
33
{
34
    class QPDFWordTokenFinder: public InputSource::Finder
35
    {
36
      public:
37
        QPDFWordTokenFinder(InputSource& is, std::string const& str) :
38
20.7k
            is(is),
39
20.7k
            str(str)
40
20.7k
        {
41
20.7k
        }
42
20.7k
        ~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
56.4k
{
54
    // Find a word token matching the given string, preceded by a delimiter, and followed by a
55
    // delimiter or EOF.
56
56.4k
    Tokenizer tokenizer;
57
56.4k
    tokenizer.nextToken(is, "finder", str.size() + 2);
58
56.4k
    qpdf_offset_t pos = is.tell();
59
56.4k
    if (tokenizer.getType() != tt::tt_word || tokenizer.getValue() != str) {
60
36.6k
        QTC::TC("qpdf", "QPDFTokenizer finder found wrong word");
61
36.6k
        return false;
62
36.6k
    }
63
19.8k
    qpdf_offset_t token_start = is.getLastOffset();
64
19.8k
    char next;
65
19.8k
    bool next_okay = false;
66
19.8k
    if (is.read(&next, 1) == 0) {
67
14
        QTC::TC("qpdf", "QPDFTokenizer inline image at EOF");
68
14
        next_okay = true;
69
19.8k
    } else {
70
19.8k
        next_okay = is_delimiter(next);
71
19.8k
    }
72
19.8k
    is.seek(pos, SEEK_SET);
73
19.8k
    if (!next_okay) {
74
0
        return false;
75
0
    }
76
19.8k
    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
19.8k
    return true;
81
19.8k
}
82
83
void
84
Tokenizer::reset()
85
21.2M
{
86
21.2M
    state = st_before_token;
87
21.2M
    type = tt::tt_bad;
88
21.2M
    val.clear();
89
21.2M
    raw_val.clear();
90
21.2M
    error_message = "";
91
21.2M
    before_token = true;
92
21.2M
    in_token = false;
93
21.2M
    char_to_unread = '\0';
94
21.2M
    inline_image_bytes = 0;
95
21.2M
    string_depth = 0;
96
21.2M
    bad = false;
97
21.2M
}
98
99
QPDFTokenizer::Token::Token(token_type_e type, std::string const& value) :
100
362
    type(type),
101
362
    value(value),
102
362
    raw_value(value)
103
362
{
104
362
    if (type == tt_string) {
105
0
        raw_value = QPDFObjectHandle::newString(value).unparse();
106
362
    } else if (type == tt_name) {
107
0
        raw_value = QPDFObjectHandle::newName(value).unparse();
108
0
    }
109
362
}
110
111
QPDFTokenizer::QPDFTokenizer() :
112
19.1k
    m(std::make_unique<qpdf::Tokenizer>())
113
19.1k
{
114
19.1k
}
115
116
19.1k
QPDFTokenizer::~QPDFTokenizer() = default;
117
118
Tokenizer::Tokenizer()
119
140k
{
120
140k
    reset();
121
140k
}
122
123
void
124
QPDFTokenizer::allowEOF()
125
19.1k
{
126
19.1k
    m->allowEOF();
127
19.1k
}
128
129
void
130
Tokenizer::allowEOF()
131
64.5k
{
132
64.5k
    allow_eof = true;
133
64.5k
}
134
135
void
136
QPDFTokenizer::includeIgnorable()
137
19.1k
{
138
19.1k
    m->includeIgnorable();
139
19.1k
}
140
141
void
142
Tokenizer::includeIgnorable()
143
19.1k
{
144
19.1k
    include_ignorable = true;
145
19.1k
}
146
147
bool
148
Tokenizer::isSpace(char ch)
149
33.8M
{
150
33.8M
    return (ch == '\0' || util::is_space(ch));
151
33.8M
}
152
153
bool
154
Tokenizer::isDelimiter(char ch)
155
225M
{
156
225M
    return is_delimiter(ch);
157
225M
}
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
26.2k
{
168
26.2k
    handleCharacter(ch);
169
170
26.2k
    if (in_token) {
171
0
        raw_val += ch;
172
0
    }
173
26.2k
}
174
175
void
176
Tokenizer::handleCharacter(char ch)
177
466M
{
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
466M
    switch (state) {
182
0
    case st_top:
183
0
        inTop(ch);
184
0
        return;
185
186
763k
    case st_in_space:
187
763k
        inSpace(ch);
188
763k
        return;
189
190
6.86M
    case st_in_comment:
191
6.86M
        inComment(ch);
192
6.86M
        return;
193
194
600k
    case st_lt:
195
600k
        inLt(ch);
196
600k
        return;
197
198
384k
    case st_gt:
199
384k
        inGt(ch);
200
384k
        return;
201
202
162M
    case st_in_string:
203
162M
        inString(ch);
204
162M
        return;
205
206
104M
    case st_name:
207
104M
        inName(ch);
208
104M
        return;
209
210
12.9M
    case st_number:
211
12.9M
        inNumber(ch);
212
12.9M
        return;
213
214
1.51M
    case st_real:
215
1.51M
        inReal(ch);
216
1.51M
        return;
217
218
156k
    case st_string_after_cr:
219
156k
        inStringAfterCR(ch);
220
156k
        return;
221
222
211k
    case st_string_escape:
223
211k
        inStringEscape(ch);
224
211k
        return;
225
226
15.6k
    case st_char_code:
227
15.6k
        inCharCode(ch);
228
15.6k
        return;
229
230
114M
    case st_literal:
231
114M
        inLiteral(ch);
232
114M
        return;
233
234
26.6M
    case st_inline_image:
235
26.6M
        inInlineImage(ch);
236
26.6M
        return;
237
238
838k
    case st_in_hexstring:
239
838k
        inHexstring(ch);
240
838k
        return;
241
242
845k
    case st_in_hexstring_2nd:
243
845k
        inHexstring2nd(ch);
244
845k
        return;
245
246
36.3k
    case st_name_hex1:
247
36.3k
        inNameHex1(ch);
248
36.3k
        return;
249
250
17.6k
    case st_name_hex2:
251
17.6k
        inNameHex2(ch);
252
17.6k
        return;
253
254
109k
    case st_sign:
255
109k
        inSign(ch);
256
109k
        return;
257
258
140k
    case st_decimal:
259
140k
        inDecimal(ch);
260
140k
        return;
261
262
33.0M
    case (st_before_token):
263
33.0M
        inBeforeToken(ch);
264
33.0M
        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
466M
    }
273
466M
}
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
33.0M
{
285
    // Note: we specifically do not use ctype here.  It is locale-dependent.
286
33.0M
    if (isSpace(ch)) {
287
17.4M
        before_token = !include_ignorable;
288
17.4M
        in_token = include_ignorable;
289
17.4M
        if (include_ignorable) {
290
535k
            state = st_in_space;
291
535k
        }
292
17.4M
    } else if (ch == '%') {
293
38.3k
        before_token = !include_ignorable;
294
38.3k
        in_token = include_ignorable;
295
38.3k
        state = st_in_comment;
296
15.5M
    } else {
297
15.5M
        before_token = false;
298
15.5M
        in_token = true;
299
15.5M
        inTop(ch);
300
15.5M
    }
301
33.0M
}
302
303
void
304
Tokenizer::inTop(char ch)
305
15.5M
{
306
15.5M
    switch (ch) {
307
211k
    case '(':
308
211k
        string_depth = 1;
309
211k
        state = st_in_string;
310
211k
        return;
311
312
608k
    case '<':
313
608k
        state = st_lt;
314
608k
        return;
315
316
388k
    case '>':
317
388k
        state = st_gt;
318
388k
        return;
319
320
27.3k
    case (')'):
321
27.3k
        type = tt::tt_bad;
322
27.3k
        QTC::TC("qpdf", "QPDFTokenizer bad )");
323
27.3k
        error_message = "unexpected )";
324
27.3k
        state = st_token_ready;
325
27.3k
        return;
326
327
350k
    case '[':
328
350k
        type = tt::tt_array_open;
329
350k
        state = st_token_ready;
330
350k
        return;
331
332
237k
    case ']':
333
237k
        type = tt::tt_array_close;
334
237k
        state = st_token_ready;
335
237k
        return;
336
337
9.86k
    case '{':
338
9.86k
        type = tt::tt_brace_open;
339
9.86k
        state = st_token_ready;
340
9.86k
        return;
341
342
8.03k
    case '}':
343
8.03k
        type = tt::tt_brace_close;
344
8.03k
        state = st_token_ready;
345
8.03k
        return;
346
347
3.37M
    case '/':
348
3.37M
        state = st_name;
349
3.37M
        val += ch;
350
3.37M
        return;
351
352
2.02M
    case '0':
353
2.92M
    case '1':
354
3.73M
    case '2':
355
4.11M
    case '3':
356
4.48M
    case '4':
357
4.89M
    case '5':
358
5.55M
    case '6':
359
6.09M
    case '7':
360
6.33M
    case '8':
361
6.47M
    case '9':
362
6.47M
        state = st_number;
363
6.47M
        return;
364
365
6.24k
    case '+':
366
115k
    case '-':
367
115k
        state = st_sign;
368
115k
        return;
369
370
163k
    case '.':
371
163k
        state = st_decimal;
372
163k
        return;
373
374
3.53M
    default:
375
3.53M
        state = st_literal;
376
3.53M
        return;
377
15.5M
    }
378
15.5M
}
379
380
void
381
Tokenizer::inSpace(char ch)
382
763k
{
383
    // We only enter this state if include_ignorable is true.
384
763k
    if (!isSpace(ch)) {
385
526k
        type = tt::tt_space;
386
526k
        in_token = false;
387
526k
        char_to_unread = ch;
388
526k
        state = st_token_ready;
389
526k
    }
390
763k
}
391
392
void
393
Tokenizer::inComment(char ch)
394
6.86M
{
395
6.86M
    if ((ch == '\r') || (ch == '\n')) {
396
37.3k
        if (include_ignorable) {
397
1.87k
            type = tt::tt_comment;
398
1.87k
            in_token = false;
399
1.87k
            char_to_unread = ch;
400
1.87k
            state = st_token_ready;
401
35.4k
        } else {
402
35.4k
            state = st_before_token;
403
35.4k
        }
404
37.3k
    }
405
6.86M
}
406
407
void
408
Tokenizer::inString(char ch)
409
162M
{
410
162M
    switch (ch) {
411
211k
    case '\\':
412
211k
        state = st_string_escape;
413
211k
        return;
414
415
792k
    case '(':
416
792k
        val += ch;
417
792k
        ++string_depth;
418
792k
        return;
419
420
302k
    case ')':
421
302k
        if (--string_depth == 0) {
422
199k
            type = tt::tt_string;
423
199k
            state = st_token_ready;
424
199k
            return;
425
199k
        }
426
427
103k
        val += ch;
428
103k
        return;
429
430
156k
    case '\r':
431
        // CR by itself is converted to LF
432
156k
        val += '\n';
433
156k
        state = st_string_after_cr;
434
156k
        return;
435
436
3.40M
    case '\n':
437
3.40M
        val += ch;
438
3.40M
        return;
439
440
157M
    default:
441
157M
        val += ch;
442
157M
        return;
443
162M
    }
444
162M
}
445
446
void
447
Tokenizer::inName(char ch)
448
104M
{
449
104M
    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
3.14M
        type = bad ? tt::tt_bad : tt::tt_name;
457
3.14M
        in_token = false;
458
3.14M
        char_to_unread = ch;
459
3.14M
        state = st_token_ready;
460
101M
    } else if (ch == '#') {
461
36.3k
        char_code = 0;
462
36.3k
        state = st_name_hex1;
463
101M
    } else {
464
101M
        val += ch;
465
101M
    }
466
104M
}
467
468
void
469
Tokenizer::inNameHex1(char ch)
470
36.3k
{
471
36.3k
    hex_char = ch;
472
473
36.3k
    if (char hval = util::hex_decode_char(ch); hval < '\20') {
474
17.6k
        char_code = int(hval) << 4;
475
17.6k
        state = st_name_hex2;
476
18.7k
    } else {
477
18.7k
        QTC::TC("qpdf", "QPDFTokenizer bad name 1");
478
18.7k
        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
18.7k
        val += '\0';
481
18.7k
        state = st_name;
482
18.7k
        inName(ch);
483
18.7k
    }
484
36.3k
}
485
486
void
487
Tokenizer::inNameHex2(char ch)
488
17.6k
{
489
17.6k
    if (char hval = util::hex_decode_char(ch); hval < '\20') {
490
8.83k
        char_code |= int(hval);
491
8.83k
    } else {
492
8.82k
        QTC::TC("qpdf", "QPDFTokenizer bad name 2");
493
8.82k
        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
8.82k
        val += '\0';
496
8.82k
        val += hex_char;
497
8.82k
        state = st_name;
498
8.82k
        inName(ch);
499
8.82k
        return;
500
8.82k
    }
501
8.83k
    if (char_code == 0) {
502
1.00k
        QTC::TC("qpdf", "QPDFTokenizer null in name");
503
1.00k
        error_message = "null character not allowed in name token";
504
1.00k
        val += "#00";
505
1.00k
        state = st_name;
506
1.00k
        bad = true;
507
7.83k
    } else {
508
7.83k
        val += char(char_code);
509
7.83k
        state = st_name;
510
7.83k
    }
511
8.83k
}
512
513
void
514
Tokenizer::inSign(char ch)
515
109k
{
516
109k
    if (util::is_digit(ch)) {
517
96.9k
        state = st_number;
518
96.9k
    } else if (ch == '.') {
519
4.31k
        state = st_decimal;
520
7.78k
    } else {
521
7.78k
        state = st_literal;
522
7.78k
        inLiteral(ch);
523
7.78k
    }
524
109k
}
525
526
void
527
Tokenizer::inDecimal(char ch)
528
140k
{
529
140k
    if (util::is_digit(ch)) {
530
21.2k
        state = st_real;
531
119k
    } else {
532
119k
        state = st_literal;
533
119k
        inLiteral(ch);
534
119k
    }
535
140k
}
536
537
void
538
Tokenizer::inNumber(char ch)
539
12.9M
{
540
12.9M
    if (util::is_digit(ch)) {
541
6.72M
    } else if (ch == '.') {
542
309k
        state = st_real;
543
5.89M
    } else if (isDelimiter(ch)) {
544
5.79M
        type = tt::tt_integer;
545
5.79M
        state = st_token_ready;
546
5.79M
        in_token = false;
547
5.79M
        char_to_unread = ch;
548
5.79M
    } else {
549
105k
        state = st_literal;
550
105k
    }
551
12.9M
}
552
553
void
554
Tokenizer::inReal(char ch)
555
1.51M
{
556
1.51M
    if (util::is_digit(ch)) {
557
1.19M
    } else if (isDelimiter(ch)) {
558
311k
        type = tt::tt_real;
559
311k
        state = st_token_ready;
560
311k
        in_token = false;
561
311k
        char_to_unread = ch;
562
311k
    } else {
563
15.4k
        state = st_literal;
564
15.4k
    }
565
1.51M
}
566
void
567
Tokenizer::inStringEscape(char ch)
568
211k
{
569
211k
    state = st_in_string;
570
211k
    switch (ch) {
571
6.15k
    case '0':
572
6.64k
    case '1':
573
7.29k
    case '2':
574
7.86k
    case '3':
575
8.47k
    case '4':
576
8.91k
    case '5':
577
9.36k
    case '6':
578
9.68k
    case '7':
579
9.68k
        state = st_char_code;
580
9.68k
        char_code = 0;
581
9.68k
        digit_count = 0;
582
9.68k
        inCharCode(ch);
583
9.68k
        return;
584
585
2.78k
    case 'n':
586
2.78k
        val += '\n';
587
2.78k
        return;
588
589
5.70k
    case 'r':
590
5.70k
        val += '\r';
591
5.70k
        return;
592
593
73.4k
    case 't':
594
73.4k
        val += '\t';
595
73.4k
        return;
596
597
706
    case 'b':
598
706
        val += '\b';
599
706
        return;
600
601
66.0k
    case 'f':
602
66.0k
        val += '\f';
603
66.0k
        return;
604
605
649
    case '\n':
606
649
        return;
607
608
353
    case '\r':
609
353
        state = st_string_after_cr;
610
353
        return;
611
612
52.2k
    default:
613
        // PDF spec says backslash is ignored before anything else
614
52.2k
        val += ch;
615
52.2k
        return;
616
211k
    }
617
211k
}
618
619
void
620
Tokenizer::inStringAfterCR(char ch)
621
156k
{
622
156k
    state = st_in_string;
623
156k
    if (ch != '\n') {
624
74.9k
        inString(ch);
625
74.9k
    }
626
156k
}
627
628
void
629
Tokenizer::inLt(char ch)
630
600k
{
631
600k
    if (ch == '<') {
632
450k
        type = tt::tt_dict_open;
633
450k
        state = st_token_ready;
634
450k
        return;
635
450k
    }
636
637
149k
    state = st_in_hexstring;
638
149k
    inHexstring(ch);
639
149k
}
640
641
void
642
Tokenizer::inGt(char ch)
643
384k
{
644
384k
    if (ch == '>') {
645
340k
        type = tt::tt_dict_close;
646
340k
        state = st_token_ready;
647
340k
    } else {
648
44.0k
        type = tt::tt_bad;
649
44.0k
        QTC::TC("qpdf", "QPDFTokenizer bad >");
650
44.0k
        error_message = "unexpected >";
651
44.0k
        in_token = false;
652
44.0k
        char_to_unread = ch;
653
44.0k
        state = st_token_ready;
654
44.0k
    }
655
384k
}
656
657
void
658
Tokenizer::inLiteral(char ch)
659
114M
{
660
114M
    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
3.20M
        in_token = false;
667
3.20M
        char_to_unread = ch;
668
3.20M
        state = st_token_ready;
669
3.20M
        type = (raw_val == "true") || (raw_val == "false")
670
3.20M
            ? tt::tt_bool
671
3.20M
            : (raw_val == "null" ? tt::tt_null : tt::tt_word);
672
3.20M
    }
673
114M
}
674
675
void
676
Tokenizer::inHexstring(char ch)
677
988k
{
678
988k
    if (char hval = util::hex_decode_char(ch); hval < '\20') {
679
834k
        char_code = int(hval) << 4;
680
834k
        state = st_in_hexstring_2nd;
681
682
834k
    } else if (ch == '>') {
683
99.7k
        type = tt::tt_string;
684
99.7k
        state = st_token_ready;
685
686
99.7k
    } else if (isSpace(ch)) {
687
        // ignore
688
689
28.0k
    } else {
690
26.1k
        type = tt::tt_bad;
691
26.1k
        QTC::TC("qpdf", "QPDFTokenizer bad hexstring character");
692
26.1k
        error_message = std::string("invalid character (") + ch + ") in hexstring";
693
26.1k
        state = st_token_ready;
694
26.1k
    }
695
988k
}
696
697
void
698
Tokenizer::inHexstring2nd(char ch)
699
845k
{
700
845k
    if (char hval = util::hex_decode_char(ch); hval < '\20') {
701
811k
        val += char(char_code) | hval;
702
811k
        state = st_in_hexstring;
703
704
811k
    } else if (ch == '>') {
705
        // PDF spec says odd hexstrings have implicit trailing 0.
706
11.1k
        val += char(char_code);
707
11.1k
        type = tt::tt_string;
708
11.1k
        state = st_token_ready;
709
710
23.1k
    } else if (isSpace(ch)) {
711
        // ignore
712
713
14.9k
    } else {
714
8.27k
        type = tt::tt_bad;
715
8.27k
        QTC::TC("qpdf", "QPDFTokenizer bad hexstring 2nd character");
716
8.27k
        error_message = std::string("invalid character (") + ch + ") in hexstring";
717
8.27k
        state = st_token_ready;
718
8.27k
    }
719
845k
}
720
721
void
722
Tokenizer::inCharCode(char ch)
723
25.3k
{
724
25.3k
    bool handled = false;
725
25.3k
    if (('0' <= ch) && (ch <= '7')) {
726
21.5k
        char_code = 8 * char_code + (int(ch) - int('0'));
727
21.5k
        if (++(digit_count) < 3) {
728
15.8k
            return;
729
15.8k
        }
730
5.67k
        handled = true;
731
5.67k
    }
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
9.52k
    val += char(char_code % 256);
735
9.52k
    state = st_in_string;
736
9.52k
    if (!handled) {
737
3.85k
        inString(ch);
738
3.85k
    }
739
9.52k
}
740
741
void
742
Tokenizer::inInlineImage(char ch)
743
26.6M
{
744
26.6M
    if ((raw_val.length() + 1) == inline_image_bytes) {
745
1.84k
        QTC::TC("qpdf", "QPDFTokenizer found EI by byte count");
746
1.84k
        type = tt::tt_inline_image;
747
1.84k
        inline_image_bytes = 0;
748
1.84k
        state = st_token_ready;
749
1.84k
    }
750
26.6M
}
751
752
void
753
QPDFTokenizer::presentEOF()
754
0
{
755
0
    m->presentEOF();
756
0
}
757
758
void
759
Tokenizer::presentEOF()
760
85.4k
{
761
85.4k
    switch (state) {
762
2.68k
    case st_name:
763
2.82k
    case st_name_hex1:
764
2.90k
    case st_name_hex2:
765
9.26k
    case st_number:
766
9.48k
    case st_real:
767
9.65k
    case st_sign:
768
9.75k
    case st_decimal:
769
26.2k
    case st_literal:
770
26.2k
        QTC::TC("qpdf", "QPDFTokenizer EOF reading appendable token");
771
        // Push any delimiter to the state machine to finish off the final token.
772
26.2k
        presentCharacter('\f');
773
26.2k
        in_token = true;
774
26.2k
        break;
775
776
0
    case st_top:
777
45.2k
    case st_before_token:
778
45.2k
        type = tt::tt_eof;
779
45.2k
        break;
780
781
8.55k
    case st_in_space:
782
8.55k
        type = include_ignorable ? tt::tt_space : tt::tt_eof;
783
8.55k
        break;
784
785
1.08k
    case st_in_comment:
786
1.08k
        type = include_ignorable ? tt::tt_comment : tt::tt_bad;
787
1.08k
        break;
788
789
0
    case st_token_ready:
790
0
        break;
791
792
4.30k
    default:
793
4.30k
        QTC::TC("qpdf", "QPDFTokenizer EOF reading token");
794
4.30k
        type = tt::tt_bad;
795
4.30k
        error_message = "EOF while reading token";
796
85.4k
    }
797
85.4k
    state = st_token_ready;
798
85.4k
}
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
362
{
809
362
    m->expectInlineImage(input);
810
362
}
811
812
void
813
Tokenizer::expectInlineImage(InputSource& input)
814
1.97k
{
815
1.97k
    if (state == st_token_ready) {
816
1.61k
        reset();
817
1.61k
    } 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
1.97k
    findEI(input);
822
1.97k
    before_token = false;
823
1.97k
    in_token = true;
824
1.97k
    state = st_inline_image;
825
1.97k
}
826
827
void
828
Tokenizer::findEI(InputSource& input)
829
1.97k
{
830
1.97k
    qpdf_offset_t last_offset = input.getLastOffset();
831
1.97k
    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
1.97k
    bool okay = false;
841
1.97k
    bool first_try = true;
842
21.8k
    while (!okay) {
843
20.7k
        QPDFWordTokenFinder f(input, "EI");
844
20.7k
        if (!input.findFirst("EI", input.tell(), 0, f)) {
845
946
            break;
846
946
        }
847
19.8k
        inline_image_bytes = QIntC::to_size(input.tell() - pos - 2);
848
849
19.8k
        Tokenizer check;
850
19.8k
        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
42.7k
        for (int i = 0; i < 10; ++i) {
857
41.6k
            check.nextToken(input, "checker");
858
41.6k
            auto typ = check.getType();
859
41.6k
            if (typ == tt::tt_eof) {
860
0
                okay = true;
861
41.6k
            } else if (typ == tt::tt_bad) {
862
3.23k
                found_bad = true;
863
38.4k
            } 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
25.0k
                bool found_alpha = false;
870
25.0k
                bool found_non_printable = false;
871
25.0k
                bool found_other = false;
872
81.6k
                for (char ch: check.getValue()) {
873
81.6k
                    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
37.3k
                        found_alpha = true;
877
44.3k
                    } 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
6.71k
                        found_non_printable = true;
880
6.71k
                        break;
881
37.6k
                    } else {
882
37.6k
                        found_other = true;
883
37.6k
                    }
884
81.6k
                }
885
25.0k
                if (found_non_printable || (found_alpha && found_other)) {
886
15.5k
                    found_bad = true;
887
15.5k
                }
888
25.0k
            }
889
41.6k
            if (okay || found_bad) {
890
18.8k
                break;
891
18.8k
            }
892
41.6k
        }
893
19.8k
        if (!found_bad) {
894
1.03k
            okay = true;
895
1.03k
        }
896
19.8k
        if (!okay) {
897
18.8k
            first_try = false;
898
18.8k
        }
899
19.8k
    }
900
1.97k
    if (okay && (!first_try)) {
901
125
        QTC::TC("qpdf", "QPDFTokenizer found EI after more than one try");
902
125
    }
903
904
1.97k
    input.seek(pos, SEEK_SET);
905
1.97k
    input.setLastOffset(last_offset);
906
1.97k
}
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
5.03M
{
917
5.03M
    bool ready = (state == st_token_ready);
918
5.03M
    unread_char = !in_token && !before_token;
919
5.03M
    ch = char_to_unread;
920
5.03M
    if (ready) {
921
5.03M
        token = (!(type == tt::tt_name || type == tt::tt_string))
922
5.03M
            ? Token(type, raw_val, raw_val, error_message)
923
5.03M
            : Token(type, val, raw_val, error_message);
924
925
5.03M
        reset();
926
5.03M
    }
927
5.03M
    return ready;
928
5.03M
}
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
1.20M
{
946
1.20M
    return m->readToken(input, context, allow_bad, max_len);
947
1.20M
}
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
5.03M
{
959
5.03M
    nextToken(input, context, max_len);
960
961
5.03M
    Token token;
962
5.03M
    bool unread_char;
963
5.03M
    char char_to_unread;
964
5.03M
    getToken(token, unread_char, char_to_unread);
965
966
5.03M
    if (token.getType() == tt::tt_bad) {
967
259k
        if (allow_bad) {
968
259k
            QTC::TC("qpdf", "QPDFTokenizer allowing bad token");
969
259k
        } 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
259k
    }
978
5.03M
    return token;
979
5.03M
}
980
981
bool
982
Tokenizer::nextToken(InputSource& input, std::string const& context, size_t max_len)
983
16.0M
{
984
16.0M
    if (state != st_inline_image) {
985
16.0M
        reset();
986
16.0M
    }
987
16.0M
    qpdf_offset_t offset = input.fastTell();
988
989
482M
    while (state != st_token_ready) {
990
466M
        char ch;
991
466M
        if (!input.fastRead(ch)) {
992
85.4k
            presentEOF();
993
994
85.4k
            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
115
                type = tt::tt_bad;
998
115
                error_message = "unexpected EOF";
999
115
                offset = input.getLastOffset();
1000
115
            }
1001
466M
        } else {
1002
466M
            handleCharacter(ch);
1003
466M
            if (before_token) {
1004
19.1M
                ++offset;
1005
19.1M
            }
1006
466M
            if (in_token) {
1007
434M
                raw_val += ch;
1008
434M
            }
1009
466M
            if (max_len && (raw_val.length() >= max_len) && (state != st_token_ready)) {
1010
                // terminate this token now
1011
1.23M
                QTC::TC("qpdf", "QPDFTokenizer block long token");
1012
1.23M
                type = tt::tt_bad;
1013
1.23M
                state = st_token_ready;
1014
1.23M
                error_message = "exceeded allowable length while reading token";
1015
1.23M
            }
1016
466M
        }
1017
466M
    }
1018
1019
16.0M
    input.fastUnread(!in_token && !before_token);
1020
1021
16.0M
    if (type != tt::tt_eof) {
1022
16.0M
        input.setLastOffset(offset);
1023
16.0M
    }
1024
1025
16.0M
    return error_message.empty();
1026
16.0M
}