Coverage Report

Created: 2026-07-12 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/Lexer.cc
Line
Count
Source
1
//========================================================================
2
//
3
// Lexer.cc
4
//
5
// Copyright 1996-2003 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stdlib.h>
12
#include <stddef.h>
13
#include <string.h>
14
#include <ctype.h>
15
#include "gmempp.h"
16
#include "Lexer.h"
17
#include "Error.h"
18
19
//------------------------------------------------------------------------
20
21
// A '1' in this array means the character is white space.  A '1' or
22
// '2' means the character ends a name or command.
23
static char specialChars[256] = {
24
  1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0,   // 0x
25
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   // 1x
26
  1, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2,   // 2x
27
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0,   // 3x
28
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   // 4x
29
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0,   // 5x
30
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   // 6x
31
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0,   // 7x
32
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   // 8x
33
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   // 9x
34
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   // ax
35
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   // bx
36
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   // cx
37
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   // dx
38
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   // ex
39
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    // fx
40
};
41
42
//------------------------------------------------------------------------
43
// Lexer
44
//------------------------------------------------------------------------
45
46
350k
Lexer::Lexer(XRef *xref, Stream *str) {
47
350k
  Object obj;
48
49
350k
  curStr.initStream(str);
50
350k
  streams = new Array(xref);
51
350k
  streams->add(curStr.copy(&obj));
52
350k
  strPtr = 0;
53
350k
  freeArray = gTrue;
54
350k
  curStr.streamReset();
55
350k
}
56
57
0
Lexer::Lexer(XRef *xref, Object *obj) {
58
0
  Object obj2;
59
60
0
  if (obj->isStream()) {
61
0
    streams = new Array(xref);
62
0
    freeArray = gTrue;
63
0
    streams->add(obj->copy(&obj2));
64
0
  } else {
65
0
    streams = obj->getArray();
66
0
    freeArray = gFalse;
67
0
  }
68
0
  strPtr = 0;
69
0
  if (streams->getLength() > 0) {
70
0
    streams->get(strPtr, &curStr);
71
0
    curStr.streamReset();
72
0
  }
73
0
}
74
75
350k
Lexer::~Lexer() {
76
350k
  if (!curStr.isNone()) {
77
108k
    curStr.streamClose();
78
108k
    curStr.free();
79
108k
  }
80
350k
  if (freeArray) {
81
350k
    delete streams;
82
350k
  }
83
350k
}
84
85
1.95G
int Lexer::getChar() {
86
1.95G
  int c;
87
88
1.95G
  c = EOF;
89
1.95G
  while (!curStr.isNone() && (c = curStr.streamGetChar()) == EOF) {
90
241k
    curStr.streamClose();
91
241k
    curStr.free();
92
241k
    ++strPtr;
93
241k
    if (strPtr < streams->getLength()) {
94
0
      streams->get(strPtr, &curStr);
95
0
      curStr.streamReset();
96
0
    }
97
241k
  }
98
1.95G
  return c;
99
1.95G
}
100
101
289M
int Lexer::lookChar() {
102
289M
  if (curStr.isNone()) {
103
2.66k
    return EOF;
104
2.66k
  }
105
289M
  return curStr.streamLookChar();
106
289M
}
107
108
165M
Object *Lexer::getObj(Object *obj) {
109
165M
  char *p;
110
165M
  int c, c2;
111
165M
  GBool comment, neg, doubleMinus, done, invalid;
112
165M
  int numParen, nErrors;
113
165M
  int xi;
114
165M
  double xf, scale;
115
165M
  GString *s;
116
165M
  int n, m;
117
118
  // skip whitespace and comments
119
165M
  comment = gFalse;
120
912M
  while (1) {
121
912M
    if ((c = getChar()) == EOF) {
122
121M
      return obj->initEOF();
123
121M
    }
124
790M
    if (comment) {
125
19.6M
      if (c == '\r' || c == '\n')
126
306k
  comment = gFalse;
127
771M
    } else if (c == '%') {
128
309k
      comment = gTrue;
129
771M
    } else if (specialChars[c] != 1) {
130
44.1M
      break;
131
44.1M
    }
132
790M
  }
133
134
  // start reading token
135
44.1M
  switch (c) {
136
137
  // number
138
4.48M
  case '0': case '1': case '2': case '3': case '4':
139
5.93M
  case '5': case '6': case '7': case '8': case '9':
140
6.44M
  case '+': case '-': case '.':
141
    // Adobe's number lexer has some "interesting" behavior:
142
    // "--123" is interpreted as 0
143
    // "--123.4" is interpreted as -123.4 [I've seen this in the wild]
144
    // "50-100" is interpreted as 50 [I've seen this in the wild]
145
    // "50--100" is interpreted as 50
146
    // "50-100.0" is an error -- but older versions of Acrobat may
147
    //   have interpreted it as 50100.0 (?)
148
    // "50--100.0" is an error -- but older versions of Acrobat may
149
    //   have interpreted it as 50100.0 (?)
150
    // "50.0-100" is interpreted as 50.0 (or maybe 50.0100?)
151
    // "50.0--100" is interpreted as 50.0 (or maybe 50.0100?)
152
    // "-50-100" is interpreted as -50
153
    // "-" is interpreted as 0
154
    // "-." is interpreted as 0.0
155
6.44M
    neg = gFalse;
156
6.44M
    doubleMinus = gFalse;
157
6.44M
    xf = xi = 0;
158
6.44M
    if (c == '+') {
159
      // just ignore it
160
6.40M
    } else if (c == '-') {
161
259k
      neg = gTrue;
162
259k
      if (lookChar() == '-') {
163
11.4k
  doubleMinus = gTrue;
164
18.3k
  do {
165
18.3k
    getChar();
166
18.3k
  } while (lookChar() == '-');
167
11.4k
      }
168
6.14M
    } else if (c == '.') {
169
211k
      goto doReal;
170
5.93M
    } else {
171
5.93M
      xf = xi = c - '0';
172
5.93M
    }
173
13.0M
    while (1) {
174
13.0M
      c = lookChar();
175
13.0M
      if (isdigit(c)) {
176
6.82M
  getChar();
177
6.82M
  xi = xi * 10 + (c - '0');
178
6.82M
  if (xf < 1e20) {
179
6.45M
    xf = xf * 10 + (c - '0');
180
6.45M
  }
181
6.82M
      } else if (c == '.') {
182
304k
  getChar();
183
304k
  goto doReal;
184
5.92M
      } else {
185
5.92M
  break;
186
5.92M
      }
187
13.0M
    }
188
5.93M
    while ((c = lookChar()) == '-' || isdigit(c)) {
189
10.8k
      getChar();
190
10.8k
    }
191
5.92M
    if (neg) {
192
253k
      xi = -xi;
193
253k
    }
194
5.92M
    if (doubleMinus) {
195
11.1k
      xi = 0;
196
11.1k
    }
197
5.92M
    obj->initInt(xi);
198
5.92M
    break;
199
515k
  doReal:
200
515k
    scale = 0.1;
201
984k
    while (1) {
202
984k
      c = lookChar();
203
984k
      if (c == '-') {
204
19.2k
  error(errSyntaxWarning, getPos(), "Badly formatted number");
205
19.2k
  getChar();
206
19.2k
  continue;
207
19.2k
      }
208
965k
      if (!isdigit(c)) {
209
515k
  break;
210
515k
      }
211
449k
      getChar();
212
449k
      xf = xf + scale * (c - '0');
213
449k
      scale *= 0.1;
214
449k
    }
215
515k
    while ((c = lookChar()) == '-' || isdigit(c)) {
216
0
      getChar();
217
0
    }
218
515k
    if (neg) {
219
5.40k
      xf = -xf;
220
5.40k
    }
221
515k
    obj->initReal(xf);
222
515k
    break;
223
224
  // string
225
181k
  case '(':
226
181k
    p = tokBuf;
227
181k
    n = 0;
228
181k
    numParen = 1;
229
181k
    done = gFalse;
230
181k
    s = NULL;
231
742M
    do {
232
742M
      c2 = EOF;
233
742M
      switch (c = getChar()) {
234
235
11.6k
      case EOF:
236
11.6k
  error(errSyntaxError, getPos(), "Unterminated string");
237
11.6k
  done = gTrue;
238
11.6k
  break;
239
240
2.76M
      case '(':
241
2.76M
  ++numParen;
242
2.76M
  c2 = c;
243
2.76M
  break;
244
245
1.61M
      case ')':
246
1.61M
  if (--numParen == 0) {
247
169k
    done = gTrue;
248
1.44M
  } else {
249
1.44M
    c2 = c;
250
1.44M
  }
251
1.61M
  break;
252
253
6.02M
      case '\r':
254
  // The PDF spec says that any literal end-of-line sequence
255
  // (LF, CR, CR+LF) is translated to a single LF char.
256
6.02M
  c = lookChar();
257
6.02M
  if (c == '\n') {
258
108k
    getChar();
259
108k
  }
260
6.02M
  c2 = '\n';
261
6.02M
  break;
262
263
1.06M
      case '\\':
264
1.06M
  switch (c = getChar()) {
265
5.59k
  case 'n':
266
5.59k
    c2 = '\n';
267
5.59k
    break;
268
15.0k
  case 'r':
269
15.0k
    c2 = '\r';
270
15.0k
    break;
271
25.4k
  case 't':
272
25.4k
    c2 = '\t';
273
25.4k
    break;
274
7.32k
  case 'b':
275
7.32k
    c2 = '\b';
276
7.32k
    break;
277
5.50k
  case 'f':
278
5.50k
    c2 = '\f';
279
5.50k
    break;
280
3.69k
  case '\\':
281
12.9k
  case '(':
282
21.1k
  case ')':
283
21.1k
    c2 = c;
284
21.1k
    break;
285
19.6k
  case '0': case '1': case '2': case '3':
286
53.5k
  case '4': case '5': case '6': case '7':
287
53.5k
    c2 = c - '0';
288
53.5k
    c = lookChar();
289
53.5k
    if (c >= '0' && c <= '7') {
290
7.52k
      getChar();
291
7.52k
      c2 = (c2 << 3) + (c - '0');
292
7.52k
      c = lookChar();
293
7.52k
      if (c >= '0' && c <= '7') {
294
680
        getChar();
295
680
        c2 = (c2 << 3) + (c - '0');
296
680
      }
297
7.52k
    }
298
53.5k
    break;
299
4.99k
  case '\r':
300
4.99k
    c = lookChar();
301
4.99k
    if (c == '\n') {
302
1.17k
      getChar();
303
1.17k
    }
304
4.99k
    break;
305
10.4k
  case '\n':
306
10.4k
    break;
307
580
  case EOF:
308
580
    error(errSyntaxError, getPos(), "Unterminated string");
309
580
    done = gTrue;
310
580
    break;
311
914k
  default:
312
914k
    c2 = c;
313
914k
    break;
314
1.06M
  }
315
1.06M
  break;
316
317
731M
      default:
318
731M
  c2 = c;
319
731M
  break;
320
742M
      }
321
322
742M
      if (c2 != EOF) {
323
742M
  if (n == tokBufSize) {
324
5.73M
    if (!s)
325
64.3k
      s = new GString(tokBuf, tokBufSize);
326
5.67M
    else
327
5.67M
      s->append(tokBuf, tokBufSize);
328
5.73M
    p = tokBuf;
329
5.73M
    n = 0;
330
5.73M
  }
331
742M
  *p++ = (char)c2;
332
742M
  ++n;
333
742M
      }
334
742M
    } while (!done);
335
181k
    if (!s)
336
117k
      s = new GString(tokBuf, n);
337
64.3k
    else
338
64.3k
      s->append(tokBuf, n);
339
181k
    obj->initString(s);
340
181k
    break;
341
342
  // name
343
6.28M
  case '/':
344
6.28M
    p = tokBuf;
345
6.28M
    n = 0;
346
6.28M
    s = NULL;
347
6.28M
    invalid = gFalse;
348
25.4M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
349
19.1M
      getChar();
350
19.1M
      if (c == '#') {
351
822k
  c2 = lookChar();
352
822k
  if (c2 >= '0' && c2 <= '9') {
353
1.32k
    c = c2 - '0';
354
821k
  } else if (c2 >= 'A' && c2 <= 'F') {
355
47.0k
    c = c2 - 'A' + 10;
356
774k
  } else if (c2 >= 'a' && c2 <= 'f') {
357
98.5k
    c = c2 - 'a' + 10;
358
675k
  } else {
359
675k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
360
675k
    goto notEscChar;
361
675k
  }
362
146k
  getChar();
363
146k
  c2 = lookChar();
364
146k
  if (c2 >= '0' && c2 <= '9') {
365
27.1k
    c = (c << 4) + (c2 - '0');
366
119k
  } else if (c2 >= 'A' && c2 <= 'F') {
367
46.6k
    c = (c << 4) + (c2 - 'A' + 10);
368
73.1k
  } else if (c2 >= 'a' && c2 <= 'f') {
369
822
    c = (c << 4) + (c2 - 'a' + 10);
370
72.3k
  } else {
371
72.3k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
372
72.3k
    goto notEscChar;
373
72.3k
  }
374
74.5k
  getChar();
375
74.5k
  if (c == 0) {
376
397
    invalid = gTrue;
377
397
  }
378
74.5k
      }
379
19.1M
     notEscChar:
380
      // the PDF spec claims that names are limited to 127 chars, but
381
      // Distiller 8 will produce longer names, and Acrobat 8 will
382
      // accept longer names
383
19.1M
      ++n;
384
19.1M
      if (n < tokBufSize) {
385
18.8M
  *p++ = (char)c;
386
18.8M
      } else if (n == tokBufSize) {
387
1.64k
  *p = (char)c;
388
1.64k
  s = new GString(tokBuf, n);
389
281k
      } else {
390
281k
  s->append((char)c);
391
281k
      }
392
19.1M
    }
393
6.28M
    if (invalid) {
394
397
      error(errSyntaxError, getPos(), "Null character in name");
395
397
      obj->initError();
396
397
      if (s) {
397
245
  delete s;
398
245
      }
399
6.28M
    } else if (n < tokBufSize) {
400
6.28M
      *p = '\0';
401
6.28M
      obj->initName(tokBuf);
402
6.28M
    } else {
403
1.40k
      obj->initName(s->getCString());
404
1.40k
      delete s;
405
1.40k
    }
406
6.28M
    break;
407
408
  // array punctuation
409
5.27M
  case '[':
410
5.77M
  case ']':
411
5.77M
    tokBuf[0] = (char)c;
412
5.77M
    tokBuf[1] = '\0';
413
5.77M
    obj->initCmd(tokBuf);
414
5.77M
    break;
415
416
  // hex string or dict punctuation
417
3.83M
  case '<':
418
3.83M
    c = lookChar();
419
420
    // dict punctuation
421
3.83M
    if (c == '<') {
422
3.11M
      getChar();
423
3.11M
      tokBuf[0] = tokBuf[1] = '<';
424
3.11M
      tokBuf[2] = '\0';
425
3.11M
      obj->initCmd(tokBuf);
426
427
    // hex string
428
3.11M
    } else {
429
728k
      p = tokBuf;
430
728k
      m = n = 0;
431
728k
      c2 = 0;
432
728k
      s = NULL;
433
728k
      nErrors = 0;
434
55.5M
      while (nErrors < 100) {
435
55.3M
  c = getChar();
436
55.3M
  if (c == '>') {
437
560k
    break;
438
54.8M
  } else if (c == EOF) {
439
2.64k
    error(errSyntaxError, getPos(), "Unterminated hex string");
440
2.64k
    break;
441
54.8M
  } else if (specialChars[c] != 1) {
442
44.9M
    c2 = c2 << 4;
443
44.9M
    if (c >= '0' && c <= '9') {
444
4.96M
      c2 += c - '0';
445
40.0M
    } else if (c >= 'A' && c <= 'F') {
446
718k
      c2 += c - 'A' + 10;
447
39.2M
    } else if (c >= 'a' && c <= 'f') {
448
6.09M
      c2 += c - 'a' + 10;
449
33.1M
    } else {
450
33.1M
      error(errSyntaxError, getPos(),
451
33.1M
      "Illegal character <{0:02x}> in hex string", c);
452
33.1M
      ++nErrors;
453
33.1M
    }
454
44.9M
    if (++m == 2) {
455
22.3M
      if (n == tokBufSize) {
456
3.74k
        if (!s)
457
1.98k
    s = new GString(tokBuf, tokBufSize);
458
1.76k
        else
459
1.76k
    s->append(tokBuf, tokBufSize);
460
3.74k
        p = tokBuf;
461
3.74k
        n = 0;
462
3.74k
      }
463
22.3M
      *p++ = (char)c2;
464
22.3M
      ++n;
465
22.3M
      c2 = 0;
466
22.3M
      m = 0;
467
22.3M
    }
468
44.9M
  }
469
55.3M
      }
470
728k
      if (!s)
471
726k
  s = new GString(tokBuf, n);
472
1.98k
      else
473
1.98k
  s->append(tokBuf, n);
474
728k
      if (m == 1)
475
328k
  s->append((char)(c2 << 4));
476
728k
      obj->initString(s);
477
728k
    }
478
3.83M
    break;
479
480
  // dict punctuation
481
2.85M
  case '>':
482
2.85M
    c = lookChar();
483
2.85M
    if (c == '>') {
484
1.76M
      getChar();
485
1.76M
      tokBuf[0] = tokBuf[1] = '>';
486
1.76M
      tokBuf[2] = '\0';
487
1.76M
      obj->initCmd(tokBuf);
488
1.76M
    } else {
489
1.08M
      error(errSyntaxError, getPos(), "Illegal character '>'");
490
1.08M
      obj->initError();
491
1.08M
    }
492
2.85M
    break;
493
494
  // error
495
842k
  case ')':
496
1.04M
  case '{':
497
1.18M
  case '}':
498
1.18M
    error(errSyntaxError, getPos(), "Illegal character '{0:c}'", c);
499
1.18M
    obj->initError();
500
1.18M
    break;
501
502
  // command
503
17.5M
  default:
504
17.5M
    p = tokBuf;
505
17.5M
    *p++ = (char)c;
506
17.5M
    n = 1;
507
229M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
508
213M
      getChar();
509
213M
      if (++n == tokBufSize) {
510
1.00M
  error(errSyntaxError, getPos(), "Command token too long");
511
1.00M
  break;
512
1.00M
      }
513
212M
      *p++ = (char)c;
514
212M
    }
