author | sandman <sandman> | 2002-05-23 23:51:52 (UTC) |
---|---|---|
committer | sandman <sandman> | 2002-05-23 23:51:52 (UTC) |
commit | 2f3bb7b07f833273d966d41813e68bfe8b9d8d76 (patch) (side-by-side diff) | |
tree | 00beb1bd9e7f4ba79e22334a0d258269b28f4564 /noncore/unsupported/qpdf/xpdf/XRef.cc | |
parent | 6e82b45dd416ceeba78765717b700e853c96a137 (diff) | |
download | opie-2f3bb7b07f833273d966d41813e68bfe8b9d8d76.zip opie-2f3bb7b07f833273d966d41813e68bfe8b9d8d76.tar.gz opie-2f3bb7b07f833273d966d41813e68bfe8b9d8d76.tar.bz2 |
Port to xpdf 1.01
Diffstat (limited to 'noncore/unsupported/qpdf/xpdf/XRef.cc') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/unsupported/qpdf/xpdf/XRef.cc | 80 |
1 files changed, 52 insertions, 28 deletions
diff --git a/noncore/unsupported/qpdf/xpdf/XRef.cc b/noncore/unsupported/qpdf/xpdf/XRef.cc index 5d526e9..0e1bbc9 100644 --- a/noncore/unsupported/qpdf/xpdf/XRef.cc +++ b/noncore/unsupported/qpdf/xpdf/XRef.cc @@ -1,11 +1,11 @@ //======================================================================== // // XRef.cc // -// Copyright 1996 Derek B. Noonburg +// Copyright 1996-2002 Glyph & Cog, LLC // //======================================================================== #ifdef __GNUC__ #pragma implementation #endif @@ -22,12 +22,13 @@ #include "Parser.h" #include "Dict.h" #ifndef NO_DECRYPTION #include "Decrypt.h" #endif #include "Error.h" +#include "ErrorCodes.h" #include "XRef.h" //------------------------------------------------------------------------ #define xrefSearchSize 1024 // read this many bytes at end of file // to look for 'startxref' @@ -46,16 +47,17 @@ //------------------------------------------------------------------------ // XRef //------------------------------------------------------------------------ XRef::XRef(BaseStream *strA, GString *ownerPassword, GString *userPassword) { - int pos; + Guint pos; int i; ok = gTrue; + errCode = errNone; size = 0; entries = NULL; streamEnds = NULL; streamEndsLen = 0; // read the trailer @@ -64,31 +66,33 @@ XRef::XRef(BaseStream *strA, GString *ownerPassword, GString *userPassword) { pos = readTrailer(); // if there was a problem with the trailer, // try to reconstruct the xref table if (pos == 0) { if (!(ok = constructXRef())) { + errCode = errDamaged; return; } // trailer is ok - read the xref table } else { entries = (XRefEntry *)gmalloc(size * sizeof(XRefEntry)); for (i = 0; i < size; ++i) { - entries[i].offset = -1; + entries[i].offset = 0xffffffff; entries[i].used = gFalse; } while (readXRef(&pos)) ; // if there was a problem with the xref table, // try to reconstruct it if (!ok) { gfree(entries); size = 0; entries = NULL; if (!(ok = constructXRef())) { + errCode = errDamaged; return; } } } // now set the trailer dictionary's xref pointer so we can fetch @@ -98,12 +102,13 @@ XRef::XRef(BaseStream *strA, GString *ownerPassword, GString *userPassword) { // check for encryption #ifndef NO_DECRYPTION encrypted = gFalse; #endif if (checkEncrypted(ownerPassword, userPassword)) { ok = gFalse; + errCode = errEncrypted; return; } } XRef::~XRef() { gfree(entries); @@ -112,23 +117,24 @@ XRef::~XRef() { gfree(streamEnds); } } // Read startxref position, xref table size, and root. Returns // first xref position. -int XRef::readTrailer() { +Guint XRef::readTrailer() { Parser *parser; Object obj; char buf[xrefSearchSize+1]; - int n, pos, pos1; + int n; + Guint pos, pos1; char *p; int c; int i; // read last xrefSearchSize bytes - str->setPos(-xrefSearchSize); + str->setPos(xrefSearchSize, -1); for (n = 0; n < xrefSearchSize; ++n) { if ((c = str->getChar()) == EOF) break; buf[n] = c; } buf[n] = '\0'; @@ -138,13 +144,13 @@ int XRef::readTrailer() { if (!strncmp(&buf[i], "startxref", 9)) break; } if (i < 0) return 0; for (p = &buf[i+9]; isspace(*p); ++p) ; - pos = lastXRefPos = atoi(p); + pos = lastXRefPos = strToUnsigned(p); // find trailer dict by looking after first xref table // (NB: we can't just use the trailer dict at the end of the file -- // this won't work for linearized files.) str->setPos(start + pos); for (i = 0; i < 4; ++i) @@ -173,14 +179,15 @@ int XRef::readTrailer() { pos1 += (p - buf) + n * 20; } pos1 += 7; // read trailer dict obj.initNull(); - parser = new Parser(NULL, new Lexer(NULL, str->makeSubStream(start + pos1, - -1, &obj))); + parser = new Parser(NULL, + new Lexer(NULL, + str->makeSubStream(start + pos1, gFalse, 0, &obj))); parser->getObj(&trailerDict); if (trailerDict.isDict()) { trailerDict.dictLookupNF("Size", &obj); if (obj.isInt()) size = obj.getInt(); else @@ -201,13 +208,13 @@ int XRef::readTrailer() { // return first xref position return pos; } // Read an xref table and the prev pointer from the trailer. -GBool XRef::readXRef(int *pos) { +GBool XRef::readXRef(Guint *pos) { Parser *parser; Object obj, obj2; char s[20]; GBool more; int first, newSize, n, i, j; int c; @@ -258,27 +265,27 @@ GBool XRef::readXRef(int *pos) { // check for buggy PDF files with an incorrect (too small) xref // table size if (first + n > size) { newSize = size + 256; entries = (XRefEntry *)grealloc(entries, newSize * sizeof(XRefEntry)); for (i = size; i < newSize; ++i) { - entries[i].offset = -1; + entries[i].offset = 0xffffffff; entries[i].used = gFalse; } size = newSize; } for (i = first; i < first + n; ++i) { for (j = 0; j < 20; ++j) { if ((c = str->getChar()) == EOF) { goto err2; } s[j] = (char)c; } - if (entries[i].offset < 0) { + if (entries[i].offset == 0xffffffff) { s[10] = '\0'; - entries[i].offset = atoi(s); + entries[i].offset = strToUnsigned(s); s[16] = '\0'; entries[i].gen = atoi(&s[11]); if (s[17] == 'n') { entries[i].used = gTrue; } else if (s[17] == 'f') { entries[i].used = gFalse; @@ -290,34 +297,35 @@ GBool XRef::readXRef(int *pos) { // instead of 0. if (i == 1 && first == 1 && entries[1].offset == 0 && entries[1].gen == 65535 && !entries[1].used) { i = first = 0; entries[0] = entries[1]; - entries[1].offset = -1; + entries[1].offset = 0xffffffff; } } } } // read prev pointer from trailer dictionary obj.initNull(); - parser = new Parser(NULL, new Lexer(NULL, str->makeSubStream(str->getPos(), - -1, &obj))); + parser = new Parser(NULL, + new Lexer(NULL, + str->makeSubStream(str->getPos(), gFalse, 0, &obj))); parser->getObj(&obj); if (!obj.isCmd("trailer")) { goto err1; } obj.free(); parser->getObj(&obj); if (!obj.isDict()) { goto err1; } obj.getDict()->lookupNF("Prev", &obj2); if (obj2.isInt()) { - *pos = obj2.getInt(); + *pos = (Guint)obj2.getInt(); more = gTrue; } else { more = gFalse; } obj.free(); obj2.free(); @@ -334,13 +342,13 @@ GBool XRef::readXRef(int *pos) { // Attempt to construct an xref table for a damaged file. GBool XRef::constructXRef() { Parser *parser; Object obj; char buf[256]; - int pos; + Guint pos; int num, gen; int newSize; int streamEndsSize; char *p; int i; GBool gotRoot; @@ -357,14 +365,15 @@ GBool XRef::constructXRef() { } p = buf; // got trailer dictionary if (!strncmp(p, "trailer", 7)) { obj.initNull(); - parser = new Parser(NULL, new Lexer(NULL, - str->makeSubStream(start + pos + 7, -1, &obj))); + parser = new Parser(NULL, + new Lexer(NULL, + str->makeSubStream(start + pos + 7, gFalse, 0, &obj))); if (!trailerDict.isNone()) trailerDict.free(); parser->getObj(&trailerDict); if (trailerDict.isDict()) { trailerDict.dictLookupNF("Root", &obj); if (obj.isRef()) { @@ -400,13 +409,13 @@ GBool XRef::constructXRef() { if (!strncmp(p, "obj", 3)) { if (num >= size) { newSize = (num + 1 + 255) & ~255; entries = (XRefEntry *) grealloc(entries, newSize * sizeof(XRefEntry)); for (i = size; i < newSize; ++i) { - entries[i].offset = -1; + entries[i].offset = 0xffffffff; entries[i].used = gFalse; } size = newSize; } if (!entries[num].used || gen >= entries[num].gen) { entries[num].offset = pos - start; @@ -418,13 +427,14 @@ GBool XRef::constructXRef() { } } } else if (!strncmp(p, "endstream", 9)) { if (streamEndsLen == streamEndsSize) { streamEndsSize += 64; - streamEnds = (int *)grealloc(streamEnds, streamEndsSize * sizeof(int)); + streamEnds = (Guint *)grealloc(streamEnds, + streamEndsSize * sizeof(int)); } streamEnds[streamEndsLen++] = pos; } } if (gotRoot) @@ -577,16 +587,17 @@ Object *XRef::fetch(int num, int gen, Object *obj) { if (num < 0 || num >= size) { obj->initNull(); return obj; } e = &entries[num]; - if (e->gen == gen && e->offset >= 0) { + if (e->gen == gen && e->offset != 0xffffffff) { obj1.initNull(); - parser = new Parser(this, new Lexer(this, - str->makeSubStream(start + e->offset, -1, &obj1))); + parser = new Parser(this, + new Lexer(this, + str->makeSubStream(start + e->offset, gFalse, 0, &obj1))); parser->getObj(&obj1); parser->getObj(&obj2); parser->getObj(&obj3); if (obj1.isInt() && obj1.getInt() == num && obj2.isInt() && obj2.getInt() == gen && obj3.isCmd("obj")) { @@ -615,18 +626,18 @@ Object *XRef::getDocInfo(Object *obj) { // Added for the pdftex project. Object *XRef::getDocInfoNF(Object *obj) { return trailerDict.dictLookupNF("Info", obj); } -int XRef::getStreamEnd(int streamStart) { +GBool XRef::getStreamEnd(Guint streamStart, Guint *streamEnd) { int a, b, m; if (streamEndsLen == 0 || streamStart > streamEnds[streamEndsLen - 1]) { - return -1; + return gFalse; } a = -1; b = streamEndsLen - 1; // invariant: streamEnds[a] < streamStart <= streamEnds[b] while (b - a > 1) { @@ -634,8 +645,21 @@ int XRef::getStreamEnd(int streamStart) { if (streamStart <= streamEnds[m]) { b = m; } else { a = m; } } - return streamEnds[b]; + *streamEnd = streamEnds[b]; + return gTrue; +} + +Guint XRef::strToUnsigned(char *s) { + Guint x; + char *p; + int i; + + x = 0; + for (p = s, i = 0; *p && isdigit(*p) && i < 10; ++p, ++i) { + x = 10 * x + (*p - '0'); + } + return x; } |