XRefTable.java
44.9 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
/*
* 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.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosEncryption;
import com.adobe.internal.pdftoolkit.core.cos.CosLinearization;
import com.adobe.internal.pdftoolkit.core.cos.CosList;
import com.adobe.internal.pdftoolkit.core.cos.CosName;
import com.adobe.internal.pdftoolkit.core.cos.CosNumeric;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectID;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectInfo;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectRef;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectStream;
import com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions;
import com.adobe.internal.pdftoolkit.core.cos.CosParseBuf;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.cos.CosToken;
import com.adobe.internal.pdftoolkit.core.cos.REPAIRTYPE;
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.ASNumber;
import com.adobe.internal.pdftoolkit.core.types.ASObject;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.core.util.ByteOps;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Iterator;
class XRefTable {
static final int tTable = 0;
static final int tStream = 1;
static final int tHybrid = 2;
private static final int mMaxChunk = 262144;
private static final int EOF_CHECK_THRESHOLD = 1024;
private int mXrefType;
private InputByteStream mFileBuf;
private InputByteStream mBuf;
private CosDocument mDoc;
private ArrayList mXRefSubSections;
private CosDictionary mTrailer;
private ArrayList mTrailerList;
private long[] mRevisions;
private boolean mNoPreload;
private boolean mIsFDF;
private int highestObjectNumInXRefEntries = 0;
private TableXRefSubSection mainXrefSubSection = null;
private boolean isXrefIntialized;
XRefTable(CosDocument doc, InputByteStream buf, boolean noPreload, boolean isFDF) {
this.mFileBuf = buf;
this.mBuf = buf;
this.mDoc = doc;
this.mNoPreload = noPreload;
this.mIsFDF = isFDF;
}
void loadMainInfos() throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
if (this.mIsFDF) {
this.rebuild();
} else {
this.mXRefSubSections = new ArrayList();
this.mTrailerList = new ArrayList();
ArrayList eofList = new ArrayList();
long nextXRefOffset = this.getLastXRefSectionPosition();
this.mBuf.seek(nextXRefOffset);
byte b = CosToken.skipWhitespace(this.mBuf);
this.mBuf.unget();
if (nextXRefOffset == 0) {
throw new PDFCosParseException("could not find xref section");
}
if (b == 120) {
this.mXrefType = 0;
this.parseTableXrefChain(nextXRefOffset, eofList);
} else if (ByteOps.isDigit(b)) {
this.mXrefType = 1;
this.parseStreamXrefChain(nextXRefOffset, eofList);
} else {
throw new PDFCosParseException("could not find xref section");
}
this.setRevisions(eofList);
if (this.mDoc.getRepairTypes().contains((Object)REPAIRTYPE.xrefRepair) && this.mainXrefSubSection != null) {
this.mainXrefSubSection.mEnd -= this.mainXrefSubSection.mBegin;
this.mainXrefSubSection.mBegin = 0;
}
if (!this.mTrailer.containsKey(ASName.k_Size)) {
XRefSubSection lastSection = (XRefSubSection)this.mXRefSubSections.get(0);
this.mTrailer.put(ASName.k_Size, lastSection.mEnd);
}
}
this.isXrefIntialized = true;
}
void rebuild() throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
this.mXRefSubSections = new ArrayList();
this.mTrailerList = new ArrayList();
this.mRevisions = null;
ArrayList<CosObject> tempTrailerList = new ArrayList<CosObject>();
ArrayList<Long> tempEOFList = new ArrayList<Long>();
this.mBuf.seek(0);
boolean first = true;
boolean isLinearized = false;
int catalogObjNum = 0;
do {
CosToken.skipWhitespace(this.mBuf);
this.mBuf.unget();
long objPos = this.mBuf.getPosition();
ASObject asObj = CosToken.readPrimitive(this.mBuf);
if (asObj == null) break;
if (asObj instanceof ASNumber) {
CosObject cosType;
CosObject cosObj = this.scanIndirectObj((ASNumber)asObj, objPos);
if (first && cosObj != null) {
first = false;
if (cosObj instanceof CosDictionary) {
isLinearized = ((CosDictionary)cosObj).containsKey(ASName.k_Linearized);
}
}
if (!(cosObj instanceof CosDictionary) || !((cosType = ((CosDictionary)cosObj).get(ASName.k_Type)) instanceof CosName) || ((CosName)cosType).nameValue() != ASName.k_Catalog) continue;
catalogObjNum = ((ASNumber)asObj).intValue();
continue;
}
if (!(asObj instanceof ASString)) continue;
String str = asObj.toString();
if ("startxref".equals(str)) {
long nextEOF = 0;
try {
nextEOF = this.findNextEOF();
this.mBuf.seek(nextEOF);
}
catch (PDFCosParseException e) {
nextEOF = this.mBuf.getPosition();
}
tempEOFList.add(nextEOF);
continue;
}
if (!"trailer".equals(str)) continue;
long savePos = this.mBuf.getPosition();
try {
CosObject trailerDict = CosToken.readObject(this.mDoc, this.mBuf, null);
if (!(trailerDict instanceof CosDictionary)) {
throw new PDFCosParseException("Invalid trailer dictionary");
}
tempTrailerList.add(trailerDict);
}
catch (PDFCosParseException e) {
this.mBuf.seek(savePos);
}
} while (true);
if (tempTrailerList.isEmpty() && catalogObjNum > 0) {
CosDictionary trailer = this.mDoc.createDirectCosDictionary();
trailer.put(ASName.k_Root, new CosObjectRef(this.mDoc, this.mDoc.getIndexedInfo(catalogObjNum)));
trailer.put(ASName.k_Size, this.mDoc.createCosNumeric(this.mDoc.getNumObjectsInternal()));
trailer.put(ASName.k_ID, this.mDoc.createUpdateDocID(true));
tempTrailerList.add(trailer);
}
while (!tempTrailerList.isEmpty()) {
this.mTrailerList.add(tempTrailerList.remove(tempTrailerList.size() - 1));
}
if (isLinearized && this.mTrailerList.size() > 1) {
Object x = this.mTrailerList.get(this.mTrailerList.size() - 1);
this.mTrailerList.set(this.mTrailerList.size() - 1, this.mTrailerList.get(this.mTrailerList.size() - 2));
this.mTrailerList.set(this.mTrailerList.size() - 2, x);
}
if (!this.mTrailerList.isEmpty()) {
this.mTrailer = (CosDictionary)this.mTrailerList.get(0);
if (catalogObjNum > 0) {
this.mTrailer.put(ASName.k_Root, new CosObjectRef(this.mDoc, this.mDoc.getIndexedInfo(catalogObjNum)));
}
this.mTrailer.put(ASName.k_Size, this.mDoc.getNumObjectsInternal());
}
ArrayList eofList = new ArrayList();
while (!tempEOFList.isEmpty()) {
eofList.add(tempEOFList.remove(tempEOFList.size() - 1));
}
if (isLinearized && eofList.size() > 1) {
Object x = eofList.get(eofList.size() - 1);
eofList.set(eofList.size() - 1, eofList.get(eofList.size() - 2));
eofList.set(eofList.size() - 2, x);
}
if (this.mTrailer == null && !this.mIsFDF) {
throw new PDFCosParseException("Rebuilt document still has no trailer");
}
this.setRevisions(eofList);
}
CosObject scanIndirectObj(ASNumber asObjNum, long objPos) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
long savePos = this.mBuf.getPosition();
ASObject asObjGen = CosToken.readPrimitive(this.mBuf);
ASObject asObjTag = CosToken.readPrimitive(this.mBuf);
if (asObjGen instanceof ASNumber && asObjTag instanceof ASString) {
Number nmObjNum = asObjNum.numberValue();
Number nmObjGen = ((ASNumber)asObjGen).numberValue();
String objTag = ((ASString)asObjTag).toString();
if ((nmObjNum instanceof Integer || nmObjNum instanceof Long) && (nmObjGen instanceof Integer || nmObjGen instanceof Long)) {
int objNum = nmObjNum.intValue();
int objGen = nmObjGen.intValue();
if (objNum > 0 && objGen >= 0 && objGen < 65535 && "obj".equals(objTag)) {
CosObject cosObj = null;
try {
cosObj = CosToken.readIndirectObject(this.mDoc, this.mBuf, null);
}
catch (PDFCosParseException e) {
// empty catch block
}
if (cosObj != null) {
CosObjectInfo info = new CosObjectInfo(this.mDoc, objNum, objGen);
info.setPosInternal(objPos);
info.markAddressed();
this.mDoc.putRebuiltInfo(objNum, info);
if (cosObj instanceof CosStream) {
CosObject lenObj = ((CosStream)cosObj).get(ASName.k_Length);
if (lenObj instanceof CosNumeric) {
int stmLen = ((CosNumeric)lenObj).intValue();
long saveStmPos = this.mBuf.getPosition();
this.mBuf.seek(saveStmPos + (long)stmLen);
ASObject asTokenObj = CosToken.readPrimitive(this.mBuf);
if (asTokenObj instanceof ASString && asTokenObj.toString().equals("endstream")) {
return cosObj;
}
this.mBuf.seek(saveStmPos);
do {
CosToken.skipWhitespace(this.mBuf);
this.mBuf.unget();
asTokenObj = CosToken.readPrimitive(this.mBuf);
if (asTokenObj != null) continue;
return cosObj;
} while (!(asTokenObj instanceof ASString) || !asTokenObj.toString().equals("endstream"));
return cosObj;
}
} else {
this.mBuf.unget();
}
return cosObj;
}
}
}
}
this.mBuf.seek(savePos);
return null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Loose catch block
* Enabled aggressive exception aggregation
*/
void rebuildLate() throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
this.mBuf.seek(0);
do {
Object var8_5;
CosToken.skipWhitespace(this.mBuf);
this.mBuf.unget();
long objPos = this.mBuf.getPosition();
ASObject asObj = CosToken.readPrimitive(this.mBuf);
if (asObj == null) break;
if (!(asObj instanceof ASNumber)) continue;
long savePos = this.mBuf.getPosition();
try {
this.scanIndirectObj((ASNumber)asObj, objPos);
var8_5 = null;
}
catch (Throwable var7_6) {
var8_5 = null;
if (this.mBuf.getPosition() < objPos) {
this.mBuf.seek(savePos);
}
throw var7_6;
}
if (this.mBuf.getPosition() >= objPos) continue;
this.mBuf.seek(savePos);
{
continue;
catch (PDFCosParseException e) {
if (!this.mDoc.getOptions().skipCorruptObjects()) {
throw e;
}
var8_5 = null;
if (this.mBuf.getPosition() >= objPos) continue;
this.mBuf.seek(savePos);
continue;
}
}
} while (true);
}
XRefTable(CosDocument doc) throws PDFCosParseException, PDFIOException, PDFSecurityException {
this.mDoc = doc;
this.mXRefSubSections = new ArrayList();
this.mTrailer = this.mDoc.createCosDictionary(0);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
void close() throws IOException {
try {
this.closeStreams();
Object var2_1 = null;
}
catch (Throwable var1_3) {
Object var2_2 = null;
this.closeXRefSubSections();
throw var1_3;
}
this.closeXRefSubSections();
{
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Enabled force condition propagation
* Lifted jumps to return sites
*/
private void closeStreams() throws IOException {
try {
if (this.mBuf != null && this.mBuf != this.mFileBuf) {
this.mBuf.close();
this.mBuf = null;
}
Object var2_1 = null;
if (this.mFileBuf == null) return;
}
catch (Throwable var1_3) {
Object var2_2 = null;
if (this.mFileBuf == null) throw var1_3;
this.mFileBuf.close();
this.mFileBuf = null;
throw var1_3;
}
this.mFileBuf.close();
this.mFileBuf = null;
}
private void closeXRefSubSections() throws IOException {
IOException ioEx = null;
for (XRefSubSection subSection : this.mXRefSubSections) {
try {
if (subSection == null) continue;
subSection.close();
}
catch (IOException e) {
ioEx = e;
}
}
if (ioEx != null) {
throw ioEx;
}
this.mXRefSubSections.clear();
}
private long find(InputByteStream byteStream, char[] key) throws IOException {
long location = -1;
while (byteStream.bytesAvailable() > 0) {
char c = (char)byteStream.read();
if (key[0] != c) continue;
location = byteStream.getPosition() - 1;
for (int i = 1; i < key.length; ++i) {
c = (char)byteStream.read();
if (key[i] == c) continue;
location = -1;
break;
}
if (location == -1) continue;
return location;
}
return location;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void parseTableXrefChain(long nextXRefOffset, ArrayList eofList) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
XRefSubSection sub;
do {
CosNumeric prev;
this.mBuf.seek(nextXRefOffset);
int nextXrefSubSectionIndex = this.mXRefSubSections.size();
byte b = this.readTableXRefTable();
if (this.mXRefSubSections.size() > nextXrefSubSectionIndex) {
this.mainXrefSubSection = (TableXRefSubSection)this.mXRefSubSections.get(nextXrefSubSectionIndex);
}
CosDictionary trailer = this.readTrailer(b);
this.mTrailerList.add(trailer);
if (this.mTrailer == null) {
this.mTrailer = trailer;
}
if ((prev = (CosNumeric)trailer.get(ASName.k_Prev)) != null) {
long prevValue = prev.longValue();
if (prevValue == nextXRefOffset) {
throw new PDFCosParseException("Fatal XRef chain loop at position " + Long.toString(prevValue));
}
nextXRefOffset = prevValue;
CosNumeric xref = (CosNumeric)trailer.get(ASName.k_XRefStm);
if (xref != null) {
Object var14_11;
long pos = this.mBuf.getPosition();
try {
this.readStreamXRefTable(xref.longValue());
var14_11 = null;
}
catch (Throwable var13_12) {
var14_11 = null;
this.mBuf.seek(pos);
this.mainXrefSubSection = null;
throw var13_12;
}
this.mBuf.seek(pos);
this.mainXrefSubSection = null;
{
}
this.mXrefType = 2;
}
} else {
nextXRefOffset = 0;
}
if (eofList == null || this.mBuf.getPosition() <= nextXRefOffset) continue;
eofList.add(this.findNextEOF());
} while (nextXRefOffset != 0);
int size = this.mXRefSubSections.size();
if (size > 0 && (sub = (XRefSubSection)this.mXRefSubSections.get(this.mXRefSubSections.size() - 1)) instanceof TableXRefSubSection && ((TableXRefSubSection)sub).mBegin != 0) {
throw new PDFCosParseException("Main XRefSubsection should start with object num 0.");
}
}
private byte readTableXRefTable() throws PDFCosParseException, IOException {
try {
byte b = 0;
CosToken.skipWhitespace(this.mBuf);
this.mBuf.unget();
if (!CosToken.readLine(this.mBuf, false).startsWith("xref")) {
throw new PDFCosParseException("Expected 'xref' : " + Long.toString(this.mBuf.getPosition()));
}
b = CosToken.skipWhitespace(this.mBuf);
while (ByteOps.isDigit(b)) {
this.mXRefSubSections.add(new TableXRefSubSection(b));
b = CosToken.skipWhitespace(this.mBuf);
}
return b;
}
catch (IOException e) {
throw new PDFCosParseException(e);
}
}
private void parseStreamXrefChain(long nextXRefOffset, ArrayList eofList) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
do {
CosNumeric prev;
this.mBuf.seek(nextXRefOffset);
CosStream xrefStm = this.readStreamXRefTable(nextXRefOffset);
this.mTrailerList.add(xrefStm);
if (this.mTrailer == null) {
this.mTrailer = xrefStm;
}
if ((prev = (CosNumeric)xrefStm.get(ASName.k_Prev)) != null) {
long prevValue = prev.longValue();
if (prevValue == nextXRefOffset) {
throw new PDFCosParseException("Fatal XRef chain loop at position " + Long.toString(prevValue));
}
nextXRefOffset = prevValue;
} else {
nextXRefOffset = 0;
}
if (eofList == null || this.mBuf.getPosition() <= nextXRefOffset) continue;
eofList.add(this.findNextEOF());
} while (nextXRefOffset != 0);
}
private CosStream readStreamXRefTable(long nextXRefOffset) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
this.mBuf.seek(nextXRefOffset);
CosParseBuf pBuf = new CosParseBuf(this.mBuf, 128);
int[] xStmID = CosToken.readObjID(this.mDoc, pBuf, CosToken.skipWhitespace(pBuf));
CosObjectInfo xStmInfo = this.mDoc.getIndexedInfo(xStmID[0]);
if (xStmInfo != null) {
xStmInfo.setObjNum(xStmID[0]);
xStmInfo.setObjGen(xStmID[1]);
} else {
xStmInfo = this.mDoc.getObjectInfo(xStmID[0], xStmID[1]);
}
CosObject xStmObj = CosToken.readIndirectObject(this.mDoc, pBuf, xStmInfo);
pBuf.close();
xStmInfo.setPosInternal(nextXRefOffset);
xStmInfo.markLoaded();
this.mDoc.putIndexedInfo(xStmID[0], null);
if (!(xStmObj instanceof CosStream)) {
throw new PDFCosParseException("Expected CosStream : " + Long.toString(nextXRefOffset));
}
CosStream xStm = (CosStream)xStmObj;
InputByteStream xStmData = xStm.getStreamDecoded();
CosObject xTypeObj = xStm.get(ASName.k_Type);
if (xTypeObj == null || !(xTypeObj instanceof CosName) || !((CosName)xTypeObj).nameValue().equals(ASName.k_XRef)) {
throw new PDFCosParseException("Expected CosName: " + Long.toString(nextXRefOffset));
}
CosObject indexObj = xStm.get(ASName.k_Index);
CosObject wObj = xStm.get(ASName.k_W);
if (!(wObj instanceof CosArray) || ((CosArray)wObj).size() != 3) {
throw new PDFCosParseException("UExpected CosArray : " + Long.toString(nextXRefOffset));
}
CosArray wArray = (CosArray)wObj;
int[] w = new int[3];
int totalW = 0;
for (int i = 0; i < 3; ++i) {
w[i] = ((CosNumeric)wArray.get(i)).intValue();
totalW += w[i];
}
int[] intArray = null;
if (indexObj == null) {
intArray = new int[]{0, ((CosNumeric)xStm.get(ASName.k_Size)).intValue()};
} else {
if (!(indexObj instanceof CosArray)) {
throw new PDFCosParseException("Expected CosArray : " + Long.toString(nextXRefOffset));
}
CosArray indexArray = (CosArray)indexObj;
intArray = new int[indexArray.size()];
for (int i2 = 0; i2 < indexArray.size(); ++i2) {
intArray[i2] = ((CosNumeric)indexArray.get(i2)).intValue();
}
}
long streamOffset = 0;
for (int i3 = 0; i3 < intArray.length; i3 += 2) {
int first = intArray[i3];
int count = intArray[i3 + 1];
while (count > 0) {
int subCount = count;
if (count * totalW > 262144) {
subCount = 262144 / totalW;
}
this.mXRefSubSections.add(new StreamXRefSubSection(first, subCount, xStm, xStmData, streamOffset, w, totalW));
first += subCount;
count -= subCount;
streamOffset += (long)(subCount * totalW);
}
}
xStmData.close();
return xStm;
}
CosDictionary getTrailer() {
return this.mTrailer;
}
CosDictionary[] getTrailerList() {
if (this.mTrailerList.isEmpty()) {
return null;
}
CosDictionary[] list = new CosDictionary[this.mTrailerList.size()];
for (int i = 0; i < this.mTrailerList.size(); ++i) {
list[i] = (CosDictionary)this.mTrailerList.get(this.mTrailerList.size() - i - 1);
}
return list;
}
/*
* Enabled force condition propagation
* Lifted jumps to return sites
*/
CosObject getIndirectObject(CosObjectInfo info) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
CosObject result = null;
if (info == null) return result;
if (!info.isAddressed()) {
info = this.getInfo(info);
}
if (info == null) {
return result;
}
if (info.isFree()) return result;
if (info.isCompressed()) {
CosObject stmObject;
int objNum = info.getObjNum();
CosObjectInfo stmInfo = info.getStreamInfo();
CosLinearization cosLin = this.mDoc.getLinearization();
if (cosLin != null && cosLin.getOldToOldObjStmMap() != null) {
objNum = cosLin.mapNewToOldObjNum(objNum);
stmObject = cosLin.mapOldToOldObjStm(stmInfo.getObjNum());
} else {
stmObject = this.mDoc.getIndirectObject(stmInfo);
}
if (!(stmObject instanceof CosObjectStream)) throw new PDFCosParseException("Expected cosobject stream.");
InputByteStream stm = ((CosObjectStream)stmObject).getBytesForObject(objNum);
info.setNextObjPos(Long.MAX_VALUE);
result = CosToken.readObject(this.mDoc, stm, info);
} else {
long pos = info.getPos();
if (pos == 0) {
throw new PDFCosParseException("The position 0 for an object is not valid.");
}
if (pos >= this.mFileBuf.length()) {
throw new PDFCosParseException("The position of (obj " + info.getObjNum() + " 0) lies outside the file boundary");
}
this.mFileBuf.seek(pos);
CosParseBuf pBuf = new CosParseBuf(this.mFileBuf, 128);
CosToken.skipObjID(this.mDoc, pBuf, CosToken.skipWhitespace(pBuf));
result = CosToken.readIndirectObject(this.mDoc, pBuf, info);
pBuf.close();
}
info.markLoaded();
return result;
}
CosObjectInfo getInfo(int objNum) throws PDFCosParseException, IOException, PDFSecurityException {
for (XRefSubSection xref : this.mXRefSubSections) {
int begin = xref.getBegin();
if (begin > objNum || objNum >= xref.getEnd() || !xref.getXRefUsed(objNum - begin)) continue;
CosObjectInfo info = new CosObjectInfo(this.mDoc, objNum, xref.getXRefGeneration(objNum - begin));
return xref.getInfo(objNum - begin, info);
}
return null;
}
private CosObjectInfo getInfo(CosObjectInfo info) throws PDFCosParseException, IOException, PDFSecurityException {
int id = info.getObjNum();
int generation = info.getObjGen();
Iterator iter = this.mXRefSubSections.iterator();
CosObjectInfo rslt = null;
while (iter.hasNext() && rslt == null) {
XRefSubSection xref = (XRefSubSection)iter.next();
int begin = xref.getBegin();
if (begin > id || id >= xref.getEnd()) continue;
if (xref.getXRefGeneration(id - begin) == generation && xref.getXRefUsed(id - begin)) {
rslt = xref.getInfo(id - begin, info);
}
return rslt;
}
return rslt;
}
void loadUpdateInfos() throws PDFCosParseException, IOException, PDFSecurityException {
for (XRefSubSection xref : this.mXRefSubSections) {
if (this.mNoPreload && this.mRevisions != null && this.getXRefSectionPos(xref) < this.mRevisions[0]) break;
for (int id = begin = xref.getBegin(); id < xref.getEnd(); ++id) {
CosObjectInfo info;
int gen;
int begin;
if (id == 0 || (info = this.mDoc.getObjectInfo(id, gen = xref.getXRefUsed(id - begin) ? xref.getXRefGeneration(id - begin) : 65535)) != null && (info.isAddressed() || info.isLoaded() || info.isDirty())) continue;
xref.getInfo(id - begin, info);
}
}
}
void resetXRef(InputByteStream stm) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
this.mFileBuf = stm;
this.mBuf = stm;
this.mXRefSubSections.clear();
ArrayList eofList = new ArrayList();
this.mTrailerList = new ArrayList();
long nextXRefOffset = this.getLastXRefSectionPosition();
this.mBuf.seek(nextXRefOffset);
byte b = CosToken.skipWhitespace(this.mBuf);
this.mBuf.unget();
if (b == 120) {
this.mXrefType = 0;
this.parseTableXrefChain(nextXRefOffset, eofList);
} else if (ByteOps.isDigit(b)) {
this.mXrefType = 1;
this.parseStreamXrefChain(nextXRefOffset, eofList);
} else {
throw new PDFCosParseException("could not find xref section");
}
this.setRevisions(eofList);
}
private long findNextEOF() throws PDFCosParseException, IOException {
char[] key_eof = new char[]{'%', '%', 'E', 'O', 'F'};
long eofPos = this.find(this.mBuf, key_eof);
if (eofPos < 0) {
throw new PDFCosParseException("Could not find EOF");
}
int nextByte = 0;
while (!this.mBuf.eof() && nextByte != 10 && nextByte != 13) {
nextByte = (byte)this.mBuf.read();
}
if (this.mBuf.eof()) {
return this.mBuf.length();
}
nextByte = (byte)this.mBuf.read();
if (this.mBuf.eof()) {
return this.mBuf.length();
}
eofPos = this.mBuf.getPosition();
if (nextByte != 10 && nextByte != 13) {
--eofPos;
}
return eofPos;
}
long getObjEOF(CosObjectInfo objInfo) {
if (objInfo == null || this.mRevisions == null) {
return 0;
}
if (objInfo.isCompressed()) {
objInfo = objInfo.getStreamInfo();
}
long objPos = objInfo.getPos();
for (int i = 0; i < this.mRevisions.length; ++i) {
if (objPos >= this.mRevisions[i]) continue;
return this.mRevisions[i];
}
return 0;
}
long getRevisionEOF(int revision) {
if (this.mRevisions == null || revision < 0 || revision >= this.mRevisions.length) {
return 0;
}
return this.mRevisions[revision];
}
int getObjRevision(CosObjectInfo objInfo) {
if (objInfo == null || this.mRevisions == null) {
return -1;
}
if (objInfo.isCompressed()) {
objInfo = objInfo.getStreamInfo();
}
long objPos = objInfo.getPos();
for (int i = 0; i < this.mRevisions.length; ++i) {
if (objPos >= this.mRevisions[i]) continue;
return i;
}
return -1;
}
int getNumRevisions() {
if (this.mRevisions == null) {
return 0;
}
return this.mRevisions.length;
}
void getChangedObjects(long eof, CosList modList) throws PDFCosParseException, IOException, PDFSecurityException {
for (XRefSubSection subSec : this.mXRefSubSections) {
if (this.getXRefSectionPos(subSec) < eof) {
return;
}
int first = subSec.getBegin();
int end = subSec.getEnd();
int subSecSize = end - first;
for (int index = 0; index < subSecSize; ++index) {
int objNum = first + index;
CosObjectID id = (CosObjectID)modList.get(objNum);
if (id != null) continue;
id = subSec.getXRefUsed(index) ? new CosObjectID(objNum, subSec.getXRefGeneration(index)) : new CosObjectID(objNum, -1);
modList.add(objNum, id);
}
}
}
private long getXRefSectionPos(XRefSubSection sec) throws PDFCosParseException, IOException, PDFSecurityException {
if (sec instanceof TableXRefSubSection) {
return ((TableXRefSubSection)sec).mXRefPos;
}
CosStream xrefStm = ((StreamXRefSubSection)sec).mXStm;
CosObjectInfo info = xrefStm.getInfo();
if (!info.isFree() && !info.isCompressed()) {
return info.getPos();
}
return 0;
}
private void setRevisions(ArrayList eofList) {
for (int i = 0; i < eofList.size() - 1; ++i) {
if ((Long)eofList.get(i) > (Long)eofList.get(i + 1)) continue;
eofList.remove(i);
}
if (!eofList.isEmpty()) {
int size = eofList.size();
this.mRevisions = new long[size];
for (int i2 = 0; i2 < size; ++i2) {
this.mRevisions[i2] = (Long)eofList.get(size - i2 - 1);
}
} else {
this.mRevisions = null;
}
}
private CosDictionary readTrailer(byte b) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
CosParseBuf pBuf = new CosParseBuf(this.mBuf, 128);
if (b != 116 || pBuf.read() != 114 || pBuf.read() != 97 || pBuf.read() != 105 || pBuf.read() != 108 || pBuf.read() != 101 || pBuf.read() != 114) {
throw new PDFCosParseException("Expected 'trailer' : " + Long.toString(pBuf.getPosition() - 1));
}
CosObject trailerDict = CosToken.readObject(this.mDoc, pBuf, null);
pBuf.close();
if (!(trailerDict instanceof CosDictionary)) {
throw new PDFCosParseException("Invalid trailer dictionary");
}
return (CosDictionary)trailerDict;
}
long getLastXRefSectionPosition() throws PDFCosParseException, IOException {
byte b;
long pos = this.getEOFPosition();
do {
this.mBuf.seek(--pos);
} while (!ByteOps.isDigit(b = (byte)this.mBuf.read()));
long rslt = 0;
long multiplier = 1;
do {
rslt += (long)((char)b - 48) * multiplier;
multiplier *= 10;
this.mBuf.seek(--pos);
} while (ByteOps.isDigit(b = (byte)this.mBuf.read()));
return rslt;
}
private long getEOFPosition() throws PDFCosParseException, IOException {
long length = this.mBuf.length();
int bufferSize = length >= 1024 ? 1024 : (int)length;
int curPos = 0;
int bufPos = bufferSize;
long garbageLength = 0;
byte[] buffer = new byte[bufferSize];
this.mBuf.seek(length - (long)bufferSize);
this.mBuf.read(buffer, 0, bufferSize);
bufPos = bufferSize;
while (bufPos >= 4) {
curPos = bufPos--;
if (buffer[bufPos] == 70 && buffer[--bufPos] == 79 && buffer[--bufPos] == 69 && buffer[--bufPos] == 37 && buffer[--bufPos] == 37) {
this.mDoc.setGarbageLength(garbageLength);
return length - (long)(bufferSize - bufPos);
}
if (garbageLength == 0 && curPos - bufPos == 1 && ByteOps.isWhitespace(buffer[bufPos])) continue;
garbageLength += (long)(curPos - bufPos);
}
throw new PDFCosParseException("could not find %%EOF");
}
void setupTrailerEncryption() throws PDFCosParseException, PDFIOException, PDFSecurityException {
if (this.mTrailerList != null) {
for (int i = 0; i < this.mTrailerList.size(); ++i) {
CosDictionary trailer = (CosDictionary)this.mTrailerList.get(i);
if (trailer == null) continue;
boolean oldDecrypt = this.mDoc.getEncryption().setEncryptionState(false);
if (trailer.containsKey(ASName.k_ID)) {
CosArray idArray = trailer.getCosArray(ASName.k_ID);
idArray.setEncryptionState(false);
}
if (trailer.containsKey(ASName.k_Encrypt)) {
CosDictionary encryptDict = trailer.getCosDictionary(ASName.k_Encrypt);
encryptDict.setEncryptionState(false);
}
this.mDoc.getEncryption().setEncryptionState(oldDecrypt);
}
}
}
private static long readInt(InputByteStream buf, byte b) throws PDFCosParseException, IOException {
boolean sign = true;
if (b == 43) {
sign = true;
b = (byte)buf.read();
}
if (!ByteOps.isDigit(b)) {
throw new PDFCosParseException("Expected digit : " + Long.toString(buf.getPosition() - 1));
}
long rslt = 0;
while (ByteOps.isDigit(b)) {
rslt *= 10;
rslt += (long)(b - 48);
b = (byte)buf.read();
}
buf.unget();
return rslt *= (long)sign ? 1 : 0;
}
int getType() {
return this.mXrefType;
}
boolean isNew() {
boolean result = false;
if (this.mXRefSubSections.isEmpty()) {
result = true;
}
return result;
}
int getNumObjectsDefinedInXRefEntries() {
return this.highestObjectNumInXRefEntries + 1;
}
boolean isXrefIntialized() {
return this.isXrefIntialized;
}
private class StreamXRefSubSection
extends XRefSubSection {
long mXrefPos;
int[] mW;
int mTotalW;
int mW0plus1;
CosStream mXStm;
byte[] mXRefData;
StreamXRefSubSection(int first, int count, CosStream cosStm, InputByteStream stm, long streamOffset, int[] w, int totalW) throws PDFCosParseException, IOException, PDFSecurityException {
super();
this.mBegin = first;
this.mEnd = first + count;
XRefTable.this.highestObjectNumInXRefEntries = XRefTable.this.highestObjectNumInXRefEntries > this.mEnd ? XRefTable.this.highestObjectNumInXRefEntries : this.mEnd;
this.mXrefPos = streamOffset;
this.mW = w;
this.mTotalW = totalW;
this.mW0plus1 = w[0] + w[1];
this.mXStm = cosStm;
this.mXRefData = new byte[count * totalW];
stm.seek(this.mXrefPos);
stm.read(this.mXRefData, 0, count * totalW);
}
void close() {
this.mXRefData = null;
}
final int getXRefGeneration(int index) throws PDFCosParseException, IOException, PDFSecurityException {
int result = 0;
if (this.mW[2] != 0) {
int entryBase = index * this.mTotalW;
if (this.getXrefEntryType(index) == 1) {
for (int i = entryBase + this.mW0plus1; i < entryBase + this.mTotalW; ++i) {
result <<= 8;
result |= this.mXRefData[i] & 255;
}
}
}
return result;
}
CosObjectInfo getInfo(int index, CosObjectInfo info) throws PDFCosParseException, IOException, PDFSecurityException {
if (info != null && info.isAssigned()) {
int entryBase = index * this.mTotalW;
int type = this.getXrefEntryType(index);
if (type == 0) {
info.markFree();
} else if (type == 1) {
long position = 0;
for (int i = entryBase + this.mW[0]; i < entryBase + this.mW0plus1; ++i) {
position <<= 8;
position |= (long)(this.mXRefData[i] & 255);
}
info.setPos(position);
info.markAddressed();
} else if (type == 2) {
int stmNumber = 0;
for (int i = entryBase + this.mW[0]; i < entryBase + this.mW0plus1; ++i) {
stmNumber <<= 8;
stmNumber |= this.mXRefData[i] & 255;
}
int objNdx = 0;
for (int i2 = entryBase + this.mW0plus1; i2 < entryBase + this.mTotalW; ++i2) {
objNdx <<= 8;
objNdx |= this.mXRefData[i2] & 255;
}
CosObjectInfo stmInfo = XRefTable.this.mDoc.getObjectInfo(stmNumber, 0);
info.setStreamInfo(stmInfo);
info.setStreamNdx(objNdx);
info.markAddressed();
} else {
throw new PDFCosParseException("Undefined XRef stream entry type");
}
}
return info;
}
private int getXrefEntryType(int index) {
if (this.mW[0] == 0) {
return 1;
}
int entryBase = index * this.mTotalW;
int type = 0;
for (int i = entryBase; i < entryBase + this.mW[0]; ++i) {
type <<= 8;
type |= this.mXRefData[i] & 255;
}
return type;
}
final boolean getXRefUsed(int index) throws PDFCosParseException, PDFSecurityException {
return this.getXrefEntryType(index) != 0;
}
}
private class TableXRefSubSection
extends XRefSubSection {
private static final int ENTRY_SIZE = 20;
private static final int ENTRY_POSITION_OFFSET = 0;
private static final int ENTRY_GENERATION_OFFSET = 11;
private static final int ENTRY_IN_USE_OFFSET = 17;
private long mXRefPos;
private long mCurIndex;
private byte[] mCurEntry;
TableXRefSubSection(byte b) throws PDFCosParseException, IOException {
super();
this.mCurIndex = -1;
this.mCurEntry = new byte[20];
this.mBegin = (int)XRefTable.readInt(XRefTable.this.mBuf, b);
b = CosToken.skipWhitespace(XRefTable.this.mBuf);
this.mEnd = this.mBegin + (int)XRefTable.readInt(XRefTable.this.mBuf, b);
CosToken.skipWhitespace(XRefTable.this.mBuf);
XRefTable.this.highestObjectNumInXRefEntries = XRefTable.this.highestObjectNumInXRefEntries > this.mEnd ? XRefTable.this.highestObjectNumInXRefEntries : this.mEnd;
this.mXRefPos = XRefTable.this.mBuf.getPosition() - 1;
XRefTable.this.mBuf.seek(this.mXRefPos + (long)(20 * (this.mEnd - this.mBegin)));
}
CosObjectInfo getInfo(int index, CosObjectInfo info) throws PDFCosParseException, IOException {
if (info != null && info.isAssigned()) {
if (this.getXRefUsed(index)) {
if ((long)index != this.mCurIndex) {
XRefTable.this.mBuf.seek(this.mXRefPos + 20 * (long)index + 0);
XRefTable.this.mBuf.read(this.mCurEntry, 0, 20);
byte b = this.mCurEntry[17];
if (b != 110 && b != 102) {
throw new PDFCosParseException("Bad xref table entry");
}
this.mCurIndex = index;
}
long pos = 0;
for (int i = 0; i < 10; ++i) {
pos *= 10;
pos += (long)(this.mCurEntry[i] - 48);
}
info.setPos(pos);
info.markAddressed();
} else {
info.markFree();
}
}
return info;
}
final int getXRefGeneration(int index) throws PDFCosParseException, IOException {
if ((long)index != this.mCurIndex) {
XRefTable.this.mBuf.seek(this.mXRefPos + 20 * (long)index + 0);
XRefTable.this.mBuf.read(this.mCurEntry, 0, 20);
byte b = this.mCurEntry[17];
if (b != 110 && b != 102) {
throw new PDFCosParseException("Bad xref table entry");
}
this.mCurIndex = index;
}
int generation = 0;
for (int i = 11; i < 16; ++i) {
generation *= 10;
generation += this.mCurEntry[i] - 48;
}
return generation;
}
final boolean getXRefUsed(int index) throws PDFCosParseException, IOException {
if ((long)index != this.mCurIndex) {
XRefTable.this.mBuf.seek(this.mXRefPos + 20 * (long)index + 0);
XRefTable.this.mBuf.read(this.mCurEntry, 0, 20);
byte b = this.mCurEntry[17];
if (b != 110 && b != 102) {
throw new PDFCosParseException("Bad xref table entry");
}
this.mCurIndex = index;
}
return this.mCurEntry[17] == 110;
}
public String toString() {
return "TableXRefSubSection (base=" + this.mBegin + " end=" + this.mEnd + ")";
}
void close() throws IOException {
}
}
private abstract class XRefSubSection {
int mBegin;
int mEnd;
private XRefSubSection() {
}
abstract void close() throws IOException;
final int getBegin() {
return this.mBegin;
}
final int getEnd() {
return this.mEnd;
}
abstract CosObjectInfo getInfo(int var1, CosObjectInfo var2) throws PDFCosParseException, IOException, PDFSecurityException;
abstract int getXRefGeneration(int var1) throws PDFCosParseException, IOException, PDFSecurityException;
abstract boolean getXRefUsed(int var1) throws PDFCosParseException, IOException, PDFSecurityException;
}
}