#include <cstdio>
#include <cstdlib>
typedef unsigned char BYTE;
typedef BYTE* LPBYTE;
typedef int INT;
//------------------------------------------------------------
// File : test.bin
// Address : 0 (0x0)
// Size : 154 (0x9A)
//------------------------------------------------------------
unsigned char comprData[154] = {
0x00, 0x54, 0x49, 0x54, 0x4C, 0x45, 0x3D, 0x4B, 0x41, 0x00, 0x4D, 0x49, 0x53, 0x48, 0x49, 0x50,
0x50, 0x4F, 0x00, 0x0D, 0x0A, 0x4D, 0x4F, 0x44, 0x45, 0x3D, 0x52, 0x00, 0x45, 0x54, 0x41, 0x49,
0x4E, 0x0D, 0x0A, 0x56, 0x00, 0x45, 0x52, 0x3D, 0x31, 0x2E, 0x30, 0x30, 0x0D, 0x00, 0x0A, 0x52,
0x45, 0x47, 0x49, 0x53, 0x54, 0x52, 0x00, 0x59, 0x3D, 0x44, 0x45, 0x53, 0x53, 0x45, 0x52, 0x02,
0x54, 0x53, 0x4F, 0x46, 0x54, 0x5C, 0x38, 0x09, 0x53, 0x03, 0x41, 0x56, 0x45, 0x44, 0x49, 0x52,
0x20, 0x0F, 0x58, 0x04, 0x00, 0x44, 0x49, 0x53, 0x50, 0x4C, 0x41, 0x59, 0x3D, 0x00, 0xE7, 0xA5,
0x9E, 0xE6, 0xA7, 0x98, 0xE3, 0x81, 0x00, 0xAE, 0xE3, 0x81, 0x97, 0xE3, 0x81, 0xA3, 0xE3, 0x00,
0x81, 0xBD, 0xEF, 0xBD, 0x9E, 0xE5, 0xB9, 0xB2, 0x10, 0xE6, 0x94, 0xAF, 0x1B, 0x00, 0xE3, 0x81,
0x95, 0xE3, 0x00, 0x81, 0xBE, 0xE3, 0x81, 0x9F, 0xE3, 0x81, 0xA1, 0x81, 0x24, 0x00, 0xE6, 0x81,
0xA9, 0xE8, 0xBF, 0x94, 0x2A, 0x00, 0x00, 0xEF, 0xBD, 0x9E
};
int main()
{
LPBYTE comprBuf = comprData;
INT comprPos = 0;
LPBYTE decomprBuf = (LPBYTE)malloc(0x10000);
INT decomprPos = 0;
// 读取第一个FLAG
BYTE flag1 = comprBuf[comprPos++];
BYTE flag2 = 0xff;
while (1)
{
// 0xAB 是解压后的长度
// 也可以通过判断 comprPos 来跳出循环
if (decomprPos == 0xAB)
break;
// 判断FLAG的每个位
if ((flag1 & 0x80) == 0)
{
// 为0则直接输出
decomprBuf[decomprPos++] = comprBuf[comprPos++];
}
else
{
BYTE v7 = comprBuf[comprPos++];
BYTE v8 = comprBuf[comprPos++]; // match count
int offset = v7 | 0x10 * (v8 & 0xf0);
int length = (v8 & 0xf) + 3;
if (!offset)
offset = 0x1000;
int copy_pos = decomprPos - offset;
for (int i = 0; i < length; i++)
decomprBuf[decomprPos++] = decomprBuf[copy_pos++];
}
flag1 <<= 1;
flag2 <<= 1;
if (!flag2)
{
// 读取下一个FLAG
flag1 = comprBuf[comprPos++];
flag2 = 0xff;
}
}
auto fp = fopen("out.txt", "wb");
fwrite(decomprBuf, decomprPos, 1, fp);
fclose(fp);
return 0;
}