Coverage Report

Created: 2026-08-13 06:56

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
479k
Lexer::Lexer(XRef *xref, Stream *str) {
47
479k
  Object obj;
48
49
479k
  curStr.initStream(str);
50
479k
  streams = new Array(xref);
51
479k
  streams->add(curStr.copy(&obj));
52
479k
  strPtr = 0;
53
479k
  freeArray = gTrue;
54
479k
  curStr.streamReset();
55
479k
}
56
57
205k
Lexer::Lexer(XRef *xref, Object *obj) {
58
205k
  Object obj2;
59
60
205k
  if (obj->isStream()) {
61
204k
    streams = new Array(xref);
62
204k
    freeArray = gTrue;
63
204k
    streams->add(obj->copy(&obj2));
64
204k
  } else {
65
975
    streams = obj->getArray();
66
975
    freeArray = gFalse;
67
975
  }
68
205k
  strPtr = 0;
69
205k
  if (streams->getLength() > 0) {
70
205k
    streams->get(strPtr, &curStr);
71
205k
    curStr.streamReset();
72
205k
  }
73
205k
}
74
75
684k
Lexer::~Lexer() {
76
684k
  if (!curStr.isNone()) {
77
323k
    curStr.streamClose();
78
323k
    curStr.free();
79
323k
  }
80
684k
  if (freeArray) {
81
683k
    delete streams;
82
683k
  }
83
684k
}
84
85
1.07G
int Lexer::getChar() {
86
1.07G
  int c;
87
88
1.07G
  c = EOF;
89
1.07G
  while (!curStr.isNone() && (c = curStr.streamGetChar()) == EOF) {
90
362k
    curStr.streamClose();
91
362k
    curStr.free();
92
362k
    ++strPtr;
93
362k
    if (strPtr < streams->getLength()) {
94
1.29k
      streams->get(strPtr, &curStr);
95
1.29k
      curStr.streamReset();
96
1.29k
    }
97
362k
  }
98
1.07G
  return c;
99
1.07G
}
100
101
314M
int Lexer::lookChar() {
102
314M
  if (curStr.isNone()) {
103
150
    return EOF;
104
150
  }
105
314M
  return curStr.streamLookChar();
106
314M
}
107
108
209M
Object *Lexer::getObj(Object *obj) {
109
209M
  char *p;
110
209M
  int c, c2;
111
209M
  GBool comment, neg, doubleMinus, done, invalid;
112
209M
  int numParen, nErrors;
113
209M
  int xi;
114
209M
  double xf, scale;
115
209M
  GString *s;
116
209M
  int n, m;
117
118
  // skip whitespace and comments
119
209M
  comment = gFalse;
120
627M
  while (1) {
121
627M
    if ((c = getChar()) == EOF) {
122
123M
      return obj->initEOF();
123
123M
    }
124
503M
    if (comment) {
125
38.9M
      if (c == '\r' || c == '\n')
126
480k
  comment = gFalse;
127
464M
    } else if (c == '%') {
128
481k
      comment = gTrue;
129
464M
    } else if (specialChars[c] != 1) {
130
85.6M
      break;
131
85.6M
    }
132
503M
  }
133
134
  // start reading token
135
85.6M
  switch (c) {
136
137
  // number
138
19.7M
  case '0': case '1': case '2': case '3': case '4':
139
29.8M
  case '5': case '6': case '7': case '8': case '9':
140
32.2M
  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
32.2M
    neg = gFalse;
156
32.2M
    doubleMinus = gFalse;
157
32.2M
    xf = xi = 0;
158
32.2M
    if (c == '+') {
159
      // just ignore it
160
32.2M
    } else if (c == '-') {
161
1.60M
      neg = gTrue;
162
1.60M
      if (lookChar() == '-') {
163
8.48k
  doubleMinus = gTrue;
164
12.4k
  do {
165
12.4k
    getChar();
166
12.4k
  } while (lookChar() == '-');
167
8.48k
      }
168
30.6M
    } else if (c == '.') {
169
821k
      goto doReal;
170
29.8M
    } else {
171
29.8M
      xf = xi = c - '0';
172
29.8M
    }
173
79.4M
    while (1) {
174
79.4M
      c = lookChar();
175
79.4M
      if (isdigit(c)) {
176
47.9M
  getChar();
177
47.9M
  xi = xi * 10 + (c - '0');
178
47.9M
  if (xf < 1e20) {
179
40.3M
    xf = xf * 10 + (c - '0');
180
40.3M
  }
181
47.9M
      } else if (c == '.') {
182
4.87M
  getChar();
183
4.87M
  goto doReal;
184
26.5M
      } else {
185
26.5M
  break;
186
26.5M
      }
187
79.4M
    }
188
26.8M
    while ((c = lookChar()) == '-' || isdigit(c)) {
189
355k
      getChar();
190
355k
    }
191
26.5M
    if (neg) {
192
798k
      xi = -xi;
193
798k
    }
194
26.5M
    if (doubleMinus) {
195
5.94k
      xi = 0;
196
5.94k
    }
197
26.5M
    obj->initInt(xi);
198
26.5M
    break;
199
5.70M
  doReal:
200
5.70M
    scale = 0.1;
201
18.4M
    while (1) {
202
18.4M
      c = lookChar();
203
18.4M
      if (c == '-') {
204
28.3k
  error(errSyntaxWarning, getPos(), "Badly formatted number");
205
28.3k
  getChar();
206
28.3k
  continue;
207
28.3k
      }
208
18.4M
      if (!isdigit(c)) {
209
5.70M
  break;
210
5.70M
      }
211
12.7M
      getChar();
212
12.7M
      xf = xf + scale * (c - '0');
213
12.7M
      scale *= 0.1;
214
12.7M
    }
215
5.70M
    while ((c = lookChar()) == '-' || isdigit(c)) {
216
0
      getChar();
217
0
    }
218
5.70M
    if (neg) {
219
806k
      xf = -xf;
220
806k
    }
221
5.70M
    obj->initReal(xf);
222
5.70M
    break;
223
224
  // string
225
671k
  case '(':
226
671k
    p = tokBuf;
227
671k
    n = 0;
228
671k
    numParen = 1;
229
671k
    done = gFalse;
230
671k
    s = NULL;
231
186M
    do {
232
186M
      c2 = EOF;
233
186M
      switch (c = getChar()) {
234
235
17.4k
      case EOF:
236
17.4k
  error(errSyntaxError, getPos(), "Unterminated string");
237
17.4k
  done = gTrue;
238
17.4k
  break;
239
240
1.04M
      case '(':
241
1.04M
  ++numParen;
242
1.04M
  c2 = c;
243
1.04M
  break;
244
245
1.30M
      case ')':
246
1.30M
  if (--numParen == 0) {
247
653k
    done = gTrue;
248
656k
  } else {
249
656k
    c2 = c;
250
656k
  }
251
1.30M
  break;
252
253
4.13M
      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
4.13M
  c = lookChar();
257
4.13M
  if (c == '\n') {
258
83.7k
    getChar();
259
83.7k
  }
260
4.13M
  c2 = '\n';
261
4.13M
  break;
262
263
476k
      case '\\':
264
476k
  switch (c = getChar()) {
265
4.80k
  case 'n':
266
4.80k
    c2 = '\n';
267
4.80k
    break;
268
69.6k
  case 'r':
269
69.6k
    c2 = '\r';
270
69.6k
    break;
271
3.39k
  case 't':
272
3.39k
    c2 = '\t';
273
3.39k
    break;
274
2.08k
  case 'b':
275
2.08k
    c2 = '\b';
276
2.08k
    break;
277
1.03k
  case 'f':
278
1.03k
    c2 = '\f';
279
1.03k
    break;
280
41.4k
  case '\\':
281
76.3k
  case '(':
282
109k
  case ')':
283
109k
    c2 = c;
284
109k
    break;
285
97.7k
  case '0': case '1': case '2': case '3':
286
117k
  case '4': case '5': case '6': case '7':
287
117k
    c2 = c - '0';
288
117k
    c = lookChar();
289
117k
    if (c >= '0' && c <= '7') {
290
93.2k
      getChar();
291
93.2k
      c2 = (c2 << 3) + (c - '0');
292
93.2k
      c = lookChar();
293
93.2k
      if (c >= '0' && c <= '7') {
294
79.6k
        getChar();
295
79.6k
        c2 = (c2 << 3) + (c - '0');
296
79.6k
      }
297
93.2k
    }
298
117k
    break;
299
2.85k
  case '\r':
300
2.85k
    c = lookChar();
301
2.85k
    if (c == '\n') {
302
23
      getChar();
303
23
    }
304
2.85k
    break;
305
1.50k
  case '\n':
306
1.50k
    break;
307
287
  case EOF:
308
287
    error(errSyntaxError, getPos(), "Unterminated string");
309
287
    done = gTrue;
310
287
    break;
311
163k
  default:
312
163k
    c2 = c;
313
163k
    break;
314
476k
  }
315
476k
  break;
316
317
179M
      default:
318
179M
  c2 = c;
319
179M
  break;
320
186M
      }
321
322
186M
      if (c2 != EOF) {
323
186M
  if (n == tokBufSize) {
324
1.36M
    if (!s)
325
54.9k
      s = new GString(tokBuf, tokBufSize);
326
1.30M
    else
327
1.30M
      s->append(tokBuf, tokBufSize);
328
1.36M
    p = tokBuf;
329
1.36M
    n = 0;
330
1.36M
  }
331
186M
  *p++ = (char)c2;
332
186M
  ++n;
333
186M
      }
334
186M
    } while (!done);
335
671k
    if (!s)
336
616k
      s = new GString(tokBuf, n);
337
54.9k
    else
338
54.9k
      s->append(tokBuf, n);
339
671k
    obj->initString(s);
340
671k
    break;
341
342
  // name
343
5.85M
  case '/':
344
5.85M
    p = tokBuf;
345
5.85M
    n = 0;
346
5.85M
    s = NULL;
347
5.85M
    invalid = gFalse;
348
43.4M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
349
37.6M
      getChar();
350
37.6M
      if (c == '#') {
351
49.1k
  c2 = lookChar();
352
49.1k
  if (c2 >= '0' && c2 <= '9') {
353
3.61k
    c = c2 - '0';
354
45.5k
  } else if (c2 >= 'A' && c2 <= 'F') {
355
3.84k
    c = c2 - 'A' + 10;
356
41.7k
  } else if (c2 >= 'a' && c2 <= 'f') {
357
3.89k
    c = c2 - 'a' + 10;
358
37.8k
  } else {
359
37.8k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
360
37.8k
    goto notEscChar;
361
37.8k
  }
362
11.3k
  getChar();
363
11.3k
  c2 = lookChar();
364
11.3k
  if (c2 >= '0' && c2 <= '9') {
365
1.72k
    c = (c << 4) + (c2 - '0');
366
9.62k
  } else if (c2 >= 'A' && c2 <= 'F') {
367
281
    c = (c << 4) + (c2 - 'A' + 10);
368
9.34k
  } else if (c2 >= 'a' && c2 <= 'f') {
369
4.92k
    c = (c << 4) + (c2 - 'a' + 10);
370
4.92k
  } else {
371
4.41k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
372
4.41k
    goto notEscChar;
373
4.41k
  }
374
6.93k
  getChar();
375
6.93k
  if (c == 0) {
376
250
    invalid = gTrue;
377
250
  }
378
6.93k
      }
379
37.6M
     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
37.6M
      ++n;
384
37.6M
      if (n < tokBufSize) {
385
32.4M
  *p++ = (char)c;
386
32.4M
      } else if (n == tokBufSize) {
387
11.2k
  *p = (char)c;
388
11.2k
  s = new GString(tokBuf, n);
389
5.19M
      } else {
390
5.19M
  s->append((char)c);
391
5.19M
      }
392
37.6M
    }
393
5.85M
    if (invalid) {
394
248
      error(errSyntaxError, getPos(), "Null character in name");
395
248
      obj->initError();
396
248
      if (s) {
397
15
  delete s;
398
15
      }
399
5.85M
    } else if (n < tokBufSize) {
400
5.84M
      *p = '\0';
401
5.84M
      obj->initName(tokBuf);
402
5.84M
    } else {
403
11.2k
      obj->initName(s->getCString());
404
11.2k
      delete s;
405
11.2k
    }
406
5.85M
    break;
407
408
  // array punctuation
409
5.30M
  case '[':
410
5.87M
  case ']':
411
5.87M
    tokBuf[0] = (char)c;
412
5.87M
    tokBuf[1] = '\0';
413
5.87M
    obj->initCmd(tokBuf);
414
5.87M
    break;
415
416
  // hex string or dict punctuation
417
15.6M
  case '<':
418
15.6M
    c = lookChar();
419
420
    // dict punctuation
421
15.6M
    if (c == '<') {
422
958k
      getChar();
423
958k
      tokBuf[0] = tokBuf[1] = '<';
424
958k
      tokBuf[2] = '\0';
425
958k
      obj->initCmd(tokBuf);
426
427
    // hex string
428
14.7M
    } else {
429
14.7M
      p = tokBuf;
430
14.7M
      m = n = 0;
431
14.7M
      c2 = 0;
432
14.7M
      s = NULL;
433
14.7M
      nErrors = 0;
434
53.3M
      while (nErrors < 100) {
435
53.3M
  c = getChar();
436
53.3M
  if (c == '>') {
437
14.6M
    break;
438
38.6M
  } else if (c == EOF) {
439
8.30k
    error(errSyntaxError, getPos(), "Unterminated hex string");
440
8.30k
    break;
441
38.6M
  } else if (specialChars[c] != 1) {
442
32.1M
    c2 = c2 << 4;
443
32.1M
    if (c >= '0' && c <= '9') {
444
8.74M
      c2 += c - '0';
445
23.4M
    } else if (c >= 'A' && c <= 'F') {
446
718k
      c2 += c - 'A' + 10;
447
22.7M
    } else if (c >= 'a' && c <= 'f') {
448
15.3M
      c2 += c - 'a' + 10;
449
15.3M
    } else {
450
7.34M
      error(errSyntaxError, getPos(),
451
7.34M
      "Illegal character <{0:02x}> in hex string", c);
452
7.34M
      ++nErrors;
453
7.34M
    }
454
32.1M
    if (++m == 2) {
455
8.93M
      if (n == tokBufSize) {
456
12.2k
        if (!s)
457
10.4k
    s = new GString(tokBuf, tokBufSize);
458
1.84k
        else
459
1.84k
    s->append(tokBuf, tokBufSize);
460
12.2k
        p = tokBuf;
461
12.2k
        n = 0;
462
12.2k
      }
463
8.93M
      *p++ = (char)c2;
464
8.93M
      ++n;
465
8.93M
      c2 = 0;
466
8.93M
      m = 0;
467
8.93M
    }
468
32.1M
  }
469
53.3M
      }
470
14.7M
      if (!s)
471
14.7M
  s = new GString(tokBuf, n);
472
10.4k
      else
473
10.4k
  s->append(tokBuf, n);
474
14.7M
      if (m == 1)
475
14.3M
  s->append((char)(c2 << 4));
476
14.7M
      obj->initString(s);
477
14.7M
    }
478
15.6M
    break;
479
480
  // dict punctuation
481
2.51M
  case '>':
482
2.51M
    c = lookChar();
483
2.51M
    if (c == '>') {
484
927k
      getChar();
485
927k
      tokBuf[0] = tokBuf[1] = '>';
486
927k
      tokBuf[2] = '\0';
487
927k
      obj->initCmd(tokBuf);
488
1.58M
    } else {
489
1.58M
      error(errSyntaxError, getPos(), "Illegal character '>'");
490
1.58M
      obj->initError();
491
1.58M
    }
492
2.51M
    break;
493
494
  // error
495
800k
  case ')':
496
1.28M
  case '{':
497
1.34M
  case '}':
498
1.34M
    error(errSyntaxError, getPos(), "Illegal character '{0:c}'", c);
499
1.34M
    obj->initError();
500
1.34M
    break;
501
502
  // command
503
21.4M
  default:
504
21.4M
    p = tokBuf;
505
21.4M
    *p++ = (char)c;
506
21.4M
    n = 1;
507
115M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
508
94.6M
      getChar();
509
94.6M
      if (++n == tokBufSize) {
510
309k
  error(errSyntaxError, getPos(), "Command token too long");
511
309k
  break;
512
309k
      }
513
94.3M
      *p++ = (char)c;
514
94.3M
    }
