Coverage Report

Created: 2025-08-26 07:24

/src/xpdf-4.05/xpdf/Lexer.cc
Line
Count
Source (jump to first uncovered line)
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
312k
Lexer::Lexer(XRef *xref, Stream *str) {
47
312k
  Object obj;
48
49
312k
  curStr.initStream(str);
50
312k
  streams = new Array(xref);
51
312k
  streams->add(curStr.copy(&obj));
52
312k
  strPtr = 0;
53
312k
  freeArray = gTrue;
54
312k
  curStr.streamReset();
55
312k
}
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
312k
Lexer::~Lexer() {
76
312k
  if (!curStr.isNone()) {
77
141k
    curStr.streamClose();
78
141k
    curStr.free();
79
141k
  }
80
312k
  if (freeArray) {
81
312k
    delete streams;
82
312k
  }
83
312k
}
84
85
876M
int Lexer::getChar() {
86
876M
  int c;
87
88
876M
  c = EOF;
89
876M
  while (!curStr.isNone() && (c = curStr.streamGetChar()) == EOF) {
90
171k
    curStr.streamClose();
91
171k
    curStr.free();
92
171k
    ++strPtr;
93
171k
    if (strPtr < streams->getLength()) {
94
0
      streams->get(strPtr, &curStr);
95
0
      curStr.streamReset();
96
0
    }
97
171k
  }
98
876M
  return c;
99
876M
}
100
101
264M
int Lexer::lookChar() {
102
264M
  if (curStr.isNone()) {
103
705
    return EOF;
104
705
  }
105
264M
  return curStr.streamLookChar();
106
264M
}
107
108
208M
Object *Lexer::getObj(Object *obj) {
109
208M
  char *p;
110
208M
  int c, c2;
111
208M
  GBool comment, neg, doubleMinus, done, invalid;
112
208M
  int numParen, nErrors;
113
208M
  int xi;
114
208M
  double xf, scale;
115
208M
  GString *s;
116
208M
  int n, m;
117
118
  // skip whitespace and comments
119
208M
  comment = gFalse;
120
361M
  while (1) {
121
361M
    if ((c = getChar()) == EOF) {
122
154M
      return obj->initEOF();
123
154M
    }
124
206M
    if (comment) {
125
14.3M
      if (c == '\r' || c == '\n')
126
249k
  comment = gFalse;
127
192M
    } else if (c == '%') {
128
251k
      comment = gTrue;
129
192M
    } else if (specialChars[c] != 1) {
130
53.7M
      break;
131
53.7M
    }
132
206M
  }
133
134
  // start reading token
135
53.7M
  switch (c) {
136
137
  // number
138
8.14M
  case '0': case '1': case '2': case '3': case '4':
139
10.3M
  case '5': case '6': case '7': case '8': case '9':
140
10.9M
  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
10.9M
    neg = gFalse;
156
10.9M
    doubleMinus = gFalse;
157
10.9M
    xf = xi = 0;
158
10.9M
    if (c == '+') {
159
      // just ignore it
160
10.8M
    } else if (c == '-') {
161
294k
      neg = gTrue;
162
294k
      if (lookChar() == '-') {
163
9.94k
  doubleMinus = gTrue;
164
15.1k
  do {
165
15.1k
    getChar();
166
15.1k
  } while (lookChar() == '-');
167
9.94k
      }
168
10.5M
    } else if (c == '.') {
169
245k
      goto doReal;
170
10.3M
    } else {
171
10.3M
      xf = xi = c - '0';
172
10.3M
    }
173
25.2M
    while (1) {
174
25.2M
      c = lookChar();
175
25.2M
      if (isdigit(c)) {
176
14.5M
  getChar();
177
14.5M
  xi = xi * 10 + (c - '0');
178
14.5M
  if (xf < 1e20) {
179
14.5M
    xf = xf * 10 + (c - '0');
180
14.5M
  }
181
14.5M
      } else if (c == '.') {
182
744k
  getChar();
183
744k
  goto doReal;
184
9.96M
      } else {
185
9.96M
  break;
186
9.96M
      }
187
25.2M
    }
188
9.97M
    while ((c = lookChar()) == '-' || isdigit(c)) {
189
8.68k
      getChar();
190
8.68k
    }
191
9.96M
    if (neg) {
192
289k
      xi = -xi;
193
289k
    }
194
9.96M
    if (doubleMinus) {
195
9.94k
      xi = 0;
196
9.94k
    }
197
9.96M
    obj->initInt(xi);
198
9.96M
    break;
199
990k
  doReal:
200
990k
    scale = 0.1;
201
2.14M
    while (1) {
202
2.14M
      c = lookChar();
203
2.14M
      if (c == '-') {
204
5.33k
  error(errSyntaxWarning, getPos(), "Badly formatted number");
205
5.33k
  getChar();
206
5.33k
  continue;
207
5.33k
      }
208
2.13M
      if (!isdigit(c)) {
209
990k
  break;
210
990k
      }
211
1.14M
      getChar();
212
1.14M
      xf = xf + scale * (c - '0');
213
1.14M
      scale *= 0.1;
214
1.14M
    }
215
990k
    while ((c = lookChar()) == '-' || isdigit(c)) {
216
0
      getChar();
217
0
    }
218
990k
    if (neg) {
219
5.07k
      xf = -xf;
220
5.07k
    }
221
990k
    obj->initReal(xf);
222
990k
    break;
223
224
  // string
225
386k
  case '(':
226
386k
    p = tokBuf;
227
386k
    n = 0;
228
386k
    numParen = 1;
229
386k
    done = gFalse;
230
386k
    s = NULL;
231
252M
    do {
232
252M
      c2 = EOF;
233
252M
      switch (c = getChar()) {
234
235
12.0k
      case EOF:
236
12.0k
  error(errSyntaxError, getPos(), "Unterminated string");
237
12.0k
  done = gTrue;
238
12.0k
  break;
239
240
627k
      case '(':
241
627k
  ++numParen;
242
627k
  c2 = c;
243
627k
  break;
244
245
934k
      case ')':
246
934k
  if (--numParen == 0) {
247
374k
    done = gTrue;
248
559k
  } else {
249
559k
    c2 = c;
250
559k
  }
251
934k
  break;
252
253
5.34M
      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
5.34M
  c = lookChar();
257
5.34M
  if (c == '\n') {
258
338k
    getChar();
259
338k
  }
260
5.34M
  c2 = '\n';
261
5.34M
  break;
262
263
245k
      case '\\':
264
245k
  switch (c = getChar()) {
265
1.62k
  case 'n':
266
1.62k
    c2 = '\n';
267
1.62k
    break;
268
19.4k
  case 'r':
269
19.4k
    c2 = '\r';
270
19.4k
    break;
271
22.0k
  case 't':
272
22.0k
    c2 = '\t';
273
22.0k
    break;
274
3.34k
  case 'b':
275
3.34k
    c2 = '\b';
276
3.34k
    break;
277
167
  case 'f':
278
167
    c2 = '\f';
279
167
    break;
280
10.7k
  case '\\':
281
21.1k
  case '(':
282
30.7k
  case ')':
283
30.7k
    c2 = c;
284
30.7k
    break;
285
9.53k
  case '0': case '1': case '2': case '3':
286
37.4k
  case '4': case '5': case '6': case '7':
287
37.4k
    c2 = c - '0';
288
37.4k
    c = lookChar();
289
37.4k
    if (c >= '0' && c <= '7') {
290
8.76k
      getChar();
291
8.76k
      c2 = (c2 << 3) + (c - '0');
292
8.76k
      c = lookChar();
293
8.76k
      if (c >= '0' && c <= '7') {
294
795
        getChar();
295
795
        c2 = (c2 << 3) + (c - '0');
296
795
      }
297
8.76k
    }
298
37.4k
    break;
299
4.63k
  case '\r':
300
4.63k
    c = lookChar();
301
4.63k
    if (c == '\n') {
302
1.07k
      getChar();
303
1.07k
    }
304
4.63k
    break;
305
6.74k
  case '\n':
306
6.74k
    break;
307
10
  case EOF:
308
10
    error(errSyntaxError, getPos(), "Unterminated string");
309
10
    done = gTrue;
310
10
    break;
311
119k
  default:
312
119k
    c2 = c;
313
119k
    break;
314
245k
  }
315
245k
  break;
316
317
245M
      default:
318
245M
  c2 = c;
319
245M
  break;
320
252M
      }
321
322
252M
      if (c2 != EOF) {
323
252M
  if (n == tokBufSize) {
324
1.85M
    if (!s)
325
76.1k
      s = new GString(tokBuf, tokBufSize);
326
1.77M
    else
327
1.77M
      s->append(tokBuf, tokBufSize);
328
1.85M
    p = tokBuf;
329
1.85M
    n = 0;
330
1.85M
  }
331
252M
  *p++ = (char)c2;
332
252M
  ++n;
333
252M
      }
334
252M
    } while (!done);
335
386k
    if (!s)
336
310k
      s = new GString(tokBuf, n);
337
76.1k
    else
338
76.1k
      s->append(tokBuf, n);
339
386k
    obj->initString(s);
340
386k
    break;
341
342
  // name
343
8.84M
  case '/':
344
8.84M
    p = tokBuf;
345
8.84M
    n = 0;
346
8.84M
    s = NULL;
347
8.84M
    invalid = gFalse;
348
41.9M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
349
33.1M
      getChar();
350
33.1M
      if (c == '#') {
351
833k
  c2 = lookChar();
352
833k
  if (c2 >= '0' && c2 <= '9') {
353
1.78k
    c = c2 - '0';
354
831k
  } else if (c2 >= 'A' && c2 <= 'F') {
355
46.4k
    c = c2 - 'A' + 10;
356
784k
  } else if (c2 >= 'a' && c2 <= 'f') {
357
96.8k
    c = c2 - 'a' + 10;
358
687k
  } else {
359
687k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
360
687k
    goto notEscChar;
361
687k
  }
362
145k
  getChar();
363
145k
  c2 = lookChar();
364
145k
  if (c2 >= '0' && c2 <= '9') {
365
26.4k
    c = (c << 4) + (c2 - '0');
366
118k
  } else if (c2 >= 'A' && c2 <= 'F') {
367
47.7k
    c = (c << 4) + (c2 - 'A' + 10);
368
70.8k
  } else if (c2 >= 'a' && c2 <= 'f') {
369
711
    c = (c << 4) + (c2 - 'a' + 10);
370
70.1k
  } else {
371
70.1k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
372
70.1k
    goto notEscChar;
373
70.1k
  }
374
74.9k
  getChar();
375
74.9k
  if (c == 0) {
376
168
    invalid = gTrue;
377
168
  }
378
74.9k
      }
379
33.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
33.1M
      ++n;
384
33.1M
      if (n < tokBufSize) {
385
28.7M
  *p++ = (char)c;
386
28.7M
      } else if (n == tokBufSize) {
387
880
  *p = (char)c;
388
880
  s = new GString(tokBuf, n);
389
4.39M
      } else {
390
4.39M
  s->append((char)c);
391
4.39M
      }
392
33.1M
    }
393
8.84M
    if (invalid) {
394
168
      error(errSyntaxError, getPos(), "Null character in name");
395
168
      obj->initError();
396
168
      if (s) {
397
0
  delete s;
398
0
      }
399
8.84M
    } else if (n < tokBufSize) {
400
8.84M
      *p = '\0';
401
8.84M
      obj->initName(tokBuf);
402
8.84M
    } else {
403
880
      obj->initName(s->getCString());
404
880
      delete s;
405
880
    }
406
8.84M
    break;
407
408
  // array punctuation
409
5.44M
  case '[':
410
6.10M
  case ']':
411
6.10M
    tokBuf[0] = (char)c;
412
6.10M
    tokBuf[1] = '\0';
413
6.10M
    obj->initCmd(tokBuf);
414
6.10M
    break;
415
416
  // hex string or dict punctuation
417
4.28M
  case '<':
418
4.28M
    c = lookChar();
419
420
    // dict punctuation
421
4.28M
    if (c == '<') {
422
3.53M
      getChar();
423
3.53M
      tokBuf[0] = tokBuf[1] = '<';
424
3.53M
      tokBuf[2] = '\0';
425
3.53M
      obj->initCmd(tokBuf);
426
427
    // hex string
428
3.53M
    } else {
429
751k
      p = tokBuf;
430
751k
      m = n = 0;
431
751k
      c2 = 0;
432
751k
      s = NULL;
433
751k
      nErrors = 0;
434
52.7M
      while (nErrors < 100) {
435
52.6M
  c = getChar();
436
52.6M
  if (c == '>') {
437
588k
    break;
438
52.0M
  } else if (c == EOF) {
439
3.89k
    error(errSyntaxError, getPos(), "Unterminated hex string");
440
3.89k
    break;
441
52.0M
  } else if (specialChars[c] != 1) {
442
45.7M
    c2 = c2 << 4;
443
45.7M
    if (c >= '0' && c <= '9') {
444
5.65M
      c2 += c - '0';
445
40.1M
    } else if (c >= 'A' && c <= 'F') {
446
888k
      c2 += c - 'A' + 10;
447
39.2M
    } else if (c >= 'a' && c <= 'f') {
448
6.26M
      c2 += c - 'a' + 10;
449
32.9M
    } else {
450
32.9M
      error(errSyntaxError, getPos(),
451
32.9M
      "Illegal character <{0:02x}> in hex string", c);
452
32.9M
      ++nErrors;
453
32.9M
    }
454
45.7M
    if (++m == 2) {
455
22.7M
      if (n == tokBufSize) {
456
6.13k
        if (!s)
457
1.42k
    s = new GString(tokBuf, tokBufSize);
458
4.71k
        else
459
4.71k
    s->append(tokBuf, tokBufSize);
460
6.13k
        p = tokBuf;
461
6.13k
        n = 0;
462
6.13k
      }
463
22.7M
      *p++ = (char)c2;
464
22.7M
      ++n;
465
22.7M
      c2 = 0;
466
22.7M
      m = 0;
467
22.7M
    }
468
45.7M
  }
469
52.6M
      }
470
751k
      if (!s)
471
750k
  s = new GString(tokBuf, n);
472
1.42k
      else
473
1.42k
  s->append(tokBuf, n);
474
751k
      if (m == 1)
475
346k
  s->append((char)(c2 << 4));
476
751k
      obj->initString(s);
477
751k
    }
478
4.28M
    break;
479
480
  // dict punctuation
481
3.44M
  case '>':
482
3.44M
    c = lookChar();
483
3.44M
    if (c == '>') {
484
2.31M
      getChar();
485
2.31M
      tokBuf[0] = tokBuf[1] = '>';
486
2.31M
      tokBuf[2] = '\0';
487
2.31M
      obj->initCmd(tokBuf);
488
2.31M
    } else {
489
1.12M
      error(errSyntaxError, getPos(), "Illegal character '>'");
490
1.12M
      obj->initError();
491
1.12M
    }
492
3.44M
    break;
493
494
  // error
495
963k
  case ')':
496
1.15M
  case '{':
497
1.24M
  case '}':
498
1.24M
    error(errSyntaxError, getPos(), "Illegal character '{0:c}'", c);
499
1.24M
    obj->initError();
500
1.24M
    break;
501
502
  // command
503
18.4M
  default:
504
18.4M
    p = tokBuf;
505
18.4M
    *p++ = (char)c;
506
18.4M
    n = 1;
507
169M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
508
151M
      getChar();
509
151M
      if (++n == tokBufSize) {
510
568k
  error(errSyntaxError, getPos(), "Command token too long");
511
568k
  break;
512
568k
      }
513
150M
      *p++ = (char)c;
514
150M
    }
