ParseOps.java
11 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.stream.InputByteStream
*/
package com.adobe.internal.pdftoolkit.core.util;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASNumber;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.core.util.ByteOps;
import java.io.IOException;
public final class ParseOps {
private static final int DEFAULT_CHUNK_SIZE = 512;
private static ThreadLocal<byte[]> tlBuffer = new ThreadLocal<byte[]>(){
@Override
protected synchronized byte[] initialValue() {
return new byte[512];
}
};
private ParseOps() {
}
public static byte[] readHex(InputByteStream buf) throws IOException, PDFParseException {
byte b = (byte)buf.read();
int begin = (int)buf.getPosition() - 1;
int whiteSpaceCount = 0;
while (b != 62) {
if (!ByteOps.isHexDigit(b)) {
if (!ByteOps.isWhitespace(b)) {
throw new PDFParseException("Expected Hex Digit" + Long.toString(buf.getPosition() - 1));
}
++whiteSpaceCount;
}
b = (byte)buf.read();
}
int end = (int)buf.getPosition() - 1;
return ParseOps.hexToByteArray(buf, begin, end, whiteSpaceCount);
}
public static void skipHex(InputByteStream buf) throws IOException, PDFParseException {
byte b = (byte)buf.read();
while (b != 62) {
if (!ByteOps.isHexDigit(b) && !ByteOps.isWhitespace(b)) {
throw new PDFParseException("Expected Hex Digit" + Long.toString(buf.getPosition() - 1));
}
b = (byte)buf.read();
}
}
public static void skipLiteral(InputByteStream inBuf) throws IOException {
int level = 1;
do {
byte cur;
if ((cur = (byte)inBuf.read()) == 40) {
++level;
continue;
}
if (cur == 41) {
--level;
continue;
}
if (cur != 92 || (cur = (byte)inBuf.read()) == 13 || cur < 48 || cur > 55) continue;
cur = (byte)(cur - 48);
byte c = (byte)inBuf.read();
if (c >= 48 && c <= 55) {
c = (byte)(c - 48);
byte d = (byte)inBuf.read();
if (d >= 48 && d <= 55) {
inBuf.read();
}
}
inBuf.unget();
} while (level != 0 && !inBuf.eof());
}
public static byte[] readLiteral(InputByteStream inBuf) throws IOException, PDFParseException {
byte[] dest = new byte[16];
int destIndex = 0;
int level = 1;
do {
int a;
if ((a = inBuf.read()) == -1) {
throw new PDFParseException("EOF occured while reading a literal from content stream.");
}
int cur = a;
if (cur == 40) {
++level;
} else if (cur == 41) {
--level;
} else if (cur == 92) {
cur = (byte)inBuf.read();
if (cur == 10) continue;
if (cur == 13) {
cur = (byte)inBuf.read();
if (cur == 10) continue;
inBuf.unget();
continue;
}
if (cur >= 48 && cur <= 55) {
cur = (byte)(cur - 48);
byte c = (byte)inBuf.read();
if (c >= 48 && c <= 55) {
c = (byte)(c - 48);
byte d = (byte)inBuf.read();
if (d >= 48 && d <= 55) {
d = (byte)(d - 48);
cur = (byte)(cur * 64 + c * 8 + d);
inBuf.read();
} else {
cur = (byte)(cur * 8 + c);
}
}
inBuf.unget();
} else if (cur == 110) {
cur = 10;
} else if (cur == 114) {
cur = 13;
} else if (cur == 116) {
cur = 9;
} else if (cur == 98) {
cur = 8;
} else if (cur == 102) {
cur = 12;
} else if (cur == 40) {
cur = 40;
} else if (cur == 41) {
cur = 41;
} else if (cur == 92) {
cur = 92;
}
}
if (destIndex == dest.length) {
byte[] newDest = new byte[dest.length * 2];
System.arraycopy(dest, 0, newDest, 0, dest.length);
dest = newDest;
}
dest[destIndex++] = cur;
} while (level != 0);
byte[] asStr = new byte[destIndex - 1];
for (int i = 0; i < destIndex - 1; ++i) {
asStr[i] = dest[i];
}
return asStr;
}
static byte readHexDigit(InputByteStream buf) throws IOException, PDFParseException {
return ByteOps.getHex((byte)buf.read());
}
public static ASName readName(InputByteStream buf) throws IOException, PDFParseException {
byte[] tmp = tlBuffer.get();
int end = 0;
int cur = buf.read();
while (ByteOps.isRegular((byte)cur)) {
if (cur == 35) {
cur = ParseOps.readHexDigit(buf) * 16 + ParseOps.readHexDigit(buf);
}
tmp[end++] = (byte)cur;
if (end >= tmp.length || buf.eof()) break;
cur = buf.read();
}
if (!ByteOps.isRegular((byte)cur)) {
buf.unget();
}
return ASName.getName(tmp, 0, end);
}
public static void skipName(InputByteStream buf) throws IOException, PDFParseException {
int cur = buf.read();
while (ByteOps.isRegular((byte)cur)) {
if (cur == 35) {
buf.read();
buf.read();
}
if (buf.eof()) break;
cur = buf.read();
}
if (!ByteOps.isRegular((byte)cur)) {
buf.unget();
}
}
public static ASNumber readNumber(byte first, InputByteStream buf) throws PDFParseException, IOException {
boolean decimalPointEncountered = false;
int end = 0;
byte[] tmp = new byte[512];
byte b = first;
if (b == 45 || b == 43) {
tmp[end++] = b;
b = (byte)buf.read();
}
while (ByteOps.isDigit(b)) {
tmp[end++] = b;
if (end >= tmp.length || buf.eof()) break;
b = (byte)buf.read();
}
if (b == 46) {
decimalPointEncountered = true;
tmp[end++] = b;
if (!buf.eof()) {
b = (byte)buf.read();
while (ByteOps.isDigit(b)) {
tmp[end++] = b;
if (buf.eof()) break;
b = (byte)buf.read();
}
}
}
if (ByteOps.isRegular(b) && !ByteOps.isDigit(b)) {
if (!decimalPointEncountered || b != 45) {
throw new PDFParseException("Expected a number at position - " + Long.toString(buf.getPosition() - 1));
}
while (!ByteOps.isWhitespace(b) && !buf.eof()) {
b = (byte)buf.read();
}
}
byte[] strBuf = new byte[end];
for (int i = 0; i < strBuf.length; ++i) {
strBuf[i] = tmp[i];
}
ASString strVal = new ASString(strBuf);
ASNumber number = new ASNumber(strVal);
if (!ByteOps.isRegular(b)) {
buf.unget();
}
return number;
}
public static void skipNumber(byte first, InputByteStream buf) throws PDFParseException, IOException {
byte b = first;
if (b == 45 || b == 43) {
b = (byte)buf.read();
}
while (ByteOps.isDigit(b) && !buf.eof()) {
b = (byte)buf.read();
}
if (b == 46 && !buf.eof()) {
b = (byte)buf.read();
while (ByteOps.isDigit(b) && !buf.eof()) {
b = (byte)buf.read();
}
}
if (!ByteOps.isRegular(b)) {
buf.unget();
}
}
public static byte skipWhitespace(InputByteStream buf) throws IOException {
int b = 32;
while (ByteOps.isWhitespace((byte)b) && !buf.eof()) {
b = (byte)buf.read();
if (b != 37) continue;
while (b != 13 && b != 10 && !buf.eof()) {
b = (byte)buf.read();
}
if (!buf.eof()) continue;
b = 32;
}
return (byte)b;
}
static byte[] hexToByteArray(InputByteStream buf, int begin, int end, int whiteSpaceCount) throws IOException, PDFParseException {
byte[] rslt = new byte[(end - begin - whiteSpaceCount + 1) / 2];
int idx = 0;
buf.seek((long)begin);
int i = begin;
while (i < end) {
byte bH;
while (!ByteOps.isHexDigit(bH = (byte)buf.read()) && ++i < end) {
}
if (!ByteOps.isHexDigit(bH)) continue;
byte h = ByteOps.getHex(bH);
int l = 0;
if (i < end) {
byte bL;
while (!ByteOps.isHexDigit(bL = (byte)buf.read()) && ++i < end) {
}
if (ByteOps.isHexDigit(bL)) {
l = ByteOps.getHex(bL);
}
}
rslt[idx++] = (byte)(h * 16 + l);
}
buf.seek(buf.getPosition() + 1);
if (idx < rslt.length) {
byte[] shortRslt = new byte[idx];
for (int i2 = 0; i2 < idx; ++i2) {
shortRslt[i2] = rslt[i2];
}
return shortRslt;
}
return rslt;
}
static void skipHexToByteArray(InputByteStream buf, int begin, int end, int whiteSpaceCount) throws IOException, PDFParseException {
buf.seek((long)begin);
int i = begin;
while (i < end) {
byte bL;
byte bH;
while (!ByteOps.isHexDigit(bH = (byte)buf.read()) && ++i < end) {
}
if (!ByteOps.isHexDigit(bH) || i >= end) continue;
while (!ByteOps.isHexDigit(bL = (byte)buf.read()) && ++i < end) {
}
}
buf.seek(buf.getPosition() + 1);
}
public static long getEndObjPos(InputByteStream mBuf) throws IOException {
int b = mBuf.read();
while (b != -1) {
if (b == 101) {
b = mBuf.read();
if (b != 110 || (b = mBuf.read()) != 100 || (b = mBuf.read()) != 111 || (b = mBuf.read()) != 98 || (b = mBuf.read()) != 106) continue;
return mBuf.getPosition() - 6;
}
b = mBuf.read();
}
return -1;
}
}