Coverage Report

Created: 2025-10-10 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.05/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
299k
Lexer::Lexer(XRef *xref, Stream *str) {
47
299k
  Object obj;
48
49
299k
  curStr.initStream(str);
50
299k
  streams = new Array(xref);
51
299k
  streams->add(curStr.copy(&obj));
52
299k
  strPtr = 0;
53
299k
  freeArray = gTrue;
54
299k
  curStr.streamReset();
55
299k
}
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
299k
Lexer::~Lexer() {
76
299k
  if (!curStr.isNone()) {
77
104k
    curStr.streamClose();
78
104k
    curStr.free();
79
104k
  }
80
299k
  if (freeArray) {
81
299k
    delete streams;
82
299k
  }
83
299k
}
84
85
911M
int Lexer::getChar() {
86
911M
  int c;
87
88
911M
  c = EOF;
89
911M
  while (!curStr.isNone() && (c = curStr.streamGetChar()) == EOF) {
90
195k
    curStr.streamClose();
91
195k
    curStr.free();
92
195k
    ++strPtr;
93
195k
    if (strPtr < streams->getLength()) {
94
0
      streams->get(strPtr, &curStr);
95
0
      curStr.streamReset();
96
0
    }
97
195k
  }
98
911M
  return c;
99
911M
}
100
101
291M
int Lexer::lookChar() {
102
291M
  if (curStr.isNone()) {
103
1.94k
    return EOF;
104
1.94k
  }
105
291M
  return curStr.streamLookChar();
106
291M
}
107
108
174M
Object *Lexer::getObj(Object *obj) {
109
174M
  char *p;
110
174M
  int c, c2;
111
174M
  GBool comment, neg, doubleMinus, done, invalid;
112
174M
  int numParen, nErrors;
113
174M
  int xi;
114
174M
  double xf, scale;
115
174M
  GString *s;
116
174M
  int n, m;
117
118
  // skip whitespace and comments
119
174M
  comment = gFalse;
120
410M
  while (1) {
121
410M
    if ((c = getChar()) == EOF) {
122
129M
      return obj->initEOF();
123
129M
    }
124
280M
    if (comment) {
125
22.5M
      if (c == '\r' || c == '\n')
126
221k
  comment = gFalse;
127
257M
    } else if (c == '%') {
128
222k
      comment = gTrue;
129
257M
    } else if (specialChars[c] != 1) {
130
44.6M
      break;
131
44.6M
    }
132
280M
  }
133
134
  // start reading token
135
44.6M
  switch (c) {
136
137
  // number
138
5.10M
  case '0': case '1': case '2': case '3': case '4':
139
6.60M
  case '5': case '6': case '7': case '8': case '9':
140
7.13M
  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
7.13M
    neg = gFalse;
156
7.13M
    doubleMinus = gFalse;
157
7.13M
    xf = xi = 0;
158
7.13M
    if (c == '+') {
159
      // just ignore it
160
7.09M
    } else if (c == '-') {
161
263k
      neg = gTrue;
162
263k
      if (lookChar() == '-') {
163
10.1k
  doubleMinus = gTrue;
164
18.7k
  do {
165
18.7k
    getChar();
166
18.7k
  } while (lookChar() == '-');
167
10.1k
      }
168
6.82M
    } else if (c == '.') {
169
225k
      goto doReal;
170
6.60M
    } else {
171
6.60M
      xf = xi = c - '0';
172
6.60M
    }
173
14.5M
    while (1) {
174
14.5M
      c = lookChar();
175
14.5M
      if (isdigit(c)) {
176
7.66M
  getChar();
177
7.66M
  xi = xi * 10 + (c - '0');
178
7.66M
  if (xf < 1e20) {
179
7.59M
    xf = xf * 10 + (c - '0');
180
7.59M
  }
181
7.66M
      } else if (c == '.') {
182
403k
  getChar();
183
403k
  goto doReal;
184
6.50M
      } else {
185
6.50M
  break;
186
6.50M
      }
187
14.5M
    }
188
6.51M
    while ((c = lookChar()) == '-' || isdigit(c)) {
189
6.54k
      getChar();
190
6.54k
    }
191
6.50M
    if (neg) {
192
258k
      xi = -xi;
193
258k
    }
194
6.50M
    if (doubleMinus) {
195
10.1k
      xi = 0;
196
10.1k
    }
197
6.50M
    obj->initInt(xi);
198
6.50M
    break;
199
628k
  doReal:
200
628k
    scale = 0.1;
201
1.21M
    while (1) {
202
1.21M
      c = lookChar();
203
1.21M
      if (c == '-') {
204
15.1k
  error(errSyntaxWarning, getPos(), "Badly formatted number");
205
15.1k
  getChar();
206
15.1k
  continue;
207
15.1k
      }
208
1.20M
      if (!isdigit(c)) {
209
628k
  break;
210
628k
      }
211
573k
      getChar();
212
573k
      xf = xf + scale * (c - '0');
213
573k
      scale *= 0.1;
214
573k
    }
215
628k
    while ((c = lookChar()) == '-' || isdigit(c)) {
216
0
      getChar();
217
0
    }
218
628k
    if (neg) {
219
5.31k
      xf = -xf;
220
5.31k
    }
221
628k
    obj->initReal(xf);
222
628k
    break;
223
224
  // string
225
217k
  case '(':
226
217k
    p = tokBuf;
227
217k
    n = 0;
228
217k
    numParen = 1;
229
217k
    done = gFalse;
230
217k
    s = NULL;
231
199M
    do {
232
199M
      c2 = EOF;
233
199M
      switch (c = getChar()) {
234
235
8.45k
      case EOF:
236
8.45k
  error(errSyntaxError, getPos(), "Unterminated string");
237
8.45k
  done = gTrue;
238
8.45k
  break;
239
240
404k
      case '(':
241
404k
  ++numParen;
242
404k
  c2 = c;
243
404k
  break;
244
245
597k
      case ')':
246
597k
  if (--numParen == 0) {
247
208k
    done = gTrue;
248
388k
  } else {
249
388k
    c2 = c;
250
388k
  }
251
597k
  break;
252
253
4.78M
      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.78M
  c = lookChar();
257
4.78M
  if (c == '\n') {
258
162k
    getChar();
259
162k
  }
260
4.78M
  c2 = '\n';
261
4.78M
  break;
262
263
187k
      case '\\':
264
187k
  switch (c = getChar()) {
265
1.70k
  case 'n':
266
1.70k
    c2 = '\n';
267
1.70k
    break;
268
15.5k
  case 'r':
269
15.5k
    c2 = '\r';
270
15.5k
    break;
271
22.0k
  case 't':
272
22.0k
    c2 = '\t';
273
22.0k
    break;
274
590
  case 'b':
275
590
    c2 = '\b';
276
590
    break;
277
329
  case 'f':
278
329
    c2 = '\f';
279
329
    break;
280
9.74k
  case '\\':
281
16.1k
  case '(':
282
19.8k
  case ')':
283
19.8k
    c2 = c;
284
19.8k
    break;
285
8.12k
  case '0': case '1': case '2': case '3':
286
32.6k
  case '4': case '5': case '6': case '7':
287
32.6k
    c2 = c - '0';
288
32.6k
    c = lookChar();
289
32.6k
    if (c >= '0' && c <= '7') {
290
6.96k
      getChar();
291
6.96k
      c2 = (c2 << 3) + (c - '0');
292
6.96k
      c = lookChar();
293
6.96k
      if (c >= '0' && c <= '7') {
294
557
        getChar();
295
557
        c2 = (c2 << 3) + (c - '0');
296
557
      }
297
6.96k
    }
298
32.6k
    break;
299
3.14k
  case '\r':
300
3.14k
    c = lookChar();
301
3.14k
    if (c == '\n') {
302
1.65k
      getChar();
303
1.65k
    }
304
3.14k
    break;
305
5.40k
  case '\n':
306
5.40k
    break;
307
10
  case EOF:
308
10
    error(errSyntaxError, getPos(), "Unterminated string");
309
10
    done = gTrue;
310
10
    break;
311
86.3k
  default:
312
86.3k
    c2 = c;
313
86.3k
    break;
314
187k
  }
315
187k
  break;
316
317
193M
      default:
318
193M
  c2 = c;
319
193M
  break;
320
199M
      }
321
322
199M
      if (c2 != EOF) {
323
199M
  if (n == tokBufSize) {
324
1.48M
    if (!s)
325
65.8k
      s = new GString(tokBuf, tokBufSize);
326
1.41M
    else
327
1.41M
      s->append(tokBuf, tokBufSize);
328
1.48M
    p = tokBuf;
329
1.48M
    n = 0;
330
1.48M
  }
331
199M
  *p++ = (char)c2;
332
199M
  ++n;
333
199M
      }
334
199M
    } while (!done);
335
217k
    if (!s)
336
151k
      s = new GString(tokBuf, n);
337
65.8k
    else
338
65.8k
      s->append(tokBuf, n);
339
217k
    obj->initString(s);
340
217k
    break;
341
342
  // name
343
6.75M
  case '/':
344
6.75M
    p = tokBuf;
345
6.75M
    n = 0;
346
6.75M
    s = NULL;
347
6.75M
    invalid = gFalse;
348
31.2M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
349
24.5M
      getChar();
350
24.5M
      if (c == '#') {
351
821k
  c2 = lookChar();
352
821k
  if (c2 >= '0' && c2 <= '9') {
353
1.05k
    c = c2 - '0';
354
820k
  } else if (c2 >= 'A' && c2 <= 'F') {
355
46.4k
    c = c2 - 'A' + 10;
356
774k
  } else if (c2 >= 'a' && c2 <= 'f') {
357
95.7k
    c = c2 - 'a' + 10;
358
678k
  } else {
359
678k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
360
678k
    goto notEscChar;
361
678k
  }
362
143k
  getChar();
363
143k
  c2 = lookChar();
364
143k
  if (c2 >= '0' && c2 <= '9') {
365
26.5k
    c = (c << 4) + (c2 - '0');
366
116k
  } else if (c2 >= 'A' && c2 <= 'F') {
367
46.3k
    c = (c << 4) + (c2 - 'A' + 10);
368
70.2k
  } else if (c2 >= 'a' && c2 <= 'f') {
369
664
    c = (c << 4) + (c2 - 'a' + 10);
370
69.5k
  } else {
371
69.5k
    error(errSyntaxError, getPos(), "Invalid hex escape in name");
372
69.5k
    goto notEscChar;
373
69.5k
  }
374
73.6k
  getChar();
375
73.6k
  if (c == 0) {
376
281
    invalid = gTrue;
377
281
  }
378
73.6k
      }
379
24.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
24.5M
      ++n;
384
24.5M
      if (n < tokBufSize) {
385
20.3M
  *p++ = (char)c;
386
20.3M
      } else if (n == tokBufSize) {
387
826
  *p = (char)c;
388
826
  s = new GString(tokBuf, n);
389
4.17M
      } else {
390
4.17M
  s->append((char)c);
391
4.17M
      }
392
24.5M
    }
393
6.75M
    if (invalid) {
394
281
      error(errSyntaxError, getPos(), "Null character in name");
395
281
      obj->initError();
396
281
      if (s) {
397
54
  delete s;
398
54
      }
399
6.75M
    } else if (n < tokBufSize) {
400
6.75M
      *p = '\0';
401
6.75M
      obj->initName(tokBuf);
402
6.75M
    } else {
403
772
      obj->initName(s->getCString());
404
772
      delete s;
405
772
    }
406
6.75M
    break;
407
408
  // array punctuation
409
5.25M
  case '[':
410
5.76M
  case ']':
411
5.76M
    tokBuf[0] = (char)c;
412
5.76M
    tokBuf[1] = '\0';
413
5.76M
    obj->initCmd(tokBuf);
414
5.76M
    break;
415
416
  // hex string or dict punctuation
417
3.90M
  case '<':
418
3.90M
    c = lookChar();
419
420
    // dict punctuation
421
3.90M
    if (c == '<') {
422
3.17M
      getChar();
423
3.17M
      tokBuf[0] = tokBuf[1] = '<';
424
3.17M
      tokBuf[2] = '\0';
425
3.17M
      obj->initCmd(tokBuf);
426
427
    // hex string
428
3.17M
    } else {
429
729k
      p = tokBuf;
430
729k
      m = n = 0;
431
729k
      c2 = 0;
432
729k
      s = NULL;
433
729k
      nErrors = 0;
434
51.0M
      while (nErrors < 100) {
435
50.9M
  c = getChar();
436
50.9M
  if (c == '>') {
437
573k
    break;
438
50.3M
  } else if (c == EOF) {
439
3.51k
    error(errSyntaxError, getPos(), "Unterminated hex string");
440
3.51k
    break;
441
50.3M
  } else if (specialChars[c] != 1) {
442
44.0M
    c2 = c2 << 4;
443
44.0M
    if (c >= '0' && c <= '9') {
444
4.87M
      c2 += c - '0';
445
39.1M
    } else if (c >= 'A' && c <= 'F') {
446
781k
      c2 += c - 'A' + 10;
447
38.3M
    } else if (c >= 'a' && c <= 'f') {
448
6.01M
      c2 += c - 'a' + 10;
449
32.3M
    } else {
450
32.3M
      error(errSyntaxError, getPos(),
451
32.3M
      "Illegal character <{0:02x}> in hex string", c);
452
32.3M
      ++nErrors;
453
32.3M
    }
454
44.0M
    if (++m == 2) {
455
21.8M
      if (n == tokBufSize) {
456
4.10k
        if (!s)
457
1.26k
    s = new GString(tokBuf, tokBufSize);
458
2.84k
        else
459
2.84k
    s->append(tokBuf, tokBufSize);
460
4.10k
        p = tokBuf;
461
4.10k
        n = 0;
462
4.10k
      }
463
21.8M
      *p++ = (char)c2;
464
21.8M
      ++n;
465
21.8M
      c2 = 0;
466
21.8M
      m = 0;
467
21.8M
    }
468
44.0M
  }
469
50.9M
      }
470
729k
      if (!s)
471
728k
  s = new GString(tokBuf, n);
472
1.26k
      else
473
1.26k
  s->append(tokBuf, n);
474
729k
      if (m == 1)
475
337k
  s->append((char)(c2 << 4));
476
729k
      obj->initString(s);
477
729k
    }
478
3.90M
    break;
479
480
  // dict punctuation
481
2.90M
  case '>':
482
2.90M
    c = lookChar();
483
2.90M
    if (c == '>') {
484
1.84M
      getChar();
485
1.84M
      tokBuf[0] = tokBuf[1] = '>';
486
1.84M
      tokBuf[2] = '\0';
487
1.84M
      obj->initCmd(tokBuf);
488
1.84M
    } else {
489
1.05M
      error(errSyntaxError, getPos(), "Illegal character '>'");
490
1.05M
      obj->initError();
491
1.05M
    }
492
2.90M
    break;
493
494
  // error
495
801k
  case ')':
496
987k
  case '{':
497
1.08M
  case '}':
498
1.08M
    error(errSyntaxError, getPos(), "Illegal character '{0:c}'", c);
499
1.08M
    obj->initError();
500
1.08M
    break;
501
502
  // command
503
16.9M
  default:
504
16.9M
    p = tokBuf;
505
16.9M
    *p++ = (char)c;
506
16.9M
    n = 1;
507
224M
    while ((c = lookChar()) != EOF && !specialChars[c]) {
508
208M
      getChar();
509
208M
      if (++n == tokBufSize) {
510
1.04M
  error(errSyntaxError, getPos(), "Command token too long");
511
1.04M
  break;
512
1.04M
      }
513
207M
      *p++ = (char)c;
514
207M
    }
515
16.9M
    *p = '\0';
516
16.9M
    if (tokBuf[0] == 't' && !strcmp(tokBuf, "true")) {
517
73.7k
      obj->initBool(gTrue);
518
16.8M
    } else if (tokBuf[0] == 'f' && !strcmp(tokBuf, "false")) {
519
1.59k
      obj->initBool(gFalse);
520
16.8M
    } else if (tokBuf[0] == 'n' && !strcmp(tokBuf, "null")) {
521
228
      obj->initNull();
522
16.8M
    } else {
523
16.8M
      obj->initCmd(tokBuf);
524
16.8M
    }
525
16.9M
    break;
526
44.6M
  }
527
528
44.6M
  return obj;
529
44.6M
}
530
531
118k
void Lexer::skipToNextLine() {
532
118k
  int c;
533
534
372k
  while (1) {
535
372k
    c = getChar();
536
372k
    if (c == EOF || c == '\n') {
537
35.7k
      return;
538
35.7k
    }
539
336k
    if (c == '\r') {
540
83.2k
      if ((c = lookChar()) == '\n') {
541
73.8k
  getChar();
542
73.8k
      }
543
83.2k
      return;
544
83.2k
    }
545
336k
  }
546
118k
}
547
548
31.6k
void Lexer::skipToEOF() {
549
2.35M
  while (getChar() != EOF) ;
550
31.6k
}
551
552
44.0M
GBool Lexer::isSpace(int c) {
553
44.0M
  return c >= 0 && c <= 0xff && specialChars[c] == 1;
554
44.0M
}