Coverage Report

Created: 2026-09-01 07:00

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
469k
Lexer::Lexer(XRef *xref, Stream *str) {
47
469k
  Object obj;
48
49
469k
  curStr.initStream(str);
50
469k
  streams = new Array(xref);
51
469k
  streams->add(curStr.copy(&obj));
52
469k
  strPtr = 0;
53
469k
  freeArray = gTrue;
54
469k
  curStr.streamReset();
55
469k
}
56
57
154k
Lexer::Lexer(XRef *xref, Object *obj) {
58
154k
  Object obj2;
59
60
154k
  if (obj->isStream()) {
61
153k
    streams = new Array(xref);
62
153k
    freeArray = gTrue;
63
153k
    streams->add(obj->copy(&obj2));
64
153k
  } else {
65
967
    streams = obj->getArray();
66
967
    freeArray = gFalse;
67
967
  }
68
154k
  strPtr = 0;
69
154k
  if (streams->getLength() > 0) {
70
154k
    streams->get(strPtr, &curStr);
71
154k
    curStr.streamReset();
72
154k
  }
73
154k
}
74
75
623k
Lexer::~Lexer() {
76
623k
  if (!curStr.isNone()) {
77
324k
    curStr.streamClose();
78
324k
    curStr.free();
79
324k
  }
80
623k
  if (freeArray) {
81
622k
    delete streams;
82
622k
  }
83
623k
}
84
85
1.22G
int Lexer::getChar() {
86
1.22G
  int c;
87
88
1.22G
  c = EOF;
89
1.22G
  while (!curStr.isNone() && (c = curStr.streamGetChar()) == EOF) {
90
300k
    curStr.streamClose();
91
300k
    curStr.free();
92
300k
    ++strPtr;
93
300k
    if (strPtr < streams->getLength()) {
94
1.32k
      streams->get(strPtr, &curStr);
95
1.32k
      curStr.streamReset();
96
1.32k
    }
97
300k
  }
98
1.22G
  return c;
99
1.22G
}
100
101
325M
int Lexer::lookChar() {
102
325M
  if (curStr.isNone()) {
103
151
    return EOF;
104
151
  }
105
325M
  return curStr.streamLookChar();
106
325M
}
107
108
183M
Object *Lexer::getObj(Object *obj) {
109
183M
  char *p;
110
183M
  int c, c2;
111
183M
  GBool comment, neg, doubleMinus, done, invalid;
112
183M
  int numParen, nErrors;
113
183M
  int xi;
114
183M
  double xf, scale;
115
183M
  GString *s;
116
183M
  int n, m;
117
118
  // skip whitespace and comments
119
183M
  comment = gFalse;
120
673M
  while (1) {
121
673M
    if ((c = getChar()) == EOF) {
122
96.7M
      return obj->initEOF();
123
96.7M
    }
124
576M
    if (comment) {
125
55.7M
      if (c == '\r' || c == '\n')
126
479k
  comment = gFalse;
127
521M
    } else if (c == '%') {
128
480k
      comment = gTrue;
129
520M
    } else if (specialChars[c] != 1) {
130
87.0M
      break;
131
87.0M
    }
132
576M
  }
133
134
  // start reading token
135
87.0M
  switch (c) {
136
137
  // number
138
20.6M
  case '0': case '1': case '2': case '3': case '4':
139
32.1M
  case '5': case '6': case '7': case '8': case '9':
140
34.7M
  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
34.7M
    neg = gFalse;
156
34.7M
    doubleMinus = gFalse;
157
34.7M
    xf = xi = 0;
158
34.7M
    if (c == '+') {
159
      // just ignore it
160
34.7M
    } else if (c == '-') {
161
1.76M
      neg = gTrue;
162
1.76M
      if (lookChar() == '-') {
163
10.2k
  doubleMinus = gTrue;
164
14.4k
  do {
165
14.4k
    getChar();
166
14.4k
  } while (lookChar() == '-');
167
10.2k
      }
168
32.9M
    } else if (c == '.') {
169
855k
      goto doReal;
170
32.1M
    } else {
171
32.1M
      xf = xi = c - '0';
172
32.1M
    }
173
84.1M
    while (1) {
174
84.1M
      c = lookChar();
175
84.1M
      if (isdigit(c)) {
176
50.2M
  getChar();
177
50.2M
  xi = xi * 10 + (c - '0');
178
50.2M
  if (xf < 1e20) {
179
42.5M
    xf = xf * 10 + (c - '0');
180
42.5M
  }
181
50.2M
      } else if (c == '.') {
182
5.26M
  getChar();
183
5.26M
  goto doReal;
184
28.6M
      } else {
185
28.6M
  break;
186
28.6M
      }
187
84.1M
    }
188
29.0M
    while ((c = lookChar()) == '-' || isdigit(c)) {
189
439k
      getChar();
190
439k
    }
191
28.6M
    if (neg) {
192
804k
      xi = -xi;
193
804k
    }
194
28.6M
    if (doubleMinus) {
195
5.93k
      xi = 0;
196
5.93k
    }
197
28.6M
    obj->initInt(xi);
198
28.6M
    break;
199
6.11M
  doReal:
200
6.11M
    scale = 0.1;
201
19.1M
    while (1) {
202
19.1M
      c = lookChar();
203
19.1M
      if (c == '-') {
204
26.6k
  error(errSyntaxWarning, getPos(), "Badly formatted number");
205
26.6k
  getChar();
206
26.6k
  continue;
207
26.6k
      }
208
19.0M
      if (!isdigit(c)) {
209
6.11M
  break;
210
6.11M
      }
211
12.9M
      getChar();
212
12.9M
      xf = xf + scale * (c - '0');
213
12.9M
      scale *= 0.1;
214
12.9M
    }
215
6.11M
    while ((c = lookChar()) == '-' || isdigit(c)) {
216
0
      getChar();
217
0
    }
218
6.11M
    if (neg) {
219
962k
      xf = -xf;
220
962k
    }
221
6.11M
    obj->initReal(xf);
222
6.11M
    break;
223
224
  // string
225
710k
  case '(':
226
710k
    p = tokBuf;
227
710k
    n = 0;
228
710k
    numParen = 1;
229
710k
    done = gFalse;
230
710k
    s = NULL;
231
287M
    do {
232
287M
      c2 = EOF;
233
287M
      switch (c = getChar()) {
234
235
22.2k
      case EOF:
236
22.2k
  error(errSyntaxError, getPos(), "Unterminated string");
237
22.2k
  done = gTrue;
238
22.2k
  break;
239
240
1.18M
      case '(':
241
1.18M
  ++numParen;
242
1.18M
  c2 = c;
243
1.18M
  break;
244
245
1.44M
      case ')':
246
1.44M
  if (--numParen == 0) {
247
687k
    done = gTrue;
248
755k
  } else {
249
755k
    c2 = c;
250
755k
  }
251
1.44M
  break;
252
253
3.88M
      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
3.88M
  c = lookChar();
257
3.88M
  if (c == '\n') {
258
88.1k
    getChar();
259
88.1k
  }
260
3.88M
  c2 = '\n';
261
3.88M
  break;
262
263
512k
      case '\\':
264
512k
  switch (c = getChar()) {
265
4.24k
  case 'n':
266
4.24k
    c2 = '\n';
267
4.24k
    break;
268
45.6k
  case 'r':
269
45.6k
    c2 = '\r';
270
45.6k
    break;
271
3.15k
  case 't':
272
3.15k
    c2 = '\t';
273
3.15k
    break;
274
2.37k
  case 'b':
275
2.37k
    c2 = '\b';
276
2.37k
    break;
277
1.71k
  case 'f':
278
1.71k
    c2 = '\f';
279
1.71k
    break;
280
30.5k
  case '\\':
281
68.6k
  case '(':
282
108k
  case ')':
283
108k
    c2 = c;
284
108k
    break;
285
94.1k
  case '0': case '1': case '2': case '3':
286
110k
  case '4': case '5': case '6': case '7':
287
110k
    c2 = c - '0';
288
110k
    c = lookChar();
289
110k
    if (c >= '0' && c <= '7') {
290
83.1k
      getChar();
291
83.1k
      c2 = (c2 << 3) + (c - '0');
292
83.1k
      c = lookChar();
293
83.1k
      if (c >= '0' && c <= '7') {
294
72.2k
        getChar();
295
72.2k
        c2 = (c2 << 3) + (c - '0');
296
72.2k
      }
297
83.1k
    }
298
110k
    break;
299
2.51k
  case '\r':
300
2.51k
    c = lookChar();
301
2.51k
    if (c == '\n') {
302
57
      getChar();
303
57
    }
304
2.51k
    break;
305
1.91k
  case '\n':
306
1.91k
    break;
307
289
  case EOF:
308
289
    error(errSyntaxError, getPos(), "Unterminated string");
309
289
    done = gTrue;
310
289
    break;
311
232k
  default:
312
232k
    c2 = c;
313
232k
    break;
314
512k
  }
315
512k
  break;
316
317
280M
      default:
318
280M
  c2 = c;
319
280M
  break;
320
287M
      }
321
322
287M
      if (c2 != EOF) {
323
286M
  if (n == tokBufSize) {
324
2.12M
    if (!s)
325
69.0k
      s = new GString(tokBuf, tokBufSize);
326
2.06M
    else
327
2.06M
      s->append(tokBuf, tokBufSize);
328
2.12M
    p = tokBuf;
329
2.12M
    n = 0;
330
2.12M
  }
331
286M
  *p++ = (char)c2;
332
286M
  ++n;
333
286M
      }
334
287M
    } while (!done);
335
710k
    if (!s)
336
641k
      s = new GString(tokBuf, n);
337
69.0k
    else
338
69.0k
      s->append(tokBuf, n);
339
710k
    obj->initString(s);
340
710k
    break;
341
342
  // name
343
5.36M
  case '/':
344
5.36M
    p = tokBuf;
345
5.36M
    n = 0;
346
5.36M
    s = NULL;
347
5.36M
    invalid = gFalse;
348
39.7M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
349
34.4M
      getChar();
350
34.4M
      if (c == '#') {
351
47.5k
  c2 = lookChar();
352
47.5k
  if (c2 >= '0' && c2 <= '9') {
353
3.55k
    c = c2 - '0';
354
43.9k
  } else if (c2 >= 'A' && c2 <= 'F') {
355
4.29k
    c = c2 - 'A' + 10;
356
39.6k
  } else if (c2 >= 'a' && c2 <= 'f') {
357
4.07k
    c = c2 - 'a' + 10;
358
35.6k
  } else {
359
35.6k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
360
35.6k
    goto notEscChar;
361
35.6k
  }
362
11.9k
  getChar();
363
11.9k
  c2 = lookChar();
364
11.9k
  if (c2 >= '0' && c2 <= '9') {
365
2.33k
    c = (c << 4) + (c2 - '0');
366
9.59k
  } else if (c2 >= 'A' && c2 <= 'F') {
367
253
    c = (c << 4) + (c2 - 'A' + 10);
368
9.33k
  } else if (c2 >= 'a' && c2 <= 'f') {
369
5.58k
    c = (c << 4) + (c2 - 'a' + 10);
370
5.58k
  } else {
371
3.75k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
372
3.75k
    goto notEscChar;
373
3.75k
  }
374
8.17k
  getChar();
375
8.17k
  if (c == 0) {
376
1.00k
    invalid = gTrue;
377
1.00k
  }
378
8.17k
      }
379
34.4M
     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
34.4M
      ++n;
384
34.4M
      if (n < tokBufSize) {
385
29.3M
  *p++ = (char)c;
386
29.3M
      } else if (n == tokBufSize) {
387
6.50k
  *p = (char)c;
388
6.50k
  s = new GString(tokBuf, n);
389
5.09M
      } else {
390
5.09M
  s->append((char)c);
391
5.09M
      }
392
34.4M
    }
393
5.36M
    if (invalid) {
394
1.00k
      error(errSyntaxError, getPos(), "Null character in name");
395
1.00k
      obj->initError();
396
1.00k
      if (s) {
397
32
  delete s;
398
32
      }
399
5.36M
    } else if (n < tokBufSize) {
400
5.35M
      *p = '\0';
401
5.35M
      obj->initName(tokBuf);
402
5.35M
    } else {
403
6.47k
      obj->initName(s->getCString());
404
6.47k
      delete s;
405
6.47k
    }
406
5.36M
    break;
407
408
  // array punctuation
409
5.29M
  case '[':
410
5.83M
  case ']':
411
5.83M
    tokBuf[0] = (char)c;
412
5.83M
    tokBuf[1] = '\0';
413
5.83M
    obj->initCmd(tokBuf);
414
5.83M
    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
929k
      getChar();
423
929k
      tokBuf[0] = tokBuf[1] = '<';
424
929k
      tokBuf[2] = '\0';
425
929k
      obj->initCmd(tokBuf);
426
427
    // hex string
428
14.6M
    } else {
429
14.6M
      p = tokBuf;
430
14.6M
      m = n = 0;
431
14.6M
      c2 = 0;
432
14.6M
      s = NULL;
433
14.6M
      nErrors = 0;
434
49.6M
      while (nErrors < 100) {
435
49.6M
  c = getChar();
436
49.6M
  if (c == '>') {
437
14.6M
    break;
438
34.9M
  } else if (c == EOF) {
439
6.59k
    error(errSyntaxError, getPos(), "Unterminated hex string");
440
6.59k
    break;
441
34.9M
  } else if (specialChars[c] != 1) {
442
30.4M
    c2 = c2 << 4;
443
30.4M
    if (c >= '0' && c <= '9') {
444
7.73M
      c2 += c - '0';
445
22.7M
    } else if (c >= 'A' && c <= 'F') {
446
580k
      c2 += c - 'A' + 10;
447
22.1M
    } else if (c >= 'a' && c <= 'f') {
448
15.5M
      c2 += c - 'a' + 10;
449
15.5M
    } else {
450
6.65M
      error(errSyntaxError, getPos(),
451
6.65M
      "Illegal character <{0:02x}> in hex string", c);
452
6.65M
      ++nErrors;
453
6.65M
    }
454
30.4M
    if (++m == 2) {
455
8.08M
      if (n == tokBufSize) {
456
11.9k
        if (!s)
457
9.51k
    s = new GString(tokBuf, tokBufSize);
458
2.47k
        else
459
2.47k
    s->append(tokBuf, tokBufSize);
460
11.9k
        p = tokBuf;
461
11.9k
        n = 0;
462
11.9k
      }
463
8.08M
      *p++ = (char)c2;
464
8.08M
      ++n;
465
8.08M
      c2 = 0;
466
8.08M
      m = 0;
467
8.08M
    }
468
30.4M
  }
469
49.6M
      }
470
14.6M
      if (!s)
471
14.6M
  s = new GString(tokBuf, n);
472
9.51k
      else
473
9.51k
  s->append(tokBuf, n);
474
14.6M
      if (m == 1)
475
14.3M
  s->append((char)(c2 << 4));
476
14.6M
      obj->initString(s);
477
14.6M
    }
