forked from Fluorohydride/threp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
93 lines (89 loc) · 2.19 KB
/
common.cpp
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
/*
* common.cpp
*
* Created on: 2010-5-9
* Author: Argon
*/
#include "common.h"
#include <memory>
#include <cstring>
unsigned int get_bit(unsigned char* buffer, unsigned int &pointer, unsigned char &filter, unsigned char length) {
unsigned char i;
unsigned int result = 0;
unsigned char current;
current = buffer[pointer];
for (i = 0; i < length; ++i) {
result <<= 1;
if (current & filter)
result |= 0x1;
filter >>= 1;
if (filter == 0) {
pointer++;
current = buffer[pointer];
filter = 0x80;
}
}
return result;
}
unsigned int decompress(unsigned char * buffer, unsigned char * decode, unsigned int length) {
unsigned int pointer = 0, dest = 0, index, bits, i;
unsigned char filter = 0x80;
unsigned char dict[0x2010];
memset(dict, 0, 0x2010);
while (pointer < length) {
bits = get_bit(buffer, pointer, filter, 1);
if (pointer >= length)
return dest;
if (bits) {
bits = get_bit(buffer, pointer, filter, 8);
if (pointer >= length)
return dest;
decode[dest] = (unsigned char) bits;
dict[dest & 0x1fff] = (unsigned char) bits;
dest++;
} else {
bits = get_bit(buffer, pointer, filter, 13);
if (pointer >= length)
return dest;
index = bits - 1;
bits = get_bit(buffer, pointer, filter, 4);
if (pointer >= length)
return dest;
bits += 3;
for (i = 0; i < bits; ++i) {
dict[dest & 0x1fff] = dict[index + i];
decode[dest] = dict[index + i];
dest++;
}
}
}
return dest;
}
void decode(unsigned char * buffer, int length, int block_size, unsigned char base, unsigned char add) {
unsigned char * tbuf = new unsigned char[length];
memcpy(tbuf, buffer, length);
int i, p = 0, tp1, tp2, hf, left = length;
if ((left % block_size) < (block_size / 4))
left -= left % block_size;
left -= length & 1;
while (left) {
if (left < block_size)
block_size = left;
tp1 = p + block_size - 1;
tp2 = p + block_size - 2;
hf = (block_size + (block_size & 0x1)) / 2;
for (i = 0; i < hf; ++i, ++p) {
buffer[tp1] = tbuf[p] ^ base;
base += add;
tp1 -= 2;
}
hf = block_size / 2;
for (i = 0; i < hf; ++i, ++p) {
buffer[tp2] = tbuf[p] ^ base;
base += add;
tp2 -= 2;
}
left -= block_size;
}
delete[] tbuf;
}