Coverage Report

Created: 2026-06-22 07:09

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
301k
Lexer::Lexer(XRef *xref, Stream *str) {
47
301k
  Object obj;
48
49
301k
  curStr.initStream(str);
50
301k
  streams = new Array(xref);
51
301k
  streams->add(curStr.copy(&obj));
52
301k
  strPtr = 0;
53
301k
  freeArray = gTrue;
54
301k
  curStr.streamReset();
55
301k
}
56
57
102k
Lexer::Lexer(XRef *xref, Object *obj) {
58
102k
  Object obj2;
59
60
102k
  if (obj->isStream()) {
61
101k
    streams = new Array(xref);
62
101k
    freeArray = gTrue;
63
101k
    streams->add(obj->copy(&obj2));
64
101k
  } else {
65
774
    streams = obj->getArray();
66
774
    freeArray = gFalse;
67
774
  }
68
102k
  strPtr = 0;
69
102k
  if (streams->getLength() > 0) {
70
102k
    streams->get(strPtr, &curStr);
71
102k
    curStr.streamReset();
72
102k
  }
73
102k
}
74
75
403k
Lexer::~Lexer() {
76
403k
  if (!curStr.isNone()) {
77
209k
    curStr.streamClose();
78
209k
    curStr.free();
79
209k
  }
80
403k
  if (freeArray) {
81
402k
    delete streams;
82
402k
  }
83
403k
}
84
85
732M
int Lexer::getChar() {
86
732M
  int c;
87
88
732M
  c = EOF;
89
732M
  while (!curStr.isNone() && (c = curStr.streamGetChar()) == EOF) {
90
194k
    curStr.streamClose();
91
194k
    curStr.free();
92
194k
    ++strPtr;
93
194k
    if (strPtr < streams->getLength()) {
94
1.12k
      streams->get(strPtr, &curStr);
95
1.12k
      curStr.streamReset();
96
1.12k
    }
97
194k
  }
98
732M
  return c;
99
732M
}
100
101
257M
int Lexer::lookChar() {
102
257M
  if (curStr.isNone()) {
103
14
    return EOF;
104
14
  }
105
257M
  return curStr.streamLookChar();
106
257M
}
107
108
116M
Object *Lexer::getObj(Object *obj) {
109
116M
  char *p;
110
116M
  int c, c2;
111
116M
  GBool comment, neg, doubleMinus, done, invalid;
112
116M
  int numParen, nErrors;
113
116M
  int xi;
114
116M
  double xf, scale;
115
116M
  GString *s;
116
116M
  int n, m;
117
118
  // skip whitespace and comments
119
116M
  comment = gFalse;
120
388M
  while (1) {
121
388M
    if ((c = getChar()) == EOF) {
122
42.4M
      return obj->initEOF();
123
42.4M
    }
124
345M
    if (comment) {
125
39.5M
      if (c == '\r' || c == '\n')
126
393k
  comment = gFalse;
127
306M
    } else if (c == '%') {
128
394k
      comment = gTrue;
129
305M
    } else if (specialChars[c] != 1) {
130
74.2M
      break;
131
74.2M
    }
132
345M
  }
133
134
  // start reading token
135
74.2M
  switch (c) {
136
137
  // number
138
16.5M
  case '0': case '1': case '2': case '3': case '4':
139
25.5M
  case '5': case '6': case '7': case '8': case '9':
140
27.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
27.9M
    neg = gFalse;
156
27.9M
    doubleMinus = gFalse;
157
27.9M
    xf = xi = 0;
158
27.9M
    if (c == '+') {
159
      // just ignore it
160
27.9M
    } else if (c == '-') {
161
1.55M
      neg = gTrue;
162
1.55M
      if (lookChar() == '-') {
163
9.63k
  doubleMinus = gTrue;
164
17.6k
  do {
165
17.6k
    getChar();
166
17.6k
  } while (lookChar() == '-');
167
9.63k
      }
168
26.3M
    } else if (c == '.') {
169
839k
      goto doReal;
170
25.5M
    } else {
171
25.5M
      xf = xi = c - '0';
172
25.5M
    }
173
70.7M
    while (1) {
174
70.7M
      c = lookChar();
175
70.7M
      if (isdigit(c)) {
176
43.6M
  getChar();
177
43.6M
  xi = xi * 10 + (c - '0');
178
43.6M
  if (xf < 1e20) {
179
35.9M
    xf = xf * 10 + (c - '0');
180
35.9M
  }
181
43.6M
      } else if (c == '.') {
182
4.51M
  getChar();
183
4.51M
  goto doReal;
184
22.5M
      } else {
185
22.5M
  break;
186
22.5M
      }
187
70.7M
    }
188
22.9M
    while ((c = lookChar()) == '-' || isdigit(c)) {
189
312k
      getChar();
190
312k
    }
191
22.5M
    if (neg) {
192
705k
      xi = -xi;
193
705k
    }
194
22.5M
    if (doubleMinus) {
195
6.47k
      xi = 0;
196
6.47k
    }
197
22.5M
    obj->initInt(xi);
198
22.5M
    break;
199
5.35M
  doReal:
200
5.35M
    scale = 0.1;
201
17.0M
    while (1) {
202
17.0M
      c = lookChar();
203
17.0M
      if (c == '-') {
204
21.1k
  error(errSyntaxWarning, getPos(), "Badly formatted number");
205
21.1k
  getChar();
206
21.1k
  continue;
207
21.1k
      }
208
17.0M
      if (!isdigit(c)) {
209
5.35M
  break;
210
5.35M
      }
211
11.6M
      getChar();
212
11.6M
      xf = xf + scale * (c - '0');
213
11.6M
      scale *= 0.1;
214
11.6M
    }
215
5.35M
    while ((c = lookChar()) == '-' || isdigit(c)) {
216
0
      getChar();
217
0
    }
218
5.35M
    if (neg) {
219
844k
      xf = -xf;
220
844k
    }
221
5.35M
    obj->initReal(xf);
222
5.35M
    break;
223
224
  // string
225
544k
  case '(':
226
544k
    p = tokBuf;
227
544k
    n = 0;
228
544k
    numParen = 1;
229
544k
    done = gFalse;
230
544k
    s = NULL;
231
138M
    do {
232
138M
      c2 = EOF;
233
138M
      switch (c = getChar()) {
234
235
10.7k
      case EOF:
236
10.7k
  error(errSyntaxError, getPos(), "Unterminated string");
237
10.7k
  done = gTrue;
238
10.7k
  break;
239
240
690k
      case '(':
241
690k
  ++numParen;
242
690k
  c2 = c;
243
690k
  break;
244
245
992k
      case ')':
246
992k
  if (--numParen == 0) {
247
533k
    done = gTrue;
248
533k
  } else {
249
459k
    c2 = c;
250
459k
  }
251
992k
  break;
252
253
3.83M
      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.83M
  c = lookChar();
257
3.83M
  if (c == '\n') {
258
51.3k
    getChar();
259
51.3k
  }
260
3.83M
  c2 = '\n';
261
3.83M
  break;
262
263
363k
      case '\\':
264
363k
  switch (c = getChar()) {
265
2.72k
  case 'n':
266
2.72k
    c2 = '\n';
267
2.72k
    break;
268
53.1k
  case 'r':
269
53.1k
    c2 = '\r';
270
53.1k
    break;
271
2.66k
  case 't':
272
2.66k
    c2 = '\t';
273
2.66k
    break;
274
1.65k
  case 'b':
275
1.65k
    c2 = '\b';
276
1.65k
    break;
277
555
  case 'f':
278
555
    c2 = '\f';
279
555
    break;
280
34.6k
  case '\\':
281
67.0k
  case '(':
282
100k
  case ')':
283
100k
    c2 = c;
284
100k
    break;
285
84.0k
  case '0': case '1': case '2': case '3':
286
97.1k
  case '4': case '5': case '6': case '7':
287
97.1k
    c2 = c - '0';
288
97.1k
    c = lookChar();
289
97.1k
    if (c >= '0' && c <= '7') {
290
81.2k
      getChar();
291
81.2k
      c2 = (c2 << 3) + (c - '0');
292
81.2k
      c = lookChar();
293
81.2k
      if (c >= '0' && c <= '7') {
294
71.3k
        getChar();
295
71.3k
        c2 = (c2 << 3) + (c - '0');
296
71.3k
      }
297
81.2k
    }
298
97.1k
    break;
299
2.38k
  case '\r':
300
2.38k
    c = lookChar();
301
2.38k
    if (c == '\n') {
302
17
      getChar();
303
17
    }
304
2.38k
    break;
305
1.43k
  case '\n':
306
1.43k
    break;
307
81
  case EOF:
308
81
    error(errSyntaxError, getPos(), "Unterminated string");
309
81
    done = gTrue;
310
81
    break;
311
100k
  default:
312
100k
    c2 = c;
313
100k
    break;
314
363k
  }
315
363k
  break;
316
317
133M
      default:
318
133M
  c2 = c;
319
133M
  break;
320
138M
      }
321
322
138M
      if (c2 != EOF) {
323
138M
  if (n == tokBufSize) {
324
998k
    if (!s)
325
47.6k
      s = new GString(tokBuf, tokBufSize);
326
950k
    else
327
950k
      s->append(tokBuf, tokBufSize);
328
998k
    p = tokBuf;
329
998k
    n = 0;
330
998k
  }
331
138M
  *p++ = (char)c2;
332
138M
  ++n;
333
138M
      }
334
138M
    } while (!done);
335
544k
    if (!s)
336
496k
      s = new GString(tokBuf, n);
337
47.6k
    else
338
47.6k
      s->append(tokBuf, n);
339
544k
    obj->initString(s);
340
544k
    break;
341
342
  // name
343
4.17M
  case '/':
344
4.17M
    p = tokBuf;
345
4.17M
    n = 0;
346
4.17M
    s = NULL;
347
4.17M
    invalid = gFalse;
348
32.0M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
349
27.8M
      getChar();
350
27.8M
      if (c == '#') {
351
20.7k
  c2 = lookChar();
352
20.7k
  if (c2 >= '0' && c2 <= '9') {
353
1.39k
    c = c2 - '0';
354
19.3k
  } else if (c2 >= 'A' && c2 <= 'F') {
355
851
    c = c2 - 'A' + 10;
356
18.4k
  } else if (c2 >= 'a' && c2 <= 'f') {
357
600
    c = c2 - 'a' + 10;
358
17.8k
  } else {
359
17.8k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
360
17.8k
    goto notEscChar;
361
17.8k
  }
362
2.84k
  getChar();
363
2.84k
  c2 = lookChar();
364
2.84k
  if (c2 >= '0' && c2 <= '9') {
365
610
    c = (c << 4) + (c2 - '0');
366
2.23k
  } else if (c2 >= 'A' && c2 <= 'F') {
367
652
    c = (c << 4) + (c2 - 'A' + 10);
368
1.58k
  } else if (c2 >= 'a' && c2 <= 'f') {
369
610
    c = (c << 4) + (c2 - 'a' + 10);
370
974
  } else {
371
974
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
372
974
    goto notEscChar;
373
974
  }
374
1.87k
  getChar();
375
1.87k
  if (c == 0) {
376
96
    invalid = gTrue;
377
96
  }
378
1.87k
      }
379
27.8M
     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
27.8M
      ++n;
384
27.8M
      if (n < tokBufSize) {
385
22.6M
  *p++ = (char)c;
386
22.6M
      } else if (n == tokBufSize) {
387
7.54k
  *p = (char)c;
388
7.54k
  s = new GString(tokBuf, n);
389
5.14M
      } else {
390
5.14M
  s->append((char)c);
391
5.14M
      }
392
27.8M
    }
393
4.17M
    if (invalid) {
394
96
      error(errSyntaxError, getPos(), "Null character in name");
395
96
      obj->initError();
396
96
      if (s) {
397
0
  delete s;
398
0
      }
399
4.17M
    } else if (n < tokBufSize) {
400
4.16M
      *p = '\0';
401
4.16M
      obj->initName(tokBuf);
402
4.16M
    } else {
403
7.54k
      obj->initName(s->getCString());
404
7.54k
      delete s;
405
7.54k
    }
406
4.17M
    break;
407
408
  // array punctuation
409
5.16M
  case '[':
410
5.53M
  case ']':
411
5.53M
    tokBuf[0] = (char)c;
412
5.53M
    tokBuf[1] = '\0';
413
5.53M
    obj->initCmd(tokBuf);
414
5.53M
    break;
415
416
  // hex string or dict punctuation
417
15.1M
  case '<':
418
15.1M
    c = lookChar();
419
420
    // dict punctuation
421
15.1M
    if (c == '<') {
422
682k
      getChar();
423
682k
      tokBuf[0] = tokBuf[1] = '<';
424
682k
      tokBuf[2] = '\0';
425
682k
      obj->initCmd(tokBuf);
426
427
    // hex string
428
14.5M
    } else {
429
14.5M
      p = tokBuf;
430
14.5M
      m = n = 0;
431
14.5M
      c2 = 0;
432
14.5M
      s = NULL;
433
14.5M
      nErrors = 0;
434
44.2M
      while (nErrors < 100) {
435
44.2M
  c = getChar();
436
44.2M
  if (c == '>') {
437
14.4M
    break;
438
29.7M
  } else if (c == EOF) {
439
4.19k
    error(errSyntaxError, getPos(), "Unterminated hex string");
440
4.19k
    break;
441
29.7M
  } else if (specialChars[c] != 1) {
442
25.1M
    c2 = c2 << 4;
443
25.1M
    if (c >= '0' && c <= '9') {
444
4.76M
      c2 += c - '0';
445
20.3M
    } else if (c >= 'A' && c <= 'F') {
446
405k
      c2 += c - 'A' + 10;
447
19.9M
    } else if (c >= 'a' && c <= 'f') {
448
14.9M
      c2 += c - 'a' + 10;
449
14.9M
    } else {
450
5.00M
      error(errSyntaxError, getPos(),
451
5.00M
      "Illegal character <{0:02x}> in hex string", c);
452
5.00M
      ++nErrors;
453
5.00M
    }
454
25.1M
    if (++m == 2) {
455
5.41M
      if (n == tokBufSize) {
456
8.01k
        if (!s)
457
6.52k
    s = new GString(tokBuf, tokBufSize);
458
1.49k
        else
459
1.49k
    s->append(tokBuf, tokBufSize);
460
8.01k
        p = tokBuf;
461
8.01k
        n = 0;
462
8.01k
      }
463
5.41M
      *p++ = (char)c2;
464
5.41M
      ++n;
465
5.41M
      c2 = 0;
466
5.41M
      m = 0;
467
5.41M
    }
468
25.1M
  }
469
44.2M
      }
470
14.5M
      if (!s)
471
14.5M
  s = new GString(tokBuf, n);
472
6.52k
      else
473
6.52k
  s->append(tokBuf, n);
474
14.5M
      if (m == 1)
475
14.2M
  s->append((char)(c2 << 4));
476
14.5M
      obj->initString(s);
477
14.5M
    }
478
15.1M
    break;
479
480
  // dict punctuation
481
2.13M
  case '>':
482
2.13M
    c = lookChar();
483
2.13M
    if (c == '>') {
484
663k
      getChar();
485
663k
      tokBuf[0] = tokBuf[1] = '>';
486
663k
      tokBuf[2] = '\0';
487
663k
      obj->initCmd(tokBuf);
488
1.47M
    } else {
489
1.47M
      error(errSyntaxError, getPos(), "Illegal character '>'");
490
1.47M
      obj->initError();
491
1.47M
    }
492
2.13M
    break;
493
494
  // error
495
767k
  case ')':
496
1.24M
  case '{':
497
1.28M
  case '}':
498
1.28M
    error(errSyntaxError, getPos(), "Illegal character '{0:c}'", c);
499
1.28M
    obj->initError();
500
1.28M
    break;
501
502
  // command
503
17.4M
  default:
504
17.4M
    p = tokBuf;
505
17.4M
    *p++ = (char)c;
506
17.4M
    n = 1;
507
86.7M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
508
69.4M
      getChar();
509
69.4M
      if (++n == tokBufSize) {
510
186k
  error(errSyntaxError, getPos(), "Command token too long");
511
186k
  break;
512
186k
      }
513
69.2M
      *p++ = (char)c;
514
69.2M
    }
