-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmcmp.cpp
406 lines (363 loc) · 9.2 KB
/
mmcmp.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* This source code is public domain.
*
* Authors: Olivier Lapicque <[email protected]>
*/
#include "stdafx.h"
#include "sndfile.h"
BOOL PP20_Unpack(LPCBYTE *ppMemFile, LPDWORD pdwMemLength);
typedef struct MMCMPFILEHEADER
{
DWORD id_ziRC; // "ziRC"
DWORD id_ONia; // "ONia"
WORD hdrsize;
} MMCMPFILEHEADER, *LPMMCMPFILEHEADER;
typedef struct MMCMPHEADER
{
WORD version;
WORD nblocks;
DWORD filesize;
DWORD blktable;
BYTE glb_comp;
BYTE fmt_comp;
} MMCMPHEADER, *LPMMCMPHEADER;
typedef struct MMCMPBLOCK
{
DWORD unpk_size;
DWORD pk_size;
DWORD xor_chk;
WORD sub_blk;
WORD flags;
WORD tt_entries;
WORD num_bits;
} MMCMPBLOCK, *LPMMCMPBLOCK;
typedef struct MMCMPSUBBLOCK
{
DWORD unpk_pos;
DWORD unpk_size;
} MMCMPSUBBLOCK, *LPMMCMPSUBBLOCK;
#define MMCMP_COMP 0x0001
#define MMCMP_DELTA 0x0002
#define MMCMP_16BIT 0x0004
#define MMCMP_STEREO 0x0100
#define MMCMP_ABS16 0x0200
#define MMCMP_ENDIAN 0x0400
typedef struct MMCMPBITBUFFER
{
UINT bitcount;
DWORD bitbuffer;
LPCBYTE pSrc;
LPCBYTE pEnd;
DWORD GetBits(UINT nBits);
} MMCMPBITBUFFER;
DWORD MMCMPBITBUFFER::GetBits(UINT nBits)
//---------------------------------------
{
DWORD d;
if (!nBits) return 0;
while (bitcount < 24)
{
bitbuffer |= ((pSrc < pEnd) ? *pSrc++ : 0) << bitcount;
bitcount += 8;
}
d = bitbuffer & ((1 << nBits) - 1);
bitbuffer >>= nBits;
bitcount -= nBits;
return d;
}
//#define MMCMP_LOG
#ifdef MMCMP_LOG
extern void Log(LPCSTR s, ...);
#endif
const DWORD MMCMP8BitCommands[8] =
{
0x01, 0x03, 0x07, 0x0F, 0x1E, 0x3C, 0x78, 0xF8
};
const UINT MMCMP8BitFetch[8] =
{
3, 3, 3, 3, 2, 1, 0, 0
};
const DWORD MMCMP16BitCommands[16] =
{
0x01, 0x03, 0x07, 0x0F, 0x1E, 0x3C, 0x78, 0xF0,
0x1F0, 0x3F0, 0x7F0, 0xFF0, 0x1FF0, 0x3FF0, 0x7FF0, 0xFFF0
};
const UINT MMCMP16BitFetch[16] =
{
4, 4, 4, 4, 3, 2, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
BOOL MMCMP_Unpack(LPCBYTE *ppMemFile, LPDWORD pdwMemLength)
//---------------------------------------------------------
{
DWORD dwMemLength = *pdwMemLength;
LPCBYTE lpMemFile = *ppMemFile;
LPBYTE pBuffer;
LPMMCMPFILEHEADER pmfh = (LPMMCMPFILEHEADER)(lpMemFile);
LPMMCMPHEADER pmmh = (LPMMCMPHEADER)(lpMemFile+10);
LPDWORD pblk_table;
DWORD dwFileSize;
if (PP20_Unpack(ppMemFile, pdwMemLength))
{
return TRUE;
}
if ((dwMemLength < 256) || (!pmfh) || (pmfh->id_ziRC != 0x4352697A) || (pmfh->id_ONia != 0x61694e4f) || (pmfh->hdrsize < 14)
|| (!pmmh->nblocks) || (pmmh->filesize < 16) || (pmmh->filesize > 0x8000000)
|| (pmmh->blktable >= dwMemLength) || (pmmh->blktable + 4*pmmh->nblocks > dwMemLength)) return FALSE;
dwFileSize = pmmh->filesize;
if ((pBuffer = (LPBYTE)GlobalAllocPtr(GHND, (dwFileSize + 31) & ~15)) == NULL) return FALSE;
pblk_table = (LPDWORD)(lpMemFile+pmmh->blktable);
for (UINT nBlock=0; nBlock<pmmh->nblocks; nBlock++)
{
DWORD dwMemPos = pblk_table[nBlock];
LPMMCMPBLOCK pblk = (LPMMCMPBLOCK)(lpMemFile+dwMemPos);
LPMMCMPSUBBLOCK psubblk = (LPMMCMPSUBBLOCK)(lpMemFile+dwMemPos+20);
if ((dwMemPos + 20 >= dwMemLength) || (dwMemPos + 20 + pblk->sub_blk*8 >= dwMemLength)) break;
dwMemPos += 20 + pblk->sub_blk*8;
#ifdef MMCMP_LOG
Log("block %d: flags=%04X sub_blocks=%d", nBlock, (UINT)pblk->flags, (UINT)pblk->sub_blk);
Log(" pksize=%d unpksize=%d", pblk->pk_size, pblk->unpk_size);
Log(" tt_entries=%d num_bits=%d\n", pblk->tt_entries, pblk->num_bits);
#endif
// Data is not packed
if (!(pblk->flags & MMCMP_COMP))
{
for (UINT i=0; i<pblk->sub_blk; i++)
{
if ((psubblk->unpk_pos > dwFileSize) || (psubblk->unpk_pos + psubblk->unpk_size > dwFileSize)) break;
#ifdef MMCMP_LOG
Log(" Unpacked sub-block %d: offset %d, size=%d\n", i, psubblk->unpk_pos, psubblk->unpk_size);
#endif
memcpy(pBuffer+psubblk->unpk_pos, lpMemFile+dwMemPos, psubblk->unpk_size);
dwMemPos += psubblk->unpk_size;
psubblk++;
}
} else
// Data is 16-bit packed
if (pblk->flags & MMCMP_16BIT)
{
MMCMPBITBUFFER bb;
LPWORD pDest = (LPWORD)(pBuffer + psubblk->unpk_pos);
DWORD dwSize = psubblk->unpk_size >> 1;
DWORD dwPos = 0;
UINT numbits = pblk->num_bits;
UINT subblk = 0, oldval = 0;
#ifdef MMCMP_LOG
Log(" 16-bit block: pos=%d size=%d ", psubblk->unpk_pos, psubblk->unpk_size);
if (pblk->flags & MMCMP_DELTA) Log("DELTA ");
if (pblk->flags & MMCMP_ABS16) Log("ABS16 ");
Log("\n");
#endif
bb.bitcount = 0;
bb.bitbuffer = 0;
bb.pSrc = lpMemFile+dwMemPos+pblk->tt_entries;
bb.pEnd = lpMemFile+dwMemPos+pblk->pk_size;
while (subblk < pblk->sub_blk)
{
UINT newval = 0x10000;
DWORD d = bb.GetBits(numbits+1);
if (d >= MMCMP16BitCommands[numbits])
{
UINT nFetch = MMCMP16BitFetch[numbits];
UINT newbits = bb.GetBits(nFetch) + ((d - MMCMP16BitCommands[numbits]) << nFetch);
if (newbits != numbits)
{
numbits = newbits & 0x0F;
} else
{
if ((d = bb.GetBits(4)) == 0x0F)
{
if (bb.GetBits(1)) break;
newval = 0xFFFF;
} else
{
newval = 0xFFF0 + d;
}
}
} else
{
newval = d;
}
if (newval < 0x10000)
{
newval = (newval & 1) ? (UINT)(-(LONG)((newval+1) >> 1)) : (UINT)(newval >> 1);
if (pblk->flags & MMCMP_DELTA)
{
newval += oldval;
oldval = newval;
} else
if (!(pblk->flags & MMCMP_ABS16))
{
newval ^= 0x8000;
}
pDest[dwPos++] = (WORD)newval;
}
if (dwPos >= dwSize)
{
subblk++;
dwPos = 0;
dwSize = psubblk[subblk].unpk_size >> 1;
pDest = (LPWORD)(pBuffer + psubblk[subblk].unpk_pos);
}
}
} else
// Data is 8-bit packed
{
MMCMPBITBUFFER bb;
LPBYTE pDest = pBuffer + psubblk->unpk_pos;
DWORD dwSize = psubblk->unpk_size;
DWORD dwPos = 0;
UINT numbits = pblk->num_bits;
UINT subblk = 0, oldval = 0;
LPCBYTE ptable = lpMemFile+dwMemPos;
bb.bitcount = 0;
bb.bitbuffer = 0;
bb.pSrc = lpMemFile+dwMemPos+pblk->tt_entries;
bb.pEnd = lpMemFile+dwMemPos+pblk->pk_size;
while (subblk < pblk->sub_blk)
{
UINT newval = 0x100;
DWORD d = bb.GetBits(numbits+1);
if (d >= MMCMP8BitCommands[numbits])
{
UINT nFetch = MMCMP8BitFetch[numbits];
UINT newbits = bb.GetBits(nFetch) + ((d - MMCMP8BitCommands[numbits]) << nFetch);
if (newbits != numbits)
{
numbits = newbits & 0x07;
} else
{
if ((d = bb.GetBits(3)) == 7)
{
if (bb.GetBits(1)) break;
newval = 0xFF;
} else
{
newval = 0xF8 + d;
}
}
} else
{
newval = d;
}
if (newval < 0x100)
{
int n = ptable[newval];
if (pblk->flags & MMCMP_DELTA)
{
n += oldval;
oldval = n;
}
pDest[dwPos++] = (BYTE)n;
}
if (dwPos >= dwSize)
{
subblk++;
dwPos = 0;
dwSize = psubblk[subblk].unpk_size;
pDest = pBuffer + psubblk[subblk].unpk_pos;
}
}
}
}
*ppMemFile = pBuffer;
*pdwMemLength = dwFileSize;
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////
//
// PowerPack PP20 Unpacker
//
typedef struct _PPBITBUFFER
{
UINT bitcount;
ULONG bitbuffer;
LPCBYTE pStart;
LPCBYTE pSrc;
ULONG GetBits(UINT n);
} PPBITBUFFER;
ULONG PPBITBUFFER::GetBits(UINT n)
{
ULONG result = 0;
for (UINT i=0; i<n; i++)
{
if (!bitcount)
{
bitcount = 8;
if (pSrc != pStart) pSrc--;
bitbuffer = *pSrc;
}
result = (result<<1) | (bitbuffer&1);
bitbuffer >>= 1;
bitcount--;
}
return result;
}
VOID PP20_DoUnpack(const BYTE *pSrc, UINT nSrcLen, BYTE *pDst, UINT nDstLen)
{
PPBITBUFFER BitBuffer;
ULONG nBytesLeft;
BitBuffer.pStart = pSrc;
BitBuffer.pSrc = pSrc + nSrcLen - 4;
BitBuffer.bitbuffer = 0;
BitBuffer.bitcount = 0;
BitBuffer.GetBits(pSrc[nSrcLen-1]);
nBytesLeft = nDstLen;
while (nBytesLeft > 0)
{
if (!BitBuffer.GetBits(1))
{
UINT n = 1;
while (n < nBytesLeft)
{
UINT code = BitBuffer.GetBits(2);
n += code;
if (code != 3) break;
}
for (UINT i=0; i<n; i++)
{
pDst[--nBytesLeft] = (BYTE)BitBuffer.GetBits(8);
}
if (!nBytesLeft) break;
}
{
UINT n = BitBuffer.GetBits(2)+1;
UINT nbits = pSrc[n-1];
UINT nofs;
if (n==4)
{
nofs = BitBuffer.GetBits( (BitBuffer.GetBits(1)) ? nbits : 7 );
while (n < nBytesLeft)
{
UINT code = BitBuffer.GetBits(3);
n += code;
if (code != 7) break;
}
} else
{
nofs = BitBuffer.GetBits(nbits);
}
for (UINT i=0; i<=n; i++)
{
pDst[nBytesLeft-1] = (nBytesLeft+nofs < nDstLen) ? pDst[nBytesLeft+nofs] : 0;
if (!--nBytesLeft) break;
}
}
}
}
BOOL PP20_Unpack(LPCBYTE *ppMemFile, LPDWORD pdwMemLength)
{
DWORD dwMemLength = *pdwMemLength;
LPCBYTE lpMemFile = *ppMemFile;
DWORD dwDstLen;
LPBYTE pBuffer;
if ((!lpMemFile) || (dwMemLength < 256) || (*(DWORD *)lpMemFile != 0x30325050)) return FALSE;
dwDstLen = (lpMemFile[dwMemLength-4]<<16) | (lpMemFile[dwMemLength-3]<<8) | (lpMemFile[dwMemLength-2]);
//Log("PP20 detected: Packed length=%d, Unpacked length=%d\n", dwMemLength, dwDstLen);
if ((dwDstLen < 512) || (dwDstLen > 0x400000) || (dwDstLen > 16*dwMemLength)) return FALSE;
if ((pBuffer = (LPBYTE)GlobalAllocPtr(GHND, (dwDstLen + 31) & ~15)) == NULL) return FALSE;
PP20_DoUnpack(lpMemFile+4, dwMemLength-4, pBuffer, dwDstLen);
*ppMemFile = pBuffer;
*pdwMemLength = dwDstLen;
return TRUE;
}