-
Notifications
You must be signed in to change notification settings - Fork 3
/
DBPFPackager.java
500 lines (457 loc) · 20.8 KB
/
DBPFPackager.java
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package jdbpfx.util;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author Jon
*/
public class DBPFPackager {
private long compressedSize = 0;
private long decompressedSize = 0;
private boolean compressed = false;
public static boolean debug = false;
/**
* Constructor.<br>
*/
public DBPFPackager() {
}
/**
* @return the compressedSize
*/
public long getCompressedSize() {
return compressedSize;
}
/**
* @return the decompressedSize
*/
public long getDecompressedSize() {
return decompressedSize;
}
/**
* @return the compressed
*/
public boolean isCompressed() {
return compressed;
}
/**
* Check, if data is compressed.<br>
*
* @param data
* The data to check
* @return TRUE, if compressed; FALSE, otherwise
*/
public static boolean isCompressed(byte[] data) {
if (data.length > 6) {
int signature = (int) DBPFUtil.getUint(data, 0x04, 2);
if (signature == DBPFUtil.MAGICNUMBER_QFS) {
// there is an s3d file in SC1.dat which would otherwise
// return true on uncompressed data,
// this workaround is not failproof
String fileType = DBPFUtil.getChars(data, 0x00, 4);
if (fileType.equals(DBPFUtil.MAGICNUMBER_3DMD)) {
return false;
}
return true;
}
}
return false;
}
public static long getDecompressedSize(byte[] data) {
long decompressedSize = -1L;
if (data.length >= 9) {
// long compressedSize = DBPFUtil.getUint(data, 0x00, 4);
int signature = (int) DBPFUtil.getUint(data, 0x04, 2);
if (signature == DBPFUtil.MAGICNUMBER_QFS) {
//decompressed size is stored big endian in contrast to everything else stored little endian
decompressedSize = DBPFUtil.getUint(data, 0x06, 1) * 0x10000
+ DBPFUtil.getUint(data, 0x07, 1) * 0x100
+ DBPFUtil.getUint(data, 0x08, 1);
}
}
return decompressedSize;
}
/**
* Copies data from source to destination array.<br>
* The copy is byte by byte from srcPos to destPos and given length.
* If the destination array is not large enough a new array will be
* created. Since the new array will be a different object, callers
* should always update their reference to the original dest array
* with the returned value.
*
* @param src
* The source array
* @param srcPos
* The source position
* @param dest
* The destination array
* @param destPos
* The destination position
* @param length
* The length
* @return
* The destination array
*/
private byte[] arrayCopy2(byte[] src, int srcPos, byte[] dest, int destPos, int length) {
// This shouldn't occur, but to prevent errors
if (dest.length < destPos + length) {
if (debug) {
System.err
.println("ATTENTION!"
+ "\nBy arrayCopy2 the destination array is not big enough!"
+ "\nWill make it bigger and make a System.arraycopy.");
}
byte[] destExt = new byte[destPos + length];
System.arraycopy(dest, 0, destExt, 0, dest.length);
dest = destExt;
}
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
return dest;
}
/**
* Copies data from array at destPos-offset to array at destPos.<br>
* If the array is not large enough a new array will be
* created. Since the new array will be a different object, callers
* should always update their reference to the original array
* with the returned value.
*
* @param array
* The array
* @param offset
* The position to copy from (reverse from end of array!)
* @param destPos
* The position to copy to
* @param length
* The length of data to copy
* @return
* The array
*/
private byte[] offsetCopy(byte[] array, int offset, int destPos, int length) {
int srcPos = destPos - offset;
// This shouldn't occur, but to prevent errors
if (array.length < destPos + length) {
if (debug) {
System.err
.println("ATTENTION!"
+ "\nBy offsetCopy the destination array is not big enough!"
+ "\nWill make it bigger and make a System.arraycopy.");
}
byte[] arrayNew = new byte[destPos + length];
System.arraycopy(array, 0, arrayNew, 0, array.length);
array = arrayNew;
}
for (int i = 0; i < length; i++) {
array[destPos + i] = array[srcPos + i];
}
return array;
}
/**
* Compress the decompressed data.<br>
*
* @param dData
* The decompressed data
* @return The compressed data
*/
public byte[] compress(byte[] dData) {
// if data is big enough for compress
if (dData.length > 6) {
// check, if data already compressed
int signature = (int) DBPFUtil.getUint(dData, 0x04, 2);
if (signature != DBPFUtil.MAGICNUMBER_QFS) {
// some Compression Data
final int MAX_OFFSET = 0x20000;
final int MAX_COPY_COUNT = 0x404;
// used to finetune the lookup (small values increase the
// compression for Big Files)
final int QFS_MAXITER = 0x80;
// contains the latest offset for a combination of two
// characters
HashMap<Integer, ArrayList<Integer>> cmpmap2 = new HashMap<Integer, ArrayList<Integer>>();
// will contain the compressed data (maximal size =
// uncompressedSize+MAX_COPY_COUNT)
byte[] cData = new byte[dData.length + MAX_COPY_COUNT];
// init some vars
int writeIndex = 9; // leave 9 bytes for the header
int lastReadIndex = 0;
ArrayList<Integer> indexList = null;
int copyOffset = 0;
int copyCount = 0;
int index = -1;
boolean end = false;
// begin main compression loop
while (index < dData.length - 3) {
// get all Compression Candidates (list of offsets for all
// occurances of the current 3 bytes)
do {
index++;
if (index >= dData.length - 2) {
end = true;
break;
}
int mapindex = (dData[index] & 0xFF)
| ((dData[index + 1] & 0xFF) << 8)
| ((dData[index + 2] & 0xFF) << 16);
indexList = cmpmap2.get(mapindex);
if (indexList == null) {
indexList = new ArrayList<Integer>();
cmpmap2.put(mapindex, indexList);
}
indexList.add(index);
} while (index < lastReadIndex);
if (end) {
break;
}
// find the longest repeating byte sequence in the index
// List (for offset copy)
int offsetCopyCount = 0;
int loopcount = 1;
while ((loopcount < indexList.size()) && (loopcount < QFS_MAXITER)) {
int foundindex = indexList.get((indexList.size() - 1) - loopcount);
if ((index - foundindex) >= MAX_OFFSET) {
break;
}
loopcount++;
copyCount = 3;
while ((dData.length > index + copyCount)
&& (dData[index + copyCount] == dData[foundindex + copyCount])
&& (copyCount < MAX_COPY_COUNT)) {
copyCount++;
}
if (copyCount > offsetCopyCount) {
offsetCopyCount = copyCount;
copyOffset = index - foundindex;
}
}
// check if we can compress this
// In FSH Tool stand additionally this:
if (offsetCopyCount > dData.length - index) {
offsetCopyCount = index - dData.length;
}
if (offsetCopyCount <= 2) {
offsetCopyCount = 0;
} else if ((offsetCopyCount == 3) && (copyOffset > 0x400)) { // 1024
offsetCopyCount = 0;
} else if ((offsetCopyCount == 4) && (copyOffset > 0x4000)) { // 16384
offsetCopyCount = 0;
}
// this is offset-compressable? so do the compression
if (offsetCopyCount > 0) {
// plaincopy
// In FSH Tool stand this (A):
while (index - lastReadIndex >= 4) {
copyCount = (index - lastReadIndex) / 4 - 1;
if (copyCount > 0x1B) {
copyCount = 0x1B;
}
cData[writeIndex++] = (byte) (0xE0 + copyCount);
copyCount = 4 * copyCount + 4;
cData = arrayCopy2(dData, lastReadIndex, cData, writeIndex, copyCount);
lastReadIndex += copyCount;
writeIndex += copyCount;
}
// while ((index - lastReadIndex) > 3) {
// copyCount = (index - lastReadIndex);
// while (copyCount > 0x71) {
// copyCount -= 0x71;
// }
// copyCount = copyCount & 0xfc;
// int realCopyCount = (copyCount >> 2);
// cData[writeIndex++] = (short) (0xdf + realCopyCount);
// arrayCopy2(dData, lastReadIndex, cData, writeIndex,
// copyCount);
// writeIndex += copyCount;
// lastReadIndex += copyCount;
// }
// offsetcopy
copyCount = index - lastReadIndex;
copyOffset--;
if ((offsetCopyCount <= 0x0A) && (copyOffset < 0x400)) {
cData[writeIndex++] = (byte) (((copyOffset >> 8) << 5) + ((offsetCopyCount - 3) << 2) + copyCount);
cData[writeIndex++] = (byte) (copyOffset & 0xff);
} else if ((offsetCopyCount <= 0x43) && (copyOffset < 0x4000)) {
cData[writeIndex++] = (byte) (0x80 + (offsetCopyCount - 4));
cData[writeIndex++] = (byte) ((copyCount << 6) + (copyOffset >> 8));
cData[writeIndex++] = (byte) (copyOffset & 0xff);
} else if ((offsetCopyCount <= MAX_COPY_COUNT) && (copyOffset < MAX_OFFSET)) {
cData[writeIndex++] = (byte) (0xc0 + ((copyOffset >> 16) << 4) + (((offsetCopyCount - 5) >> 8) << 2) + copyCount);
cData[writeIndex++] = (byte) ((copyOffset >> 8) & 0xff);
cData[writeIndex++] = (byte) (copyOffset & 0xff);
cData[writeIndex++] = (byte) ((offsetCopyCount - 5) & 0xff);
}
// else {
// copyCount = 0;
// offsetCopyCount = 0;
// }
// do the offset copy
cData = arrayCopy2(dData, lastReadIndex, cData, writeIndex, copyCount);
writeIndex += copyCount;
lastReadIndex += copyCount;
lastReadIndex += offsetCopyCount;
}
}
// add the End Record
index = dData.length;
// in FSH Tool stand the same as above (A)
while (index - lastReadIndex >= 4) {
copyCount = (index - lastReadIndex) / 4 - 1;
if (copyCount > 0x1B) {
copyCount = 0x1B;
}
cData[writeIndex++] = (byte) (0xE0 + copyCount);
copyCount = 4 * copyCount + 4;
cData = arrayCopy2(dData, lastReadIndex, cData, writeIndex, copyCount);
lastReadIndex += copyCount;
writeIndex += copyCount;
}
// lastReadIndex = Math.min(index, lastReadIndex);
// while ((index - lastReadIndex) > 3) {
// copyCount = (index - lastReadIndex);
// while (copyCount > 0x71) {
// copyCount -= 0x71;
// }
// copyCount = copyCount & 0xfc;
// int realCopyCount = (copyCount >> 2);
// cData[writeIndex++] = (short) (0xdf + realCopyCount);
// arrayCopy2(dData, lastReadIndex, cData, writeIndex,
// copyCount);
// writeIndex += copyCount;
// lastReadIndex += copyCount;
// }
copyCount = index - lastReadIndex;
cData[writeIndex++] = (byte) (0xfc + copyCount);
cData = arrayCopy2(dData, lastReadIndex, cData, writeIndex, copyCount);
writeIndex += copyCount;
lastReadIndex += copyCount;
// write the header for the compressed data
// set the compressed size
DBPFUtil.setUint(writeIndex, cData, 0x00, 4);
this.compressedSize = writeIndex;
// set the MAGICNUMBER
DBPFUtil.setUint(DBPFUtil.MAGICNUMBER_QFS, cData, 0x04, 2);
// set the decompressed size
byte[] revData = new byte[3];
DBPFUtil.setUint(dData.length, revData, 0x00, 3);
for (int j = 0; j < revData.length; j++) {
cData[j + 6] = revData[2 - j];
}
this.decompressedSize = dData.length;
compressed = false;
if (compressedSize < decompressedSize) {
compressed = true;
}
// get the compressed data
byte[] retData = new byte[writeIndex];
System.arraycopy(cData, 0, retData, 0, writeIndex);
return retData;
}
}
return dData;
}
/**
* Decompress the compressed data.<br>
*
* If the data are not compressed, this will return the same data.
*
* @param cData
* The compressed data
* @return The decompressed data
*/
public byte[] decompress(byte[] cData) {
compressed = false;
if (cData.length > 6) {
// HEADER
compressedSize = DBPFUtil.getUint(cData, 0x00, 4);
int signature = (int) DBPFUtil.getUint(cData, 0x04, 2);
// if not compressed
decompressedSize = compressedSize;
if (signature == DBPFUtil.MAGICNUMBER_QFS
// see static isCompressed method for explanation
&& !DBPFUtil.getChars(cData, 0x00, 4).equals(DBPFUtil.MAGICNUMBER_3DMD)) {
//decompressed size is stored big endian in contrast to everything else stored little endian
decompressedSize = DBPFUtil.getUint(cData, 0x06, 1) * 0x10000
+ DBPFUtil.getUint(cData, 0x07, 1) * 0x100
+ DBPFUtil.getUint(cData, 0x08, 1);
// There seems sometimes that given compressedSize is
// not exactly the read data size.
// Don't know why but take real data size for decompress
if (debug) {
System.err.println("RawData-Size: " + cData.length
+ " Found in RawData: " + compressedSize
+ " DecompressedSize: " + decompressedSize);
}
byte[] dData = new byte[(int) decompressedSize];
int dpos = 0;
// COMPRESSED DATA
compressed = true;
int pos = 9;
int control1 = 0;
while (control1 < 0xFC && pos < cData.length) {
control1 = cData[pos] & 0xFF;
// System.out.println(">>> Position: " + pos +
// " ## Control: "
// + Integer.toHexString((int)
// control1)+" ## Rest: "+(compressedSize-pos));
pos++;
if (control1 >= 0 && control1 <= 127) {
// 0x00 - 0x7F
int control2 = cData[pos] & 0xFF;
pos++;
int numberOfPlainText = (control1 & 0x03);
dData = arrayCopy2(cData, pos, dData, dpos, numberOfPlainText);
dpos += numberOfPlainText;
pos += numberOfPlainText;
int offset = ((control1 & 0x60) << 3) + (control2) + 1;
int numberToCopyFromOffset = ((control1 & 0x1C) >> 2) + 3;
dData = offsetCopy(dData, offset, dpos, numberToCopyFromOffset);
dpos += numberToCopyFromOffset;
} else if (control1 >= 128 && control1 <= 191) {
// 0x80 - 0xBF
int control2 = cData[pos] & 0xFF;
pos++;
int control3 = cData[pos] & 0xFF;
pos++;
int numberOfPlainText = (control2 >> 6) & 0x03;
dData = arrayCopy2(cData, pos, dData, dpos, numberOfPlainText);
dpos += numberOfPlainText;
pos += numberOfPlainText;
int offset = ((control2 & 0x3F) << 8) + (control3) + 1;
int numberToCopyFromOffset = (control1 & 0x3F) + 4;
dData = offsetCopy(dData, offset, dpos, numberToCopyFromOffset);
dpos += numberToCopyFromOffset;
} else if (control1 >= 192 && control1 <= 223) {
// 0xC0 - 0xDF
int numberOfPlainText = (control1 & 0x03);
int control2 = cData[pos] & 0xFF;
pos++;
int control3 = cData[pos] & 0xFF;
pos++;
int control4 = cData[pos] & 0xFF;
pos++;
dData = arrayCopy2(cData, pos, dData, dpos, numberOfPlainText);
dpos += numberOfPlainText;
pos += numberOfPlainText;
int offset = ((control1 & 0x10) << 12) + (control2 << 8) + (control3) + 1;
int numberToCopyFromOffset = ((control1 & 0x0C) << 6) + (control4) + 5;
dData = offsetCopy(dData, offset, dpos, numberToCopyFromOffset);
dpos += numberToCopyFromOffset;
} else if (control1 >= 224 && control1 <= 251) {
// 0xE0 - 0xFB
int numberOfPlainText = ((control1 & 0x1F) << 2) + 4;
dData = arrayCopy2(cData, pos, dData, dpos, numberOfPlainText);
dpos += numberOfPlainText;
pos += numberOfPlainText;
} else {
int numberOfPlainText = (control1 & 0x03);
dData = arrayCopy2(cData, pos, dData, dpos, numberOfPlainText);
dpos += numberOfPlainText;
pos += numberOfPlainText;
}
}
return dData;
}
}
// no data to decompress
compressed = false;
return cData;
}
}