478
15.6M
    break;
479
480
  // dict punctuation
481
2.40M
  case '>':
482
2.40M
    c = lookChar();
483
2.40M
    if (c == '>') {
484
853k
      getChar();
485
853k
      tokBuf[0] = tokBuf[1] = '>';
486
853k
      tokBuf[2] = '\0';
487
853k
      obj->initCmd(tokBuf);
488
1.55M
    } else {
489
1.55M
      error(errSyntaxError, getPos(), "Illegal character '>'");
490
1.55M
      obj->initError();
491
1.55M
    }
492
2.40M
    break;
493
494
  // error
495
813k
  case ')':
496
1.29M
  case '{':
497
1.38M
  case '}':
498
1.38M
    error(errSyntaxError, getPos(), "Illegal character '{0:c}'", c);
499
1.38M
    obj->initError();
500
1.38M
    break;
501
502
  // command
503
21.0M
  default:
504
21.0M
    p = tokBuf;
505
21.0M
    *p++ = (char)c;
506
21.0M
    n = 1;
507
123M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
508
102M
      getChar();
509
102M
      if (++n == tokBufSize) {
510
374k
  error(errSyntaxError, getPos(), "Command token too long");
511
374k
  break;
512
374k
      }
513
102M
      *p++ = (char)c;
