summaryrefslogtreecommitdiff
path: root/plugins/adplug/adplug/dmo.cpp
blob: 8a3d8e24941c025e00234cf51a262541808491b4 (plain)
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
/*
  Adplug - Replayer for many OPL2/OPL3 audio file formats.
  Copyright (C) 1999 - 2004, 2006 Simon Peter, <dn.tlp@gmx.net>, et al.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser 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

  dmo.cpp - TwinTeam loader by Riven the Mage <riven@ok.ru>
*/
/*
  NOTES:
  Panning is ignored.

  A WORD ist 16 bits, a DWORD is 32 bits and a BYTE is 8 bits in this context.
*/

#include <string.h>
#include <binstr.h>

#include "dmo.h"
#include "debug.h"

#define LOWORD(l) ((l) & 0xffff)
#define HIWORD(l) ((l) >> 16)
#define LOBYTE(w) ((w) & 0xff)
#define HIBYTE(w) ((w) >> 8)

#define ARRAY_AS_DWORD(a, i) \
((a[i + 3] << 24) + (a[i + 2] << 16) + (a[i + 1] << 8) + a[i])
#define ARRAY_AS_WORD(a, i)	((a[i + 1] << 8) + a[i])

#define CHARP_AS_WORD(p)	(((*(p + 1)) << 8) + (*p))

/* -------- Public Methods -------------------------------- */

CPlayer *CdmoLoader::factory(Copl *newopl)
{
  return new CdmoLoader(newopl);
}

bool CdmoLoader::load(const char *filename, const CFileProvider &fp)
{
  int i,j;
  binistream *f;

  // check header
  dmo_unpacker *unpacker = new dmo_unpacker;
  unsigned char chkhdr[16];

  if(!fp.extension(filename, ".dmo")) return false;
  f = fp.open(filename); if(!f) return false;

  f->readString((char *)chkhdr, 16);

  if (!unpacker->decrypt(chkhdr, 16))
    {
      delete unpacker;
      fp.close(f);
      return false;
    }

  // get file size
  long packed_length = fp.filesize(f);
  f->seek(0);

  unsigned char *packed_module = new unsigned char [packed_length];

  // load file
  f->readString((char *)packed_module, packed_length);
  fp.close(f);

  // decrypt
  unpacker->decrypt(packed_module,packed_length);

  long unpacked_length = 0x2000 * ARRAY_AS_WORD(packed_module, 12);
  unsigned char *module = new unsigned char [unpacked_length];

  // unpack
  if (!unpacker->unpack(packed_module+12,module,unpacked_length))
    {
      delete unpacker;
      delete [] packed_module;
      delete [] module;
      return false;
    }

  delete unpacker;
  delete [] packed_module;

  // "TwinTeam" - signed ?
  if (memcmp(module,"TwinTeam Module File""\x0D\x0A",22))
    {
      delete module;
      return false;
    }

  // load header
  binisstream	uf(module, unpacked_length);
  uf.setFlag(binio::BigEndian, false); uf.setFlag(binio::FloatIEEE);

  memset(&header,0,sizeof(s3mheader));

  uf.ignore(22);				// ignore DMO header ID string
  uf.readString(header.name, 28);

  uf.ignore(2);				// _unk_1
  header.ordnum  = uf.readInt(2);
  header.insnum  = uf.readInt(2);
  header.patnum  = uf.readInt(2);
  uf.ignore(2);				// _unk_2
  header.is      = uf.readInt(2);
  header.it      = uf.readInt(2);

  memset(header.chanset,0xFF,32);

  for (i=0;i<9;i++)
    header.chanset[i] = 0x10 + i;

  uf.ignore(32);				// ignore panning settings for all 32 channels

  // load orders
  for(i = 0; i < 256; i++) orders[i] = uf.readInt(1);

  orders[header.ordnum] = 0xFF;

  // load pattern lengths
  unsigned short my_patlen[100];
  for(i = 0; i < 100; i++) my_patlen[i] = uf.readInt(2);

  // load instruments
  for (i = 0; i < header.insnum; i++)
    {
      memset(&inst[i],0,sizeof(s3minst));

      uf.readString(inst[i].name, 28);

      inst[i].volume = uf.readInt(1);
      inst[i].dsk    = uf.readInt(1);
      inst[i].c2spd  = uf.readInt(4);
      inst[i].type   = uf.readInt(1);
      inst[i].d00    = uf.readInt(1);
      inst[i].d01    = uf.readInt(1);
      inst[i].d02    = uf.readInt(1);
      inst[i].d03    = uf.readInt(1);
      inst[i].d04    = uf.readInt(1);
      inst[i].d05    = uf.readInt(1);
      inst[i].d06    = uf.readInt(1);
      inst[i].d07    = uf.readInt(1);
      inst[i].d08    = uf.readInt(1);
      inst[i].d09    = uf.readInt(1);
      inst[i].d0a    = uf.readInt(1);
      /*
       * Originally, riven sets d0b = d0a and ignores 1 byte in the
       * stream, but i guess this was a typo, so i read it here.
       */
      inst[i].d0b    = uf.readInt(1);
    }

  // load patterns
  for (i = 0; i < header.patnum; i++) {
    long cur_pos = uf.pos();

    for (j = 0; j < 64; j++) {
      while (1) {
	unsigned char token = uf.readInt(1);

	if (!token)
	  break;

	unsigned char chan = token & 31;

	// note + instrument ?
	if (token & 32) {
	  unsigned char bufbyte = uf.readInt(1);

	  pattern[i][j][chan].note = bufbyte & 15;
	  pattern[i][j][chan].oct = bufbyte >> 4;
	  pattern[i][j][chan].instrument = uf.readInt(1);
	}

	// volume ?
	if (token & 64)
	  pattern[i][j][chan].volume = uf.readInt(1);

	// command ?
	if (token & 128) {
	  pattern[i][j][chan].command = uf.readInt(1);
	  pattern[i][j][chan].info = uf.readInt(1);
	}
      }
    }

    uf.seek(cur_pos + my_patlen[i]);
  }

  delete [] module;
  rewind(0);
  return true;
}