515
17.4M
    *p = '\0';
516
17.4M
    if (tokBuf[0] == 't' && !strcmp(tokBuf, "true")) {
517
84.7k
      obj->initBool(gTrue);
518
17.3M
    } else if (tokBuf[0] == 'f' && !strcmp(tokBuf, "false")) {
519
7.92k
      obj->initBool(gFalse);
520
17.3M
    } else if (tokBuf[0] == 'n' && !strcmp(tokBuf, "null")) {
521
14.7k
      obj->initNull();
522
17.3M
    } else {
523
17.3M
      obj->initCmd(tokBuf);
524
17.3M
    }
525
17.4M
    break;
526
74.2M
  }
527
528
74.2M
  return obj;
529
74.2M
}
530
531
109k
void Lexer::skipToNextLine() {
532
109k
  int c;
533
534
244k
  while (1) {
535
244k
    c = getChar();
536
244k
    if (c == EOF || c == '\n') {
537
68.2k
      return;
538
68.2k
    }
539
176k
    if (c == '\r') {
540
41.4k
      if ((c = lookChar()) == '\n') {
541
36.2k
  getChar();
542
36.2k
      }
543
41.4k
      return;
544
41.4k
    }
545
176k
  }
546
109k
}
547
548
27.9k
void Lexer::skipToEOF() {
549
1.27M
  while (getChar() != EOF) ;
550
27.9k
}
551
552
152M
GBool Lexer::isSpace(int c) {
553
152M
  return c >= 0 && c <= 0xff && specialChars[c] == 1;
554
152M
}