Mercurial > audlegacy
annotate Plugins/Input/adplug/core/mtk.cpp @ 1594:44f67f556b60 trunk
[svn] - precision in title format is regarded as character count, not byte count.
author | yaz |
---|---|
date | Thu, 24 Aug 2006 23:08:22 -0700 |
parents | 705d4c089fce |
children |
rev | line source |
---|---|
359 | 1 /* |
2 * Adplug - Replayer for many OPL2/OPL3 audio file formats. | |
1376
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
3 * Copyright (C) 1999 - 2006 Simon Peter, <dn.tlp@gmx.net>, et al. |
359 | 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 | |
1459 | 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
359 | 18 * |
19 * mtk.cpp - MPU-401 Trakker Loader by Simon Peter (dn.tlp@gmx.net) | |
20 */ | |
21 | |
22 #include "mtk.h" | |
23 | |
24 /*** public methods **************************************/ | |
25 | |
26 CPlayer *CmtkLoader::factory(Copl *newopl) | |
27 { | |
28 return new CmtkLoader(newopl); | |
29 } | |
30 | |
31 bool CmtkLoader::load(const std::string &filename, const CFileProvider &fp) | |
32 { | |
33 binistream *f = fp.open(filename); if(!f) return false; | |
34 struct { | |
35 char id[18]; | |
36 unsigned short crc,size; | |
37 } header; | |
38 struct mtkdata { | |
39 char songname[34],composername[34],instname[0x80][34]; | |
40 unsigned char insts[0x80][12],order[0x80],dummy,patterns[0x32][0x40][9]; | |
41 } *data; | |
42 unsigned char *cmp,*org; | |
43 unsigned int i; | |
44 unsigned long cmpsize,cmpptr=0,orgptr=0; | |
45 unsigned short ctrlbits=0,ctrlmask=0,cmd,cnt,offs; | |
46 | |
47 // read header | |
48 f->readString(header.id, 18); | |
49 header.crc = f->readInt(2); | |
50 header.size = f->readInt(2); | |
51 | |
52 // file validation section | |
53 if(strncmp(header.id,"mpu401tr\x92kk\xeer@data",18)) | |
54 { fp.close(f); return false; } | |
55 | |
56 // load section | |
57 cmpsize = fp.filesize(f) - 22; | |
58 cmp = new unsigned char[cmpsize]; | |
59 org = new unsigned char[header.size]; | |
60 for(i = 0; i < cmpsize; i++) cmp[i] = f->readInt(1); | |
61 fp.close(f); | |
62 | |
63 while(cmpptr < cmpsize) { // decompress | |
64 ctrlmask >>= 1; | |
65 if(!ctrlmask) { | |
66 ctrlbits = cmp[cmpptr] + (cmp[cmpptr + 1] << 8); | |
67 cmpptr += 2; | |
68 ctrlmask = 0x8000; | |
69 } | |
70 if(!(ctrlbits & ctrlmask)) { // uncompressed data | |
1376
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
71 if(orgptr >= header.size) |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
72 goto err; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
73 |
359 | 74 org[orgptr] = cmp[cmpptr]; |
75 orgptr++; cmpptr++; | |
76 continue; | |
77 } | |
78 | |
79 // compressed data | |
80 cmd = (cmp[cmpptr] >> 4) & 0x0f; | |
81 cnt = cmp[cmpptr] & 0x0f; | |
82 cmpptr++; | |
83 switch(cmd) { | |
1376
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
84 case 0: |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
85 if(orgptr + cnt > header.size) goto err; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
86 cnt += 3; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
87 memset(&org[orgptr],cmp[cmpptr],cnt); |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
88 cmpptr++; orgptr += cnt; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
89 break; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
90 |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
91 case 1: |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
92 if(orgptr + cnt > header.size) goto err; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
93 cnt += (cmp[cmpptr] << 4) + 19; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
94 memset(&org[orgptr],cmp[++cmpptr],cnt); |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
95 cmpptr++; orgptr += cnt; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
96 break; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
97 |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
98 case 2: |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
99 if(orgptr + cnt > header.size) goto err; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
100 offs = (cnt+3) + (cmp[cmpptr] << 4); |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
101 cnt = cmp[++cmpptr] + 16; cmpptr++; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
102 memcpy(&org[orgptr],&org[orgptr - offs],cnt); |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
103 orgptr += cnt; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
104 break; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
105 |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
106 default: |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
107 if(orgptr + cmd > header.size) goto err; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
108 offs = (cnt+3) + (cmp[cmpptr++] << 4); |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
109 memcpy(&org[orgptr],&org[orgptr-offs],cmd); |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
110 orgptr += cmd; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
111 break; |
359 | 112 } |
113 } | |
114 delete [] cmp; | |
115 data = (struct mtkdata *) org; | |
116 | |
117 // convert to HSC replay data | |
118 memset(title,0,34); strncpy(title,data->songname+1,33); | |
119 memset(composer,0,34); strncpy(composer,data->composername+1,33); | |
120 memset(instname,0,0x80*34); | |
121 for(i=0;i<0x80;i++) | |
122 strncpy(instname[i],data->instname[i]+1,33); | |
123 memcpy(instr,data->insts,0x80 * 12); | |
124 memcpy(song,data->order,0x80); | |
125 memcpy(patterns,data->patterns,header.size-6084); | |
126 for (i=0;i<128;i++) { // correct instruments | |
127 instr[i][2] ^= (instr[i][2] & 0x40) << 1; | |
128 instr[i][3] ^= (instr[i][3] & 0x40) << 1; | |
129 instr[i][11] >>= 4; // make unsigned | |
130 } | |
131 | |
132 delete [] org; | |
133 rewind(0); | |
134 return true; | |
1376
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
135 |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
136 err: |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
137 delete [] cmp; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
138 delete [] org; |
c71e2ef2dcf4
[svn] Security fixes from AdPlug CVS (their July 7 commit shortly before the secunia announcement).
chainsaw
parents:
359
diff
changeset
|
139 return false; |
359 | 140 } |