const char * CdmoLoader::gettype()
{
  return "TwinTeam (packed S3M)";
}

const char * CdmoLoader::getauthor()
{
  /*
    All available .DMO modules written by one composer. And because all .DMO
    stuff was lost due to hd crash (TwinTeam guys said this), there are
    never(?) be another.
  */
  return "Benjamin GERARDIN";
}

/* -------- Private Methods ------------------------------- */

unsigned short CdmoLoader::dmo_unpacker::brand(unsigned short range)
{
  unsigned short ax,bx,cx,dx;

  ax = LOWORD(bseed);
  bx = HIWORD(bseed);
  cx = ax;
  ax = LOWORD(cx * 0x8405);
  dx = HIWORD(cx * 0x8405);
  cx <<= 3;
  cx = (((HIBYTE(cx) + LOBYTE(cx)) & 0xFF) << 8) + LOBYTE(cx);
  dx += cx;
  dx += bx;
  bx <<= 2;
  dx += bx;
  dx = (((HIBYTE(dx) + LOBYTE(bx)) & 0xFF) << 8) + LOBYTE(dx);
  bx <<= 5;
  dx = (((HIBYTE(dx) + LOBYTE(bx)) & 0xFF) << 8) + LOBYTE(dx);
  ax += 1;
  if (!ax) dx += 1;

  // leave it that way or amd64 might get it wrong
  bseed = dx;
  bseed <<= 16;
  bseed += ax;

  return HIWORD(HIWORD(LOWORD(bseed) * range) + HIWORD(bseed) * range);
}