514
102M
    }
515
21.0M
    *p = '\0';
516
21.0M
    if (tokBuf[0] == 't' && !strcmp(tokBuf, "true")) {
517
60.6k
      obj->initBool(gTrue);
518
20.9M
    } else if (tokBuf[0] == 'f' && !strcmp(tokBuf, "false")) {
519
15.1k
      obj->initBool(gFalse);
520
20.9M
    } else if (tokBuf[0] == 'n' && !strcmp(tokBuf, "null")) {
521
10.9k
      obj->initNull();
522
20.9M
    } else {
523
20.9M
      obj->initCmd(tokBuf);
524
20.9M
    }
525
21.0M
    break;
526
87.0M
  }
527
528
87.0M
  return obj;
529
87.0M
}
530
531
167k
void Lexer::skipToNextLine() {
532
167k
  int c;
533
534
435k
  while (1) {
535
435k
    c = getChar();
536
435k
    if (c == EOF || c == '\n') {
537
102k
      return;
538
102k
    }
539
333k
    if (c == '\r') {
540
65.2k
      if ((c = lookChar()) == '\n') {
541
55.8k
  getChar();
542
55.8k
      }
543
65.2k
      return;
544
65.2k
    }
545
333k
  }
546
167k
}
547
548
35.6k
void Lexer::skipToEOF() {
549
1.76M
  while (getChar() != EOF) ;
550
35.6k
}
551
552
217M
GBool Lexer::isSpace(int c) {
553
217M
  return c >= 0 && c <= 0xff && specialChars[c] == 1;
554
217M
}