CosNumeric.java
10.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.stream.OutputByteStream
*/
package com.adobe.internal.pdftoolkit.core.cos;
import com.adobe.internal.io.stream.OutputByteStream;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectInfo;
import com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions;
import com.adobe.internal.pdftoolkit.core.cos.CosScalar;
import com.adobe.internal.pdftoolkit.core.cos.CosToken;
import com.adobe.internal.pdftoolkit.core.cos.DoubleToStringFormatter;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosNumberParseRuntimeException;
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.BooleanHolder;
import com.adobe.internal.pdftoolkit.core.util.StringOps;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.Locale;
public class CosNumeric
extends CosScalar {
private static final DoubleToStringFormatter customFormatter = new DoubleToStringFormatter(6, '.', '-', false);
private static final int NCACHEDINTEGERS = 4096;
private static Integer[] mCachedIntegers = new Integer[4096];
private static final DecimalFormat mPDFDecimalFormat;
private byte[] mInputRep;
private Number mValue;
CosNumeric(CosDocument doc, Number value, CosObjectInfo info) {
int val;
super(doc, info);
if (value instanceof Integer && (val = value.intValue()) >= 0 && val < 4096) {
if (mCachedIntegers[val] == null) {
CosNumeric.mCachedIntegers[val] = (Integer)value;
}
value = mCachedIntegers[val];
}
this.mInputRep = null;
this.mValue = value;
}
CosNumeric(CosDocument doc, byte[] inputRep, CosObjectInfo info) throws PDFCosParseException {
super(doc, info);
if (inputRep != null) {
int count;
for (count = 0; count < inputRep.length && inputRep[count] != 0; ++count) {
}
if (count < inputRep.length) {
byte[] copy = new byte[count];
System.arraycopy(inputRep, 0, copy, 0, count);
inputRep = copy;
}
}
this.mInputRep = inputRep;
}
private void generateNumberValue() {
Number value;
block7 : {
if (this.mValue != null) {
return;
}
value = null;
try {
BooleanHolder wasRepaired = this.getDocument().getOptions().getRepairEnabled() ? new BooleanHolder(false) : null;
value = CosToken.readNumber(this.mInputRep, wasRepaired);
if (wasRepaired != null && wasRepaired.getValue()) {
this.setRepaired(true);
}
}
catch (PDFCosParseException e) {
if (!e.hasErrorType(PDFCosParseException.CosParseErrorType.NumberParseError)) break block7;
throw new PDFCosNumberParseRuntimeException(e);
}
}
if (value instanceof Integer) {
int val = value.intValue();
if (val >= 0 && val < 4096) {
if (mCachedIntegers[val] == null) {
CosNumeric.mCachedIntegers[val] = (Integer)value;
}
value = mCachedIntegers[val];
}
this.mInputRep = null;
}
this.mValue = value;
}
CosNumeric(CosDocument doc, CosNumeric source) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
super(doc, null);
if (source.isIndirect()) {
CosObjectInfo info = doc.newObjectInfo();
this.setInfo(info);
info.setObject(this);
info.markDirty();
}
this.mInputRep = source.mInputRep;
this.mValue = source.mValue;
}
public int getType() {
return 1;
}
public int intValue() {
this.generateNumberValue();
return this.mValue.intValue();
}
public int fixedValue() throws PDFCosParseException {
this.generateNumberValue();
if (this.mValue instanceof Integer || this.mValue instanceof Long) {
long value = this.mValue.longValue();
if (value > 32767 || value < -32768) {
throw new PDFCosParseException("Fixed value out of range");
}
return (int)(value << 16);
}
double value = this.mValue.doubleValue();
if (value == 32768.0) {
return Integer.MAX_VALUE;
}
if (value > 32767.0 || value < -32768.0) {
throw new PDFCosParseException("Fixed value out of range");
}
value = (value *= 65536.0) >= 0.0 ? (value += 0.5) : (value -= 0.5);
return (int)value;
}
public long longValue() {
this.generateNumberValue();
return this.mValue.longValue();
}
public double doubleValue() {
this.generateNumberValue();
return this.mValue.doubleValue();
}
public Number numberValue() {
this.generateNumberValue();
return this.mValue;
}
public Object getValue() {
this.generateNumberValue();
return this.numberValue();
}
void writeOut(OutputByteStream outStream, boolean inString, boolean inDebugMode) throws PDFCosParseException, PDFIOException, PDFSecurityException, IOException {
if (this.mInputRep != null) {
outStream.write(this.mInputRep);
} else if (this.mValue instanceof Double || this.mValue instanceof Float) {
String raw = customFormatter.appendFormatted(this.mValue.doubleValue());
if (raw != null) {
outStream.write(StringOps.toByteArray(raw));
} else {
int count;
raw = mPDFDecimalFormat.format(this.mValue);
int length = raw.length();
boolean sign = false;
boolean expSign = false;
boolean expSeen = false;
int digitPos = 0;
int exponent = 0;
int numDigits = 0;
for (count = 0; count < length; ++count) {
char curChar = raw.charAt(count);
if (curChar == '-') {
if (expSeen) {
expSign = true;
continue;
}
sign = true;
continue;
}
if (curChar == 'E') {
expSeen = true;
continue;
}
if (curChar < '0' || curChar > '9') continue;
if (expSeen) {
exponent *= 10;
exponent += curChar - 48;
continue;
}
if (digitPos == 0) {
digitPos = count;
}
++numDigits;
}
if (numDigits == 0 || !expSeen) {
outStream.write(48);
} else {
if (sign) {
outStream.write(45);
}
if (expSign) {
outStream.write(48);
outStream.write(46);
for (count = 0; count < exponent; ++count) {
outStream.write(48);
}
for (count = 0; count < numDigits; ++count) {
outStream.write((int)raw.charAt(digitPos + count));
}
} else if (exponent >= numDigits) {
for (count = 0; count < numDigits; ++count) {
outStream.write((int)raw.charAt(digitPos + count));
}
for (count = numDigits; count < exponent; ++count) {
outStream.write(48);
}
} else {
if (exponent == 0) {
outStream.write(48);
}
for (count = 0; count < exponent; ++count) {
outStream.write((int)raw.charAt(digitPos + count));
}
if (numDigits != exponent + 1 || raw.charAt(digitPos + exponent) != '0') {
outStream.write(46);
for (count = exponent; count < numDigits; ++count) {
outStream.write((int)raw.charAt(digitPos + count));
}
}
}
}
}
} else {
outStream.write(StringOps.toByteArray(String.valueOf(this.mValue.longValue())));
}
}
public boolean equals(CosObject value) {
if (!(value instanceof CosNumeric) || value.getDocument() != this.getDocument()) {
return false;
}
if (value == this) {
return true;
}
CosNumeric numericValue = (CosNumeric)value;
byte[] numericValueBytes1 = numericValue.mInputRep;
byte[] numericValueBytes2 = this.mInputRep;
if (numericValueBytes1 != null && numericValueBytes2 != null) {
return Arrays.equals(numericValueBytes1, numericValueBytes2);
}
this.generateNumberValue();
((CosNumeric)value).generateNumberValue();
return numericValue.mValue.equals(this.mValue);
}
static {
DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
dfs.setZeroDigit('0');
dfs.setDecimalSeparator('.');
dfs.setMinusSign('-');
mPDFDecimalFormat = new DecimalFormat(".######E0", dfs);
mPDFDecimalFormat.setGroupingUsed(false);
}
}