Coverage Report

Created: 2026-08-31 06:45

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
458k
Lexer::Lexer(XRef *xref, Stream *str) {
47
458k
  Object obj;
48
49
458k
  curStr.initStream(str);
50
458k
  streams = new Array(xref);
51
458k
  streams->add(curStr.copy(&obj));
52
458k
  strPtr = 0;
53
458k
  freeArray = gTrue;
54
458k
  curStr.streamReset();
55
458k
}
56
57
150k
Lexer::Lexer(XRef *xref, Object *obj) {
58
150k
  Object obj2;
59
60
150k
  if (obj->isStream()) {
61
149k
    streams = new Array(xref);
62
149k
    freeArray = gTrue;
63
149k
    streams->add(obj->copy(&obj2));
64
149k
  } else {
65
941
    streams = obj->getArray();
66
941
    freeArray = gFalse;
67
941
  }
68
150k
  strPtr = 0;
69
150k
  if (streams->getLength() > 0) {
70
150k
    streams->get(strPtr, &curStr);
71
150k
    curStr.streamReset();
72
150k
  }
73
150k
}
74
75
608k
Lexer::~Lexer() {
76
608k
  if (!curStr.isNone()) {
77
318k
    curStr.streamClose();
78
318k
    curStr.free();
79
318k
  }
80
608k
  if (freeArray) {
81
607k
    delete streams;
82
607k
  }
83
608k
}
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
291k
    curStr.streamClose();
91
291k
    curStr.free();
92
291k
    ++strPtr;
93
291k
    if (strPtr < streams->getLength()) {
94
1.25k
      streams->get(strPtr, &curStr);
95
1.25k
      curStr.streamReset();
96
1.25k
    }
97
291k
  }
98
1.22G
  return c;
99
1.22G
}
100
101
315M
int Lexer::lookChar() {
102
315M
  if (curStr.isNone()) {
103
179
    return EOF;
104
179
  }
105
315M
  return curStr.streamLookChar();
106
315M
}
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
676M
  while (1) {
121
676M
    if ((c = getChar()) == EOF) {
122
97.7M
      return obj->initEOF();
123
97.7M
    }
124
578M
    if (comment) {
125
56.0M
      if (c == '\r' || c == '\n')
126
470k
  comment = gFalse;
127
522M
    } else if (c == '%') {
128
472k
      comment = gTrue;
129
521M
    } else if (specialChars[c] != 1) {
130
86.0M
      break;
131
86.0M
    }
132
578M
  }
133
134
  // start reading token
