diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index 46c4544..102bd4c 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -991,7 +991,7 @@ void PDFDoc::saveCompleteRewrite (OutStream* outStr) } void PDFDoc::writeDictionnary (Dict* dict, OutStream* outStr, XRef *xRef, Guint numOffset, Guchar *fileKey, - CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen) + CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen, GBool useOC) { Object obj1; outStr->printf("<<"); @@ -1000,7 +1000,21 @@ void PDFDoc::writeDictionnary (Dict* dict, OutStream* outStr, XRef *xRef, Guint GooString *keyNameToPrint = keyName.sanitizedName(gFalse /* non ps mode */); outStr->printf("/%s ", keyNameToPrint->getCString()); delete keyNameToPrint; - writeObject(dict->getValNF(i, &obj1), outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen); + Object *val = dict->getValNF(i, &obj1); + Guint localNumOffset = numOffset; + if (useOC && keyName.cmp("OC") == 0) { + Object ocObject; + Object type; + dict->getVal(i, &ocObject); + if (ocObject.isDict() && ocObject.getDict()->lookup("Type", &type)->isName()) { + if (strcmp(type.getName(), "OCMD") == 0 || strcmp(type.getName(), "OCG") == 0) { + localNumOffset = 0; + } + } + type.free(); + ocObject.free(); + } + writeObject(val, outStr, xRef, localNumOffset, fileKey, encAlgorithm, keyLength, objNum, objGen, useOC); obj1.free(); } outStr->printf(">> "); @@ -1110,7 +1124,7 @@ Goffset PDFDoc::writeObjectHeader (Ref *ref, OutStream* outStr) } void PDFDoc::writeObject (Object* obj, OutStream* outStr, XRef *xRef, Guint numOffset, Guchar *fileKey, - CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen) + CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen, GBool useOC) { Array *array; Object obj1; @@ -1151,13 +1165,13 @@ void PDFDoc::writeObject (Object* obj, OutStream* outStr, XRef *xRef, Guint numO array = obj->getArray(); outStr->printf("["); for (int i=0; igetLength(); i++) { - writeObject(array->getNF(i, &obj1), outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen); + writeObject(array->getNF(i, &obj1), outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen, useOC); obj1.free(); } outStr->printf("] "); break; case objDict: - writeDictionnary (obj->getDict(), outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen); + writeDictionnary (obj->getDict(), outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen, useOC); break; case objStream: { @@ -1220,7 +1234,7 @@ void PDFDoc::writeObject (Object* obj, OutStream* outStr, XRef *xRef, Guint numO } stream->getDict()->remove("DecodeParms"); - writeDictionnary (stream->getDict(),outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen); + writeDictionnary (stream->getDict(),outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen, useOC); writeStream (stream,outStr); delete encStream; obj1.free(); @@ -1238,7 +1252,7 @@ void PDFDoc::writeObject (Object* obj, OutStream* outStr, XRef *xRef, Guint numO } } } - writeDictionnary (stream->getDict(), outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen); + writeDictionnary (stream->getDict(), outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen, useOC); writeRawStream (stream, outStr); } break; @@ -1751,11 +1765,11 @@ Guint PDFDoc::writePageObjects(OutStream *outStr, XRef *xRef, Guint numOffset, G getXRef()->fetch(ref.num - numOffset, ref.gen, &obj); Goffset offset = writeObjectHeader(&ref, outStr); if (combine) { - writeObject(&obj, outStr, getXRef(), numOffset, NULL, cryptRC4, 0, 0, 0); + writeObject(&obj, outStr, getXRef(), numOffset, NULL, cryptRC4, 0, 0, 0, getOptContentConfig() != NULL); } else if (xRef->getEntry(n)->getFlag(XRefEntry::Unencrypted)) { - writeObject(&obj, outStr, NULL, cryptRC4, 0, 0, 0); + writeObject(&obj, outStr, NULL, cryptRC4, 0, 0, 0, getOptContentConfig() != NULL); } else { - writeObject(&obj, outStr, fileKey, encAlgorithm, keyLength, ref.num, ref.gen); + writeObject(&obj, outStr, fileKey, encAlgorithm, keyLength, ref.num, ref.gen, getOptContentConfig() != NULL); } writeObjectFooter(outStr); xRef->add(ref.num, ref.gen, offset, gTrue); diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h index 6c40f7b..00a9b6a 100644 --- a/poppler/PDFDoc.h +++ b/poppler/PDFDoc.h @@ -254,7 +254,7 @@ public: // write all objects used by pageDict to outStr Guint writePageObjects(OutStream *outStr, XRef *xRef, Guint numOffset, GBool combine = gFalse); static void writeObject (Object *obj, OutStream* outStr, XRef *xref, Guint numOffset, Guchar *fileKey, - CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen); + CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen, GBool useOC = gFalse); static void writeHeader(OutStream *outStr, int major, int minor); // Ownership goes to the caller @@ -270,18 +270,18 @@ private: void markDictionnary (Dict* dict, XRef *xRef, XRef *countRef, Guint numOffset, int oldRefNum, int newRefNum); void markObject (Object *obj, XRef *xRef, XRef *countRef, Guint numOffset, int oldRefNum, int newRefNum); static void writeDictionnary (Dict* dict, OutStream* outStr, XRef *xRef, Guint numOffset, Guchar *fileKey, - CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen); + CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen, GBool useOC = gFalse); // Write object header to current file stream and return its offset static Goffset writeObjectHeader (Ref *ref, OutStream* outStr); static void writeObjectFooter (OutStream* outStr); void writeObject (Object *obj, OutStream* outStr, Guchar *fileKey, CryptAlgorithm encAlgorithm, - int keyLength, int objNum, int objGen) - { writeObject(obj, outStr, getXRef(), 0, fileKey, encAlgorithm, keyLength, objNum, objGen); } + int keyLength, int objNum, int objGen, GBool useOC = gFalse) + { writeObject(obj, outStr, getXRef(), 0, fileKey, encAlgorithm, keyLength, objNum, objGen, useOC); } void writeDictionnary (Dict* dict, OutStream* outStr, Guchar *fileKey, CryptAlgorithm encAlgorithm, - int keyLength, int objNum, int objGen) - { writeDictionnary(dict, outStr, getXRef(), 0, fileKey, encAlgorithm, keyLength, objNum, objGen); } + int keyLength, int objNum, int objGen, GBool useOC = gFalse) + { writeDictionnary(dict, outStr, getXRef(), 0, fileKey, encAlgorithm, keyLength, objNum, objGen, useOC); } static void writeStream (Stream* str, OutStream* outStr); static void writeRawStream (Stream* str, OutStream* outStr); void writeXRefTableTrailer (Goffset uxrefOffset, XRef *uxref, GBool writeAllEntries,