515
18.4M
    *p = '\0';
516
18.4M
    if (tokBuf[0] == 't' && !strcmp(tokBuf, "true")) {
517
71.6k
      obj->initBool(gTrue);
518
18.4M
    } else if (tokBuf[0] == 'f' && !strcmp(tokBuf, "false")) {
519
4.19k
      obj->initBool(gFalse);
520
18.4M
    } else if (tokBuf[0] == 'n' && !strcmp(tokBuf, "null")) {
521
974
      obj->initNull();
522
18.4M
    } else {
523
18.4M
      obj->initCmd(tokBuf);
524
18.4M
    }
525
18.4M
    break;
526
53.7M
  }
527
528
53.7M
  return obj;
529
53.7M
}
530
531
129k
void Lexer::skipToNextLine() {
532
129k
  int c;
533
534
447k
  while (1) {
535
447k
    c = getChar();
536
447k
    if (c == EOF || c == '\n') {
537
29.9k
      return;
538
29.9k
    }
539
417k
    if (c == '\r') {
540
100k
      if ((c = lookChar()) == '\n') {
541
85.3k
  getChar();
542
85.3k
      }
543
100k
      return;
544
100k
    }
545
417k
  }
546
129k
}
547
548
35.5k
void Lexer::skipToEOF() {
549
1.45M
  while (getChar() != EOF) ;
550
35.5k
}
551
552
43.8M
GBool Lexer::isSpace(int c) {
553
43.8M
  return c >= 0 && c <= 0xff && specialChars[c] == 1;
554
43.8M
}