ByteArrayUtility.java
5.66 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
/*
* 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.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions;
import com.adobe.internal.pdftoolkit.core.cos.CosToken;
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.util.ByteOps;
import java.io.IOException;
final class ByteArrayUtility {
private ByteArrayUtility() {
}
static byte[] readLiteral(CosDocument doc, InputByteStream inBuf, long nextObjPos) throws PDFCosParseException, PDFIOException, PDFSecurityException {
try {
byte[] dest = new byte[16];
int destIndex = 0;
int level = 1;
do {
if (doc.getOptions().skipCorruptObjects() && inBuf.getPosition() >= nextObjPos) {
return null;
}
int cur = inBuf.read();
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 && !inBuf.eof());
byte[] result = new byte[destIndex - 1];
System.arraycopy(dest, 0, result, 0, destIndex - 1);
return result;
}
catch (IOException e) {
throw new PDFIOException(e);
}
}
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) continue;
cur = (byte)inBuf.read();
} while (level != 0 && !inBuf.eof());
}
static byte[] readHex(InputByteStream buf) throws PDFCosParseException, IOException, PDFIOException {
byte b = (byte)buf.read();
int begin = (int)buf.getPosition() - 1;
while (b != 62) {
if (!ByteOps.isHexDigit(b) && !ByteArrayUtility.ignoreChar(b)) {
throw new PDFCosParseException("Expected Hex Digit" + Long.toString(buf.getPosition() - 1));
}
b = (byte)buf.read();
}
int end = (int)buf.getPosition() - 1;
return ByteArrayUtility.hexToByteArray(buf, begin, end);
}
private static byte[] hexToByteArray(InputByteStream buf, int begin, int end) throws PDFCosParseException, IOException {
byte[] rslt = new byte[(end - begin) / 2];
int idx = 0;
byte h = 0;
byte l = 0;
buf.seek((long)begin);
for (int i = begin; i < end; i += 2) {
byte currentChar = (byte)buf.read();
while (ByteArrayUtility.ignoreChar(currentChar)) {
currentChar = (byte)buf.read();
++i;
}
h = CosToken.toHexDigit(currentChar);
l = CosToken.toHexDigit((byte)buf.read());
rslt[idx++] = (byte)(h * 16 + l);
}
buf.seek(buf.getPosition() + 1);
return rslt;
}
private static boolean ignoreChar(byte byteValue) {
if (byteValue == 13) {
return true;
}
if (byteValue == 10) {
return true;
}
if (ByteOps.isWhitespace(byteValue)) {
return true;
}
return false;
}
}