Coverage Report

Created: 2026-10-13 06:47

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
267k
Lexer::Lexer(XRef *xref, Stream *str) {
47
267k
  Object obj;
48
49
267k
  curStr.initStream(str);
50
267k
  streams = new Array(xref);
51
267k
  streams->add(curStr.copy(&obj));
52
267k
  strPtr = 0;
53
267k
  freeArray = gTrue;
54
267k
  curStr.streamReset();
55
267k
}
56
57
46.4k
Lexer::Lexer(XRef *xref, Object *obj) {
58
46.4k
  Object obj2;
59
60
46.4k
  if (obj->isStream()) {
61
45.9k
    streams = new Array(xref);
62
45.9k
    freeArray = gTrue;
63
45.9k
    streams->add(obj->copy(&obj2));
64
45.9k
  } else {
65
494
    streams = obj->getArray();
66
494
    freeArray = gFalse;
67
494
  }
68
46.4k
  strPtr = 0;
69
46.4k
  if (streams->getLength() > 0) {
70
46.4k
    streams->get(strPtr, &curStr);
71
46.4k
    curStr.streamReset();
72
46.4k
  }
73
46.4k
}
74
75
314k
Lexer::~Lexer() {
76
314k
  if (!curStr.isNone()) {
77
215k
    curStr.streamClose();
78
215k
    curStr.free();
79
215k
  }
80
314k
  if (freeArray) {
81
313k
    delete streams;
82
313k
  }
83
314k
}
84
85
487M
int Lexer::getChar() {
86
487M
  int c;
87
88
487M
  c = EOF;
89
487M
  while (!curStr.isNone() && (c = curStr.streamGetChar()) == EOF) {
90
100k
    curStr.streamClose();
91
100k
    curStr.free();
92
100k
    ++strPtr;
93
100k
    if (strPtr < streams->getLength()) {
94
1.38k
      streams->get(strPtr, &curStr);
95
1.38k
      curStr.streamReset();
96
1.38k
    }
97
100k
  }
98
487M
  return c;
99
487M
}
100
101
235M
int Lexer::lookChar() {
102
235M
  if (curStr.isNone()) {
103
0
    return EOF;
104
0
  }
105
235M
  return curStr.streamLookChar();
106
235M
}
107
108
70.8M
Object *Lexer::getObj(Object *obj) {
109
70.8M
  char *p;
110
70.8M
  int c, c2;
111
70.8M
  GBool comment, neg, doubleMinus, done, invalid;
112
70.8M
  int numParen, nErrors;
113
70.8M
  int xi;
114
70.8M
  double xf, scale;
115
70.8M
  GString *s;
116
70.8M
  int n, m;
117
118
  // skip whitespace and comments
119
70.8M
  comment = gFalse;
120
155M
  while (1) {
121
155M
    if ((c = getChar()) == EOF) {
122
11.1M
      return obj->initEOF();
123
11.1M
    }
124
143M
    if (comment) {
125
4.35M
      if (c == '\r' || c == '\n')
126
50.9k
  comment = gFalse;
127
139M
    } else if (c == '%') {
128
51.1k
      comment = gTrue;
129
139M
    } else if (specialChars[c] != 1) {
130
59.7M
      break;
131
59.7M
    }
132
143M
  }
133
134
  // start reading token
135
59.7M
  switch (c) {
136
137
  // number
138
11.5M
  case '0': case '1': case '2': case '3': case '4':
139
15.9M
  case '5': case '6': case '7': case '8': case '9':
140
17.3M
  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
17.3M
    neg = gFalse;
156
17.3M
    doubleMinus = gFalse;
157
17.3M
    xf = xi = 0;
158
17.3M
    if (c == '+') {
159
      // just ignore it
160
17.3M
    } else if (c == '-') {
161
684k
      neg = gTrue;
162
684k
      if (lookChar() == '-') {
163
3.28k
  doubleMinus = gTrue;
164
3.42k
  do {
165
3.42k
    getChar();
166
3.42k
  } while (lookChar() == '-');
167
3.28k
      }
168
16.6M
    } else if (c == '.') {
169
629k
      goto doReal;
170
15.9M
    } else {
171
15.9M
      xf = xi = c - '0';
172
15.9M
    }
173
51.5M
    while (1) {
174
51.5M
      c = lookChar();
175
51.5M
      if (isdigit(c)) {
176
34.8M
  getChar();
177
34.8M
  xi = xi * 10 + (c - '0');
178
34.8M
  if (xf < 1e20) {
179
27.1M
    xf = xf * 10 + (c - '0');
180
27.1M
  }
181
34.8M
      } else if (c == '.') {
182
2.50M
  getChar();
183
2.50M
  goto doReal;
184
14.1M
      } else {
185
14.1M
  break;
186
14.1M
      }
187
51.5M
    }
188
14.3M
    while ((c = lookChar()) == '-' || isdigit(c)) {
189
139k
      getChar();
190
139k
    }
191
14.1M
    if (neg) {
192
447k
      xi = -xi;
193
447k
    }
194
14.1M
    if (doubleMinus) {
195
1.58k
      xi = 0;
196
1.58k
    }
197
14.1M
    obj->initInt(xi);
198
14.1M
    break;
199
3.13M
  doReal:
200
3.13M
    scale = 0.1;
201
10.9M
    while (1) {
202
10.9M
      c = lookChar();
203
10.9M
      if (c == '-') {
204
19.3k
  error(errSyntaxWarning, getPos(), "Badly formatted number");
205
19.3k
  getChar();
206
19.3k
  continue;
207
19.3k
      }
208
10.9M
      if (!isdigit(c)) {
209
3.13M
  break;
210
3.13M
      }
211
7.78M
      getChar();
212
7.78M
      xf = xf + scale * (c - '0');
213
7.78M
      scale *= 0.1;
214
7.78M
    }
215
3.13M
    while ((c = lookChar()) == '-' || isdigit(c)) {
216
0
      getChar();
217
0
    }
218
3.13M
    if (neg) {
219
237k
      xf = -xf;
220
237k
    }
221
3.13M
    obj->initReal(xf);
222
3.13M
    break;
223
224
  // string
225
469k
  case '(':
226
469k
    p = tokBuf;
227
469k
    n = 0;
228
469k
    numParen = 1;
229
469k
    done = gFalse;
230
469k
    s = NULL;
231
122M
    do {
232
122M
      c2 = EOF;
233
122M
      switch (c = getChar()) {
234
235
7.00k
      case EOF:
236
7.00k
  error(errSyntaxError, getPos(), "Unterminated string");
237
7.00k
  done = gTrue;
238
7.00k
  break;
239
240
705k
      case '(':
241
705k
  ++numParen;
242
705k
  c2 = c;
243
705k
  break;
244
245
951k
      case ')':
246
951k
  if (--numParen == 0) {
247
462k
    done = gTrue;
248
489k
  } else {
249
489k
    c2 = c;
250
489k
  }
251
951k
  break;
252
253
3.63M
      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.63M
  c = lookChar();
257
3.63M
  if (c == '\n') {
258
54.2k
    getChar();
259
54.2k
  }
260
3.63M
  c2 = '\n';
261
3.63M
  break;
262
263
339k
      case '\\':
264
339k
  switch (c = getChar()) {
265
1.87k
  case 'n':
266
1.87k
    c2 = '\n';
267
1.87k
    break;
268
50.0k
  case 'r':
269
50.0k
    c2 = '\r';
270
50.0k
    break;
271
2.37k
  case 't':
272
2.37k
    c2 = '\t';
273
2.37k
    break;
274
1.96k
  case 'b':
275
1.96k
    c2 = '\b';
276
1.96k
    break;
277
812
  case 'f':
278
812
    c2 = '\f';
279
812
    break;
280
28.3k
  case '\\':
281
58.5k
  case '(':
282
85.4k
  case ')':
283
85.4k
    c2 = c;
284
85.4k
    break;
285
58.5k
  case '0': case '1': case '2': case '3':
286
72.7k
  case '4': case '5': case '6': case '7':
287
72.7k
    c2 = c - '0';
288
72.7k
    c = lookChar();
289
72.7k
    if (c >= '0' && c <= '7') {
290
48.4k
      getChar();
291
48.4k
      c2 = (c2 << 3) + (c - '0');
292
48.4k
      c = lookChar();
293
48.4k
      if (c >= '0' && c <= '7') {
294
38.1k
        getChar();
295
38.1k
        c2 = (c2 << 3) + (c - '0');
296
38.1k
      }
297
48.4k
    }
298
72.7k
    break;
299
2.84k
  case '\r':
300
2.84k
    c = lookChar();
301
2.84k
    if (c == '\n') {
302
0
      getChar();
303
0
    }
304
2.84k
    break;
305
1.07k
  case '\n':
306
1.07k
    break;
307
200
  case EOF:
308
200
    error(errSyntaxError, getPos(), "Unterminated string");
309
200
    done = gTrue;
310
200
    break;
311
119k
  default:
312
119k
    c2 = c;
313
119k
    break;
314
339k
  }
315
339k
  break;
316
317
116M
      default:
318
116M
  c2 = c;
319
116M
  break;
320
122M
      }
321
322
122M
      if (c2 != EOF) {
323
121M
  if (n == tokBufSize) {
324
892k
    if (!s)
325
37.8k
      s = new GString(tokBuf, tokBufSize);
326
855k
    else
327
855k
      s->append(tokBuf, tokBufSize);
328
892k
    p = tokBuf;
329
892k
    n = 0;
330
892k
  }
331
121M
  *p++ = (char)c2;
332
121M
  ++n;
333
121M
      }
334
122M
    } while (!done);
335
469k
    if (!s)
336
431k
      s = new GString(tokBuf, n);
337
37.8k
    else
338
37.8k
      s->append(tokBuf, n);
339
469k
    obj->initString(s);
340
469k
    break;
341
342
  // name
343
3.74M
  case '/':
344
3.74M
    p = tokBuf;
345
3.74M
    n = 0;
346
3.74M
    s = NULL;
347
3.74M
    invalid = gFalse;
348
25.1M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
349
21.3M
      getChar();
350
21.3M
      if (c == '#') {
351
30.5k
  c2 = lookChar();
352
30.5k
  if (c2 >= '0' && c2 <= '9') {
353
1.49k
    c = c2 - '0';
354
29.0k
  } else if (c2 >= 'A' && c2 <= 'F') {
355
2.62k
    c = c2 - 'A' + 10;
356
26.4k
  } else if (c2 >= 'a' && c2 <= 'f') {
357
2.57k
    c = c2 - 'a' + 10;
358
23.8k
  } else {
359
23.8k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
360
23.8k
    goto notEscChar;
361
23.8k
  }
362
6.69k
  getChar();
363
6.69k
  c2 = lookChar();
364
6.69k
  if (c2 >= '0' && c2 <= '9') {
365
876
    c = (c << 4) + (c2 - '0');
366
5.82k
  } else if (c2 >= 'A' && c2 <= 'F') {
367
29
    c = (c << 4) + (c2 - 'A' + 10);
368
5.79k
  } else if (c2 >= 'a' && c2 <= 'f') {
369
3.55k
    c = (c << 4) + (c2 - 'a' + 10);
370
3.55k
  } else {
371
2.23k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
372
2.23k
    goto notEscChar;
373
2.23k
  }
374
4.46k
  getChar();
375
4.46k
  if (c == 0) {
376
9
    invalid = gTrue;
377
9
  }
378
4.46k
      }
379
21.3M
     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
21.3M
      ++n;
384
21.3M
      if (n < tokBufSize) {
385
20.8M
  *p++ = (char)c;
386
20.8M
      } else if (n == tokBufSize) {
387
6.42k
  *p = (char)c;
388
6.42k
  s = new GString(tokBuf, n);
389
562k
      } else {
390
562k
  s->append((char)c);
391
562k
      }
392
21.3M
    }
393
3.74M
    if (invalid) {
394
9
      error(errSyntaxError, getPos(), "Null character in name");
395
9
      obj->initError();
396
9
      if (s) {
397
0
  delete s;
398
0
      }
399
3.74M
    } else if (n < tokBufSize) {
400
3.74M
      *p = '\0';
401
3.74M
      obj->initName(tokBuf);
402
3.74M
    } else {
403
6.42k
      obj->initName(s->getCString());
404
6.42k
      delete s;
405
6.42k
    }
406
3.74M
    break;
407
408
  // array punctuation
409
5.09M
  case '[':
410
5.46M
  case ']':
411
5.46M
    tokBuf[0] = (char)c;
412
5.46M
    tokBuf[1] = '\0';
413
5.46M
    obj->initCmd(tokBuf);
414
5.46M
    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
624k
      getChar();
423
624k
      tokBuf[0] = tokBuf[1] = '<';
424
624k
      tokBuf[2] = '\0';
425
624k
      obj->initCmd(tokBuf);
426
427
    // hex string
428
14.8M
    } else {
429
14.8M
      p = tokBuf;
430
14.8M
      m = n = 0;
431
14.8M
      c2 = 0;
432
14.8M
      s = NULL;
433
14.8M
      nErrors = 0;
434
45.4M
      while (nErrors < 100) {
435
45.4M
  c = getChar();
436
45.4M
  if (c == '>') {
437
14.7M
    break;
438
30.6M
  } else if (c == EOF) {
439
3.37k
    error(errSyntaxError, getPos(), "Unterminated hex string");
440
3.37k
    break;
441
30.6M
  } else if (specialChars[c] != 1) {
442
26.8M
    c2 = c2 << 4;
443
26.8M
    if (c >= '0' && c <= '9') {
444
6.34M
      c2 += c - '0';
445
20.5M
    } else if (c >= 'A' && c <= 'F') {
446
493k
      c2 += c - 'A' + 10;
447
20.0M
    } else if (c >= 'a' && c <= 'f') {
448
14.9M
      c2 += c - 'a' + 10;
449
14.9M
    } else {
450
5.06M
      error(errSyntaxError, getPos(),
451
5.06M
      "Illegal character <{0:02x}> in hex string", c);
452
5.06M
      ++nErrors;
453
5.06M
    }
454
26.8M
    if (++m == 2) {
455
6.30M
      if (n == tokBufSize) {
456
7.13k
        if (!s)
457
5.67k
    s = new GString(tokBuf, tokBufSize);
458
1.45k
        else
459
1.45k
    s->append(tokBuf, tokBufSize);
460
7.13k
        p = tokBuf;
461
7.13k
        n = 0;
462
7.13k
      }
463
6.30M
      *p++ = (char)c2;
464
6.30M
      ++n;
465
6.30M
      c2 = 0;
466
6.30M
      m = 0;
467
6.30M
    }
468
26.8M
  }
469
45.4M
      }
470
14.8M
      if (!s)
471
14.8M
  s = new GString(tokBuf, n);
472
5.67k
      else
473
5.67k
  s->append(tokBuf, n);
474
14.8M
      if (m == 1)
475
14.2M
  s->append((char)(c2 << 4));
476
14.8M
      obj->initString(s);
477
14.8M
    }
