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(Stream *str1) {
50 // get rid of old xref (otherwise it will try to fetch the Root object
51 // in the new document, using the old xref)
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(Stream *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(str->subStream (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(Stream *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(str->subStream (str->getPos(), -1, &obj)));
262 parser->getObj(&obj);
263 if (!obj.isCmd("trailer"))
266 parser->getObj(&obj);
269 obj.getDict()->lookupNF("Prev", &obj2);
271 *pos = obj2.getInt();
289 // Attempt to construct an xref table for a damaged file.
290 GBool XRef::constructXRef(Stream *str) {
301 error(0, "PDF file is damaged - attempting to reconstruct xref table...");
307 if (!str->getLine(buf, 256))
311 // got trailer dictionary
312 if (!strncmp(p, "trailer", 7)) {
314 parser = new Parser(new Lexer(str->subStream(start + pos + 8, -1, &obj)));
315 if (!trailerDict.isNone())
317 parser->getObj(&trailerDict);
318 if (trailerDict.isDict()) {
319 trailerDict.dictLookupNF("Root", &obj);
321 rootNum = obj.getRefNum();
322 rootGen = obj.getRefGen();
332 } else if (isdigit(*p)) {
336 } while (*p && isdigit(*p));
340 } while (*p && isspace(*p));
345 } while (*p && isdigit(*p));
349 } while (*p && isspace(*p));
350 if (!strncmp(p, "obj", 3)) {
352 newSize = (num + 1 + 255) & ~255;
353 entries = (XRefEntry *)
354 grealloc(entries, newSize * sizeof(XRefEntry));
355 for (i = size; i < newSize; ++i) {
356 entries[i].offset = -1;
357 entries[i].used = gFalse;
361 if (!entries[num].used || gen >= entries[num].gen) {
362 entries[num].offset = pos - start;
363 entries[num].gen = gen;
364 entries[num].used = gTrue;
376 error(-1, "Couldn't find trailer dictionary");
380 GBool XRef::checkEncrypted() {
384 trailerDict.dictLookup("Encrypt", &obj);
385 if ((encrypted = !obj.isNull())) {
386 error(-1, "PDF file is encrypted and cannot be displayed");
387 error(-1, "* Decryption support is currently not included in xpdf");
388 error(-1, "* due to legal restrictions: the U.S.A. still has bogus");
389 error(-1, "* export controls on cryptography software.");
395 GBool XRef::okToPrint() {
399 GBool XRef::okToCopy() {
403 Object *XRef::fetch(int num, int gen, Object *obj) {
406 Object obj1, obj2, obj3;
408 // check for bogus ref - this can happen in corrupted PDF files
409 if (num < 0 || num >= size) {
415 if (e->gen == gen && e->offset >= 0) {
417 parser = new Parser(new Lexer(str->subStream(start + e->offset, -1, &obj1)));
418 parser->getObj(&obj1);
419 parser->getObj(&obj2);
420 parser->getObj(&obj3);
421 if (obj1.isInt() && obj1.getInt() == num &&
422 obj2.isInt() && obj2.getInt() == gen &&
438 Object *XRef::getDocInfo(Object *obj) {
439 return trailerDict.dictLookup("Info", obj);