diff --git a/core/core-awt/src/main/java/org/icepdf/core/util/redaction/Redactor.java b/core/core-awt/src/main/java/org/icepdf/core/util/redaction/Redactor.java index 2d4758320..00160b734 100644 --- a/core/core-awt/src/main/java/org/icepdf/core/util/redaction/Redactor.java +++ b/core/core-awt/src/main/java/org/icepdf/core/util/redaction/Redactor.java @@ -4,6 +4,7 @@ import org.icepdf.core.pobjects.PObject; import org.icepdf.core.pobjects.Page; import org.icepdf.core.pobjects.StateManager; +import org.icepdf.core.pobjects.annotations.Annotation; import org.icepdf.core.pobjects.annotations.RedactionAnnotation; import java.io.IOException; @@ -40,13 +41,11 @@ public static void burnRedactions(Document document) throws InterruptedException private static void convertRedactionToSquareAnnotation(StateManager stateManager, List redactionAnnotations) { for (RedactionAnnotation redactionAnnotation : redactionAnnotations) { - // todo clean up popup as square isn't markup -// redactionAnnotation.setSubtype(Annotation.SUBTYPE_SQUARE); -// redactionAnnotation.setFlag(Annotation.FLAG_LOCKED, true); -// redactionAnnotation.setFlag(Annotation.FLAG_READ_ONLY, true); -// redactionAnnotation.setFlag(Annotation.FLAG_LOCKED_CONTENTS, true); -// stateManager.addChange(new PObject(redactionAnnotation, redactionAnnotation.getPObjectReference())); - stateManager.addDeletion(new PObject(redactionAnnotation, redactionAnnotation.getPObjectReference())); + redactionAnnotation.setSubtype(Annotation.SUBTYPE_SQUARE); + redactionAnnotation.setFlag(Annotation.FLAG_LOCKED, true); + redactionAnnotation.setFlag(Annotation.FLAG_READ_ONLY, true); + redactionAnnotation.setFlag(Annotation.FLAG_LOCKED_CONTENTS, true); + stateManager.addChange(new PObject(redactionAnnotation, redactionAnnotation.getPObjectReference())); } } } diff --git a/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/BaseWriter.java b/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/BaseWriter.java index 96c09ce21..17881dee6 100644 --- a/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/BaseWriter.java +++ b/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/BaseWriter.java @@ -248,7 +248,7 @@ protected void writeByteString(String str, CountingOutputStream output) throws I } } - protected byte[] encryptStream(Stream stream) throws IOException { + protected byte[] encryptStream(Stream stream, byte[] outputData) throws IOException { // check if we need to encrypt the stream if (securityManager != null) { DictionaryEntries decodeParams; @@ -264,7 +264,7 @@ protected byte[] encryptStream(Stream stream) throws IOException { stream.getPObjectReference(), securityManager.getDecryptionKey(), decodeParams, - new ByteArrayInputStream(stream.getRawBytes()), true); + new ByteArrayInputStream(outputData), true); ByteArrayOutputStream out = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[16384]; diff --git a/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/HexStringObjectWriter.java b/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/HexStringObjectWriter.java index 41c3f0f88..ce0557364 100644 --- a/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/HexStringObjectWriter.java +++ b/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/HexStringObjectWriter.java @@ -13,6 +13,9 @@ public class HexStringObjectWriter extends BaseWriter { private static final byte[] BEGIN_HEX_STRING = "<".getBytes(); private static final byte[] END_HEX_STRING = ">".getBytes(); + private static final String HEX_REGEX = "(?=[<>\\\\])"; + private static final String HEX_REPLACEMENT = "\\\\"; + public HexStringObjectWriter(SecurityManager securityManager) { this.securityManager = securityManager; } @@ -20,20 +23,20 @@ public HexStringObjectWriter(SecurityManager securityManager) { public void write(PObject pObject, CountingOutputStream output) throws IOException { HexStringObject writeable = (HexStringObject) pObject.getObject(); if (pObject.isDoNotEncrypt()) { - writeRaw(writeable.getHexString(), output); + writeRaw(writeable.getHexString().replaceAll(HEX_REGEX, HEX_REPLACEMENT), output); } else if (securityManager != null) { if (writeable.isModified()) { // encryption will take care of any escape issue. String writeableString = writeable.encryption(writeable.getHexString(), pObject.getReference(), securityManager); - writeRaw(writeableString, output); + writeRaw(writeableString.replaceAll(HEX_REGEX, HEX_REPLACEMENT), output); } else { // just need to write the string data as is, string data will already be in the correct state writeRaw(writeable.toString(), output); } } else { // plain string make sure it's properly escaped. - writeRaw(writeable.getHexString(), output); + writeRaw(writeable.getHexString().replaceAll(HEX_REGEX, HEX_REPLACEMENT), output); } } diff --git a/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/ImageStreamWriter.java b/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/ImageStreamWriter.java index 367a3e761..62427a27f 100644 --- a/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/ImageStreamWriter.java +++ b/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/ImageStreamWriter.java @@ -22,7 +22,7 @@ public ImageStreamWriter(SecurityManager securityManager) { public void write(ImageStream imageStream, SecurityManager securityManager, CountingOutputStream output) throws IOException { byte[] outputData; - // decoded image is only set if the image was touch via a redaction burn. + // decoded image is only set if the image was touch via a redaction burn and will always be unencrypted if (imageStream.getDecodedImage() != null) { ImageEncoder imageEncoder = ImageEncoderFactory.createEncodedImage(imageStream); imageStream = imageEncoder.encode(); @@ -30,7 +30,7 @@ public void write(ImageStream imageStream, SecurityManager securityManager, Coun // check if we need to encrypt the stream if (securityManager != null) { - outputData = encryptStream(imageStream); + outputData = encryptStream(imageStream, outputData); } } else { // no modification, just write out the image unaltered. diff --git a/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/LiteralStringWriter.java b/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/LiteralStringWriter.java index 59045dfd6..e5fdec2d6 100644 --- a/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/LiteralStringWriter.java +++ b/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/LiteralStringWriter.java @@ -29,10 +29,10 @@ public void write(PObject pObject, CountingOutputStream output) throws IOExcepti // encryption will take care of any escape issue. String writeableString = writeable.encryption(writeable.getLiteralString(), pObject.getReference(), securityManager); - writeRaw(writeableString, output); + writeRaw(writeableString.replaceAll(LITERAL_REGEX, LITERAL_REPLACEMENT), output); } else { // just need to write the string data as is, string data will already be in the correct state - writeRaw(writeable.getLiteralString(), output); + writeRaw(writeable.getLiteralString().replaceAll(LITERAL_REGEX, LITERAL_REPLACEMENT), output); } } else { // plain string make sure it's properly escaped. diff --git a/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/StreamWriter.java b/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/StreamWriter.java index c8033ada2..5e53c4042 100644 --- a/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/StreamWriter.java +++ b/core/core-awt/src/main/java/org/icepdf/core/util/updater/writeables/StreamWriter.java @@ -42,7 +42,7 @@ public void write(Stream stream, SecurityManager securityManager, CountingOutput // check if we need to encrypt the stream if (securityManager != null) { - outputData = encryptStream(stream); + outputData = encryptStream(stream, outputData); } } else { outputData = stream.getRawBytes();