-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSource.cpp
More file actions
353 lines (323 loc) · 8.72 KB
/
Source.cpp
File metadata and controls
353 lines (323 loc) · 8.72 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdint>
#include <memory>
#include <intrin.h>
#include <unordered_map>
#include <string>
#include <direct.h>
#include <Windows.h>
#include <algorithm>
#include "lzhuf.h"
#include "mm3filelist.h"
void decryptHeader(uint8_t* buf, size_t size)
{
uint8_t key = 0xAC;
for (size_t i = 0; i < size; i++)
{
buf[i] = _rotl8(buf[i], 2) + key;
key += 0x67;
}
}
void encryptHeader(uint8_t* buf, size_t size)
{
uint8_t key = 0xAC;
for (size_t i = 0; i < size; i++)
{
buf[i] = _rotr8(buf[i] - key, 2);
key += 0x67;
}
}
uint16_t hashFileName(const char* fileName)
{
uint16_t hash = 0;
while (0 != *fileName)
{
uint8_t c = ((*fileName & 0x7F) < 0x60) ? *fileName : *fileName - 0x20;
hash = _rotl16(hash, 9); // xchg bl, bh | rol bx, 1
hash += c;
fileName++;
}
return hash;
}
#pragma pack(push, 1)
struct FileEntry
{
uint16_t hash;
uint16_t offsetLo;
uint8_t offsetHi;
uint16_t compressedSize;
uint8_t padding;
FileEntry(uint16_t hash, uint32_t offset, uint16_t compressedSize) :
hash(hash), offsetLo(offset & 0xFFFF), offsetHi((offset >> 16) & 0xFF), compressedSize(compressedSize), padding(0) {}
uint32_t getOffset() const { return (offsetHi << 16) | offsetLo; }
void setOffset(uint32_t off) { offsetHi = (off >> 16) & 0xFF; offsetLo = off & 0xFFFF; }
};
#pragma pack(pop)
std::unordered_map<uint16_t, std::string> hashToName;
void initFNames()
{
for (int i = 0; i < _countof(filenames); i++)
{
hashToName[hashFileName(filenames[i])] = filenames[i];
}
}
void saveFile(uint16_t hash, uint8_t* buf, size_t size, char* nameHint = nullptr)
{
char fname[0x40];
auto r = hashToName.find(hash);
if (r != hashToName.end())
{
sprintf(fname, "out\\%s", r->second.c_str());
}
else if (nullptr != nameHint)
{
sprintf(fname, "out\\%s_%04X.BIN", nameHint, hash);
}
else
sprintf(fname, "out\\_UNKNOWN_FILE_%04X.BIN", hash);
printf("%s\n", fname);
FILE* f = fopen(fname, "wb");
if (nullptr == f)
{
printf("Can't create file: %s\n", fname);
return;
}
fwrite(buf, 1, size, f);
fclose(f);
}
uint8_t statFile(uint8_t* buf, size_t size)
{
int stats[0x100] = { 0 };
int max = 0;
uint8_t maxC = 0;
for (size_t i = 0; i < size; i++)
{
if ((buf[i] != 0) && (stats[buf[i]]++ > max))
{
max = stats[buf[i]];
maxC = buf[i];
}
}
return maxC;
}
uint8_t* readFile(const char* path, size_t& fSize)
{
FILE* f = fopen(path, "rb");
if (nullptr == f)
{
printf("Can't open: %s\n", path);
return nullptr;
}
fseek(f, 0, SEEK_END);
fSize = ftell(f);
uint8_t* buf = (uint8_t*)malloc(fSize);
if (nullptr == buf)
{
fclose(f);
printf("File too big to fit in memory buffer ?\n");
return nullptr;
}
fseek(f, 0, SEEK_SET);
fread(buf, 1, fSize, f);
fclose(f);
return buf;
}
void dump(char* mm3ccPath)
{
initFNames();
size_t fSize;
uint8_t* buf = readFile(mm3ccPath, fSize);
if (nullptr == buf)
return;
size_t headerSize = (*(uint16_t*)buf) << 3;
if (headerSize > fSize)
{
printf("Invalid header size.\n");
free(buf);
return;
}
decryptHeader(buf + 2, headerSize);
_mkdir("out");
FileEntry* entries = (FileEntry*)(buf + 2);
// first two files are special, not compressed, those are two 0-ended strings
for (size_t i = 0; i < headerSize / sizeof(FileEntry); i++)
{
size_t offset = entries[i].offsetHi << 16 | entries[i].offsetLo;
uint16_t w1 = *(uint16_t*)(buf + offset);
uint16_t w2 = _rotl16(*(uint16_t*)(buf + offset + 2), 8);
printf(": %04X: %08X, %04X, %02X, %04X, %04X : ", entries[i].hash, offset, entries[i].compressedSize, entries[i].padding, w2, w1);
if (offset + entries[i].compressedSize > fSize)
{
printf("invalid entry, skipping...\n");
continue;
}
if (((w1 & 0xFF) == w1 >> 8) && (i >= 2))
{
// no need to check malloc result against nullptr, as it should be always possible to allocate 65k memory
uint8_t* out = (uint8_t*)malloc(w2 + 0x100);
/*size_t retSize = */rwf_lzhuf_decompress(buf + offset + 4, entries[i].compressedSize - 4, out, w2, (uint8_t)w1);
saveFile(entries[i].hash, out, w2);
free(out);
}
else
{
// not compressed, just dump
char tmp[0x20];
sprintf(tmp, "_NOT_COMPRESSED_%04X", i);
saveFile(entries[i].hash, buf + offset, entries[i].compressedSize, tmp);
}
}
}
bool createFileList(char* path, std::vector<std::pair<std::string, size_t>>& fileList)
{
WIN32_FIND_DATA wfd;
std::string sp = path;
sp += "\\*";
HANDLE hFind = FindFirstFile(sp.c_str(), &wfd);
if (INVALID_HANDLE_VALUE == hFind)
{
printf("Can't find any file: %s\n", path);
return false;
}
do
{
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
fileList.emplace_back(std::pair<std::string, size_t>(wfd.cFileName, wfd.nFileSizeLow));
}
}
while (FindNextFile(hFind, &wfd) != 0);
FindClose(hFind);
return true;
}
void pack(char* path, char* outFileName)
{
printf("Obtaining the list of files... ");
std::vector<std::pair<std::string, size_t>> fileList;
if (!createFileList(path, fileList))
return;
printf("[ %d files ]\n", fileList.size());
std::vector<uint8_t> filesBuffer;
std::vector<FileEntry> entries;
auto it = std::remove_if(fileList.begin(), fileList.end(), [&entries, &filesBuffer, path](const std::pair<std::string, size_t>& arg)
{
bool ret = false;
if (0 == arg.first.compare(0, strlen("_NOT_COMPRESSED_"), "_NOT_COMPRESSED_"))
{
std::string filePath = path;
filePath += "\\" + arg.first;
size_t fSize;
printf("Adding %-40s", filePath.c_str());
uint8_t* fbuf = readFile(filePath.c_str(), fSize);
if (nullptr == fbuf)
return false;
size_t curOff = filesBuffer.size();
filesBuffer.resize(curOff + fSize);
memcpy(filesBuffer.data() + curOff, fbuf, fSize);
free(fbuf);
printf("[ s: %5d -> ]\n", fSize);
uint32_t hash;
if (1 == sscanf(arg.first.c_str(), "_NOT_COMPRESSED_0000_%04X.BIN", &hash))
{
// put it as a first entry
entries.emplace(entries.begin(), FileEntry((uint16_t)hash, curOff, (uint16_t)arg.second));
ret = true;
}
else if (1 == sscanf(arg.first.c_str(), "_NOT_COMPRESSED_0001_%04X.BIN", &hash))
{
// put it as a second entry
entries.emplace_back(FileEntry((uint16_t)hash, curOff, (uint16_t)arg.second));
ret = true;
}
}
return ret;
});
fileList.erase(it, fileList.end());
for (auto& f : fileList)
{
std::string filePath = path;
filePath += "\\" + f.first;
size_t fSize;
printf("Adding %-40s", filePath.c_str());
uint8_t* fbuf = readFile(filePath.c_str(), fSize);
if (nullptr == fbuf)
return;
printf("[ s: %5d -> ", fSize);
if (fSize > 0xFFFF)
{
printf("file too big ]\n");
free(fbuf);
continue;
}
// +0x100 to not crash on really small files
uint8_t* compressed = (uint8_t*)malloc(fSize + 0x100);
if (nullptr == compressed)
{
printf("Can't allocate memory for compressed data.\n");
free(fbuf);
return;
}
uint8_t iv = statFile(fbuf, fSize);
size_t compSize = lzhuf_compress(fbuf, fSize, compressed, fSize + 0x100, iv);
free(fbuf);
size_t curOff = filesBuffer.size();
filesBuffer.resize(curOff + compSize + 4);
filesBuffer[curOff] = iv;
filesBuffer[curOff + 1] = iv;
filesBuffer[curOff + 2] = (fSize >> 8) & 0xFF;
filesBuffer[curOff + 3] = fSize & 0xFF;
memcpy(filesBuffer.data() + curOff + 4, compressed, compSize);
free(compressed);
printf("%5d ]\n", compSize);
uint32_t hash;
if (1 == sscanf(f.first.c_str(), "_UNKNOWN_FILE_%04X.BIN", &hash))
{
entries.emplace_back(FileEntry((uint16_t)hash, curOff, (uint16_t)compSize + 4));
}
else
{
entries.emplace_back(FileEntry(hashFileName(f.first.c_str()), curOff, (uint16_t)compSize + 4));
}
}
printf("Fixing offsets...\n");
for (auto& e : entries)
e.setOffset(e.getOffset() + sizeof(uint16_t) + sizeof(FileEntry)*entries.size());
printf("Encrypting the header...\n");
encryptHeader((uint8_t*)entries.data(), sizeof(FileEntry)*entries.size());
printf("Writing %s...\n", outFileName);
FILE* f = fopen(outFileName, "wb");
if (nullptr == f)
{
printf("Can't create file: %s\n", outFileName);
return;
}
uint16_t numOfEntries = (uint16_t)entries.size();
fwrite(&numOfEntries, sizeof(uint16_t), 1, f);
fwrite(entries.data(), sizeof(FileEntry), numOfEntries, f);
fwrite(filesBuffer.data(), 1, filesBuffer.size(), f);
fclose(f);
printf("Done.\n");
}
int main(int argc, char *argv[])
{
printf("\nMight and Magic III CC file packer/unpacker v1.0\n");
printf("Copyright (c) 2015 ReWolf\n");
printf("http://blog.rewolf.pl\n");
printf("rewolf [at] rewolf.pl\n\n");
if ((argc == 3) && (0 == strcmp(argv[1], "dump")))
{
dump(argv[2]);
}
else if ((argc == 4) && (0 == strcmp(argv[1], "pack")))
{
pack(argv[2], argv[3]);
}
else
{
printf("Usage:\n\n");
printf("Unpack:\t%s dump input_file.cc\n", argv[0]);
printf("Pack:\t%s pack input_directory output_file.cc\n", argv[0]);
}
return 0;
}