CosPDFOptimizer.java
11.2 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* 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.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosBoolean;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosName;
import com.adobe.internal.pdftoolkit.core.cos.CosNull;
import com.adobe.internal.pdftoolkit.core.cos.CosNumeric;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.cos.CosString;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
final class CosPDFOptimizer {
private static final byte kNullType = 0;
private static final byte kIntegerType = 1;
private static final byte kRealType = 2;
private static final byte kBooleanType = 3;
private static final byte kNameType = 4;
private static final byte kStringType = 5;
private static final byte kDictionaryType = 6;
private static final byte kArrayType = 7;
private static final byte kStreamType = 8;
private CosDocument mCosDoc;
MessageDigest mDigester;
Digest mNullDigest;
HashMap mObjToDigest;
HashMap mDigestToObj;
private CosPDFOptimizer(CosDocument cosDocument) {
this.mCosDoc = cosDocument;
}
protected static CosPDFOptimizer newInstance(CosDocument cosDocument) {
return new CosPDFOptimizer(cosDocument);
}
protected void freeDuplicateResources() throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
this.mDigester = CosPDFOptimizer.newDigesterInstance();
if (this.mDigester == null) {
return;
}
this.mNullDigest = new Digest(null);
this.mObjToDigest = new HashMap();
this.mDigestToObj = new HashMap();
CosDictionary node = CosPDFOptimizer.safeGetDict(this.mCosDoc.getRoot(), ASName.k_Pages);
this.freeDupPageTreeNode(node);
}
private void freeDupPageTreeNode(CosDictionary node) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
if (node == null) {
return;
}
CosArray kids = CosPDFOptimizer.safeGetArray(node, ASName.k_Kids);
if (kids != null) {
Iterator<CosObject> iter = kids.iterator();
while (iter.hasNext()) {
this.freeDupPageTreeNode((CosDictionary)iter.next());
}
} else {
this.freeDupPageRes(CosPDFOptimizer.safeGetDict(node, ASName.k_Resources));
}
}
private void freeDupPageRes(CosDictionary pageRes) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
if (pageRes == null) {
return;
}
Iterator<ASName> iter = pageRes.keyIterator();
while (iter.hasNext()) {
ASName className = iter.next();
CosDictionary resClass = CosPDFOptimizer.safeGetDict(pageRes, className);
this.freeDupResObj(resClass);
}
}
private CosObject freeDupResObj(CosObject resource) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
if (resource == null || resource instanceof CosNull) {
this.updateByte(0);
return null;
}
MessageDigest savedDigester = this.mDigester;
int objNum = resource.getObjNum();
Integer objNumInt = objNum;
if (objNum != 0) {
if (this.mObjToDigest.containsKey(objNumInt)) {
byte[] localDigest = ((Digest)this.mObjToDigest.get(objNumInt)).getBytes();
if (localDigest != null) {
this.mDigester.update(localDigest);
}
return null;
}
this.mDigester = CosPDFOptimizer.newDigesterInstance();
this.mObjToDigest.put(objNumInt, this.mNullDigest);
}
if (resource instanceof CosNumeric) {
this.updateNumber(resource.numberValue());
} else if (resource instanceof CosName) {
this.updateName(resource.nameValue());
} else if (resource instanceof CosBoolean) {
this.updateBoolean(resource.booleanValue());
} else if (resource instanceof CosString) {
this.updateString(resource.stringValue());
} else if (resource instanceof CosDictionary) {
if (resource instanceof CosStream) {
this.updateByte(8);
} else {
this.updateByte(6);
}
this.updateInteger(((CosDictionary)resource).size());
ArrayList<ASName> keys = new ArrayList<ASName>(((CosDictionary)resource).keySet());
Collections.sort(keys);
for (ASName name : keys) {
this.updateName(name);
CosObject value = ((CosDictionary)resource).get(name);
CosObject prevValue = this.freeDupResObj(value);
if (prevValue == null) continue;
((CosDictionary)resource).put(name, prevValue);
}
if (resource instanceof CosStream) {
int type = 0;
long length = 0;
InputByteStream stream = ((CosStream)resource).getStreamEncoded();
if (stream != null && stream.bytesAvailable() != 0) {
type = 1;
length = stream.bytesAvailable();
} else {
stream = ((CosStream)resource).getStreamDecoded();
if (stream != null && stream.bytesAvailable() != 0) {
type = 2;
length = stream.bytesAvailable();
}
}
this.updateInteger(type);
this.updateInteger((int)length);
byte[] buffer = new byte[4096];
while (length > 0) {
int bytesRead = stream.read(buffer, 0, buffer.length);
this.mDigester.update(buffer, 0, bytesRead);
length -= (long)bytesRead;
}
}
} else if (resource instanceof CosArray) {
this.updateByte(7);
int size = ((CosArray)resource).size();
this.updateInteger(size);
for (int i = 0; i < size; ++i) {
CosObject value = ((CosArray)resource).get(i);
CosObject prevValue = this.freeDupResObj(value);
if (prevValue == null) continue;
((CosArray)resource).set(i, prevValue);
}
}
if (objNum != 0) {
byte[] localDigest = this.mDigester.digest();
this.mDigester = savedDigester;
this.mDigester.update(localDigest);
Digest digestObj = new Digest(localDigest);
if (this.mDigestToObj.containsKey(digestObj)) {
return (CosObject)this.mDigestToObj.get(digestObj);
}
this.mDigestToObj.put(digestObj, resource);
this.mObjToDigest.put(objNumInt, digestObj);
}
return null;
}
private final void updateNumber(Number num) {
if (num instanceof Integer) {
this.updateByte(1);
this.updateInteger(num.intValue());
} else {
this.updateByte(2);
long bits = Double.doubleToLongBits(num.doubleValue());
this.updateInteger((int)(bits >> 32));
this.updateInteger((int)bits);
}
}
private final void updateBoolean(boolean bool) {
this.updateByte(3);
if (bool) {
this.updateByte(1);
} else {
this.updateByte(0);
}
}
private final void updateName(ASName name) {
this.updateByte(4);
byte[] nameContent = name.getBytes();
this.updateInteger(nameContent.length);
this.updateByteArray(nameContent);
}
private final void updateString(ASString string) {
this.updateByte(5);
byte[] stringBytes = string.getBytes();
this.updateInteger(stringBytes.length);
this.updateByteArray(stringBytes);
}
private final void updateByte(byte b) {
this.mDigester.update(b);
}
private final void updateByteArray(byte[] data) {
this.mDigester.update(data);
}
private final void updateInteger(int num) {
byte[] data = new byte[]{(byte)(num >> 24), (byte)(num >> 16), (byte)(num >> 8), (byte)num};
this.updateByteArray(data);
}
private static final MessageDigest newDigesterInstance() {
try {
return MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e) {
return null;
}
}
private static final CosDictionary safeGetDict(CosDictionary dict, ASName key) throws PDFCosParseException, PDFIOException, PDFSecurityException {
CosObject obj = dict.get(key);
if (obj instanceof CosDictionary) {
return (CosDictionary)obj;
}
return null;
}
private static final CosArray safeGetArray(CosDictionary dict, ASName key) throws PDFCosParseException, PDFIOException, PDFSecurityException {
CosObject obj = dict.get(key);
if (obj instanceof CosArray) {
return (CosArray)obj;
}
return null;
}
private static final class Digest {
byte[] mDigest;
Digest(byte[] digest) {
this.mDigest = digest;
}
byte[] getBytes() {
return this.mDigest;
}
public boolean equals(Object digest) {
if (!(digest instanceof Digest)) {
return false;
}
byte[] digBytes = ((Digest)digest).getBytes();
if (this.mDigest == null && digBytes == null) {
return true;
}
if (this.mDigest == null || digBytes == null) {
return false;
}
int length = this.mDigest.length;
if (digBytes.length != length) {
return false;
}
for (int i = 0; i < length; ++i) {
if (digBytes[i] == this.mDigest[i]) continue;
return false;
}
return true;
}
public int hashCode() {
if (this.mDigest == null) {
return 0;
}
int hash = 0;
int len = this.mDigest.length;
for (int i = 0; i < len; ++i) {
hash ^= this.mDigest[i] * -1640531527;
}
return hash;
}
}
}