bool CdmoLoader::dmo_unpacker::decrypt(unsigned char *buf, long len)
{
  unsigned long seed = 0;
  int i;

  bseed = ARRAY_AS_DWORD(buf, 0);

  for (i=0; i < ARRAY_AS_WORD(buf, 4) + 1; i++)
    seed += brand(0xffff);

  bseed = seed ^ ARRAY_AS_DWORD(buf, 6);

  if (ARRAY_AS_WORD(buf, 10) != brand(0xffff))
    return false;

  for (i=0;i<(len-12);i++)
    buf[12+i] ^= brand(0x100);

  buf[len - 2] = buf[len - 1] = 0;

  return true;
}

short CdmoLoader::dmo_unpacker::unpack_block(unsigned char *ibuf, long ilen, unsigned char *obuf)
{
  unsigned char code,par1,par2;
  unsigned short ax,bx,cx;

  unsigned char *ipos = ibuf;
  unsigned char *opos = obuf;

  // LZ77 child
  while (ipos - ibuf < ilen)
    {
      code = *ipos++;

      // 00xxxxxx: copy (xxxxxx + 1) bytes
      if ((code >> 6) == 0)
	{
	  cx = (code & 0x3F) + 1;

	  if(opos + cx >= oend)
	    return -1;

	  for (int i=0;i<cx;i++)
	    *opos++ = *ipos++;

	  continue;
	}

      // 01xxxxxx xxxyyyyy: copy (Y + 3) bytes from (X + 1)
      if ((code >> 6) == 1)
	{
	  par1 = *ipos++;

	  ax = ((code & 0x3F) << 3) + ((par1 & 0xE0) >> 5) + 1;
	  cx = (par1 & 0x1F) + 3;

	  if(opos + cx >= oend)
	    return -1;

	  for(int i=0;i<cx;i++)
	    *opos++ = *(opos - ax);

	  continue;
	}

      // 10xxxxxx xyyyzzzz: copy (Y + 3) bytes from (X + 1); copy Z bytes
      if ((code >> 6) == 2)
	{
	  int i;

	  par1 = *ipos++;

	  ax = ((code & 0x3F) << 1) + (par1 >> 7) + 1;
	  cx = ((par1 & 0x70) >> 4) + 3;
	  bx = par1 & 0x0F;

	  if(opos + bx + cx >= oend)
	    return -1;

	  for(i=0;i<cx;i++)
	    *opos++ = *(opos - ax);

	  for (i=0;i<bx;i++)
	    *opos++ = *ipos++;

	  continue;
	}

      // 11xxxxxx xxxxxxxy yyyyzzzz: copy (Y + 4) from X; copy Z bytes
      if ((code >> 6) == 3)
	{
	  int i;

	  par1 = *ipos++;
	  par2 = *ipos++;

	  bx = ((code & 0x3F) << 7) + (par1 >> 1);
	  cx = ((par1 & 0x01) << 4) + (par2 >> 4) + 4;
	  ax = par2 & 0x0F;

	  if(opos + ax + cx >= oend)
	    return -1;

	  for(i=0;i<cx;i++)
	    *opos++ = *(opos - bx);

	  for (i=0;i<ax;i++)
	    *opos++ = *ipos++;

	  continue;
	}
    }

  return opos - obuf;
}

long CdmoLoader::dmo_unpacker::unpack(unsigned char *ibuf, unsigned char *obuf,
				      unsigned long outputsize)
{
  long			olen = 0;
  unsigned short	block_count = CHARP_AS_WORD(ibuf);

  ibuf += 2;
  unsigned char *block_length = ibuf;
  ibuf += 2 * block_count;

  oend = obuf + outputsize;

  for (int i=0;i<block_count;i++)
    {
      unsigned short bul = CHARP_AS_WORD(ibuf);

      if(unpack_block(ibuf + 2,CHARP_AS_WORD(block_length) - 2,obuf) != bul)
	return 0;

      obuf += bul;
      olen += bul;

      ibuf += CHARP_AS_WORD(block_length);
      block_length += 2;
    }

  return olen;
}