Index: poppler/Parser.h =================================================================== --- poppler/Parser.h (revision 54) +++ poppler/Parser.h (working copy) @@ -22,16 +22,14 @@ class Parser { public: - // Constructor. Parser(XRef *xrefA, Lexer *lexerA); - // Destructor. ~Parser(); // Get the next object from the input stream. Object *getObj(Object *obj, - Guchar *fileKey = NULL, int keyLength = 0, - int objNum = 0, int objGen = 0); + Guchar *fileKey = NULL, int keyLength = 0, + int objNum = 0, int objGen = 0); // Get stream. Stream *getStream() { return lexer->getStream(); } @@ -41,10 +39,10 @@ private: - XRef *xref; // the xref table for this PDF file - Lexer *lexer; // input stream - Object buf1, buf2; // next two tokens - int inlineImg; // set when inline image data is encountered + XRef *xref; // the xref table for this PDF file + Lexer *lexer; // input stream + Object buf1, buf2; // next two tokens + int inlineImg; // set when inline image data is encountered Stream *makeStream(Object *dict); void shift(int objNum = -1); Index: poppler/Object.h =================================================================== --- poppler/Object.h (revision 54) +++ poppler/Object.h (working copy) @@ -107,6 +107,10 @@ // Copy an object. Object *copy(Object *obj); + Object *shallowCopy(Object *obj) { + *obj = *this; + return obj; + } // If object is a Ref, fetch and return the referenced object. // Otherwise, return a copy of the object. @@ -164,7 +168,9 @@ // Dict accessors. int dictGetLength(); + void dictAddOwnKeyVal(UGooString *key, Object *val); void dictAdd(const UGooString &key, Object *val); + void dictAddOwnVal(const char *key, Object *val); GBool dictIs(char *dictType); Object *dictLookup(const UGooString &key, Object *obj); Object *dictLookupNF(const UGooString &key, Object *obj); @@ -249,6 +255,12 @@ inline void Object::dictAdd(const UGooString &key, Object *val) { dict->add(key, val); } +inline void Object::dictAddOwnVal(const char *key, Object *val) + { dict->addOwnVal(key, val); } + +inline void Object::dictAddOwnKeyVal(UGooString *key, Object *val) + { dict->addOwnKeyVal(key, val); } + inline GBool Object::dictIs(char *dictType) { return dict->is(dictType); } Index: poppler/Parser.cc =================================================================== --- poppler/Parser.cc (revision 54) +++ poppler/Parser.cc (working copy) @@ -39,7 +39,6 @@ Object *Parser::getObj(Object *obj, Guchar *fileKey, int keyLength, int objNum, int objGen) { - UGooString key; Stream *str; Object obj2; int num; @@ -76,13 +75,13 @@ error(getPos(), "Dictionary key must be a name object"); shift(); } else { - // buf1 might go away in shift(), so remember the name - key.Set(buf1.getName()); + // buf1 might go away in shift(), so construct the key + UGooString *key = new UGooString(buf1.getName()); shift(); if (buf1.isEOF() || buf1.isError()) { break; } - obj->dictAdd(key, getObj(&obj2, fileKey, keyLength, objNum, objGen)); + obj->dictAddOwnKeyVal(key, getObj(&obj2, fileKey, keyLength, objNum, objGen)); } } if (buf1.isEOF()) @@ -129,7 +128,11 @@ // simple object } else { - buf1.copy(obj); + // avoid re-allocating memory for complex objects like strings by + // shallow copy of to and nulling so that + // subsequent buf1.free() won't free this memory + buf1.shallowCopy(obj); + buf1.initNull(); shift(); } @@ -212,7 +215,7 @@ inlineImg = 1; } buf1.free(); - buf1 = buf2; + buf2.shallowCopy(&buf1); if (inlineImg > 0) // don't buffer inline image data buf2.initNull(); else Index: poppler/Dict.cc =================================================================== --- poppler/Dict.cc (revision 54) +++ poppler/Dict.cc (working copy) @@ -43,7 +43,7 @@ gfree(entries); } -void Dict::add(const UGooString &key, Object *val) { +void Dict::addOwnKeyVal(UGooString *key, Object *val) { if (length == size) { if (length == 0) { size = 8; @@ -52,7 +52,7 @@ } entries = (DictEntry *)greallocn(entries, size, sizeof(DictEntry)); } - entries[length].key = new UGooString(key); + entries[length].key = key; entries[length].val = *val; ++length; } Index: poppler/Dict.h =================================================================== --- poppler/Dict.h (revision 54) +++ poppler/Dict.h (working copy) @@ -40,7 +40,14 @@ int getLength() const { return length; } // Add an entry - void add(const UGooString &key, Object *val); + void addOwnKeyVal(UGooString *key, Object *val); + // FIXME: should also be renamed to addOwnVal() + void add(const UGooString &key, Object *val) { + addOwnKeyVal(new UGooString(key), val); + } + void addOwnVal(const char *key, Object *val) { + addOwnKeyVal(new UGooString(key), val); + } // Check if dictionary is of specified type. GBool is(char *type) const;