EncryptingOutputStream.java
5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.stream.OutputByteStream
*/
package com.adobe.internal.pdftoolkit.core.cos;
import com.adobe.internal.io.stream.OutputByteStream;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosEncryption;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityConfigurationException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandler;
import com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandlerState;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import java.io.IOException;
import java.io.OutputStream;
public class EncryptingOutputStream
extends OutputStream {
private EncryptionHandlerState mEncryptionHandler;
private boolean mNeedsEncryption;
private byte[] mStreamKey;
private OutputByteStream mOutStream;
boolean mInitialized;
boolean mDone;
EncryptingOutputStream(CosStream cosStream, OutputByteStream destination, EncryptionHandlerState encryptionHandler) throws PDFIOException, PDFSecurityException, PDFCosParseException, IOException {
this.mNeedsEncryption = cosStream.needsEncryption();
this.mEncryptionHandler = encryptionHandler;
this.initialize(cosStream, destination);
}
EncryptingOutputStream(CosStream cosStream, OutputByteStream destination) throws PDFIOException, PDFSecurityException, PDFCosParseException, IOException {
this.mNeedsEncryption = cosStream.needsEncryption();
if (this.mNeedsEncryption) {
ASName cryptFilter;
EncryptionHandler streamEncryption = cosStream.getDocument().getEncryption().getStreamEncryptionHandler((cryptFilter = cosStream.getCryptFilter()) == null ? null : cryptFilter.asString(true));
if (streamEncryption == null) {
throw new PDFSecurityConfigurationException("Cannot find Security Handler for a stream");
}
this.mEncryptionHandler = streamEncryption.createEncryptionHandlerState();
}
this.initialize(cosStream, destination);
}
private void initialize(CosStream cosStream, OutputByteStream destination) throws IOException, PDFSecurityException, PDFCosParseException, PDFIOException {
if (this.mNeedsEncryption) {
if (this.mEncryptionHandler == null) {
throw new PDFSecurityConfigurationException("Cannot find Security Handler for a stream");
}
this.mStreamKey = cosStream.getDocument().getEncryption().getStreamEncryptionKey(cosStream, true);
}
this.mOutStream = destination;
this.mInitialized = false;
this.mDone = false;
}
public void write(int buffer) throws IOException {
byte[] in = new byte[]{(byte)buffer};
this.write(in);
}
public void write(byte[] buffer) throws IOException {
this.write(buffer, 0, buffer.length);
}
public void write(byte[] buffer, int off, int len) throws IOException {
if (this.mDone) {
throw new IOException("Cannot write to a closed stream");
}
byte[] decrypted = buffer;
int offset = off;
int bufLen = len;
try {
if (this.mNeedsEncryption) {
byte[] arrby = decrypted = this.mInitialized ? this.mEncryptionHandler.update(buffer, off, len) : this.mEncryptionHandler.init(buffer, off, len, this.mStreamKey, 1);
if (decrypted == null) {
decrypted = new byte[]{};
}
offset = 0;
bufLen = decrypted.length;
this.mInitialized = true;
}
this.mOutStream.write(decrypted, offset, bufLen);
}
catch (PDFSecurityException e) {
IOException excp = new IOException();
excp.initCause(e);
throw excp;
}
}
public void flush() throws IOException {
if (this.mDone) {
throw new IOException("Cannot flush closed stream");
}
this.mOutStream.flush();
}
public void close() throws IOException {
if (this.mDone) {
throw new IOException("Cannot close already closed stream");
}
if (this.mNeedsEncryption) {
byte[] encrypted;
try {
encrypted = this.mEncryptionHandler.finish();
}
catch (PDFSecurityException e) {
IOException excp = new IOException();
excp.initCause(e);
throw excp;
}
if (encrypted.length > 0) {
this.mNeedsEncryption = false;
this.write(encrypted);
}
}
this.mDone = false;
}
}