-
Notifications
You must be signed in to change notification settings - Fork 29
/
mscompress.cpp
202 lines (159 loc) · 5.37 KB
/
mscompress.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
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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include <vector>
#include <compressapi.h>
#pragma comment(lib, "cabinet.lib")
//constexpr size_t BLOCK_SIZE = 1 * 1024 * 1024;
constexpr size_t BLOCK_SIZE = 4 * 1024 * 1024;
#if 1
struct compressor_handle_t
{
COMPRESSOR_HANDLE handle;
compressor_handle_t()
{
auto ret = CreateCompressor(COMPRESS_ALGORITHM_LZMS, NULL, &handle);
assert(ret);
DWORD blocksize = BLOCK_SIZE;
SetCompressorInformation(handle, COMPRESS_INFORMATION_CLASS_BLOCK_SIZE, &blocksize, sizeof(blocksize));
}
~compressor_handle_t()
{
CloseCompressor(handle);
handle = NULL;
}
operator COMPRESSOR_HANDLE ()
{
return handle;
}
};
static void mscompress(const std::vector<BYTE>& inbuf, std::vector<BYTE>& outbuf)
{
static compressor_handle_t handle;
// guesswork
outbuf.resize(inbuf.size() + 0x1000);
SIZE_T compressed = 0;
auto ret = Compress(handle, inbuf.data(), inbuf.size(), outbuf.data(), outbuf.size(), &compressed);
outbuf.resize(compressed);
assert(ret && compressed != 0);
}
#else // wimlib produces smaller results, but (obviously) needs a copy of wimlib
#pragma warning(push)
#pragma warning(disable: 4200) // 0-size array in struct
#include "wimlib/wimlib.h"
#pragma warning(pop)
#pragma comment(lib, "ntdll.lib")
#pragma comment(lib, "wimlib/libwim.lib")
// 200 is pretty close to the hard limit
// https://github.com/ebiggers/wimlib/blob/v1.14.3/src/lzms_compress.c#L2141-L2146
constexpr unsigned int COMPRESSION_LEVEL = 200;
extern "C"
DWORD WINAPI RtlComputeCrc32(DWORD dwInitial, const BYTE* pData, INT iLen);
struct COMPRESS_BUFFER_HEADER {
DWORD dwMagic; // = 0xC0E5510A
WORD wHeaderSize; // = 24
BYTE bHeaderCrc; // LSB of RtlComputeCrc32 of whole hdr, skipping this byte
BYTE bAlgorithm; // LSB of COMPRESS_ALGORITHM_*; LZMS = 5
DWORD64 qwUncompressedDataSize;
DWORD64 qwUncompressedBlockSize; // <= 0x4000000; Unrelated to the LZMS setting
};
static_assert(sizeof(COMPRESS_BUFFER_HEADER) == 24, "sizeof(COMPRESS_BUFFER_HEADER)");
struct compressor_handle_t
{
wimlib_compressor* handle;
compressor_handle_t()
{
auto ret = wimlib_create_compressor(WIMLIB_COMPRESSION_TYPE_LZMS, BLOCK_SIZE, COMPRESSION_LEVEL, &handle);
assert(ret == 0);
}
~compressor_handle_t()
{
wimlib_free_compressor(handle);
handle = nullptr;
}
operator wimlib_compressor* ()
{
return handle;
}
};
static void mscompress(const std::vector<BYTE>& inbuf, std::vector<BYTE>& outbuf)
{
static compressor_handle_t handle;
static std::vector<BYTE> workspace;
if (workspace.size() == 0)
{
auto worksize = wimlib_get_compressor_needed_memory(WIMLIB_COMPRESSION_TYPE_LZMS, BLOCK_SIZE, COMPRESSION_LEVEL);
workspace.resize(worksize);
}
// guesswork
outbuf.resize(inbuf.size() + 0x1000);
auto hdr = (COMPRESS_BUFFER_HEADER*)outbuf.data();
hdr->dwMagic = 0xC0E5510A;
hdr->wHeaderSize = sizeof(COMPRESS_BUFFER_HEADER);
//hdr->bHeaderCrc = 0; // temp value
hdr->bAlgorithm = COMPRESS_ALGORITHM_LZMS;
hdr->qwUncompressedDataSize = inbuf.size();
hdr->qwUncompressedBlockSize = BLOCK_SIZE;
auto crcoff = offsetof(COMPRESS_BUFFER_HEADER, bHeaderCrc);
auto crc = RtlComputeCrc32(0, outbuf.data(), (INT)crcoff);
crc = RtlComputeCrc32(crc, outbuf.data() + crcoff + 1, (INT)(sizeof(COMPRESS_BUFFER_HEADER) - crcoff - 1));
hdr->bHeaderCrc = (BYTE)crc;
size_t outoff = sizeof(COMPRESS_BUFFER_HEADER);
for (size_t inoff = 0; inoff < inbuf.size(); inoff += BLOCK_SIZE)
{
auto compressed = wimlib_compress(&inbuf[inoff], min(inbuf.size() - inoff, BLOCK_SIZE), &outbuf[outoff] + 4, outbuf.size() - outoff - 4, handle);
assert(compressed != 0);
*(DWORD*)&outbuf[outoff] = (DWORD)compressed;
outoff += 4 + compressed;
}
outbuf.resize(outoff);
}
#endif
int wmain(int argc, wchar_t* argv[])
{
if (argc != 3)
{
fwprintf(stderr, L"usage: %s <in> <out>\n", argv[0]);
return 0;
}
FILE* fp = NULL;
_wfopen_s(&fp, argv[1], L"rb");
if (!fp)
{
return 1;
}
size_t insize;
fseek(fp, 0, SEEK_END);
insize = ftell(fp);
fseek(fp, 0, SEEK_SET);
wprintf(L"insize = %zu\n", insize);
std::vector<BYTE> inbuf(insize);
if (fread(inbuf.data(), 1, inbuf.size(), fp) != insize)
{
return 1;
}
fclose(fp);
// ---
std::vector<BYTE> outbuf;
mscompress(inbuf, outbuf);
wprintf(L"outsize = %zu %5.2f%%\n", outbuf.size(), (100.0 * outbuf.size() / inbuf.size()));
// ---
_wfopen_s(&fp, argv[2], L"wb");
if (!fp)
{
return 1;
}
if (fwrite(outbuf.data(), 1, outbuf.size(), fp) != outbuf.size())
{
return 1;
}
// ---
DECOMPRESSOR_HANDLE handle;
auto ret = CreateDecompressor(COMPRESS_ALGORITHM_LZMS, NULL, &handle);
assert(ret);
ret = Decompress(handle, outbuf.data(), outbuf.size(), inbuf.data(), inbuf.size(), &insize);
assert(ret && insize == inbuf.size());
CloseDecompressor(handle);
return 0;
}