135
86.0M
  switch (c) {
136
137
  // number
138
20.3M
  case '0': case '1': case '2': case '3': case '4':
139
31.6M
  case '5': case '6': case '7': case '8': case '9':
140
34.2M
  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.2M
    neg = gFalse;
156
34.2M
    doubleMinus = gFalse;
157
34.2M
    xf = xi = 0;
158
34.2M
    if (c == '+') {
159
      // just ignore it
160
34.2M
    } else if (c == '-') {
161
1.73M
      neg = gTrue;
162
1.73M
      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.5M
    } else if (c == '.') {
169
842k
      goto doReal;
170
31.6M
    } else {
171
31.6M
      xf = xi = c - '0';
172
31.6M
    }
173
83.2M
    while (1) {
174
83.2M
      c = lookChar();
175
83.2M
      if (isdigit(c)) {
176
49.7M
  getChar();
177
49.7M
  xi = xi * 10 + (c - '0');
178
49.7M
  if (xf < 1e20) {
179
42.1M
    xf = xf * 10 + (c - '0');
180
42.1M
  }
181
49.7M
      } else if (c == '.') {
182
5.17M
  getChar();
183
5.17M
  goto doReal;
184
28.2M
      } else {
185
28.2M
  break;
186
28.2M
      }
187
83.2M
    }
188
28.7M
    while ((c = lookChar()) == '-' || isdigit(c)) {
189
431k
      getChar();
190
431k
    }
191
28.2M
    if (neg) {
192
792k
      xi = -xi;
193
792k
    }
194
28.2M
    if (doubleMinus) {
195
5.92k
      xi = 0;
196
5.92k
    }
197
28.2M
    obj->initInt(xi);
198
28.2M
    break;
199
6.01M
  doReal:
200
6.01M
    scale = 0.1;
201
18.7M
    while (1) {
202
18.7M
      c = lookChar();
203
18.7M
      if (c == '-') {
204
25.6k
  error(errSyntaxWarning, getPos(), "Badly formatted number");
205
25.6k
  getChar();
206
25.6k
  continue;
207
25.6k
      }
208
18.7M
      if (!isdigit(c)) {
209
6.01M
  break;
210
6.01M
      }
211
12.6M
      getChar();
212
12.6M
      xf = xf + scale * (c - '0');
213
12.6M
      scale *= 0.1;
214
12.6M
    }
215
6.01M
    while ((c = lookChar()) == '-' || isdigit(c)) {
216
0
      getChar();
217
0
    }
218
6.01M
    if (neg) {
219
946k
      xf = -xf;
220
946k
    }
221
6.01M
    obj->initReal(xf);
222
6.01M
    break;
223
224
  // string
225
686k
  case '(':
226
686k
    p = tokBuf;
227
686k
    n = 0;
228
686k
    numParen = 1;
229
686k
    done = gFalse;
230
686k
    s = NULL;
231
292M
    do {
232
292M
      c2 = EOF;
233
292M
      switch (c = getChar()) {
234
235
21.7k
      case EOF:
236
21.7k
  error(errSyntaxError, getPos(), "Unterminated string");
237
21.7k
  done = gTrue;
238
21.7k
  break;
239
240
1.19M
      case '(':
241
1.19M
  ++numParen;
242
1.19M
  c2 = c;
243
1.19M
  break;
244
245
1.42M
      case ')':
246
1.42M
  if (--numParen == 0) {
247
664k
    done = gTrue;
248
764k
  } else {
249
764k
    c2 = c;
250
764k
  }
251
1.42M
  break;
252
253
3.91M
      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.91M
  c = lookChar();
257
3.91M
  if (c == '\n') {
258
85.9k
    getChar();
259
85.9k
  }
260
3.91M
  c2 = '\n';
261
3.91M
  break;
262
263
511k
      case '\\':
264
511k
  switch (c = getChar()) {
265
4.31k
  case 'n':
266
4.31k
    c2 = '\n';
267
4.31k
    break;
268
45.1k
  case 'r':
269
45.1k
    c2 = '\r';
270
45.1k
    break;
271
3.10k
  case 't':
272
3.10k
    c2 = '\t';
273
3.10k
    break;
274
2.51k
  case 'b':
275
2.51k
    c2 = '\b';
276
2.51k
    break;
277
1.90k
  case 'f':
278
1.90k
    c2 = '\f';
279
1.90k
    break;
280
30.6k
  case '\\':
281
67.4k
  case '(':
282
105k
  case ')':
283
105k
    c2 = c;
284
105k
    break;
285
92.5k
  case '0': case '1': case '2': case '3':
286
108k
  case '4': case '5': case '6': case '7':
287
108k
    c2 = c - '0';
288
108k
    c = lookChar();
289
108k
    if (c >= '0' && c <= '7') {
290
81.5k
      getChar();
291
81.5k
      c2 = (c2 << 3) + (c - '0');
292
81.5k
      c = lookChar();
293
81.5k
      if (c >= '0' && c <= '7') {
294
70.8k
        getChar();
295
70.8k
        c2 = (c2 << 3) + (c - '0');
296
70.8k
      }
297
81.5k
    }
298
108k
    break;
299
2.53k
  case '\r':
300
2.53k
    c = lookChar();
301
2.53k
    if (c == '\n') {
302
73
      getChar();
303
73
    }
304
2.53k
    break;
305
1.86k
  case '\n':
306
1.86k
    break;
307
289
  case EOF:
308
289
    error(errSyntaxError, getPos(), "Unterminated string");
309
289
    done = gTrue;
310
289
    break;
311
236k
  default:
312
236k
    c2 = c;
313
236k
    break;
314
511k
  }
315
511k
  break;
316
317
285M
      default:
318
285M
  c2 = c;
319
285M
  break;
320
292M
      }
321
322
292M
      if (c2 != EOF) {
323
291M
  if (n == tokBufSize) {
324
2.17M
    if (!s)
325
67.6k
      s = new GString(tokBuf, tokBufSize);
326
2.10M
    else
327
2.10M
      s->append(tokBuf, tokBufSize);
328
2.17M
    p = tokBuf;
329
2.17M
    n = 0;
330
2.17M
  }
331
291M
  *p++ = (char)c2;
332
291M
  ++n;
333
291M
      }
334
292M
    } while (!done);
335
686k
    if (!s)
336
619k
      s = new GString(tokBuf, n);
337
67.6k
    else
338
67.6k
      s->append(tokBuf, n);
339
686k
    obj->initString(s);
340
686k
    break;
341
342
  // name
343
5.21M
  case '/':
344
5.21M
    p = tokBuf;
345
5.21M
    n = 0;
346
5.21M
    s = NULL;
347
5.21M
    invalid = gFalse;
348
38.7M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
349
33.5M
      getChar();
350
33.5M
      if (c == '#') {
351
46.8k
  c2 = lookChar();
352
46.8k
  if (c2 >= '0' && c2 <= '9') {
353
3.55k
    c = c2 - '0';
354
43.3k
  } else if (c2 >= 'A' && c2 <= 'F') {
355
4.36k
    c = c2 - 'A' + 10;
356
38.9k
  } else if (c2 >= 'a' && c2 <= 'f') {
357
4.21k
    c = c2 - 'a' + 10;
358
34.7k
  } else {
359
34.7k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
360
34.7k
    goto notEscChar;
361
34.7k
  }
362
12.1k
  getChar();
363
12.1k
  c2 = lookChar();
364
12.1k
  if (c2 >= '0' && c2 <= '9') {
365
2.35k
    c = (c << 4) + (c2 - '0');
366
9.77k
  } else if (c2 >= 'A' && c2 <= 'F') {
367
292
    c = (c << 4) + (c2 - 'A' + 10);
368
9.48k
  } else if (c2 >= 'a' && c2 <= 'f') {
369
5.73k
    c = (c << 4) + (c2 - 'a' + 10);
370
5.73k
  } else {
371
3.74k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
372
3.74k
    goto notEscChar;
373
3.74k
  }
374
8.38k
  getChar();
375
8.38k
  if (c == 0) {
376
989
    invalid = gTrue;
377
989
  }
378
8.38k
      }
379
33.5M
     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.5M
      ++n;
384
33.5M
      if (n < tokBufSize) {
385
28.4M
  *p++ = (char)c;
386
28.4M
      } else if (n == tokBufSize) {
387
6.49k
  *p = (char)c;
388
6.49k
  s = new GString(tokBuf, n);
389
5.11M
      } else {
390
5.11M
  s->append((char)c);
391
5.11M
      }
392
33.5M
    }
