[poppler] 2 commits - poppler/PDFDoc.cc poppler/XRef.cc poppler/XRef.h

Albert Astals Cid aacid at kemper.freedesktop.org
Fri Jul 25 12:34:21 PDT 2008


 poppler/PDFDoc.cc |    5 ++--
 poppler/XRef.cc   |   57 +++++++++++++++++++++++++++++++++++++++++++-----------
 poppler/XRef.h    |    2 -
 3 files changed, 50 insertions(+), 14 deletions(-)

New commits:
commit 040d244b97a554342061c777a286e99dbb9acabd
Author: Albert Astals Cid <aacid at kde.org>
Date:   Fri Jul 25 21:31:55 2008 +0200

    Need this or otherwise it crashes on complete overwrite

diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
index c6e3032..c5abe30 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -838,6 +838,7 @@ void PDFDoc::writeTrailer (Guint uxrefOffset, int uxrefSize, OutStream* outStr,
   } else {
     //new file => same values for the two identifiers
     obj2.arrayAdd(&obj1);
+    obj1.initString(new GooString((const char*)digest, 16));
     obj2.arrayAdd(&obj1);
     trailerDict->set("ID", &obj2);
   }
commit 11ebceeef938a7e6fa9b5437e65b5b4b822f3018
Author: Albert Astals Cid <aacid at kde.org>
Date:   Fri Jul 25 21:30:24 2008 +0200

    Fix my last fix about saving, i think this is the correct one
    
     - One incremental update only write the modified entries xref, not all
     - Update gen to 0 when resizing entries
     - On XRef::add correctly initialize all newly allocated entries

diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
index da712a5..c6e3032 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -552,7 +552,7 @@ void PDFDoc::saveIncrementalUpdate (OutStream* outStr)
   }
 
   Guint uxrefOffset = outStr->getPos();
-  uxref->writeToFile(outStr);
+  uxref->writeToFile(outStr, gFalse /* do not write unnecessary entries */);
 
   writeTrailer(uxrefOffset, objectsCount, outStr, gTrue);
 
@@ -592,7 +592,7 @@ void PDFDoc::saveCompleteRewrite (OutStream* outStr)
     }
   }
   Guint uxrefOffset = outStr->getPos();
-  uxref->writeToFile(outStr);
+  uxref->writeToFile(outStr, gTrue /* write all entries */);
 
   writeTrailer(uxrefOffset, uxref->getSize(), outStr, gFalse);
 
diff --git a/poppler/XRef.cc b/poppler/XRef.cc
index 30d514c..c3c4717 100644
--- a/poppler/XRef.cc
+++ b/poppler/XRef.cc
@@ -413,6 +413,7 @@ GBool XRef::readXRefTable(Parser *parser, Guint *pos) {
 	entries[i].type = xrefEntryFree;
 	entries[i].obj.initNull ();
 	entries[i].updated = false;
+	entries[i].gen = 0;
       }
       size = newSize;
     }
@@ -527,6 +528,7 @@ GBool XRef::readXRefStream(Stream *xrefStr, Guint *pos) {
       entries[i].type = xrefEntryFree;
       entries[i].obj.initNull ();
       entries[i].updated = false;
+      entries[i].gen = 0;
     }
     size = newSize;
   }
@@ -623,6 +625,7 @@ GBool XRef::readXRefStreamSection(Stream *xrefStr, int *w, int first, int n) {
       entries[i].type = xrefEntryFree;
       entries[i].obj.initNull ();
       entries[i].updated = false;
+      entries[i].gen = 0;
     }
     size = newSize;
   }
@@ -1005,8 +1008,15 @@ Guint XRef::strToUnsigned(char *s) {
 
 void XRef::add(int num, int gen, Guint offs, GBool used) {
   if (num >= size) {
+    entries = (XRefEntry *)greallocn(entries, num + 1, sizeof(XRefEntry));
+    for (int i = size; i < num + 1; ++i) {
+      entries[i].offset = 0xffffffff;
+      entries[i].type = xrefEntryFree;
+      entries[i].obj.initNull ();
+      entries[i].updated = false;
+      entries[i].gen = 0;
+    }
     size = num + 1;
-    entries = (XRefEntry *)greallocn(entries, size, sizeof(XRefEntry));
   }
   XRefEntry *e = &entries[num];
   e->gen = gen;
@@ -1059,26 +1069,51 @@ Ref XRef::addIndirectObject (Object* o) {
   return r;
 }
 
-
-//used to sort the entries
-void XRef::writeToFile(OutStream* outStr) {
+void XRef::writeToFile(OutStream* outStr, GBool writeAllEntries) {
   //create free entries linked-list
   if (entries[0].gen != 65535) {
     error(-1, "XRef::writeToFile, entry 0 of the XRef is invalid (gen != 65535)\n");
   }
-  int lastFreeEntry = 0; 
+  int lastFreeEntry = 0;
   for (int i=0; i<size; i++) {
     if (entries[i].type == xrefEntryFree) {
       entries[lastFreeEntry].offset = i;
       lastFreeEntry = i;
     }
   }
-  //write the new xref
-  outStr->printf("xref\r\n");
-  outStr->printf("%i %i\r\n", 0, size);
-  for (int i=0; i<size; i++) {
-    if(entries[i].gen > 65535) entries[i].gen = 65535; //cap generation number to 65535 (required by PDFReference)
-    outStr->printf("%010i %05i %c\r\n", entries[i].offset, entries[i].gen, (entries[i].type==xrefEntryFree)?'f':'n');
+
+  if (writeAllEntries) {
+    //write the new xref
+    outStr->printf("xref\r\n");
+    outStr->printf("%i %i\r\n", 0, size);
+    for (int i=0; i<size; i++) {
+      XRefEntry &e = entries[i];
+
+      if(e.gen > 65535) e.gen = 65535; //cap generation number to 65535 (required by PDFReference)
+      outStr->printf("%010i %05i %c\r\n", e.offset, e.gen, (e.type==xrefEntryFree)?'f':'n');
+    }
+  } else {
+    //write the new xref
+    outStr->printf("xref\r\n");
+    int i = 0;
+    while (i < size) {
+      int j;
+      for(j=i; j<size; j++) { //look for consecutive entries
+        if ((entries[j].type == xrefEntryFree) && (entries[j].gen == 0))
+          break;
+      }
+      if (j-i != 0)
+      {
+        outStr->printf("%i %i\r\n", i, j-i);
+        for (int k=i; k<j; k++) {
+          XRefEntry &e = entries[k];
+          if(e.gen > 65535) e.gen = 65535; //cap generation number to 65535 (required by PDFReference)
+          outStr->printf("%010i %05i %c\r\n", e.offset, e.gen, (e.type==xrefEntryFree)?'f':'n');
+        }
+        i = j;
+      }
+      else ++i;
+    }
   }
 }
 
diff --git a/poppler/XRef.h b/poppler/XRef.h
index a7e047f..2081db5 100644
--- a/poppler/XRef.h
+++ b/poppler/XRef.h
@@ -111,7 +111,7 @@ public:
   void setModifiedObject(Object* o, Ref r);
   Ref addIndirectObject (Object* o);
   void add(int num, int gen,  Guint offs, GBool used);
-  void writeToFile(OutStream* outStr);
+  void writeToFile(OutStream* outStr, GBool writeAllEntries);
 
 private:
 


More information about the poppler mailing list