CosString.java
10.4 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
/*
* 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.CosContainer;
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.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectInfo;
import com.adobe.internal.pdftoolkit.core.cos.CosScalar;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASDate;
import com.adobe.internal.pdftoolkit.core.types.ASHexString;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.core.util.ByteOps;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class CosString
extends CosScalar {
private byte[] mValue;
private boolean mIsEncrypted;
private byte[] mBase;
private int mBegin;
private int mLength;
private boolean mWriteHex;
private boolean mOddBall;
private boolean mToEncrypt;
private CosContainer mParentObj;
private static final byte[] hexrep = new byte[]{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70};
private void init(byte[] base, int begin, int length, boolean isEncrypted, boolean writeHex, boolean oddBall, boolean toEncrypt) {
this.mValue = null;
this.mBase = base;
this.mBegin = begin;
this.mLength = length;
this.mIsEncrypted = isEncrypted;
this.mWriteHex = writeHex;
this.mOddBall = oddBall;
this.mToEncrypt = toEncrypt;
}
CosString(CosDocument doc, byte[] base, int begin, int length, boolean isEncrypted, CosObjectInfo info, boolean writeHex) {
super(doc, info);
this.init(base, begin, length, isEncrypted, writeHex, false, true);
}
CosString(CosDocument doc, byte[] base, int begin, int length, boolean isEncrypted, CosObjectInfo info, boolean writeHex, boolean oddBall) {
super(doc, info);
this.init(base, begin, length, isEncrypted, writeHex, oddBall, true);
}
CosString(CosDocument doc, byte[] base, int begin, int length, boolean isEncrypted, CosObjectInfo info) {
super(doc, info);
this.init(base, begin, length, isEncrypted, false, false, true);
}
CosString copy() throws PDFSecurityException {
if (this.isIndirect()) {
return this;
}
CosString copy = new CosString(this.getDocument(), this.byteArrayValue(), 0, this.byteArrayValue().length, false, null, this.getWriteHex());
copy.setToEncrypt(this.getToEncrypt());
return copy;
}
public int getType() {
return 4;
}
private void initValue(boolean decrypt) throws PDFSecurityException {
if (this.mBegin == 0 && this.mLength == this.mBase.length) {
this.mValue = this.mBase;
} else {
this.mValue = new byte[this.mLength];
System.arraycopy(this.mBase, this.mBegin, this.mValue, 0, this.mLength);
}
if (this.mIsEncrypted && decrypt) {
this.mValue = this.getDocument().getEncryption().decryptString(this, this.mValue);
}
}
void setParentObj(CosContainer parent) {
this.mParentObj = parent;
}
CosContainer getParentObj() {
return this.mParentObj;
}
public byte[] byteArrayValue() throws PDFSecurityException {
return this.byteArrayValue(true);
}
private byte[] byteArrayValue(boolean decrypt) throws PDFSecurityException {
if (this.mValue == null) {
this.initValue(decrypt);
}
return this.mValue;
}
public ASString stringValue() throws PDFSecurityException {
return new ASString(this.byteArrayValue());
}
public ASHexString hexStringValue() throws PDFSecurityException {
return new ASHexString(this.byteArrayValue());
}
public String asString() throws PDFSecurityException {
return this.stringValue().asString();
}
public Object getValue() throws PDFSecurityException {
return this.byteArrayValue();
}
public String textValue() throws PDFSecurityException {
String rslt = "";
byte[] src = this.byteArrayValue();
boolean isUnicode = src.length >= 2 && src[0] == -2 && src[1] == -1;
try {
rslt = new String(src, isUnicode ? "UTF-16" : "ISO-8859-1");
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException("Platform does not support encoding.", e);
}
return rslt;
}
byte[] getObjectEncryptionKey(boolean write) {
CosLinearization cosLin;
byte[] key = new byte[5];
int objNum = this.isIndirect() ? this.getInfo().getObjNum() : this.mParentObj.getInfo().getObjNum();
int gen = this.isIndirect() ? this.getInfo().getObjGen() : this.mParentObj.getInfo().getObjGen();
if (!write && (cosLin = this.getDocument().getLinearization()) != null) {
objNum = cosLin.mapNewToOldObjNum(objNum);
gen = cosLin.mapNewToOldObjGen(objNum);
}
System.arraycopy(ByteOps.splitInt2Bytes(objNum, 3), 0, key, 0, 3);
System.arraycopy(ByteOps.splitInt2Bytes(gen, 2), 0, key, 3, 2);
return key;
}
boolean markNotDirty() {
if (this.isIndirect()) {
return super.markNotDirty();
}
if (this.mParentObj != null) {
return this.mParentObj.markNotDirty();
}
return false;
}
public void setDataInternal(byte[] newData, boolean markDirty) throws PDFCosParseException, PDFIOException, PDFSecurityException {
this.mValue = null;
this.mBase = newData;
this.mBegin = 0;
this.mLength = newData.length;
this.mIsEncrypted = false;
if (markDirty) {
try {
if (this.isIndirect()) {
this.getInfo().markDirty();
} else if (this.mParentObj != null) {
this.mParentObj.getInfo().markDirty();
}
}
catch (IOException e) {
throw new PDFIOException(e);
}
}
}
void writeOut(OutputByteStream outStream, boolean inString, boolean inDebug) throws PDFCosParseException, PDFSecurityException, IOException {
byte[] outValue = this.byteArrayValue(!inDebug);
if (this.mToEncrypt && !inDebug) {
outValue = this.getDocument().getEncryption().encryptString(this, outValue);
}
if (this.mWriteHex) {
outStream.write(60);
for (int i = 0; i < outValue.length; ++i) {
byte b = outValue[i];
outStream.write((int)hexrep[b >> 4 & 15]);
outStream.write((int)hexrep[b & 15]);
}
outStream.write(62);
} else {
outStream.write(40);
for (int i = 0; i < outValue.length; ++i) {
byte b = outValue[i];
if (b == 10) {
outStream.write(92);
outStream.write(110);
continue;
}
if (b == 13) {
outStream.write(92);
outStream.write(114);
continue;
}
if (b == 40) {
outStream.write(92);
outStream.write(40);
continue;
}
if (b == 41) {
outStream.write(92);
outStream.write(41);
continue;
}
if (b == 92) {
outStream.write(92);
outStream.write(92);
continue;
}
outStream.write((int)b);
}
outStream.write(41);
}
this.mOddBall = false;
}
public String toString() {
boolean showEncrypted = true;
return this.toString(!showEncrypted);
}
public ASDate asDate() throws PDFCosParseException, PDFSecurityException {
try {
return new ASDate(new ASString(this.byteArrayValue()));
}
catch (PDFParseException e) {
throw new PDFCosParseException(e);
}
}
public void setWriteHex(boolean b) {
this.mWriteHex = b;
}
public boolean getWriteHex() {
return this.mWriteHex;
}
public boolean isOddBall() {
return this.mOddBall;
}
public void setToEncrypt(boolean encrypted) {
this.mToEncrypt = encrypted;
}
public boolean getToEncrypt() {
return this.mToEncrypt;
}
public void setIsEncrypted(boolean encrypted) {
if (encrypted != this.mIsEncrypted) {
this.mValue = null;
this.mIsEncrypted = encrypted;
}
}
public boolean getIsEncrypted() {
return this.mIsEncrypted;
}
public boolean equals(ASString string) {
return this.equals(string.getBytes());
}
public boolean equals(CosString string) {
try {
return this.equals(string.byteArrayValue());
}
catch (PDFSecurityException e) {
return false;
}
}
public boolean equals(CosObject value) {
if (!(value instanceof CosString) || value.getDocument() != this.getDocument()) {
return false;
}
if (value == this) {
return true;
}
CosString cosString = (CosString)value;
return this.equals(cosString);
}
public boolean equals(byte[] b) {
try {
byte[] myBytes = this.byteArrayValue();
if (myBytes.length != b.length) {
return false;
}
for (int i = 0; i < myBytes.length; ++i) {
if (myBytes[i] == b[i]) continue;
return false;
}
return true;
}
catch (PDFSecurityException e) {
return false;
}
}
}