515
21.4M
    *p = '\0';
516
21.4M
    if (tokBuf[0] == 't' && !strcmp(tokBuf, "true")) {
517
100k
      obj->initBool(gTrue);
518
21.3M
    } else if (tokBuf[0] == 'f' && !strcmp(tokBuf, "false")) {
519
15.3k
      obj->initBool(gFalse);
520
21.3M
    } else if (tokBuf[0] == 'n' && !strcmp(tokBuf, "null")) {
521
23.9k
      obj->initNull();
522
21.2M
    } else {
523
21.2M
      obj->initCmd(tokBuf);
524
21.2M
    }
525
21.4M
    break;
526
85.6M
  }
527
528
85.6M
  return obj;
529
85.6M
}
530
531
162k
void Lexer::skipToNextLine() {
532
162k
  int c;
533
534
354k
  while (1) {
535
354k
    c = getChar();
536
354k
    if (c == EOF || c == '\n') {
537
97.3k
      return;
538
97.3k
    }
539
257k
    if (c == '\r') {
540
65.3k
      if ((c = lookChar()) == '\n') {
541
56.7k
  getChar();
542
56.7k
      }
543
65.3k
      return;
544
65.3k
    }
545
257k
  }
546
162k
}
547
548
40.6k
void Lexer::skipToEOF() {
549
1.78M
  while (getChar() != EOF) ;
550
40.6k
}
551
552
223M
GBool Lexer::isSpace(int c) {
553
223M
  return c >= 0 && c <= 0xff && specialChars[c] == 1;
554
223M
}