1 /*
2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2006 Simon Peter, <dn.tlp@gmx.net>, et al.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * mtk.cpp - MPU-401 Trakker Loader by Simon Peter (dn.tlp@gmx.net)
20 */
21
22 #include <string.h>
23 #include "mtk.h"
24
25 /*** public methods **************************************/
26
factory(Copl * newopl)27 CPlayer *CmtkLoader::factory(Copl *newopl)
28 {
29 return new CmtkLoader(newopl);
30 }
31
load(const char * filename,const CFileProvider & fp)32 bool CmtkLoader::load(const char *filename, const CFileProvider &fp)
33 {
34 binistream *f = fp.open(filename); if(!f) return false;
35 struct {
36 char id[18];
37 unsigned short crc,size;
38 } header;
39 struct mtkdata {
40 char songname[34],composername[34],instname[0x80][34];
41 unsigned char insts[0x80][12],order[0x80],dummy,patterns[0x32][0x40][9];
42 } *data;
43 unsigned char *cmp,*org;
44 unsigned int i;
45 unsigned long cmpsize,cmpptr=0,orgptr=0;
46 unsigned short ctrlbits=0,ctrlmask=0,cmd,cnt,offs;
47
48 // read header
49 f->readString(header.id, 18);
50 header.crc = f->readInt(2);
51 header.size = f->readInt(2);
52
53 // file validation section
54 if(strncmp(header.id,"mpu401tr\x92kk\xeer@data",18))
55 { fp.close(f); return false; }
56
57 // load section
58 cmpsize = fp.filesize(f) - 22;
59 cmp = new unsigned char[cmpsize];
60 org = new unsigned char[header.size];
61 for(i = 0; i < cmpsize; i++) cmp[i] = f->readInt(1);
62 fp.close(f);
63
64 while(cmpptr < cmpsize) { // decompress
65 ctrlmask >>= 1;
66 if(!ctrlmask) {
67 ctrlbits = cmp[cmpptr] + (cmp[cmpptr + 1] << 8);
68 cmpptr += 2;
69 ctrlmask = 0x8000;
70 }
71 if(!(ctrlbits & ctrlmask)) { // uncompressed data
72 if(orgptr >= header.size)
73 goto err;
74
75 org[orgptr] = cmp[cmpptr];
76 orgptr++; cmpptr++;
77 continue;
78 }
79
80 // compressed data
81 cmd = (cmp[cmpptr] >> 4) & 0x0f;
82 cnt = cmp[cmpptr] & 0x0f;
83 cmpptr++;
84 switch(cmd) {
85 case 0:
86 if(orgptr + cnt > header.size) goto err;
87 cnt += 3;
88 memset(&org[orgptr],cmp[cmpptr],cnt);
89 cmpptr++; orgptr += cnt;
90 break;
91
92 case 1:
93 if(orgptr + cnt > header.size) goto err;
94 cnt += (cmp[cmpptr] << 4) + 19;
95 memset(&org[orgptr],cmp[++cmpptr],cnt);
96 cmpptr++; orgptr += cnt;
97 break;
98
99 case 2:
100 if(orgptr + cnt > header.size) goto err;
101 offs = (cnt+3) + (cmp[cmpptr] << 4);
102 cnt = cmp[++cmpptr] + 16; cmpptr++;
103 memcpy(&org[orgptr],&org[orgptr - offs],cnt);
104 orgptr += cnt;
105 break;
106
107 default:
108 if(orgptr + cmd > header.size) goto err;
109 offs = (cnt+3) + (cmp[cmpptr++] << 4);
110 memcpy(&org[orgptr],&org[orgptr-offs],cmd);
111 orgptr += cmd;
112 break;
113 }
114 }
115 delete [] cmp;
116 data = (struct mtkdata *) org;
117
118 // convert to HSC replay data
119 memset(title,0,34); strncpy(title,data->songname+1,33);
120 memset(composer,0,34); strncpy(composer,data->composername+1,33);
121 memset(instname,0,0x80*34);
122 for(i=0;i<0x80;i++)
123 strncpy(instname[i],data->instname[i]+1,33);
124 memcpy(instr,data->insts,0x80 * 12);
125 memcpy(song,data->order,0x80);
126 memcpy(patterns,data->patterns,header.size-6084);
127 for (i=0;i<128;i++) { // correct instruments
128 instr[i][2] ^= (instr[i][2] & 0x40) << 1;
129 instr[i][3] ^= (instr[i][3] & 0x40) << 1;
130 instr[i][11] >>= 4; // make unsigned
131 }
132
133 delete [] org;
134 rewind(0);
135 return true;
136
137 err:
138 delete [] cmp;
139 delete [] org;
140 return false;
141 }
142