File: mmcmp.c

package info (click to toggle)
libmikmod 3.3.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,188 kB
  • sloc: ansic: 36,299; sh: 5,013; makefile: 548; javascript: 1
file content (439 lines) | stat: -rw-r--r-- 10,920 bytes parent folder | download
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* MikMod sound library (c) 2003-2015 Raphael Assenat and others -
 * see AUTHORS file for a complete list.
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

/* MMCMP ("ziRCONia") decompression support
 *
 * Based on the public domain version from the libmodplug library by
 * Olivier Lapicque <olivierl@jps.net> (sezero's fork of libmodplug
 * at github: http://github.com/sezero/libmodplug/tree/sezero )
 *
 * Rewritten for libmikmod by O. Sezer <sezero@users.sourceforge.net>
 * with some extra ideas from the libxmp version by Claudio Matsuoka.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifndef NO_DEPACKERS

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <stdio.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <string.h>

#include "mikmod_internals.h"

#ifdef SUNOS
extern int fprintf(FILE *, const char *, ...);
#endif

/*#define MMCMP_DEBUG*/

typedef struct MMCMPFILEHDR
{
	UBYTE id[8]; /* string 'ziRCONia' */
	UWORD hdrsize; /* sizeof MMCMPHEADER */
} MMCMPFILEHDR; /* 10 bytes */

typedef struct MMCMPHEADER
{
	UWORD version;
	UWORD nblocks;
	ULONG filesize;
	ULONG blktable;
	UBYTE glb_comp;
	UBYTE fmt_comp;
} MMCMPHEADER; /* 14 bytes */

typedef struct MMCMPBLOCK
{
	ULONG unpk_size;
	ULONG pk_size;
	ULONG xor_chk;
	UWORD sub_blk;
	UWORD flags;
	UWORD tt_entries;
	UWORD num_bits;
} MMCMPBLOCK; /* 20 bytes */

typedef struct MMCMPSUBBLOCK
{
	ULONG unpk_pos;
	ULONG unpk_size;
} MMCMPSUBBLOCK; /* 8 bytes */

#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
{
	ULONG bitcount;
	ULONG bitbuffer;
	const UBYTE *start;
	const UBYTE *end;
} MMCMPBITBUFFER;

static ULONG MMCMP_GetBits(MMCMPBITBUFFER* b, ULONG nBits)
{
	ULONG d;
	if (!nBits) return 0;
	while (b->bitcount < 24)
	{
		b->bitbuffer |= ((b->start < b->end) ? *b->start++ : 0) << b->bitcount;
		b->bitcount += 8;
	}
	d = b->bitbuffer & ((1 << nBits) - 1);
	b->bitbuffer >>= nBits;
	b->bitcount -= nBits;
	return d;
}

static const ULONG MMCMP8BitCommands[8] =
{
	0x01, 0x03,	0x07, 0x0F,	0x1E, 0x3C,	0x78, 0xF8
};

static const ULONG MMCMP8BitFetch[8] =
{
	3, 3, 3, 3, 2, 1, 0, 0
};

static const ULONG MMCMP16BitCommands[16] =
{
	0x01, 0x03,	0x07, 0x0F,	0x1E, 0x3C,	0x78, 0xF0,
	0x1F0, 0x3F0, 0x7F0, 0xFF0, 0x1FF0, 0x3FF0, 0x7FF0, 0xFFF0
};

static const ULONG MMCMP16BitFetch[16] =
{
	4, 4, 4, 4, 3, 2, 1, 0,
	0, 0, 0, 0, 0, 0, 0, 0
};