393
5.21M
    if (invalid) {
394
987
      error(errSyntaxError, getPos(), "Null character in name");
395
987
      obj->initError();
396
987
      if (s) {
397
15
  delete s;
398
15
      }
399
5.21M
    } else if (n < tokBufSize) {
400
5.21M
      *p = '\0';
401
5.21M
      obj->initName(tokBuf);
402
5.21M
    } else {
403
6.47k
      obj->initName(s->getCString());
404
6.47k
      delete s;
405
6.47k
    }
406
5.21M
    break;
407
408
  // array punctuation
409
5.27M
  case '[':
410
5.79M
  case ']':
411
5.79M
    tokBuf[0] = (char)c;
412
5.79M
    tokBuf[1] = '\0';
413
5.79M
    obj->initCmd(tokBuf);
414
5.79M
    break;
415
416
  // hex string or dict punctuation
417
15.5M
  case '<':
418
15.5M
    c = lookChar();
419
420
    // dict punctuation
421
15.5M
    if (c == '<') {
422
908k
      getChar();
423
908k
      tokBuf[0] = tokBuf[1] = '<';
424
908k
      tokBuf[2] = '\0';
425
908k
      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.5M
  c = getChar();
436
49.5M
  if (c == '>') {
437
14.6M
    break;
438
34.9M
  } else if (c == EOF) {
439
6.45k
    error(errSyntaxError, getPos(), "Unterminated hex string");
440
6.45k
    break;
441
34.9M
  } else if (specialChars[c] != 1) {
442
30.2M
    c2 = c2 << 4;
443
30.2M
    if (c >= '0' && c <= '9') {
444
7.63M
      c2 += c - '0';
445
22.6M
    } else if (c >= 'A' && c <= 'F') {
446
572k
      c2 += c - 'A' + 10;
447
22.0M
    } else if (c >= 'a' && c <= 'f') {
448
15.4M
      c2 += c - 'a' + 10;
449
15.4M
    } else {
450
6.59M
      error(errSyntaxError, getPos(),
451
6.59M
      "Illegal character <{0:02x}> in hex string", c);
452
6.59M
      ++nErrors;
453
6.59M
    }
454
30.2M
    if (++m == 2) {
455
7.98M
      if (n == tokBufSize) {
456
11.8k
        if (!s)
457
9.44k
    s = new GString(tokBuf, tokBufSize);
458
2.36k
        else
459
2.36k
    s->append(tokBuf, tokBufSize);
460
11.8k
        p = tokBuf;
461
11.8k
        n = 0;
462
11.8k
      }
463
7.98M
      *p++ = (char)c2;
464
7.98M
      ++n;
465
7.98M
      c2 = 0;
466
7.98M
      m = 0;
467
7.98M
    }
468
30.2M
  }
469
49.5M
      }
