From ab99a09f8a85cd9dcc8db7c7a36bb638a23a1c94 Mon Sep 17 00:00:00 2001 From: Jakub Kucharski Date: Wed, 15 Jun 2016 00:12:33 +0200 Subject: [PATCH] prevent PDFDoc::saveIncrementalUpdate from saving the trailer dict twice --- poppler/PDFDoc.cc | 33 ++++++++++++++++++++++++++++++++- poppler/PDFDoc.h | 2 ++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index 9fc43e5..fbc45d9 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -34,6 +34,7 @@ // Copyright (C) 2015 Li Junling // Copyright (C) 2015 André Guerreiro // Copyright (C) 2015 André Esser +// Copyright (C) 2016 Jakub Kucharski // // To see a description of the changes please see the Changelog file that // came with your tarball or type make ChangeLog if you are building from git @@ -920,6 +921,35 @@ int PDFDoc::saveWithoutChangesAs(OutStream *outStr) { return errNone; } +Goffset PDFDoc::getEndXRefTablePos () +{ + BaseStream *copyStr = str->copy(); + + static const char * trailerStr = "trailer"; + GooString gooStr; + + int c; + copyStr->setPos(1, -1); + for (Goffset i = 1; (c = copyStr->getChar()) != EOF;) { + copyStr->setPos(++i, -1); + + if (c != trailerStr[6-gooStr.getLength()]) { + gooStr.clear(); + continue; + } + + gooStr.insert(0, c); + if (!gooStr.cmp(trailerStr)) { + copyStr->close(); + delete copyStr; + return str->getLength() - (i-1); + } + } + + delete copyStr; + return str->getLength(); +} + void PDFDoc::saveIncrementalUpdate (OutStream* outStr) { XRef *uxref; @@ -927,7 +957,8 @@ void PDFDoc::saveIncrementalUpdate (OutStream* outStr) //copy the original file BaseStream *copyStr = str->copy(); copyStr->reset(); - while ((c = copyStr->getChar()) != EOF) { + Goffset endXRefTablePos = getEndXRefTablePos(); + for (Goffset i = 0; (c = copyStr->getChar()) != EOF && i < endXRefTablePos; i++) { outStr->put(c); } copyStr->close(); diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h index 3a65ecb..6fba488 100644 --- a/poppler/PDFDoc.h +++ b/poppler/PDFDoc.h @@ -29,6 +29,7 @@ // Copyright (C) 2013 Adrian Perez de Castro // Copyright (C) 2015 André Guerreiro // Copyright (C) 2015 André Esser +// Copyright (C) 2016 Jakub Kucharski // // To see a description of the changes please see the Changelog file that // came with your tarball or type make ChangeLog if you are building from git @@ -294,6 +295,7 @@ private: int uxrefSize, OutStream* outStr, GBool incrUpdate); static void writeString (GooString* s, OutStream* outStr, Guchar *fileKey, CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen); + Goffset getEndXRefTablePos (); void saveIncrementalUpdate (OutStream* outStr); void saveCompleteRewrite (OutStream* outStr); -- 2.8.3