Skip to content

Commit

Permalink
GH-314 fix encoding and encrypting streams regression
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Corless committed Feb 5, 2024
1 parent 4fd6585 commit f3b43bc
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,13 +41,11 @@ public static void burnRedactions(Document document) throws InterruptedException
private static void convertRedactionToSquareAnnotation(StateManager stateManager,
List<RedactionAnnotation> 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()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,30 @@ 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;
}

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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ 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();
outputData = imageStream.getRawBytes();

// 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f3b43bc

Please sign in to comment.