-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls11_decoder.cpp
151 lines (126 loc) · 3.41 KB
/
ls11_decoder.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
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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <vector>
#include "ls11_decoder.h"
uint8_t header[16];
uint8_t dict[256];
struct FAT_ENTRY
{
uint32_t comp_size;
uint32_t uncomp_size;
uint32_t offset;
};
std::vector<FAT_ENTRY> fat_entries;
uint32_t comp_pos = 0;
uint32_t uncomp_pos = 0;
int32_t bit_pos = 7;
static int get_code(uint8_t *comp, uint8_t *uncomp)
{
int code1 = 0;
int code2 = 0;
int count = 0;
int bit = 0;
do
{
bit = (comp[comp_pos] >> bit_pos) & 0x01;
code1 = (code1 << 1) | bit;
count++;
bit_pos--;
if (bit_pos < 0)
{
bit_pos = 7;
comp_pos++;
}
} while (bit == 1);
for (int i = 0; i < count; i++)
{
bit = (comp[comp_pos] >> bit_pos) & 0x01;
code2 = (code2 << 1) | bit;
bit_pos--;
if (bit_pos < 0)
{
bit_pos = 7;
comp_pos++;
}
}
return (code1 + code2);
}
static void fat_decode(uint8_t *comp, uint32_t comp_size, uint8_t *uncomp, uint32_t uncomp_size)
{
static const int MR = 3;
comp_pos = 0;
uncomp_pos = 0;
bit_pos = 7;
while (comp_pos < comp_size && uncomp_pos < uncomp_size)
{
int code = get_code(comp, uncomp);
if (code < 256)
{
uncomp[uncomp_pos] = dict[code];
uncomp_pos++;
}
else
{
int offset = code - 256;
int len = get_code(comp, uncomp) + MR;
for (int i = 0; i < len; i++)
{
uncomp[uncomp_pos] = uncomp[uncomp_pos - offset];
uncomp_pos++;
}
}
}
}
void ls11_decode(uint8_t *buf, size_t size, std::function<void(uint8_t *buf, uint32_t size)> on_decode)
{
if (!buf)
{
return;
}
buf_reader_t *reader = create_buffer_reader(buf, size, true);
fat_entries.clear();
read_bytes(reader, header, 16);
read_bytes(reader, dict, 256);
// Decode Fat
while (!buf_end(reader))
{
FAT_ENTRY fat_entry;
memset(&fat_entry, 0, sizeof(FAT_ENTRY));
// Default endian of reader is big endian.
// Values of fat_entry are also big endian.
fat_entry.comp_size = read_uint32(reader);
fat_entry.uncomp_size = read_uint32(reader);
fat_entry.offset = read_uint32(reader);
if (fat_entry.comp_size == 0)
{
break;
}
fat_entries.push_back(fat_entry);
}
// Decode Dat
for (int i = 0; i < fat_entries.size(); i++)
{
uint8_t *comp_data = new uint8_t[fat_entries[i].comp_size];
uint8_t *uncomp_data = new uint8_t[fat_entries[i].uncomp_size];
memset(comp_data, 0, fat_entries[i].comp_size);
memset(uncomp_data, 0, fat_entries[i].uncomp_size);
buf_seek(reader, fat_entries[i].offset);
read_bytes(reader, comp_data, fat_entries[i].comp_size);
if (fat_entries[i].comp_size == fat_entries[i].uncomp_size)
{
memcpy(uncomp_data, comp_data, fat_entries[i].comp_size);
}
else
{
fat_decode(comp_data, fat_entries[i].comp_size, uncomp_data, fat_entries[i].uncomp_size);
}
if (on_decode)
{
on_decode(uncomp_data, fat_entries[i].uncomp_size);
}
delete[] comp_data;
delete[] uncomp_data;
}
destroy_buffer_reader(reader);
}