515
17.5M
    *p = '\0';
516
17.5M
    if (tokBuf[0] == 't' && !strcmp(tokBuf, "true")) {
517
67.7k
      obj->initBool(gTrue);
518
17.5M
    } else if (tokBuf[0] == 'f' && !strcmp(tokBuf, "false")) {
519
1.82k
      obj->initBool(gFalse);
520
17.5M
    } else if (tokBuf[0] == 'n' && !strcmp(tokBuf, "null")) {
521
1.41k
      obj->initNull();
522
17.5M
    } else {
523
17.5M
      obj->initCmd(tokBuf);
524
17.5M
    }
525
17.5M
    break;
526
44.1M
  }
527
528
44.1M
  return obj;
529
44.1M
}
530
531
94.3k
void Lexer::skipToNextLine() {
532
94.3k
  int c;
533
534
410k
  while (1) {
535
410k
    c = getChar();
536
410k
    if (c == EOF || c == '\n') {
537
36.0k
      return;
538
36.0k
    }
539
374k
    if (c == '\r') {
540
58.2k
      if ((c = lookChar()) == '\n') {
541
53.3k
  getChar();
542
53.3k
      }
543
58.2k
      return;
544
58.2k
    }
545
374k
  }
546
94.3k
}
547
548
35.3k
void Lexer::skipToEOF() {
549
2.09M
  while (getChar() != EOF) ;
550
35.3k
}
551
552
42.9M
GBool Lexer::isSpace(int c) {
553
42.9M
  return c >= 0 && c <= 0xff && specialChars[c] == 1;
554
42.9M
}