-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblz.cpp
More file actions
230 lines (195 loc) · 4.98 KB
/
Copy pathblz.cpp
File metadata and controls
230 lines (195 loc) · 4.98 KB
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
#include "blz.hpp"
#include "common.h"
#include <stdexcept>
#include <cstring>
static const char* SRC_SHORTAGE = "Source shortage.";
static const char* DEST_OVERRUN = "Destination overrun.";
static int CompressBackward_sub2(const u8 *a1, const u8 *a2, int a3)
{
int i;
for (i = 0; i < a3 && *a1 == *a2; ++i)
{
--a1;
--a2;
}
return i;
}
static int CompressBackward_sub1(const u8 *a1, int a2, const u8 *a3, int a4, int& a5)
{
u8 v10 = a1[a2 - 1];
int v7 = 0;
for (int i = 0; i < a4; ++i)
{
if (a3[i] == v10)
{
int v6 = i + 1;
if (i + 1 > a2)
v6 = a2;
int v8 = CompressBackward_sub2(&a1[a2 - 1], &a3[i], v6);
if (v7 < v8)
{
v7 = v8;
a5 = i;
}
}
}
return v7;
}
/**
* @brief Compress module data.
*
* @param src Pointer to input data begin.
* @param size Size of the input data.
* @param dst Pointer to output data begin.
*
* @return The number of compressed bytes. (in_size - out_size)
*/
static size_t CompressBackward(const void *src, size_t size, void *dst)
{
const u8* src_ = reinterpret_cast<const u8*>(src);
u8* dst_ = reinterpret_cast<u8*>(dst);
size_t v13 = size;
size_t v12 = size;
while (v13 > 0)
{
if (v12 <= 0)
return -1;
int v11 = 0;
int v10 = --v12;
for (int i = 0; i <= 7; ++i)
{
v11 *= 2;
if (v13 > 0)
{
const u8* v9 = &src_[v13];
int v8 = size - v13;
int v2 = v13;
if (v13 > 18)
v2 = 18;
int v6 = v2;
const u8* v7 = &v9[-v2];
int v1 = v8;
if (v8 > 4098)
v1 = 4098;
int v5;
int v4 = CompressBackward_sub1(v7, v6, v9, v1, v5);
if (v4 <= 2)
{
if (v12 <= 0)
return -1;
dst_[--v12] = src_[--v13];
}
else
{
if (v12 <= 1)
return -1;
v13 -= v4;
v5 -= 2;
u16 v3 = (v5 & 0xFFF) | (((u16)v4 - 3) << 12);
--v12;
dst_[v12--] = v3 >> 8;
dst_[v12] = v3;
v11 |= 1;
}
}
}
dst_[v10] = v11;
}
return v12;
}
/**
* @brief Uncompress module data.
*
* @param bottom Pointer to input data end.
*/
static void UncompressBackward(void* bottom)
{
u32 offsetOut = readU32(static_cast<u8*>(bottom) - 4);
u32 offsetIn = readU32(static_cast<u8*>(bottom) - 8);
u32 offsetInBtm = offsetIn >> 24;
u32 offsetInTop = offsetIn & 0xFFFFFF;
u8* pOut = reinterpret_cast<u8*>(bottom) + offsetOut;
u8* pInBtm = reinterpret_cast<u8*>(bottom) - offsetInBtm;
u8* pInTop = reinterpret_cast<u8*>(bottom) - offsetInTop;
while (pInTop < pInBtm)
{
u8 flag = *--pInBtm;
for (int i = 0; i < 8; ++i)
{
if (pInBtm < pInTop)
throw std::runtime_error(SRC_SHORTAGE);
if (pOut < pInTop)
throw std::runtime_error(DEST_OVERRUN);
if (!(flag & 0x80))
{
*--pOut = *--pInBtm;
}
else
{
if (pInBtm - 2 < pInTop)
throw std::runtime_error(DEST_OVERRUN);
u32 length = *--pInBtm;
u32 offset = (((length & 0xF) << 8) | (*--pInBtm)) + 3;
length = (length >> 4) + 3;
u8* pTmp = pOut + offset;
if (pOut - length < pInTop)
throw std::runtime_error(DEST_OVERRUN);
for (unsigned j = 0; j < length; ++j)
*--pOut = *--pTmp;
}
if (pInBtm <= pInTop)
break;
flag <<= 1;
}
}
}
namespace BLZ
{
std::vector<u8> compress(const std::vector<u8>& data, u8 padding)
{
const size_t dataSize = data.size();
std::vector<u8> dest(dataSize);
const size_t reduction = CompressBackward(data.data(), dataSize, dest.data());
if (reduction == (size_t)-1)
throw std::runtime_error("compression failed");
const ptrdiff_t destSize = dest.size() - reduction;
std::memmove(dest.data(), dest.data() + reduction, destSize);
const size_t paddingSize = -destSize & 3;
const size_t footerOffset = destSize + paddingSize;
dest.resize(footerOffset + 8);
std::memset(dest.data() + destSize, padding, paddingSize);
// full compressed size, including padding and footer
const size_t offset1 = dest.size();
// (uncompressed size) - (full compressed size, including padding and footer)
const size_t offset2 = reduction - paddingSize - 8;
dest[footerOffset] = offset1 & 0xff;
dest[footerOffset + 1] = (offset1 >> 8) & 0xff;
dest[footerOffset + 2] = (offset1 >> 16) & 0xff;
dest[footerOffset + 3] = paddingSize + 8; // size of footer + padding
dest[footerOffset + 4] = offset2 & 0xff;
dest[footerOffset + 5] = (offset2 >> 8) & 0xff;
dest[footerOffset + 6] = (offset2 >> 16) & 0xff;
dest[footerOffset + 7] = (offset2 >> 24) & 0xff;
return dest;
}
std::vector<u8> uncompress(const std::vector<u8>& data)
{
size_t dataSize = data.size();
u32 destSize = dataSize + readU32(&data[dataSize - 4]);
std::vector<u8> dest(destSize);
std::copy(data.begin(), data.end(), dest.begin());
UncompressBackward(dest.data() + dataSize);
return dest;
}
void uncompressInplace(std::vector<u8>& data)
{
size_t dataSize = data.size();
u32 destSize = dataSize + readU32(&data[dataSize - 4]);
data.resize(destSize);
UncompressBackward(data.data() + dataSize);
}
void uncompressInplace(u8* data_end)
{
UncompressBackward(data_end);
}
}