478
15.4M
    break;
479
480
  // dict punctuation
481
2.05M
  case '>':
482
2.05M
    c = lookChar();
483
2.05M
    if (c == '>') {
484
592k
      getChar();
485
592k
      tokBuf[0] = tokBuf[1] = '>';
486
592k
      tokBuf[2] = '\0';
487
592k
      obj->initCmd(tokBuf);
488
1.46M
    } else {
489
1.46M
      error(errSyntaxError, getPos(), "Illegal character '>'");
490
1.46M
      obj->initError();
491
1.46M
    }
492
2.05M
    break;
493
494
  // error
495
688k
  case ')':
496
1.12M
  case '{':
497
1.17M
  case '}':
498
1.17M
    error(errSyntaxError, getPos(), "Illegal character '{0:c}'", c);
499
1.17M
    obj->initError();
500
1.17M
    break;
501
502
  // command
503
14.0M
  default:
504
14.0M
    p = tokBuf;
505
14.0M
    *p++ = (char)c;
506
14.0M
    n = 1;
507
108M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
508
94.7M
      getChar();
509
94.7M
      if (++n == tokBufSize) {
510
411k
  error(errSyntaxError, getPos(), "Command token too long");
511
411k
  break;
512
411k
      }
513
94.2M
      *p++ = (char)c;
