Coverage Report

Created: 2026-07-12 06:46

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
425k
Lexer::Lexer(XRef *xref, Stream *str) {
47
425k
  Object obj;
48
49
425k
  curStr.initStream(str);
50
425k
  streams = new Array(xref);
51
425k
  streams->add(curStr.copy(&obj));
52
425k
  strPtr = 0;
53
425k
  freeArray = gTrue;
54
425k
  curStr.streamReset();
55
425k
}
56
57
135k
Lexer::Lexer(XRef *xref, Object *obj) {
58
135k
  Object obj2;
59
60
135k
  if (obj->isStream()) {
61
134k
    streams = new Array(xref);
62
134k
    freeArray = gTrue;
63
134k
    streams->add(obj->copy(&obj2));
64
134k
  } else {
65
1.02k
    streams = obj->getArray();
66
1.02k
    freeArray = gFalse;
67
1.02k
  }
68
135k
  strPtr = 0;
69
135k
  if (streams->getLength() > 0) {
70
135k
    streams->get(strPtr, &curStr);
71
135k
    curStr.streamReset();
72
135k
  }
73
135k
}
74
75
560k
Lexer::~Lexer() {
76
560k
  if (!curStr.isNone()) {
77
275k
    curStr.streamClose();
78
275k
    curStr.free();
79
275k
  }
80
560k
  if (freeArray) {
81
559k
    delete streams;
82
559k
  }
83
560k
}
84
85
1.57G
int Lexer::getChar() {
86
1.57G
  int c;
87
88
1.57G
  c = EOF;
89
1.58G
  while (!curStr.isNone() && (c = curStr.streamGetChar()) == EOF) {
90
286k
    curStr.streamClose();
91
286k
    curStr.free();
92
286k
    ++strPtr;
93
286k
    if (strPtr < streams->getLength()) {
94
1.16k
      streams->get(strPtr, &curStr);
95
1.16k
      curStr.streamReset();
96
1.16k
    }
97
286k
  }
98
1.57G
  return c;
99
1.57G
}
100
101
322M
int Lexer::lookChar() {
102
322M
  if (curStr.isNone()) {
103
286
    return EOF;
104
286
  }
105
322M
  return curStr.streamLookChar();
106
322M
}
107
108
204M
Object *Lexer::getObj(Object *obj) {
109
204M
  char *p;
110
204M
  int c, c2;
111
204M
  GBool comment, neg, doubleMinus, done, invalid;
112
204M
  int numParen, nErrors;
113
204M
  int xi;
114
204M
  double xf, scale;
115
204M
  GString *s;
116
204M
  int n, m;
117
118
  // skip whitespace and comments
119
204M
  comment = gFalse;
120
1.04G
  while (1) {
121
1.04G
    if ((c = getChar()) == EOF) {
122
123M
      return obj->initEOF();
123
123M
    }
124
921M
    if (comment) {
125
213M
      if (c == '\r' || c == '\n')
126
484k
  comment = gFalse;
127
707M
    } else if (c == '%') {
128
487k
      comment = gTrue;
129
707M
    } else if (specialChars[c] != 1) {
130
80.3M
      break;
131
80.3M
    }
132
921M
  }
133
134
  // start reading token
135
80.3M
  switch (c) {
136
137
  // number
138
17.9M
  case '0': case '1': case '2': case '3': case '4':
139
27.8M
  case '5': case '6': case '7': case '8': case '9':
140
30.4M
  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
30.4M
    neg = gFalse;
156
30.4M
    doubleMinus = gFalse;
157
30.4M
    xf = xi = 0;
158
30.4M
    if (c == '+') {
159
      // just ignore it
160
30.4M
    } else if (c == '-') {
161
1.66M
      neg = gTrue;
162
1.66M
      if (lookChar() == '-') {
163
12.1k
  doubleMinus = gTrue;
164
20.3k
  do {
165
20.3k
    getChar();
166
20.3k
  } while (lookChar() == '-');
167
12.1k
      }
168
28.7M
    } else if (c == '.') {
169
900k
      goto doReal;
170
27.8M
    } else {
171
27.8M
      xf = xi = c - '0';
172
27.8M
    }
173
75.0M
    while (1) {
174
75.0M
      c = lookChar();
175
75.0M
      if (isdigit(c)) {
176
45.4M
  getChar();
177
45.4M
  xi = xi * 10 + (c - '0');
178
45.4M
  if (xf < 1e20) {
179
37.7M
    xf = xf * 10 + (c - '0');
180
37.7M
  }
181
45.4M
      } else if (c == '.') {
182
5.02M
  getChar();
183
5.02M
  goto doReal;
184
24.5M
      } else {
185
24.5M
  break;
186
24.5M
      }
187
75.0M
    }
188
24.8M
    while ((c = lookChar()) == '-' || isdigit(c)) {
189
345k
      getChar();
190
345k
    }
191
24.5M
    if (neg) {
192
782k
      xi = -xi;
193
782k
    }
194
24.5M
    if (doubleMinus) {
195
5.85k
      xi = 0;
196
5.85k
    }
197
24.5M
    obj->initInt(xi);
198
24.5M
    break;
199
5.93M
  doReal:
200
5.93M
    scale = 0.1;
201
19.3M
    while (1) {
202
19.3M
      c = lookChar();
203
19.3M
      if (c == '-') {
204
25.3k
  error(errSyntaxWarning, getPos(), "Badly formatted number");
205
25.3k
  getChar();
206
25.3k
  continue;
207
25.3k
      }
208
19.3M
      if (!isdigit(c)) {
209
5.93M
  break;
210
5.93M
      }
211
13.3M
      getChar();
212
13.3M
      xf = xf + scale * (c - '0');
213
13.3M
      scale *= 0.1;
214
13.3M
    }
215
5.93M
    while ((c = lookChar()) == '-' || isdigit(c)) {
216
0
      getChar();
217
0
    }
218
5.93M
    if (neg) {
219
881k
      xf = -xf;
220
881k
    }
221
5.93M
    obj->initReal(xf);
222
5.93M
    break;
223
224
  // string
225
603k
  case '(':
226
603k
    p = tokBuf;
227
603k
    n = 0;
228
603k
    numParen = 1;
229
603k
    done = gFalse;
230
603k
    s = NULL;
231
267M
    do {
232
267M
      c2 = EOF;
233
267M
      switch (c = getChar()) {
234
235
14.7k
      case EOF:
236
14.7k
  error(errSyntaxError, getPos(), "Unterminated string");
237
14.7k
  done = gTrue;
238
14.7k
  break;
239
240
1.26M
      case '(':
241
1.26M
  ++numParen;
242
1.26M
  c2 = c;
243
1.26M
  break;
244
245
1.28M
      case ')':
246
1.28M
  if (--numParen == 0) {
247
588k
    done = gTrue;
248
701k
  } else {
249
701k
    c2 = c;
250
701k
  }
251
1.28M
  break;
252
253
4.25M
      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.25M
  c = lookChar();
257
4.25M
  if (c == '\n') {
258
75.0k
    getChar();
259
75.0k
  }
260
4.25M
  c2 = '\n';
261
4.25M
  break;
262
263
532k
      case '\\':
264
532k
  switch (c = getChar()) {
265
4.21k
  case 'n':
266
4.21k
    c2 = '\n';
267
4.21k
    break;
268
55.0k
  case 'r':
269
55.0k
    c2 = '\r';
270
55.0k
    break;
271
3.41k
  case 't':
272
3.41k
    c2 = '\t';
273
3.41k
    break;
274
3.10k
  case 'b':
275
3.10k
    c2 = '\b';
276
3.10k
    break;
277
2.53k
  case 'f':
278
2.53k
    c2 = '\f';
279
2.53k
    break;
280
36.9k
  case '\\':
281
69.2k
  case '(':
282
102k
  case ')':
283
102k
    c2 = c;
284
102k
    break;
285
104k
  case '0': case '1': case '2': case '3':
286
120k
  case '4': case '5': case '6': case '7':
287
120k
    c2 = c - '0';
288
120k
    c = lookChar();
289
120k
    if (c >= '0' && c <= '7') {
290
97.9k
      getChar();
291
97.9k
      c2 = (c2 << 3) + (c - '0');
292
97.9k
      c = lookChar();
293
97.9k
      if (c >= '0' && c <= '7') {
294
87.0k
        getChar();
295
87.0k
        c2 = (c2 << 3) + (c - '0');
296
87.0k
      }
297
97.9k
    }
298
120k
    break;
299
3.27k
  case '\r':
300
3.27k
    c = lookChar();
301
3.27k
    if (c == '\n') {
302
64
      getChar();
303
64
    }
304
3.27k
    break;
305
2.07k
  case '\n':
306
2.07k
    break;
307
320
  case EOF:
308
320
    error(errSyntaxError, getPos(), "Unterminated string");
309
320
    done = gTrue;
310
320
    break;
311
235k
  default:
312
235k
    c2 = c;
313
235k
    break;
314
532k
  }
315
532k
  break;
316
317
259M
      default:
318
259M
  c2 = c;
319
259M
  break;
320
267M
      }
321
322
267M
      if (c2 != EOF) {
323
266M
  if (n == tokBufSize) {
324
1.99M
    if (!s)
325
54.3k
      s = new GString(tokBuf, tokBufSize);
326
1.93M
    else
327
1.93M
      s->append(tokBuf, tokBufSize);
328
1.99M
    p = tokBuf;
329
1.99M
    n = 0;
330
1.99M
  }
331
266M
  *p++ = (char)c2;
332
266M
  ++n;
333
266M
      }
334
267M
    } while (!done);
335
603k
    if (!s)
336
549k
      s = new GString(tokBuf, n);
337
54.3k
    else
338
54.3k
      s->append(tokBuf, n);
339
603k
    obj->initString(s);
340
603k
    break;
341
342
  // name
343
4.68M
  case '/':
344
4.68M
    p = tokBuf;
345
4.68M
    n = 0;
346
4.68M
    s = NULL;
347
4.68M
    invalid = gFalse;
348
36.3M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
349
31.6M
      getChar();
350
31.6M
      if (c == '#') {
351
40.6k
  c2 = lookChar();
352
40.6k
  if (c2 >= '0' && c2 <= '9') {
353
2.35k
    c = c2 - '0';
354
38.3k
  } else if (c2 >= 'A' && c2 <= 'F') {
355
3.04k
    c = c2 - 'A' + 10;
356
35.2k
  } else if (c2 >= 'a' && c2 <= 'f') {
357
2.86k
    c = c2 - 'a' + 10;
358
32.3k
  } else {
359
32.3k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
360
32.3k
    goto notEscChar;
361
32.3k
  }
362
8.26k
  getChar();
363
8.26k
  c2 = lookChar();
364
8.26k
  if (c2 >= '0' && c2 <= '9') {
365
1.32k
    c = (c << 4) + (c2 - '0');
366
6.93k
  } else if (c2 >= 'A' && c2 <= 'F') {
367
753
    c = (c << 4) + (c2 - 'A' + 10);
368
6.17k
  } else if (c2 >= 'a' && c2 <= 'f') {
369
3.25k
    c = (c << 4) + (c2 - 'a' + 10);
370
3.25k
  } else {
371
2.92k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
372
2.92k
    goto notEscChar;
373
2.92k
  }
374
5.33k
  getChar();
375
5.33k
  if (c == 0) {
376
164
    invalid = gTrue;
377
164
  }
378
5.33k
      }
379
31.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
31.6M
      ++n;
384
31.6M
      if (n < tokBufSize) {
385
26.0M
  *p++ = (char)c;
386
26.0M
      } else if (n == tokBufSize) {
387
8.42k
  *p = (char)c;
388
8.42k
  s = new GString(tokBuf, n);
389
5.58M
      } else {
390
5.58M
  s->append((char)c);
391
5.58M
      }
392
31.6M
    }
393
4.68M
    if (invalid) {
394
149
      error(errSyntaxError, getPos(), "Null character in name");
395
149
      obj->initError();
396
149
      if (s) {
397
18
  delete s;
398
18
      }
399
4.68M
    } else if (n < tokBufSize) {
400
4.67M
      *p = '\0';
401
4.67M
      obj->initName(tokBuf);
402
4.67M
    } else {
403
8.40k
      obj->initName(s->getCString());
404
8.40k
      delete s;
405
8.40k
    }
406
4.68M
    break;
407
408
  // array punctuation
409
5.25M
  case '[':
410
5.72M
  case ']':
411
5.72M
    tokBuf[0] = (char)c;
412
5.72M
    tokBuf[1] = '\0';
413
5.72M
    obj->initCmd(tokBuf);
414
5.72M
    break;
415
416
  // hex string or dict punctuation
417
15.4M
  case '<':
418
15.4M
    c = lookChar();
419
420
    // dict punctuation
421
15.4M
    if (c == '<') {
422
799k
      getChar();
423
799k
      tokBuf[0] = tokBuf[1] = '<';
424
799k
      tokBuf[2] = '\0';
425
799k
      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.3M
      while (nErrors < 100) {
435
49.3M
  c = getChar();
436
49.3M
  if (c == '>') {
437
14.6M
    break;
438
34.7M
  } else if (c == EOF) {
439
8.22k
    error(errSyntaxError, getPos(), "Unterminated hex string");
440
8.22k
    break;
441
34.7M
  } else if (specialChars[c] != 1) {
442
28.1M
    c2 = c2 << 4;
443
28.1M
    if (c >= '0' && c <= '9') {
444
6.33M
      c2 += c - '0';
445
21.8M
    } else if (c >= 'A' && c <= 'F') {
446
531k
      c2 += c - 'A' + 10;
447
21.2M
    } else if (c >= 'a' && c <= 'f') {
448
15.1M
      c2 += c - 'a' + 10;
449
15.1M
    } else {
450
6.14M
      error(errSyntaxError, getPos(),
451
6.14M
      "Illegal character <{0:02x}> in hex string", c);
452
6.14M
      ++nErrors;
453
6.14M
    }
454
28.1M
    if (++m == 2) {
455
6.93M
      if (n == tokBufSize) {
456
10.5k
        if (!s)
457
8.76k
    s = new GString(tokBuf, tokBufSize);
458
1.76k
        else
459
1.76k
    s->append(tokBuf, tokBufSize);
460
10.5k
        p = tokBuf;
461
10.5k
        n = 0;
462
10.5k
      }
463
6.93M
      *p++ = (char)c2;
464
6.93M
      ++n;
465
6.93M
      c2 = 0;
466
6.93M
      m = 0;
467
6.93M
    }
468
28.1M
  }
469
49.3M
      }
470
14.6M
      if (!s)
471
14.6M
  s = new GString(tokBuf, n);
472
8.76k
      else
473
8.76k
  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.4M
    break;
479
480
  // dict punctuation
481
2.26M
  case '>':
482
2.26M
    c = lookChar();
483
2.26M
    if (c == '>') {
484
749k
      getChar();
485
749k
      tokBuf[0] = tokBuf[1] = '>';
486
749k
      tokBuf[2] = '\0';
487
749k
      obj->initCmd(tokBuf);
488
1.51M
    } else {
489
1.51M
      error(errSyntaxError, getPos(), "Illegal character '>'");
490
1.51M
      obj->initError();
491
1.51M
    }
492
2.26M
    break;
493
494
  // error
495
815k
  case ')':
496
1.30M
  case '{':
497
1.36M
  case '}':
498
1.36M
    error(errSyntaxError, getPos(), "Illegal character '{0:c}'", c);
499
1.36M
    obj->initError();
500
1.36M
    break;
501
502
  // command
503
19.7M
  default:
504
19.7M
    p = tokBuf;
505
19.7M
    *p++ = (char)c;
506
19.7M
    n = 1;
507
137M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
508
117M
      getChar();
509
117M
      if (++n == tokBufSize) {
510
521k
  error(errSyntaxError, getPos(), "Command token too long");
511
521k
  break;
512
521k
      }
513
117M
      *p++ = (char)c;
514
117M
    }
515
19.7M
    *p = '\0';
516
19.7M
    if (tokBuf[0] == 't' && !strcmp(tokBuf, "true")) {
517
68.1k
      obj->initBool(gTrue);
518
19.7M
    } else if (tokBuf[0] == 'f' && !strcmp(tokBuf, "false")) {
519
11.5k
      obj->initBool(gFalse);
520
19.7M
    } else if (tokBuf[0] == 'n' && !strcmp(tokBuf, "null")) {
521
15.0k
      obj->initNull();
522
19.7M
    } else {
523
19.7M
      obj->initCmd(tokBuf);
524
19.7M
    }
525
19.7M
    break;
526
80.3M
  }
527
528
80.3M
  return obj;
529
80.3M
}
530
531
148k
void Lexer::skipToNextLine() {
532
148k
  int c;
533
534
377k
  while (1) {
535
377k
    c = getChar();
536
377k
    if (c == EOF || c == '\n') {
537
86.7k
      return;
538
86.7k
    }
539
290k
    if (c == '\r') {
540
61.7k
      if ((c = lookChar()) == '\n') {
541
48.0k
  getChar();
542
48.0k
      }
543
61.7k
      return;
544
61.7k
    }
545
290k
  }
546
148k
}
547
548
33.9k
void Lexer::skipToEOF() {
549
1.64M
  while (getChar() != EOF) ;
550
33.9k
}
551
552
192M
GBool Lexer::isSpace(int c) {
553
192M
  return c >= 0 && c <= 0xff && specialChars[c] == 1;
554
192M
}