Coverage Report

Created: 2026-12-01 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/Parser.cc
Line
Count
Source
1
//========================================================================
2
//
3
// Parser.cc
4
//
5
// Copyright 1996-2003 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stddef.h>
12
#include <string.h>
13
#include "gmempp.h"
14
#include "Object.h"
15
#include "Array.h"
16
#include "Dict.h"
17
#include "Decrypt.h"
18
#include "Parser.h"
19
#include "XRef.h"
20
#include "Error.h"
21
22
824k
Parser::Parser(XRef *xrefA, Lexer *lexerA, GBool allowStreamsA) {
23
824k
  xref = xrefA;
24
824k
  lexer = lexerA;
25
824k
  inlineImg = 0;
26
824k
  allowStreams = allowStreamsA;
27
824k
  lexer->getObj(&buf1);
28
824k
  lexer->getObj(&buf2);
29
824k
}
30
31
824k
Parser::~Parser() {
32
824k
  buf1.free();
33
824k
  buf2.free();
34
824k
  delete lexer;
35
824k
}
36
37
Object *Parser::getObj(Object *obj, GBool simpleOnly,
38
           Guchar *fileKey,
39
           CryptAlgorithm encAlgorithm, int keyLength,
40
253M
           int objNum, int objGen, int recursion) {
41
253M
  char *key;
42
253M
  Stream *str;
43
253M
  Object obj2;
44
253M
  int num;
45
253M
  DecryptStream *decrypt;
46
253M
  GString *s, *s2;
47
253M
  int c;
48
49
  // refill buffer after inline image data
50
253M
  if (inlineImg == 2) {
51
147k
    buf1.free();
52
147k
    buf2.free();
53
147k
    lexer->getObj(&buf1);
54
147k
    lexer->getObj(&buf2);
55
147k
    inlineImg = 0;
56
147k
  }
57
58
  // array
59
253M
  if (!simpleOnly && recursion < objectRecursionLimit && buf1.isCmd("[")) {
60
2.08M
    shift();
61
2.08M
    obj->initArray(xref);
62
53.0M
    while (!buf1.isCmd("]") && !buf1.isEOF())
63
50.9M
      obj->arrayAdd(getObj(&obj2, gFalse, fileKey, encAlgorithm, keyLength,
64
50.9M
         objNum, objGen, recursion + 1));
65
2.08M
    if (buf1.isEOF())
66
1.38M
      error(errSyntaxError, getPos(), "End of file inside array");
67
2.08M
    shift();
68
69
  // dictionary or stream
70
251M
  } else if (!simpleOnly && recursion < objectRecursionLimit &&
71
59.5M
       buf1.isCmd("<<")) {
72
1.26M
    shift();
73
1.26M
    obj->initDict(xref);
74
22.4M
    while (!buf1.isCmd(">>") && !buf1.isEOF()) {
75
21.2M
      if (!buf1.isName()) {
76
16.0M
  error(errSyntaxError, getPos(),
77
16.0M
        "Dictionary key must be a name object");
78
16.0M
  shift();
79
16.0M
      } else {
80
5.23M
  key = copyString(buf1.getName());
81
5.23M
  shift();
82
5.23M
  if (buf1.isEOF() || buf1.isError()) {
83
53.4k
    gfree(key);
84
53.4k
    break;
85
53.4k
  }
86
5.18M
  obj->dictAdd(key, getObj(&obj2, gFalse,
87
5.18M
         fileKey, encAlgorithm, keyLength,
88
5.18M
         objNum, objGen, recursion + 1));
89
5.18M
      }
90
21.2M
    }
91
1.26M
    if (buf1.isEOF())
92
88.1k
      error(errSyntaxError, getPos(), "End of file inside dictionary");
93
    // stream objects are not allowed inside content streams or
94
    // object streams
95
1.26M
    if (allowStreams && buf2.isCmd("stream")) {
96
215k
      if ((str = makeStream(obj, fileKey, encAlgorithm, keyLength,
97
215k
          objNum, objGen, recursion + 1))) {
98
207k
  obj->initStream(str);
99
207k
      } else {
100
7.98k
  obj->free();
101
7.98k
  obj->initError();
102
7.98k
      }
103
1.04M
    } else {
104
1.04M
      shift();
105
1.04M
    }
106
107
  // indirect reference or integer
108
250M
  } else if (buf1.isInt()) {
109
28.0M
    num = buf1.getInt();
110
28.0M
    shift();
111
28.0M
    if (buf1.isInt() && buf2.isCmd("R")) {
112
1.37M
      int gen = buf1.getInt();
113
1.37M
      if (num >= 0 && gen >= 0) {
114
1.36M
  obj->initRef(num, gen);
115
1.36M
      } else {
116
3.25k
  error(errSyntaxError, getPos(),
117
3.25k
        "Negative number or generation in indirect reference");
118
3.25k
  obj->initError();
119
3.25k
      }
120
1.37M
      shift();
121
1.37M
      shift();
122
26.7M
    } else {
123
26.7M
      obj->initInt(num);
124
26.7M
    }
125
126
  // string
127
222M
  } else if (buf1.isString() && fileKey) {
128
23.3k
    s = buf1.getString();
129
23.3k
    s2 = new GString();
130
23.3k
    obj2.initNull();
131
23.3k
    decrypt = new DecryptStream(new MemStream(s->getCString(), 0,
132
23.3k
                s->getLength(), &obj2),
133
23.3k
        fileKey, encAlgorithm, keyLength,
134
23.3k
        objNum, objGen);
135
23.3k
    decrypt->reset();
136
611k
    while ((c = decrypt->getChar()) != EOF) {
137
588k
      s2->append((char)c);
138
588k
    }
139
23.3k
    delete decrypt;
140
23.3k
    obj->initString(s2);
141
23.3k
    shift();
142
143
  // simple object
144
222M
  } else {
145
222M
    buf1.copy(obj);
146
222M
    shift();
147
222M
  }
148
149
253M
  return obj;
150
253M
}
151
152
Stream *Parser::makeStream(Object *dict, Guchar *fileKey,
153
         CryptAlgorithm encAlgorithm, int keyLength,
154
215k
         int objNum, int objGen, int recursion) {
155
  // get stream start position
156
215k
  lexer->skipToNextLine();
157
215k
  Stream *curStr = lexer->getStream();
158
215k
  if (!curStr) {
159
1.71k
    return NULL;
160
1.71k
  }
161
214k
  GFileOffset pos = curStr->getPos();
162
163
214k
  GBool haveLength = gFalse;
164
214k
  GFileOffset length = 0;
165
214k
  GFileOffset endPos;
166
167
  // check for length in damaged file
168
214k
  if (xref && xref->getStreamEnd(pos, &endPos)) {
169
150k
    length = endPos - pos;
170
150k
    haveLength = gTrue;
171
172
  // get length from the stream object
173
150k
  } else {
174
63.8k
    Object obj;
175
63.8k
    dict->dictLookup("Length", &obj, recursion);
176
63.8k
    if (obj.isInt()) {
177
48.5k
      length = (GFileOffset)(Guint)obj.getInt();
178
48.5k
      haveLength = gTrue;
179
48.5k
    } else {
180
15.2k
      error(errSyntaxError, getPos(),
181
15.2k
      "Missing or invalid 'Length' attribute in stream");
182
15.2k
    }
183
63.8k
    obj.free();
184
63.8k
  }
185
186
  // in badly damaged PDF files, we can run off the end of the input
187
  // stream immediately after the "stream" token
188
214k
  if (!lexer->getStream()) {
189
0
    return NULL;
190
0
  }
191
192
  // copy the base stream (Lexer will free stream objects when it gets
193
  // to end of stream -- which can happen in the shift() calls below)
194
214k
  BaseStream *baseStr =
195
214k
      (BaseStream *)lexer->getStream()->getBaseStream()->copy();
196
197
  // 'Length' attribute is missing -- search for 'endstream'
198
214k
  if (!haveLength) {
199
15.2k
    GBool foundEndstream = gFalse;
200
15.2k
    char endstreamBuf[8];
201
15.2k
    if ((curStr = lexer->getStream())) {
202
15.2k
      int c;
203
9.78M
      while ((c = curStr->getChar()) != EOF) {
204
9.77M
  if (c == 'e' &&
205
250k
      curStr->getBlock(endstreamBuf, 8) == 8 &&
206
249k
      !memcmp(endstreamBuf, "ndstream", 8)) {
207
9.00k
    length = curStr->getPos() - 9 - pos;
208
9.00k
    foundEndstream = gTrue;
209
9.00k
    break;
210
9.00k
  }
211
9.77M
      }
212
15.2k
    }
213
15.2k
    if (!foundEndstream) {
214
6.26k
      error(errSyntaxError, getPos(), "Couldn't find 'endstream' for stream");
215
6.26k
      delete baseStr;
216
6.26k
      return NULL;
217
6.26k
    }
218
15.2k
  }
219
220
  // make new base stream
221
207k
  Stream *str = baseStr->makeSubStream(pos, gTrue, length, dict);
222
223
  // look for the 'endstream' marker
224
207k
  if (haveLength) {
225
    // skip over stream data
226
198k
    lexer->setPos(pos + length);
227
228
    // check for 'endstream'
229
    // NB: we never reuse the Parser object to parse objects after a
230
    // stream, and we could (if the PDF file is damaged) be in the
231
    // middle of binary data at this point, so we check the stream
232
    // data directly for 'endstream', rather than calling shift() to
233
    // parse objects
234
198k
    GBool foundEndstream = gFalse;
235
198k
    char endstreamBuf[8];
236
198k
    if ((curStr = lexer->getStream())) {
237
      // skip up to 100 whitespace chars
238
198k
      int c;
239
313k
      for (int i = 0; i < 100; ++i) {
240
312k
  c = curStr->getChar();
241
312k
  if (!Lexer::isSpace(c)) {
242
197k
    break;
243
197k
  }
244
312k
      }
245
198k
      if (c == 'e') {
246
155k
  if (curStr->getBlock(endstreamBuf, 8) == 8 &&
247
155k
      !memcmp(endstreamBuf, "ndstream", 8)) {
248
152k
    foundEndstream = gTrue;
249
152k
  }
250
155k
      }
251
198k
    }
252
198k
    if (!foundEndstream) {
253
45.8k
      error(errSyntaxError, getPos(), "Missing 'endstream'");
254
      // kludge for broken PDF files: just add 5k to the length, and
255
      // hope it's enough
256
      // (dict is now owned by str, so we need to copy it before deleting str)
257
45.8k
      Object obj;
258
45.8k
      dict->copy(&obj);
259
45.8k
      delete str;
260
45.8k
      length += 5000;
261
45.8k
      str = baseStr->makeSubStream(pos, gTrue, length, &obj);
262
45.8k
    }
263
198k
  }
264
265
  // free the copied base stream
266
207k
  delete baseStr;
267
268
  // handle decryption
269
207k
  if (fileKey) {
270
    // the 'Crypt' filter is used to mark unencrypted metadata streams
271
    //~ this should also check for an empty DecodeParams entry
272
6.26k
    GBool encrypted = gTrue;
273
6.26k
    Object obj;
274
6.26k
    dict->dictLookup("Filter", &obj, recursion);
275
6.26k
    if (obj.isName("Crypt")) {
276
17
      encrypted = gFalse;
277
6.24k
    } else if (obj.isArray() && obj.arrayGetLength() >= 1) {
278
853
      Object obj2;
279
853
      if (obj.arrayGet(0, &obj2)->isName("Crypt")) {
280
0
  encrypted = gFalse;
281
0
      }
282
853
      obj2.free();
283
853
    }
284
6.26k
    obj.free();
285
6.26k
    if (encrypted) {
286
6.24k
      str = new DecryptStream(str, fileKey, encAlgorithm, keyLength,
287
6.24k
            objNum, objGen);
288
6.24k
    }
289
6.26k
  }
290
291
  // get filters
292
207k
  str = str->addFilters(dict, recursion);
293
294
207k
  return str;
295
214k
}
296
297
281M
void Parser::shift() {
298
281M
  if (inlineImg > 0) {
299
631k
    if (inlineImg < 2) {
300
389k
      ++inlineImg;
301
389k
    } else {
302
      // in a damaged content stream, if 'ID' shows up in the middle
303
      // of a dictionary, we need to reset
304
241k
      inlineImg = 0;
305
241k
    }
306
280M
  } else if (buf2.isCmd("ID")) {
307
389k
    lexer->skipChar();    // skip char after 'ID' command
308
389k
    inlineImg = 1;
309
389k
  }
310
281M
  buf1.free();
311
281M
  buf1 = buf2;
312
281M
  if (inlineImg > 0)    // don't buffer inline image data
313
779k
    buf2.initNull();
314
280M
  else
315
280M
    lexer->getObj(&buf2);
316
281M
}