514
94.2M
    }
515
14.0M
    *p = '\0';
516
14.0M
    if (tokBuf[0] == 't' && !strcmp(tokBuf, "true")) {
517
74.4k
      obj->initBool(gTrue);
518
13.9M
    } else if (tokBuf[0] == 'f' && !strcmp(tokBuf, "false")) {
519
12.2k
      obj->initBool(gFalse);
520
13.9M
    } else if (tokBuf[0] == 'n' && !strcmp(tokBuf, "null")) {
521
14.3k
      obj->initNull();
522
13.9M
    } else {
523
13.9M
      obj->initCmd(tokBuf);
524
13.9M
    }
525
14.0M
    break;
526
59.7M
  }
527
528
59.7M
  return obj;
529
59.7M
}
530
531
113k
void Lexer::skipToNextLine() {
532
113k
  int c;
533
534
185k
  while (1) {
535
185k
    c = getChar();
536
185k
    if (c == EOF || c == '\n') {
537
56.4k
      return;
538
56.4k
    }
539
128k
    if (c == '\r') {
540
57.4k
      if ((c = lookChar()) == '\n') {
541
53.5k
  getChar();
542
53.5k
      }
543
57.4k
      return;
544
57.4k
    }
545
128k
  }
546
113k
}
547
548
29.7k
void Lexer::skipToEOF() {
549
1.36M
  while (getChar() != EOF) ;
550
29.7k
}
551
552
201M
GBool Lexer::isSpace(int c) {
553
201M
  return c >= 0 && c <= 0xff && specialChars[c] == 1;
554
201M
}