BOOL MMCMP_Unpack(MREADER* reader, void** out, long* outlen)
{
	ULONG srclen, destlen;
	UBYTE *destbuf, *destptr, *destend;
	MMCMPHEADER mmh;
	ULONG *pblk_table;
	MMCMPSUBBLOCK *subblocks;
	ULONG i, blockidx, numsubs;
	UBYTE *buf;  ULONG bufsize;

	_mm_fseek(reader,0,SEEK_END);
	srclen = _mm_ftell(reader);
	if (srclen < 256) return 0;

	_mm_rewind(reader);
	if (_mm_read_I_ULONG(reader) != 0x4352697A)	/* 'ziRC' */
		return 0;
	if (_mm_read_I_ULONG(reader) != 0x61694e4f)	/* 'ONia' */
		return 0;
	if (_mm_read_I_UWORD(reader) != 14)		/* header size */
		return 0;

	mmh.version = _mm_read_I_UWORD(reader);
	mmh.nblocks = _mm_read_I_UWORD(reader);
	mmh.filesize = _mm_read_I_ULONG(reader);
	mmh.blktable = _mm_read_I_ULONG(reader);
	mmh.glb_comp = _mm_read_UBYTE(reader);
	mmh.fmt_comp = _mm_read_UBYTE(reader);

	if ((!mmh.nblocks) || (mmh.filesize < 16) || (mmh.filesize > 0x8000000) ||
	    (mmh.blktable >= srclen) || (mmh.blktable + 4*mmh.nblocks > srclen)) {
		return 0;
	}
	destlen = mmh.filesize;
	numsubs = 32;
	bufsize = 65536;

	destbuf = (UBYTE*)MikMod_malloc(destlen);
	buf = (UBYTE*)MikMod_malloc(bufsize);
	pblk_table = (ULONG*)MikMod_malloc(mmh.nblocks*4);
	subblocks = (MMCMPSUBBLOCK*)MikMod_malloc(numsubs*sizeof(MMCMPSUBBLOCK));
	if (!destbuf || !buf || !pblk_table || !subblocks)
		goto err;
	destend = destbuf + destlen;

	_mm_fseek(reader,mmh.blktable,SEEK_SET);
	for (blockidx = 0; blockidx < mmh.nblocks; blockidx++) {
		pblk_table[blockidx] = _mm_read_I_ULONG(reader);
	}

	for (blockidx = 0; blockidx < mmh.nblocks; blockidx++)
	{
		ULONG srcpos = pblk_table[blockidx];
		MMCMPBLOCK block;

		if (srcpos + 20 >= srclen) goto err;

		_mm_fseek(reader,srcpos,SEEK_SET);
		block.unpk_size = _mm_read_I_ULONG(reader);
		block.pk_size = _mm_read_I_ULONG(reader);
		block.xor_chk = _mm_read_I_ULONG(reader);
		block.sub_blk = _mm_read_I_UWORD(reader);
		block.flags = _mm_read_I_UWORD(reader);
		block.tt_entries = _mm_read_I_UWORD(reader);
		block.num_bits = _mm_read_I_UWORD(reader);

		if (!block.unpk_size || !block.pk_size || !block.sub_blk)
			goto err;
		if (block.pk_size <= block.tt_entries)
			goto err;
		if (block.flags & MMCMP_COMP) {
			if (block.flags & MMCMP_16BIT) {
				if (block.num_bits >= 16)
					goto err;
			}
			else {
				if (block.num_bits >=  8)
					goto err;
			}
		}

		srcpos += 20 + block.sub_blk*8;
		if (srcpos >= srclen) goto err;

		if (numsubs < block.sub_blk) {
			numsubs = (block.sub_blk + 31) & ~31;
			MikMod_free(subblocks);
			subblocks = (MMCMPSUBBLOCK*)MikMod_malloc(numsubs*sizeof(MMCMPSUBBLOCK));
			if (!subblocks) goto err;
		}
		for (i = 0; i < block.sub_blk; i++) {
			subblocks[i].unpk_pos = _mm_read_I_ULONG(reader);
			subblocks[i].unpk_size = _mm_read_I_ULONG(reader);
			if (subblocks[i].unpk_pos >= destlen) goto err;
			if (subblocks[i].unpk_size > destlen - subblocks[i].unpk_pos) goto err;
		}

#ifdef MMCMP_DEBUG
		fprintf(stderr, "block %u: flags=%04X sub_blocks=%u",
				  blockidx, (unsigned)block.flags, (unsigned)block.sub_blk);
		fprintf(stderr, " pksize=%u unpksize=%u", block.pk_size, block.unpk_size);
		fprintf(stderr, " tt_entries=%u num_bits=%u\n", block.tt_entries, block.num_bits);
#endif
		if (!(block.flags & MMCMP_COMP))
		{ /* Data is not packed */
			_mm_fseek(reader,srcpos,SEEK_SET);
			destptr = destbuf + subblocks[0].unpk_pos;
			i = 0;

			while (1) {
#ifdef MMCMP_DEBUG
				fprintf(stderr, "  Unpacked sub-block %u: offset %u, size=%u\n",
						i, subblocks[i].unpk_pos, subblocks[i].unpk_size);
#endif
				_mm_read_UBYTES(destptr,subblocks[i].unpk_size,reader);
				destptr += subblocks[i].unpk_size;
				if (++i == block.sub_blk) break;
			}
		}
		else if (block.flags & MMCMP_16BIT)
		{ /* Data is 16-bit packed */
			MMCMPBITBUFFER bb;
			ULONG size;
			ULONG pos = 0;
			ULONG numbits = block.num_bits;
			ULONG oldval = 0;

#ifdef MMCMP_DEBUG
			fprintf(stderr, "  16-bit block: pos=%u size=%u ",
					subblocks[0].unpk_pos, subblocks[0].unpk_size);
			if (block.flags & MMCMP_DELTA) fprintf(stderr, "DELTA ");
			if (block.flags & MMCMP_ABS16) fprintf(stderr, "ABS16 ");
			fprintf(stderr, "\n");
#endif
			size = block.pk_size - block.tt_entries;
			if (bufsize < size) {
				while (bufsize < size) bufsize += 65536;
				MikMod_free(buf);
				if (!(buf = (UBYTE*)MikMod_malloc(bufsize)))
					goto err;
			}

			bb.bitcount = 0;
			bb.bitbuffer = 0;
			bb.start = buf;
			bb.end = buf + size;

			_mm_fseek(reader,srcpos+block.tt_entries,SEEK_SET);
			_mm_read_UBYTES(buf,size,reader);
			destptr = destbuf + subblocks[0].unpk_pos;
			size = subblocks[0].unpk_size;
			i = 0;

			while (1)
			{
				ULONG newval = 0x10000;
				ULONG d = MMCMP_GetBits(&bb, numbits+1);

				if (d >= MMCMP16BitCommands[numbits])
				{
					ULONG nFetch = MMCMP16BitFetch[numbits];
					ULONG newbits = MMCMP_GetBits(&bb, nFetch) + ((d - MMCMP16BitCommands[numbits]) << nFetch);
					if (newbits != numbits)
					{
						numbits = newbits & 0x0F;
					} else
					{
						if ((d = MMCMP_GetBits(&bb, 4)) == 0x0F)
						{
							if (MMCMP_GetBits(&bb, 1)) break;
							newval = 0xFFFF;
						} else
						{
							newval = 0xFFF0 + d;
						}
					}
				} else
				{
					newval = d;
				}
				if (newval < 0x10000)
				{
					newval = (newval & 1) ? (ULONG)(-(SLONG)((newval+1) >> 1)) : (ULONG)(newval >> 1);
					if (block.flags & MMCMP_DELTA)
					{
						newval += oldval;
						oldval = newval;
					} else
					if (!(block.flags & MMCMP_ABS16))
					{
						newval ^= 0x8000;
					}
					if (destend - destptr < 2) goto err;
					pos += 2;
					*destptr++ = (UBYTE) (((UWORD)newval) & 0xff);
					*destptr++ = (UBYTE) (((UWORD)newval) >> 8);
				}
				if (pos >= size)
				{
					if (++i == block.sub_blk) break;
					size = subblocks[i].unpk_size;
					destptr = destbuf + subblocks[i].unpk_pos;
					pos = 0;
				}
			}
		}
		else
		{ /* Data is 8-bit packed */
			MMCMPBITBUFFER bb;
			ULONG size;
			ULONG pos = 0;
			ULONG numbits = block.num_bits;
			ULONG oldval = 0;
			UBYTE ptable[0x100];

			size = block.pk_size - block.tt_entries;
			if (bufsize < size) {
				while (bufsize < size) bufsize += 65536;
				MikMod_free(buf);
				if (!(buf = (UBYTE*)MikMod_malloc(bufsize)))
					goto err;
			}

			bb.bitcount = 0;
			bb.bitbuffer = 0;
			bb.start = buf;
			bb.end = buf + size;

			_mm_read_UBYTES(ptable,0x100,reader);
			_mm_fseek(reader,srcpos+block.tt_entries,SEEK_SET);
			_mm_read_UBYTES(buf,size,reader);
			destptr = destbuf + subblocks[0].unpk_pos;
			size = subblocks[0].unpk_size;
			i = 0;

			while (1)
			{
				ULONG newval = 0x100;
				ULONG d = MMCMP_GetBits(&bb, numbits+1);

				if (d >= MMCMP8BitCommands[numbits])
				{
					ULONG nFetch = MMCMP8BitFetch[numbits];
					ULONG newbits = MMCMP_GetBits(&bb, nFetch) + ((d - MMCMP8BitCommands[numbits]) << nFetch);
					if (newbits != numbits)
					{
						numbits = newbits & 0x07;
					} else
					{
						if ((d = MMCMP_GetBits(&bb, 3)) == 7)
						{
							if (MMCMP_GetBits(&bb, 1)) break;
							newval = 0xFF;
						} else
						{
							newval = 0xF8 + d;
						}
					}
				} else
				{
					newval = d;
				}
				if (newval < 0x100)
				{
					int n = ptable[newval];
					if (block.flags & MMCMP_DELTA)
					{
						n += oldval;
						oldval = n;
					}
					destptr[pos++] = (UBYTE)n;
				}
				if (pos >= size)
				{
					if (++i == block.sub_blk) break;
					size = subblocks[i].unpk_size;
					destptr = destbuf + subblocks[i].unpk_pos;
					pos = 0;
				}
			}
		}
	}

	MikMod_free(buf);
	MikMod_free(pblk_table);
	MikMod_free(subblocks);
	*out = destbuf;
	*outlen = destlen;
	return 1;

  err:
	MikMod_free(buf);
	MikMod_free(pblk_table);
	MikMod_free(subblocks);
	MikMod_free(destbuf);
	return 0;
}

#endif /* NO_DEPACKERS */