/
yj1.c
495 lines (456 loc) · 12.8 KB
/
yj1.c
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
//
// PAL DOS compress format (YJ_1) library
//
// Author: Lou Yihua <louyihua@21cn.com>
//
// Copyright 2006 - 2007 Lou Yihua
//
// This file is part of PAL library.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// Ported to C from C++ and modified for compatibility with Big-Endian
// by Wei Mingzhi <whistler@openoffice.org>.
// TODO: fix YJ_2 for big-endian
#include "commonx.h"
#ifndef PAL_WIN95
typedef struct _TreeNode
{
unsigned char value;
unsigned char leaf;
unsigned short level;
unsigned int weight;
struct _TreeNode *parent;
struct _TreeNode *left;
struct _TreeNode *right;
} TreeNode;
typedef struct _TreeNodeList
{
TreeNode *node;
struct _TreeNodeList *next;
} TreeNodeList;
typedef struct _YJ_1_FILEHEADER
{
unsigned int Signature; // 'YJ_1'
unsigned int UncompressedLength; // size before compression
unsigned int CompressedLength; // size after compression
unsigned short BlockCount; // number of blocks
unsigned char Unknown;
unsigned char HuffmanTreeLength; // length of huffman tree
} YJ_1_FILEHEADER, *PYJ_1_FILEHEADER;
typedef struct _YJ_1_BLOCKHEADER
{
unsigned short UncompressedLength; // maximum 0x4000
unsigned short CompressedLength; // including the header
unsigned short LZSSRepeatTable[4];
unsigned char LZSSOffsetCodeLengthTable[4];
unsigned char LZSSRepeatCodeLengthTable[3];
unsigned char CodeCountCodeLengthTable[3];
unsigned char CodeCountTable[2];
} YJ_1_BLOCKHEADER, *PYJ_1_BLOCKHEADER;
static unsigned int
get_bits(
const void *src,
unsigned int *bitptr,
unsigned int count
)
{
unsigned char *temp = ((unsigned char *)src) + ((*bitptr >> 4) << 1);
unsigned int bptr = *bitptr & 0xf;
unsigned short mask;
*bitptr += count;
if (count > 16 - bptr)
{
count = count + bptr - 16;
mask = 0xffff >> bptr;
return (((temp[0] | (temp[1] << 8)) & mask) << count) | ((temp[2] | (temp[3] << 8)) >> (16 - count));
}
else
return (((unsigned short)((temp[0] | (temp[1] << 8)) << bptr)) >> (16 - count));
}
static unsigned short
get_loop(
const void *src,
unsigned int *bitptr,
PYJ_1_BLOCKHEADER header
)
{
if (get_bits(src, bitptr, 1))
return header->CodeCountTable[0];
else
{
unsigned int temp = get_bits(src, bitptr, 2);
if (temp)
return get_bits(src, bitptr, header->CodeCountCodeLengthTable[temp - 1]);
else
return header->CodeCountTable[1];
}
}
static unsigned short
get_count(
const void *src,
unsigned int *bitptr,
PYJ_1_BLOCKHEADER header
)
{
unsigned short temp;
if ((temp = get_bits(src, bitptr, 2)) != 0)
{
if (get_bits(src, bitptr, 1))
return get_bits(src, bitptr, header->LZSSRepeatCodeLengthTable[temp - 1]);
else
return SWAP16(header->LZSSRepeatTable[temp]);
}
else
return SWAP16(header->LZSSRepeatTable[0]);
}
INT
Decompress(
LPCVOID Source,
LPVOID Destination,
INT DestSize
)
{
PYJ_1_FILEHEADER hdr = (PYJ_1_FILEHEADER)Source;
unsigned char *src = (unsigned char *)Source;
unsigned char *dest;
unsigned int i;
TreeNode *root, *node;
if (Source == NULL)
return -1;
if (SWAP32(hdr->Signature) != 0x315f4a59)
return -1;
if (SWAP32(hdr->UncompressedLength) > (unsigned int)DestSize)
return -1;
do
{
unsigned short tree_len = ((unsigned short)hdr->HuffmanTreeLength) * 2;
unsigned int bitptr = 0;
unsigned char *flag = (unsigned char *)src + 16 + tree_len;
if ((node = root = (TreeNode *)malloc(sizeof(TreeNode) * (tree_len + 1))) == NULL)
return -1;
root[0].leaf = 0;
root[0].value = 0;
root[0].left = root + 1;
root[0].right = root + 2;
for (i = 1; i <= tree_len; i++)
{
root[i].leaf = !get_bits(flag, &bitptr, 1);
root[i].value = src[15 + i];
if (root[i].leaf)
root[i].left = root[i].right = NULL;
else
{
root[i].left = root + (root[i].value << 1) + 1;
root[i].right = root[i].left + 1;
}
}
src += 16 + tree_len + (((tree_len & 0xf) ? (tree_len >> 4) + 1 : (tree_len >> 4)) << 1);
} while (0);
dest = (unsigned char *)Destination;
for (i = 0; i < SWAP16(hdr->BlockCount); i++)
{
unsigned int bitptr;
PYJ_1_BLOCKHEADER header;
header = (PYJ_1_BLOCKHEADER)src;
src += 4;
if (!SWAP16(header->CompressedLength))
{
unsigned short hul = SWAP16(header->UncompressedLength);
while (hul--)
{
*dest++ = *src++;
}
continue;
}
src += 20;
bitptr = 0;
for (;;)
{
unsigned short loop;
if ((loop = get_loop(src, &bitptr, header)) == 0)
break;
while (loop--)
{
node = root;
for(; !node->leaf;)
{
if (get_bits(src, &bitptr, 1))
node = node->right;
else
node = node->left;
}
*dest++ = node->value;
}
if ((loop = get_loop(src, &bitptr, header)) == 0)
break;
while (loop--)
{
unsigned int pos, count;
count = get_count(src, &bitptr, header);
pos = get_bits(src, &bitptr, 2);
pos = get_bits(src, &bitptr, header->LZSSOffsetCodeLengthTable[pos]);
while (count--)
{
*dest = *(dest - pos);
dest++;
}
}
}
src = ((unsigned char *)header) + SWAP16(header->CompressedLength);
}
free(root);
return SWAP32(hdr->UncompressedLength);
}
#else
typedef struct _TreeNode
{
unsigned short weight;
unsigned short value;
struct _TreeNode* parent;
struct _TreeNode* left;
struct _TreeNode* right;
} TreeNode;
typedef struct _Tree
{
TreeNode* node;
TreeNode** list;
} Tree;
static unsigned char data1[0x100] =
{
0x3f,0x0b,0x17,0x03,0x2f,0x0a,0x16,0x00,0x2e,0x09,0x15,0x02,0x2d,0x01,0x08,0x00,
0x3e,0x07,0x14,0x03,0x2c,0x06,0x13,0x00,0x2b,0x05,0x12,0x02,0x2a,0x01,0x04,0x00,
0x3d,0x0b,0x11,0x03,0x29,0x0a,0x10,0x00,0x28,0x09,0x0f,0x02,0x27,0x01,0x08,0x00,
0x3c,0x07,0x0e,0x03,0x26,0x06,0x0d,0x00,0x25,0x05,0x0c,0x02,0x24,0x01,0x04,0x00,
0x3b,0x0b,0x17,0x03,0x23,0x0a,0x16,0x00,0x22,0x09,0x15,0x02,0x21,0x01,0x08,0x00,
0x3a,0x07,0x14,0x03,0x20,0x06,0x13,0x00,0x1f,0x05,0x12,0x02,0x1e,0x01,0x04,0x00,
0x39,0x0b,0x11,0x03,0x1d,0x0a,0x10,0x00,0x1c,0x09,0x0f,0x02,0x1b,0x01,0x08,0x00,
0x38,0x07,0x0e,0x03,0x1a,0x06,0x0d,0x00,0x19,0x05,0x0c,0x02,0x18,0x01,0x04,0x00,
0x37,0x0b,0x17,0x03,0x2f,0x0a,0x16,0x00,0x2e,0x09,0x15,0x02,0x2d,0x01,0x08,0x00,
0x36,0x07,0x14,0x03,0x2c,0x06,0x13,0x00,0x2b,0x05,0x12,0x02,0x2a,0x01,0x04,0x00,
0x35,0x0b,0x11,0x03,0x29,0x0a,0x10,0x00,0x28,0x09,0x0f,0x02,0x27,0x01,0x08,0x00,
0x34,0x07,0x0e,0x03,0x26,0x06,0x0d,0x00,0x25,0x05,0x0c,0x02,0x24,0x01,0x04,0x00,
0x33,0x0b,0x17,0x03,0x23,0x0a,0x16,0x00,0x22,0x09,0x15,0x02,0x21,0x01,0x08,0x00,
0x32,0x07,0x14,0x03,0x20,0x06,0x13,0x00,0x1f,0x05,0x12,0x02,0x1e,0x01,0x04,0x00,
0x31,0x0b,0x11,0x03,0x1d,0x0a,0x10,0x00,0x1c,0x09,0x0f,0x02,0x1b,0x01,0x08,0x00,
0x30,0x07,0x0e,0x03,0x1a,0x06,0x0d,0x00,0x19,0x05,0x0c,0x02,0x18,0x01,0x04,0x00
};
static unsigned char data2[0x10] =
{
0x08,0x05,0x06,0x04,0x07,0x05,0x06,0x03,0x07,0x05,0x06,0x04,0x07,0x04,0x05,0x03
};
static void adjust_tree(Tree tree, unsigned short value)
{
TreeNode* node = tree.list[value];
TreeNode tmp;
TreeNode* tmp1;
TreeNode* temp;
while(node->value != 0x280)
{
temp = node + 1;
while(node->weight == temp->weight)
temp++;
temp--;
if (temp != node)
{
tmp1 = node->parent;
node->parent = temp->parent;
temp->parent = tmp1;
if (node->value > 0x140)
{
node->left->parent = temp;
node->right->parent = temp;
}
else
tree.list[node->value] = temp;
if (temp->value > 0x140)
{
temp->left->parent = node;
temp->right->parent = node;
}
else
tree.list[temp->value] = node;
tmp = *node; *node = *temp; *temp = tmp;
node = temp;
}
node->weight++;
node = node->parent;
}
node->weight++;
}
static int build_tree(Tree *tree)
{
int i, ptr;
TreeNode** list;
TreeNode* node;
if ((tree->list = list = (TreeNode **)malloc(sizeof(TreeNode*) * 321)) == NULL)
return 0;
if ((tree->node = node = (TreeNode *)malloc(sizeof(TreeNode) * 641)) == NULL)
{
free(list);
return 0;
}
memset(list, 0, 321 * sizeof(TreeNode*));
memset(node, 0, 641 * sizeof(TreeNode));
for(i = 0; i <= 0x140; i++)
list[i] = node + i;
for(i = 0; i <= 0x280; i++)
{
node[i].value = i;
node[i].weight = 1;
}
tree->node[0x280].parent = tree->node + 0x280;
for(i = 0, ptr = 0x141; ptr <= 0x280; i += 2, ptr++)
{
node[ptr].left = node + i;
node[ptr].right = node + i + 1;
node[i].parent = node[i + 1].parent = node + ptr;
node[ptr].weight = node[i].weight + node[i + 1].weight;
}
return 1;
}
#pragma pack(1)
typedef struct _BitField
{
unsigned char b0: 1;
unsigned char b1: 1;
unsigned char b2: 1;
unsigned char b3: 1;
unsigned char b4: 1;
unsigned char b5: 1;
unsigned char b6: 1;
unsigned char b7: 1;
} BitField;
#pragma pack()
static int bt(const void* data, unsigned int pos)
{
BitField* bit = (BitField*)((unsigned char*)data + (pos >> 3));
switch(pos & 0x7)
{
case 0: return bit->b0;
case 1: return bit->b1;
case 2: return bit->b2;
case 3: return bit->b3;
case 4: return bit->b4;
case 5: return bit->b5;
case 6: return bit->b6;
case 7: return bit->b7;
}
return 0;
}
static void bit(void* data, unsigned int pos, int set)
{
BitField* bit = (BitField*)((unsigned char*)data + (pos >> 3));
switch(pos & 0x7)
{
case 0:
bit->b0 = set;
break;
case 1:
bit->b1 = set;
break;
case 2:
bit->b2 = set;
break;
case 3:
bit->b3 = set;
break;
case 4:
bit->b4 = set;
break;
case 5:
bit->b5 = set;
break;
case 6:
bit->b6 = set;
break;
case 7:
bit->b7 = set;
break;
}
}
INT
Decompress(
LPCVOID Source,
LPVOID Destination,
INT DestSize
)
{
unsigned int len = 0, ptr = 0, Length = 0;
unsigned char* src = (unsigned char*)Source + 4;
unsigned char* dest;
Tree tree;
TreeNode* node;
if (Source == NULL)
return -1;
if (!build_tree(&tree))
return -1;
Length = SWAP32(*((unsigned int*)Source));
if (Length > DestSize)
return -1;
dest = (unsigned char*)Destination;
while (1)
{
unsigned short val;
node = tree.node + 0x280;
while(node->value > 0x140)
{
if (bt(src, ptr))
node = node->right;
else
node = node->left;
ptr++;
}
val = node->value;
if (tree.node[0x280].weight == 0x8000)
{
int i;
for(i = 0; i < 0x141; i++)
if (tree.list[i]->weight & 0x1)
adjust_tree(tree, i);
for(i = 0; i <= 0x280; i++)
tree.node[i].weight >>= 1;
}
adjust_tree(tree, val);
if (val > 0xff)
{
int i;
unsigned int temp, tmp, pos;
unsigned char* pre;
for(i = 0, temp = 0; i < 8; i++, ptr++)
temp |= (unsigned int)bt(src, ptr) << i;
tmp = temp & 0xff;
for(; i < data2[tmp & 0xf] + 6; i++, ptr++)
temp |= (unsigned int)bt(src, ptr) << i;
temp >>= data2[tmp & 0xf];
pos = (temp & 0x3f) | ((unsigned int)data1[tmp] << 6);
if (pos == 0xfff)
break;
pre = dest - pos - 1;
for(i = 0; i < val - 0xfd; i++)
*dest++ = *pre++;
len += val - 0xfd;
}
else
{
*dest++ = (unsigned char)val;
len++;
}
}
free(tree.list);
free(tree.node);
return Length;
}
#endif