1 //========================================================================
5 // Copyright 1996 Derek B. Noonburg
7 //========================================================================
10 #pragma implementation
26 //------------------------------------------------------------------------
28 #define xrefSearchSize 1024 // read this many bytes at end of file
29 // to look for 'startxref'
31 //------------------------------------------------------------------------
32 // The global xref table
33 //------------------------------------------------------------------------
37 //------------------------------------------------------------------------
39 //------------------------------------------------------------------------
41 XRef::XRef(FileStream *str) {
50 // get rid of old xref (otherwise it will try to fetch the Root object
51 // in the new document, using the old xref)
56 file = str->getFile();
57 start = str->getStart();
58 pos = readTrailer(str);
60 // if there was a problem with the trailer,
61 // try to reconstruct the xref table
63 if (!(ok = constructXRef(str))) {
68 // trailer is ok - read the xref table
70 entries = (XRefEntry *)gmalloc(size * sizeof(XRefEntry));
71 for (i = 0; i < size; ++i) {
72 entries[i].offset = -1;
73 entries[i].used = gFalse;
75 while (readXRef(str, &pos)) ;
77 // if there was a problem with the xref table,
78 // try to reconstruct it
83 if (!(ok = constructXRef(str))) {
90 // set up new xref table
93 // check for encryption
94 if (checkEncrypted()) {
106 // Read startxref position, xref table size, and root. Returns
107 // first xref position.
108 int XRef::readTrailer(FileStream *str) {
111 char buf[xrefSearchSize+1];
117 // read last xrefSearchSize bytes
118 str->setPos(-xrefSearchSize);
119 for (n = 0; n < xrefSearchSize; ++n) {
120 if ((c = str->getChar()) == EOF)
127 for (i = n - 9; i >= 0; --i) {
128 if (!strncmp(&buf[i], "startxref", 9))
133 for (p = &buf[i+9]; isspace(*p); ++p) ;
136 // find trailer dict by looking after first xref table
137 // (NB: we can't just use the trailer dict at the end of the file --
138 // this won't work for linearized files.)
139 str->setPos(start + pos);
140 for (i = 0; i < 4; ++i)
141 buf[i] = str->getChar();
142 if (strncmp(buf, "xref", 4))
146 str->setPos(start + pos1);
147 for (i = 0; i < 35; ++i) {
148 if ((c = str->getChar()) == EOF)
152 if (!strncmp(buf, "trailer", 7))
155 while (isspace(*p)) ++p;
156 while ('0' <= *p && *p <= '9') ++p;
157 while (isspace(*p)) ++p;
159 while ('0' <= *p && *p <= '9') ++p;
160 while (isspace(*p)) ++p;
163 pos1 += (p - buf) + n * 20;
169 parser = new Parser(new Lexer(new FileStream(file, start + pos1, -1, &obj)));
170 parser->getObj(&trailerDict);
171 if (trailerDict.isDict()) {
172 trailerDict.dictLookupNF("Size", &obj);
178 trailerDict.dictLookupNF("Root", &obj);
180 rootNum = obj.getRefNum();
181 rootGen = obj.getRefGen();
191 // return first xref position
195 // Read an xref table and the prev pointer from the trailer.
196 GBool XRef::readXRef(FileStream *str, int *pos) {
204 // seek to xref in stream
205 str->setPos(start + *pos);
207 // make sure it's an xref table
208 while ((c = str->getChar()) != EOF && isspace(c)) ;
210 s[1] = (char)str->getChar();
211 s[2] = (char)str->getChar();
212 s[3] = (char)str->getChar();
213 if (!(s[0] == 'x' && s[1] == 'r' && s[2] == 'e' && s[3] == 'f'))
218 while ((c = str->lookChar()) != EOF && isspace(c))
222 for (i = 0; (c = str->getChar()) != EOF && isdigit(c) && i < 20; ++i)
228 while ((c = str->lookChar()) != EOF && isspace(c))
230 for (i = 0; (c = str->getChar()) != EOF && isdigit(c) && i < 20; ++i)
236 while ((c = str->lookChar()) != EOF && isspace(c))
238 for (i = first; i < first + n; ++i) {
239 for (j = 0; j < 20; ++j) {
240 if ((c = str->getChar()) == EOF)
244 if (entries[i].offset < 0) {
246 entries[i].offset = atoi(s);
248 entries[i].gen = atoi(&s[11]);
250 entries[i].used = gTrue;
251 else if (s[17] == 'f')
252 entries[i].used = gFalse;
259 // read prev pointer from trailer dictionary
261 parser = new Parser(new Lexer(
262 new FileStream(file, str->getPos(), -1, &obj)));
263 parser->getObj(&obj);
264 if (!obj.isCmd("trailer"))
267 parser->getObj(&obj);
270 obj.getDict()->lookupNF("Prev", &obj2);
272 *pos = obj2.getInt();
290 // Attempt to construct an xref table for a damaged file.
291 GBool XRef::constructXRef(FileStream *str) {
302 error(0, "PDF file is damaged - attempting to reconstruct xref table...");
308 if (!str->getLine(buf, 256))
312 // got trailer dictionary
313 if (!strncmp(p, "trailer", 7)) {
315 parser = new Parser(new Lexer(
316 new FileStream(file, start + pos + 8, -1, &obj)));
317 if (!trailerDict.isNone())
319 parser->getObj(&trailerDict);
320 if (trailerDict.isDict()) {
321 trailerDict.dictLookupNF("Root", &obj);
323 rootNum = obj.getRefNum();
324 rootGen = obj.getRefGen();
334 } else if (isdigit(*p)) {
338 } while (*p && isdigit(*p));
342 } while (*p && isspace(*p));
347 } while (*p && isdigit(*p));
351 } while (*p && isspace(*p));
352 if (!strncmp(p, "obj", 3)) {
354 newSize = (num + 1 + 255) & ~255;
355 entries = (XRefEntry *)
356 grealloc(entries, newSize * sizeof(XRefEntry));
357 for (i = size; i < newSize; ++i) {
358 entries[i].offset = -1;
359 entries[i].used = gFalse;
363 if (!entries[num].used || gen >= entries[num].gen) {
364 entries[num].offset = pos - start;
365 entries[num].gen = gen;
366 entries[num].used = gTrue;
378 error(-1, "Couldn't find trailer dictionary");
382 GBool XRef::checkEncrypted() {
386 trailerDict.dictLookup("Encrypt", &obj);
387 if ((encrypted = !obj.isNull())) {
388 error(-1, "PDF file is encrypted and cannot be displayed");
389 error(-1, "* Decryption support is currently not included in xpdf");
390 error(-1, "* due to legal restrictions: the U.S.A. still has bogus");
391 error(-1, "* export controls on cryptography software.");
397 GBool XRef::okToPrint() {
401 GBool XRef::okToCopy() {
405 Object *XRef::fetch(int num, int gen, Object *obj) {
408 Object obj1, obj2, obj3;
410 // check for bogus ref - this can happen in corrupted PDF files
411 if (num < 0 || num >= size) {
417 if (e->gen == gen && e->offset >= 0) {
419 parser = new Parser(new Lexer(
420 new FileStream(file, start + e->offset, -1, &obj1)));
421 parser->getObj(&obj1);
422 parser->getObj(&obj2);
423 parser->getObj(&obj3);
424 if (obj1.isInt() && obj1.getInt() == num &&
425 obj2.isInt() && obj2.getInt() == gen &&
441 Object *XRef::getDocInfo(Object *obj) {
442 return trailerDict.dictLookup("Info", obj);