DecryptingInputStream.java
5.61 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.stream.InputByteStream
*/
package com.adobe.internal.pdftoolkit.core.cos;
import com.adobe.internal.io.stream.InputByteStream;
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 com.adobe.internal.pdftoolkit.core.util.ByteOps;
import java.io.IOException;
import java.io.InputStream;
class DecryptingInputStream
extends InputStream {
private static final int BUF_LEN = 65536;
private InputStream mReadStream;
private EncryptionHandlerState mEncryptionHandler;
private byte[] mEncrypted = new byte[65536];
private byte[] mSingleByte = new byte[1];
private byte[] mDecrypted;
private int mDecryptedPos;
private boolean mNeedsDecryption;
private boolean mHasBuffer;
private boolean mDone;
private boolean mClosed;
DecryptingInputStream(CosStream cosStream, InputStream content, EncryptionHandlerState encryptionStateHandler) throws PDFIOException, PDFSecurityException, PDFCosParseException, IOException {
this.mNeedsDecryption = cosStream.needsDecryption(null);
this.mEncryptionHandler = encryptionStateHandler;
this.initialize(cosStream, content);
}
DecryptingInputStream(CosStream cosStream, InputStream content) throws PDFIOException, PDFSecurityException, PDFCosParseException, IOException {
this.mNeedsDecryption = cosStream.needsDecryption(null);
if (this.mNeedsDecryption) {
ASName cryptFilter;
EncryptionHandler streamDecryption = cosStream.getDocument().getEncryption().getStreamDecryptionHandler((cryptFilter = cosStream.getCryptFilter()) == null ? null : cryptFilter.asString(true));
if (streamDecryption == null) {
throw new PDFSecurityConfigurationException("Cannot find Security Handler for a stream");
}
this.mEncryptionHandler = streamDecryption.createEncryptionHandlerState();
}
this.initialize(cosStream, content);
}
public int read() throws IOException {
if (this.read(this.mSingleByte) != 1) {
return -1;
}
return this.mSingleByte[0];
}
public int read(byte[] buf) throws IOException {
return this.read(buf, 0, buf.length);
}
public int read(byte[] buf, int off, int len) throws IOException {
if (this.mClosed) {
throw new IOException("Cannot read from a closed stream");
}
if (!this.mNeedsDecryption) {
return this.mReadStream.read(buf, off, len);
}
if (!this.readBuf()) {
return -1;
}
int retLen = ByteOps.copy(this.mDecrypted, this.mDecryptedPos, buf, off, len);
this.mDecryptedPos += retLen;
if (this.mDecryptedPos >= this.mDecrypted.length) {
this.mHasBuffer = false;
}
return retLen;
}
private void initialize(CosStream cosStream, InputStream content) throws PDFIOException, IOException, PDFSecurityException, PDFCosParseException {
this.mReadStream = content;
int inLen = this.mReadStream.read(this.mEncrypted);
if (this.mNeedsDecryption) {
if (this.mEncryptionHandler == null) {
throw new PDFSecurityConfigurationException("Cannot find Security Handler for a stream");
}
this.mDecrypted = this.mEncryptionHandler.init(this.mEncrypted, 0, inLen, cosStream.getDocument().getEncryption().getStreamEncryptionKey(cosStream, false), 2);
if (this.mDecrypted == null) {
this.mDecrypted = new byte[0];
}
}
this.mDecryptedPos = 0;
this.mHasBuffer = true;
this.mDone = false;
this.mClosed = false;
}
private boolean readBuf() throws IOException {
if (this.mDone) {
return false;
}
if (!this.mHasBuffer) {
try {
int inLen = this.mReadStream.read(this.mEncrypted);
if (inLen < 0) {
this.mDecrypted = this.mEncryptionHandler.finish();
this.mDone = true;
} else {
this.mDecrypted = this.mEncryptionHandler.update(this.mEncrypted, 0, inLen);
}
}
catch (PDFSecurityException e) {
IOException excp = new IOException();
excp.initCause(e);
throw excp;
}
this.mDecryptedPos = 0;
this.mHasBuffer = true;
}
return true;
}
public void close() throws IOException {
if (this.mReadStream != null) {
this.mReadStream.close();
}
this.mClosed = true;
}
public int available() throws IOException {
if (!this.mDone && !this.mClosed) {
int approxBytes = this.mDecrypted.length - this.mDecryptedPos - 1;
if (approxBytes <= 0) {
return 1;
}
return approxBytes;
}
return 0;
}
}