ContentParser.java
26.1 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.stream.InputByteStream
* com.adobe.internal.pdftoolkit.core.cos.CosArray
* com.adobe.internal.pdftoolkit.core.cos.CosDictionary
* com.adobe.internal.pdftoolkit.core.cos.CosDocument
* com.adobe.internal.pdftoolkit.core.cos.CosName
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidContentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFUnableToCompleteOperationException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFUnsupportedFeatureException
* com.adobe.internal.pdftoolkit.core.filter.CCITTFaxInputStream
* com.adobe.internal.pdftoolkit.core.filter.DCTInputStream
* com.adobe.internal.pdftoolkit.core.filter.DecodeInputStream
* com.adobe.internal.pdftoolkit.core.filter.FilterParams
* com.adobe.internal.pdftoolkit.core.filter.FilterStream
* com.adobe.internal.pdftoolkit.core.filter.LZWInputStream
* com.adobe.internal.pdftoolkit.core.filter.RunLengthInputStream
* com.adobe.internal.pdftoolkit.core.filter.TIFFCountingStreamWrapper
* com.adobe.internal.pdftoolkit.core.types.ASArray
* com.adobe.internal.pdftoolkit.core.types.ASBoolean
* com.adobe.internal.pdftoolkit.core.types.ASDictionary
* com.adobe.internal.pdftoolkit.core.types.ASName
* com.adobe.internal.pdftoolkit.core.types.ASNumber
* com.adobe.internal.pdftoolkit.core.types.ASObject
* com.adobe.internal.pdftoolkit.core.util.ByteOps
*/
package com.adobe.internal.pdftoolkit.pdf.content;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
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.CosObject;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidContentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFUnableToCompleteOperationException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFUnsupportedFeatureException;
import com.adobe.internal.pdftoolkit.core.filter.CCITTFaxInputStream;
import com.adobe.internal.pdftoolkit.core.filter.DCTInputStream;
import com.adobe.internal.pdftoolkit.core.filter.DecodeInputStream;
import com.adobe.internal.pdftoolkit.core.filter.FilterParams;
import com.adobe.internal.pdftoolkit.core.filter.FilterStream;
import com.adobe.internal.pdftoolkit.core.filter.LZWInputStream;
import com.adobe.internal.pdftoolkit.core.filter.RunLengthInputStream;
import com.adobe.internal.pdftoolkit.core.filter.TIFFCountingStreamWrapper;
import com.adobe.internal.pdftoolkit.core.types.ASArray;
import com.adobe.internal.pdftoolkit.core.types.ASBoolean;
import com.adobe.internal.pdftoolkit.core.types.ASDictionary;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASNumber;
import com.adobe.internal.pdftoolkit.core.types.ASObject;
import com.adobe.internal.pdftoolkit.core.util.ByteOps;
import com.adobe.internal.pdftoolkit.pdf.content.Instruction;
import com.adobe.internal.pdftoolkit.pdf.content.OperandStack;
import com.adobe.internal.pdftoolkit.pdf.content.Token;
import com.adobe.internal.pdftoolkit.pdf.content.TokenType;
import com.adobe.internal.pdftoolkit.pdf.content.Tokenizer;
import com.adobe.internal.pdftoolkit.pdf.document.PDFContents;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
class ContentParser {
private OperandStack operandStack;
private InputByteStream contentStream;
private CosDictionary colorSpaceMap;
private CosDocument cosDoc;
private Tokenizer tokenizer;
ContentParser(PDFContents content, CosDictionary csMap) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this(content, csMap, null);
}
ContentParser(PDFContents content, CosDictionary csMap, HashSet<ASName> operators) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.contentStream = content.getContents();
this.operandStack = new OperandStack();
this.colorSpaceMap = csMap;
this.cosDoc = content.getPDFDocument().getCosDocument();
this.tokenizer = new Tokenizer(this.contentStream, operators);
}
private ContentParser(InputByteStream contentStream, CosDictionary csMap, CosDocument cosDoc) {
this.operandStack = new OperandStack();
this.contentStream = contentStream;
this.colorSpaceMap = csMap;
this.cosDoc = cosDoc;
this.tokenizer = new Tokenizer(contentStream, null);
}
ContentParser slice() throws PDFIOException {
InputByteStream slicedStream;
try {
long sliceStart = this.contentStream.getPosition();
slicedStream = this.contentStream.slice(sliceStart, this.contentStream.length() - sliceStart);
}
catch (IOException e) {
throw new PDFIOException("Could not slice ContentParser", (Throwable)e);
}
return new ContentParser(slicedStream, this.colorSpaceMap, this.cosDoc);
}
void close() throws IOException {
this.contentStream.close();
}
Instruction nextInstruction() throws PDFParseException, PDFIOException, PDFSecurityException, PDFInvalidDocumentException {
do {
Token result;
if ((result = this.tokenizer.nextToken()) == null) {
return null;
}
if (result.getType() == TokenType.Operator) {
if (result.getValue() == ASName.k_ID) {
InputByteStream imageData;
ASDictionary imageDict = this.operandStack.stackToImageDictionary();
try {
try {
imageData = this.readInlineImage(imageDict);
}
catch (PDFInvalidParameterException e) {
throw new PDFInvalidContentException((Throwable)e);
}
catch (PDFIOException e) {
throw new PDFInvalidContentException((Throwable)e);
}
}
catch (PDFUnableToCompleteOperationException e) {
throw new PDFInvalidContentException((Throwable)e);
}
this.operandStack.pushInputByteStream(imageData);
}
Instruction instruction = new Instruction(this.operandStack, (ASName)result.getValue());
this.operandStack = new OperandStack();
return instruction;
}
if (result.getType() != TokenType.Operand) break;
this.operandStack.push(result.getValue());
} while (true);
throw new PDFInvalidContentException("unrecognized token");
}
private DecodeInputStream createDecodeInputStream(InputStream is, ASName filter, FilterParams filterParams) {
if (filter == ASName.k_LZW || filter == ASName.k_LZWDecode) {
return new LZWInputStream(is, filterParams);
}
if (filter == ASName.k_RL || filter == ASName.k_RunLengthDecode) {
return new RunLengthInputStream(is, filterParams);
}
if (filter == ASName.k_CCF || filter == ASName.k_CCITTFaxDecode) {
return new CCITTFaxInputStream(is, filterParams);
}
if (filter == ASName.k_DCT || filter == ASName.k_DCTDecode) {
return new DCTInputStream(is, filterParams);
}
return null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Unable to fully structure code
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
* Lifted jumps to return sites
*/
private InputByteStream readInlineImage(ASDictionary imageDict) throws PDFIOException, PDFParseException, PDFSecurityException, PDFUnableToCompleteOperationException, PDFInvalidParameterException, PDFInvalidDocumentException {
block82 : {
block79 : {
try {
bpc = 0;
cpp = 0;
width = 0;
height = 0;
imageSize = 0;
isImageMask = false;
imageByteStream = null;
if (imageDict.containsKey((Object)ASName.k_IM)) {
isImageMask = imageDict.getBoolean(ASName.k_IM).isTrue();
} else if (imageDict.containsKey((Object)ASName.k_ImageMask)) {
isImageMask = imageDict.getBoolean(ASName.k_ImageMask).isTrue();
}
if (isImageMask) {
bpc = 1;
cpp = 1;
} else {
if (imageDict.containsKey((Object)ASName.k_BPC)) {
bpc = imageDict.getNumber(ASName.k_BPC).intValue();
} else {
if (imageDict.containsKey((Object)ASName.k_BitsPerComponent) == false) throw new PDFInvalidContentException("Image dictionary did not contain required BPC entry");
bpc = imageDict.getNumber(ASName.k_BitsPerComponent).intValue();
}
if (imageDict.containsKey((Object)ASName.k_CS)) {
cs = imageDict.get((Object)ASName.k_CS);
} else {
if (imageDict.containsKey((Object)ASName.k_ColorSpace) == false) throw new PDFInvalidContentException("Image dictionary did not contain required CS entry");
cs = imageDict.get((Object)ASName.k_ColorSpace);
}
if (cs instanceof ASName) {
if (cs == ASName.k_G || cs == ASName.k_DeviceGray) {
cpp = 1;
} else if (cs == ASName.k_RGB || cs == ASName.k_DeviceRGB) {
cpp = 3;
} else if (cs == ASName.k_CMYK || cs == ASName.k_DeviceCMYK) {
cpp = 4;
} else {
if (this.colorSpaceMap == null) throw new PDFInvalidContentException("Image dictionary contained unknown CS type");
if (this.colorSpaceMap.get((ASName)cs) == null) throw new PDFInvalidContentException("Image dictionary contained unknown CS type");
resourceCS = this.colorSpaceMap.get((ASName)cs);
if (resourceCS.getType() == 5) {
cosCSArray = (CosArray)resourceCS;
cpp = this.countCSComponentsForInlineImages(cosCSArray);
} else {
if (resourceCS.getType() != 3) throw new PDFInvalidContentException("Image dictionary contained unknown CS type");
csName = ((CosName)resourceCS).nameValue();
cpp = this.getCSComponentsForInlineImages(csName);
}
}
} else {
if (cs instanceof ASArray == false) throw new PDFInvalidContentException("Image dictionary contained unknown CS type");
cpp = this.countCSComponentsForInlineImages((ASArray)cs);
}
}
if (imageDict.containsKey((Object)ASName.k_W)) {
width = imageDict.getNumber(ASName.k_W).longValue();
} else {
if (imageDict.containsKey((Object)ASName.k_Width) == false) throw new PDFInvalidContentException("Image dictionary missing required width");
width = imageDict.getNumber(ASName.k_Width).longValue();
}
if (imageDict.containsKey((Object)ASName.k_H)) {
height = imageDict.getNumber(ASName.k_H).longValue();
} else {
if (imageDict.containsKey((Object)ASName.k_Height) == false) throw new PDFInvalidContentException("Image dictionary missing required height");
height = imageDict.getNumber(ASName.k_Height).longValue();
}
if (bpc != 1 && bpc != 2 && bpc != 4 && bpc != 8) {
throw new PDFInvalidContentException("Invalid image dictionary");
}
if (cpp != 1 && cpp != 3 && cpp != 4) {
throw new PDFInvalidContentException("Invalid image dictionary");
}
if (width * height == 0) {
throw new PDFInvalidContentException("Invalid image dictionary");
}
filters = null;
filterDictEntry = null;
if (imageDict.containsKey((Object)ASName.k_F)) {
filterDictEntry = imageDict.get((Object)ASName.k_F);
if (filterDictEntry instanceof ASArray) {
filters = (ASArray)filterDictEntry;
} else {
if (filterDictEntry instanceof ASName == false) throw new PDFUnsupportedFeatureException("Image dictionary contains unsupported filter - " + (Object)filterDictEntry);
filters = new ASArray();
filters.add(filterDictEntry);
}
} else if (imageDict.containsKey((Object)ASName.k_Filter)) {
filterDictEntry = imageDict.get((Object)ASName.k_Filter);
if (filterDictEntry instanceof ASArray) {
filters = (ASArray)filterDictEntry;
} else {
if (filterDictEntry instanceof ASName == false) throw new PDFUnsupportedFeatureException("Image dictionary contains unsupported filter - " + (Object)filterDictEntry);
filters = new ASArray();
filters.add(filterDictEntry);
}
}
expectSpaceAfterID = true;
if (filters == null) ** GOTO lbl-1000
if (filters.contains((Object)ASName.k_ASCIIHexDecode) || filters.contains((Object)ASName.k_ASCII85Decode)) {
expectSpaceAfterID = false;
}
if (expectSpaceAfterID) lbl-1000: // 2 sources:
{
if (!ByteOps.isWhitespace((byte)(b = (byte)this.contentStream.read()))) {
this.contentStream.unget();
}
}
if (filters != null && filters.size() != 0) {
params = imageDict.get((Object)ASName.k_DecodeParms);
if (params == null) {
params = imageDict.get((Object)ASName.k_DP);
}
paramsDict = null;
if (params instanceof ASDictionary) {
paramsDict = (ASDictionary)params;
} else if (params instanceof ASArray && (paramsArray = (ASArray)params).size() > 0 && (paramsObj = paramsArray.get(0)) instanceof ASDictionary) {
paramsDict = (ASDictionary)paramsObj;
}
startPos = this.contentStream.getPosition();
eod = false;
filter = filters.getName(0);
if (filter == ASName.k_AHx || filter == ASName.k_ASCIIHexDecode) {
do {
if (this.contentStream.read() == 62) {
return this.contentStream.slice(startPos, imageSize + 1);
}
++imageSize;
} while (true);
}
if (filter == ASName.k_A85 || filter == ASName.k_ASCII85Decode) {
b1 = (byte)this.contentStream.read();
b2 = (byte)this.contentStream.read();
break block79;
}
if (filter == ASName.k_Fl || filter == ASName.k_FlateDecode) {
wrapper = null;
contentStreamSliced = null;
try {
endPos = this.contentStream.length();
contentStreamSliced = this.contentStream.slice(startPos, endPos - startPos);
wrapper = new TIFFCountingStreamWrapper(contentStreamSliced.toInputStream(), paramsDict);
imageByteStream = wrapper.getData();
imageSize = wrapper.getTotalIn();
this.contentStream.seek(startPos + imageSize);
imageByteStream = this.contentStream.slice(startPos, imageSize + 1);
return imageByteStream;
}
finally {
try {
if (wrapper != null) {
wrapper.close();
}
}
finally {
if (contentStreamSliced != null) {
contentStreamSliced.close();
}
}
}
}
if (filter != ASName.k_LZW && filter != ASName.k_LZWDecode && filter != ASName.k_RL && filter != ASName.k_RunLengthDecode && filter != ASName.k_CCF && filter != ASName.k_CCITTFaxDecode && filter != ASName.k_DCT) {
if (filter != ASName.k_DCTDecode) throw new PDFUnsupportedFeatureException("Image dictionary contains unsupported filter - " + (Object)filter);
}
is = null;
dis = null;
try {
filterParams = null;
if (paramsDict != null) {
filterParams = FilterStream.buildFilterParams((ASObject)paramsDict);
}
if (filterParams == null) {
filterParams = new FilterParams();
}
length = this.contentStream.length() - startPos;
imageByteStream = this.contentStream.slice(startPos, length);
try {
is = imageByteStream.toInputStream();
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
finally {
if (imageByteStream != null) {
imageByteStream.close();
}
}
dis = this.createDecodeInputStream(is, filter, filterParams);
buffer = new byte[4096];
read = 0;
while (read > -1) {
read = dis.read(buffer);
}
imageSize = dis.getInPos();
this.contentStream.seek(startPos + imageSize);
imageByteStream = this.contentStream.slice(startPos, imageSize + 1);
return imageByteStream;
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
finally {
if (dis != null) {
dis.close();
}
if (is != null) {
is.close();
}
}
}
rowBytes = ((long)(bpc * cpp) * width + 7) / 8;
imageSize = height * rowBytes;
if (imageSize > this.contentStream.bytesAvailable()) throw new PDFInvalidContentException("Unable to parse image at" + this.contentStream.getPosition());
startPos = this.contentStream.getPosition();
this.contentStream.seek(this.contentStream.getPosition() + imageSize);
b1 = (byte)this.contentStream.read();
extraJunkLength = 0;
if (ByteOps.isWhitespace((byte)b1) != false) return this.contentStream.slice(startPos, imageSize + extraJunkLength);
gotEI = false;
b2 = (byte)this.contentStream.read();
break block82;
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
}
do {
if (b1 == 126 && b2 == 62) {
eod = true;
continue;
}
++imageSize;
b1 = b2;
if (this.contentStream.eof()) return this.contentStream.slice(startPos, imageSize + 2);
b2 = (byte)this.contentStream.read();
} while (!eod);
return this.contentStream.slice(startPos, imageSize + 2);
}
do {
if (b1 == 69 && b2 == 73) {
gotEI = true;
this.contentStream.unget();
this.contentStream.unget();
continue;
}
b1 = b2;
++extraJunkLength;
b2 = (byte)this.contentStream.read();
} while (!gotEI);
return this.contentStream.slice(startPos, imageSize + extraJunkLength);
}
private int getCSComponentsForInlineImages(ASName cs) throws PDFInvalidContentException {
if (cs == ASName.k_G || cs == ASName.k_DeviceGray) {
return 1;
}
if (cs == ASName.k_RGB || cs == ASName.k_DeviceRGB) {
return 3;
}
if (cs == ASName.k_CMYK || cs == ASName.k_DeviceCMYK) {
return 4;
}
throw new PDFInvalidContentException("CS array contained unknown CS type");
}
private int countCSComponentsForInlineImages(ASArray array) throws PDFInvalidContentException, PDFUnableToCompleteOperationException {
ASName name = array.getName(0);
if (name == ASName.k_DeviceGray || name == ASName.k_G || name == ASName.k_CalGray || name == ASName.k_Separation || name == ASName.k_Indexed || name == ASName.k_I) {
return 1;
}
if (name == ASName.k_DeviceRGB || name == ASName.k_RGB || name == ASName.k_CalRGB || name == ASName.k_Lab) {
return 3;
}
if (name == ASName.k_DeviceCMYK || name == ASName.k_CMYK) {
return 4;
}
if (name == ASName.k_ICCBased) {
ASDictionary iccDict = array.getDictionary(1);
return iccDict.getNumber(ASName.k_N).intValue();
}
if (name == ASName.k_DeviceN) {
ASArray names = array.getArray(1);
return names.size();
}
throw new PDFInvalidContentException("CS array contained unknown CS type");
}
private int countCSComponentsForInlineImages(CosArray cosCSArray) throws PDFInvalidContentException, PDFParseException, PDFSecurityException, PDFIOException {
try {
ASName name = cosCSArray.getName(0);
if (name == ASName.k_DeviceGray || name == ASName.k_G || name == ASName.k_CalGray || name == ASName.k_Separation || name == ASName.k_Indexed || name == ASName.k_I) {
return 1;
}
if (name == ASName.k_DeviceRGB || name == ASName.k_RGB || name == ASName.k_CalRGB || name == ASName.k_Lab) {
return 3;
}
if (name == ASName.k_DeviceCMYK || name == ASName.k_CMYK) {
return 4;
}
if (name == ASName.k_ICCBased) {
CosDictionary iccDict = cosCSArray.getCosDictionary(1);
return iccDict.getInt(ASName.k_N);
}
if (name == ASName.k_DeviceN) {
CosArray names = cosCSArray.getCosArray(1);
return names.size();
}
throw new PDFInvalidContentException("CS array contained unknown CS type");
}
catch (PDFCosParseException e) {
throw new PDFParseException((Throwable)e);
}
}
}