Tokenizer.java
10.8 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.stream.InputByteStream
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException
* 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.ASHexString
* com.adobe.internal.pdftoolkit.core.types.ASName
* com.adobe.internal.pdftoolkit.core.types.ASNull
* com.adobe.internal.pdftoolkit.core.types.ASObject
* com.adobe.internal.pdftoolkit.core.types.ASString
* com.adobe.internal.pdftoolkit.core.util.ByteOps
* com.adobe.internal.pdftoolkit.core.util.ParseOps
*/
package com.adobe.internal.pdftoolkit.pdf.content;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException;
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.ASHexString;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASNull;
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 com.adobe.internal.pdftoolkit.core.util.ParseOps;
import com.adobe.internal.pdftoolkit.pdf.content.Token;
import java.io.IOException;
import java.util.HashSet;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
class Tokenizer {
private static final byte[] closeDictionary = new byte[]{62, 62};
private static final byte[] closeArray = new byte[]{93};
private static final byte[] endOfStream = new byte[]{-1};
private InputByteStream ibs;
private HashSet<ASName> operators;
private boolean ignoreThisToken = true;
private long lastInstrPos = 0;
private final boolean skipOperators;
public Tokenizer(InputByteStream ibs, HashSet<ASName> operators) {
this.ibs = ibs;
this.operators = operators;
this.skipOperators = operators != null && !operators.isEmpty();
}
Token nextToken() throws PDFParseException, PDFIOException {
if (this.skipOperators) {
return this.nextTokenWithSkipping();
}
return this.getNextToken();
}
private Token getNextToken() throws PDFParseException, PDFIOException {
try {
if (this.ibs == null) {
return null;
}
if (this.ibs.eof()) {
return null;
}
byte cur = ParseOps.skipWhitespace((InputByteStream)this.ibs);
if (ByteOps.isWhitespace((byte)cur) || cur == 37) {
return null;
}
if (ByteOps.isDigit((byte)cur) || cur == 43 || cur == 45 || cur == 46) {
return Token.newOperand((Object)ParseOps.readNumber((byte)cur, (InputByteStream)this.ibs));
}
if (cur == 47) {
return Token.newOperand((Object)ParseOps.readName((InputByteStream)this.ibs));
}
if (cur == 40) {
return Token.newOperand((Object)new ASString(ParseOps.readLiteral((InputByteStream)this.ibs)));
}
if (cur != 60 && cur != 62 && cur != 91 && cur != 93) {
this.ibs.unget();
ASName bareWord = ParseOps.readName((InputByteStream)this.ibs);
if (bareWord.getBytes().length == 0) {
throw new PDFParseException("Error in tokenizing content stream.");
}
if (bareWord == ASName.k_true) {
return Token.newOperand((Object)new ASBoolean(true));
}
if (bareWord == ASName.k_false) {
return Token.newOperand((Object)new ASBoolean(false));
}
if (bareWord == ASName.k_null) {
return Token.newOperand((Object)new ASNull());
}
if (!this.ignoreThisToken) {
this.ignoreThisToken = true;
}
return Token.newOperator(bareWord);
}
if (cur == 60) {
cur = (byte)this.ibs.read();
if (cur == 60) {
return Token.newOperand((Object)this.readDictionary());
}
this.ibs.unget();
byte[] str = ParseOps.readHex((InputByteStream)this.ibs);
return Token.newOperand((Object)new ASHexString(str));
}
if (cur == 62) {
cur = (byte)this.ibs.read();
if (cur == 62) {
return Token.newOperand(closeDictionary);
}
throw new PDFParseException("Expected '>' - " + Long.toString(this.ibs.getPosition()));
}
if (cur == 91) {
return Token.newOperand((Object)this.readArray());
}
if (cur == 93) {
return Token.newOperand(closeArray);
}
throw new PDFParseException("Unexpected token - " + Long.toString(this.ibs.getPosition()));
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
}
Token nextTokenWithSkipping() throws PDFParseException, PDFIOException {
try {
while (this.ibs != null && !this.ibs.eof() && this.ignoreThisToken) {
this.skipNextToken();
}
return this.getNextToken();
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
}
Token skipNextToken() throws PDFParseException, PDFIOException {
block20 : {
try {
if (this.ibs == null) {
return Token.newOperand(endOfStream);
}
if (this.ibs.eof()) {
return Token.newOperand(endOfStream);
}
byte cur = ParseOps.skipWhitespace((InputByteStream)this.ibs);
if (ByteOps.isWhitespace((byte)cur)) {
return null;
}
if (ByteOps.isDigit((byte)cur) || cur == 43 || cur == 45 || cur == 46) {
ParseOps.skipNumber((byte)cur, (InputByteStream)this.ibs);
return null;
}
if (cur == 47) {
ParseOps.skipName((InputByteStream)this.ibs);
return null;
}
if (cur == 40) {
ParseOps.skipLiteral((InputByteStream)this.ibs);
return null;
}
if (cur != 60 && cur != 62 && cur != 91 && cur != 93) {
this.ibs.unget();
ASName bareWord = ParseOps.readName((InputByteStream)this.ibs);
if (bareWord == ASName.k_true) {
return null;
}
if (bareWord == ASName.k_false) {
return null;
}
if (bareWord == ASName.k_null) {
return null;
}
if (bareWord.getBytes().length == 0) {
this.ibs.read();
return null;
}
long lastPos = this.lastInstrPos;
this.lastInstrPos = this.ibs.getPosition();
if (this.operators.contains((Object)bareWord)) {
this.ignoreThisToken = false;
this.ibs.seek(lastPos);
return null;
}
break block20;
}
if (cur == 60) {
cur = (byte)this.ibs.read();
if (cur == 60) {
this.skipDictionary();
return null;
}
this.ibs.unget();
ParseOps.skipHex((InputByteStream)this.ibs);
return null;
}
if (cur == 62) {
cur = (byte)this.ibs.read();
if (cur == 62) {
return Token.newOperand(closeDictionary);
}
throw new PDFParseException("Expected '>' - " + Long.toString(this.ibs.getPosition()));
}
if (cur == 91) {
this.skipArray();
return null;
}
if (cur == 93) {
return Token.newOperand(closeArray);
}
throw new PDFParseException("Unexpected token - " + Long.toString(this.ibs.getPosition()));
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
}
return null;
}
private void skipDictionary() throws PDFParseException, PDFIOException {
Token keyToken;
while ((keyToken = this.skipNextToken()) == null || keyToken.getValue() != closeDictionary && keyToken.getValue() != endOfStream) {
this.skipNextToken();
}
return;
}
private ASDictionary readDictionary() throws PDFParseException, PDFIOException {
try {
ASDictionary asDictionary = new ASDictionary();
do {
Token keyToken;
if ((keyToken = this.getNextToken()) == null || keyToken.getValue() == closeDictionary) {
return asDictionary;
}
if (!(keyToken.getValue() instanceof ASName)) {
throw new PDFParseException("name expected" + Long.toString(this.ibs.getPosition()));
}
Token valueToken = this.nextToken();
asDictionary.put((ASName)keyToken.getValue(), (ASObject)valueToken.getValue());
} while (true);
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
}
private void skipArray() throws PDFParseException, PDFIOException {
Token token;
while ((token = this.skipNextToken()) == null || token.getValue() != closeArray && token.getValue() != endOfStream) {
}
}
private ASArray readArray() throws PDFParseException, PDFIOException {
ASArray asArray = new ASArray();
Token token;
while ((token = this.getNextToken()) != null && token.getValue() != closeArray) {
asArray.add((ASObject)token.getValue());
}
return asArray;
}
}