This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
unpack.c
98 lines (90 loc) · 2.12 KB
/
unpack.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
#include <stdint.h>
struct UnpackCtx {
int size;
uint32_t crc;
uint32_t bits;
uint8_t *dst;
const uint8_t *src;
};
static uint32_t READ_BE_UINT32(const uint8_t *b) {
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
}
static int nextBit(struct UnpackCtx *uc) {
int carry = (uc->bits & 1) != 0;
uc->bits >>= 1;
if (uc->bits == 0) { // getnextlwd
uc->bits = READ_BE_UINT32(uc->src); uc->src -= 4;
uc->crc ^= uc->bits;
carry = (uc->bits & 1) != 0;
uc->bits = (1 << 31) | (uc->bits >> 1);
}
return carry;
}
static int getBits(struct UnpackCtx *uc, int count) { // rdd1bits
int bits = 0;
for (int i = 0; i < count; ++i) {
bits |= nextBit(uc) << (count - 1 - i);
}
return bits;
}
static void copyLiteral(struct UnpackCtx *uc, int bitsCount, int len) { // getd3chr
int count = getBits(uc, bitsCount) + len + 1;
uc->size -= count;
if (uc->size < 0) {
count += uc->size;
uc->size = 0;
}
for (int i = 0; i < count; ++i) {
*(uc->dst - i) = (uint8_t)getBits(uc, 8);
}
uc->dst -= count;
}
static void copyReference(struct UnpackCtx *uc, int bitsCount, int count) { // copyd3bytes
uc->size -= count;
if (uc->size < 0) {
count += uc->size;
uc->size = 0;
}
const int offset = getBits(uc, bitsCount);
for (int i = 0; i < count; ++i) {
*(uc->dst - i) = *(uc->dst - i + offset);
}
uc->dst -= count;
}
uint32_t bytekiller_unpack(uint8_t *dst, int dstSize, const uint8_t *src, int srcSize) {
struct UnpackCtx uc;
uc.src = src + srcSize - 4;
uc.size = READ_BE_UINT32(uc.src); uc.src -= 4;
if (uc.size > dstSize) {
return 0;
}
uc.dst = dst + uc.size - 1;
uc.crc = READ_BE_UINT32(uc.src); uc.src -= 4;
uc.bits = READ_BE_UINT32(uc.src); uc.src -= 4;
uc.crc ^= uc.bits;
do {
if (!nextBit(&uc)) {
if (!nextBit(&uc)) {
copyLiteral(&uc, 3, 0);
} else {
copyReference(&uc, 8, 2);
}
} else {
switch (getBits(&uc, 2)) {
case 3:
copyLiteral(&uc, 8, 8);
break;
case 2:
copyReference(&uc, 12, getBits(&uc, 8) + 1);
break;
case 1:
copyReference(&uc, 10, 4);
break;
case 0:
copyReference(&uc, 9, 3);
break;
}
}
} while (uc.size > 0);
return uc.crc;
}