470
14.6M
      if (!s)
471
14.6M
  s = new GString(tokBuf, n);
472
9.44k
      else
473
9.44k
  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.5M
    break;
479
480
  // dict punctuation
481
2.38M
  case '>':
482
2.38M
    c = lookChar();
483
2.38M
    if (c == '>') {
484
836k
      getChar();
485
836k
      tokBuf[0] = tokBuf[1] = '>';
486
836k
      tokBuf[2] = '\0';
487
836k
      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.38M
    break;
493
494
  // error
495
814k
  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
20.7M
  default:
504
20.7M
    p = tokBuf;
505
20.7M
    *p++ = (char)c;
506
20.7M
    n = 1;
507
116M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
508
95.6M
      getChar();
509
95.6M
      if (++n == tokBufSize) {
510
323k
  error(errSyntaxError, getPos(), "Command token too long");
511
323k
  break;
512
323k
      }
513
95.3M
      *p++ = (char)c;
514
95.3M
    }
515
20.7M
    *p = '\0';
516
20.7M
    if (tokBuf[0] == 't' && !strcmp(tokBuf, "true")) {
517
59.8k
      obj->initBool(gTrue);
518
20.6M
    } else if (tokBuf[0] == 'f' && !strcmp(tokBuf, "false")) {
519
14.7k
      obj->initBool(gFalse);
520
20.6M
    } else if (tokBuf[0] == 'n' && !strcmp(tokBuf, "null")) {
521
10.9k
      obj->initNull();
522
20.6M
    } else {
523
20.6M
      obj->initCmd(tokBuf);
524
20.6M
    }
525
20.7M
    break;
526
86.0M
  }
527
528
86.0M
  return obj;
529
86.0M
}
530
531
162k
void Lexer::skipToNextLine() {
532
162k
  int c;
533
534
411k
  while (1) {
535
411k
    c = getChar();
536
411k
    if (c == EOF || c == '\n') {
537
98.0k
      return;
538
98.0k
    }
539
313k
    if (c == '\r') {
540
64.0k
      if ((c = lookChar()) == '\n') {
541
54.5k
  getChar();
542
54.5k
      }
543
64.0k
      return;
544
64.0k
    }
545
313k
  }
546
162k
}
547
548
34.6k
void Lexer::skipToEOF() {
549
1.70M
  while (getChar() != EOF) ;
550
34.6k
}
551
552
211M
GBool Lexer::isSpace(int c) {
553
211M
  return c >= 0 && c <= 0xff && specialChars[c] == 1;
554
211M
}