summaryrefslogtreecommitdiff
path: root/apps/plugins/mikmod/load_m15.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/mikmod/load_m15.c')
-rw-r--r--apps/plugins/mikmod/load_m15.c107
1 files changed, 54 insertions, 53 deletions
diff --git a/apps/plugins/mikmod/load_m15.c b/apps/plugins/mikmod/load_m15.c
index b5d5329bf0..2359175bb3 100644
--- a/apps/plugins/mikmod/load_m15.c
+++ b/apps/plugins/mikmod/load_m15.c
@@ -6,12 +6,12 @@
6 it under the terms of the GNU Library General Public License as 6 it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of 7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version. 8 the License, or (at your option) any later version.
9 9
10 This program is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details. 13 GNU Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public 15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software 16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
@@ -20,7 +20,7 @@
20 20
21/*============================================================================== 21/*==============================================================================
22 22
23 $Id: load_m15.c,v 1.3 2005/04/07 19:57:38 realtech Exp $ 23 $Id$
24 24
25 15 instrument MOD loader 25 15 instrument MOD loader
26 Also supports Ultimate Sound Tracker (old M15 format) 26 Also supports Ultimate Sound Tracker (old M15 format)
@@ -79,33 +79,31 @@ static int ust_loader = 0; /* if TRUE, load as an ust module. */
79 79
80/* known file formats which can confuse the loader */ 80/* known file formats which can confuse the loader */
81#define REJECT 2 81#define REJECT 2
82static char *signatures[REJECT]={ 82static const char *signatures[REJECT]={
83 "CAKEWALK", /* cakewalk midi files */ 83 "CAKEWALK", /* cakewalk midi files */
84 "SZDD" /* Microsoft compressed files */ 84 "SZDD" /* Microsoft compressed files */
85}; 85};
86static int siglen[REJECT]={8,4}; 86static const int siglen[REJECT]={8,4};
87 87
88/*========== Loader code */ 88/*========== Loader code */
89 89
90static int LoadModuleHeader(MODULEHEADER *mh) 90static int LoadModuleHeader(MODULEHEADER *h)
91{ 91{
92 int t,u; 92 int t,u;
93 93
94 _mm_read_string(mh->songname,20,modreader); 94 _mm_read_string(h->songname,20,modreader);
95 mh->songname[20]=0; /* just in case */
96 95
97 /* sanity check : title should contain printable characters and a bunch 96 /* sanity check : title should contain printable characters and a bunch
98 of null chars */ 97 of null chars */
99 for(t=0;t<20;t++) 98 for(t=0;t<20;t++)
100 if((mh->songname[t])&&(mh->songname[t]<32)) return 0; 99 if((h->songname[t])&&(h->songname[t]<32)) return 0;
101 for(t=0;(mh->songname[t])&&(t<20);t++); 100 for(t=0;(h->songname[t])&&(t<20);t++);
102 if(t<20) for(;t<20;t++) if(mh->songname[t]) return 0; 101 if(t<20) for(;t<20;t++) if(h->songname[t]) return 0;
103 102
104 for(t=0;t<15;t++) { 103 for(t=0;t<15;t++) {
105 MSAMPINFO *s=&mh->samples[t]; 104 MSAMPINFO *s=&h->samples[t];
106 105
107 _mm_read_string(s->samplename,22,modreader); 106 _mm_read_string(s->samplename,22,modreader);
108 s->samplename[22]=0; /* just in case */
109 s->length =_mm_read_M_UWORD(modreader); 107 s->length =_mm_read_M_UWORD(modreader);
110 s->finetune =_mm_read_UBYTE(modreader); 108 s->finetune =_mm_read_UBYTE(modreader);
111 s->volume =_mm_read_UBYTE(modreader); 109 s->volume =_mm_read_UBYTE(modreader);
@@ -123,26 +121,26 @@ static int LoadModuleHeader(MODULEHEADER *mh)
123 if(s->finetune>>4) return 0; 121 if(s->finetune>>4) return 0;
124 } 122 }
125 123
126 mh->songlength =_mm_read_UBYTE(modreader); 124 h->songlength =_mm_read_UBYTE(modreader);
127 mh->magic1 =_mm_read_UBYTE(modreader); /* should be 127 */ 125 h->magic1 =_mm_read_UBYTE(modreader); /* should be 127 */
128 126
129 /* sanity check : no more than 128 positions, restart position in range */ 127 /* sanity check : no more than 128 positions, restart position in range */
130 if((!mh->songlength)||(mh->songlength>128)) return 0; 128 if((!h->songlength)||(h->songlength>128)) return 0;
131 /* values encountered so far are 0x6a and 0x78 */ 129 /* values encountered so far are 0x6a and 0x78 */
132 if(((mh->magic1&0xf8)!=0x78)&&(mh->magic1!=0x6a)&&(mh->magic1>mh->songlength)) return 0; 130 if(((h->magic1&0xf8)!=0x78)&&(h->magic1!=0x6a)&&(h->magic1>h->songlength)) return 0;
133 131
134 _mm_read_UBYTES(mh->positions,128,modreader); 132 _mm_read_UBYTES(h->positions,128,modreader);
135 133
136 /* sanity check : pattern range is 0..63 */ 134 /* sanity check : pattern range is 0..63 */
137 for(t=0;t<128;t++) 135 for(t=0;t<128;t++)
138 if(mh->positions[t]>63) return 0; 136 if(h->positions[t]>63) return 0;
139 137
140 return(!_mm_eof(modreader)); 138 return(!_mm_eof(modreader));
141} 139}
142 140
143/* Checks the patterns in the modfile for UST / 15-inst indications. 141/* Checks the patterns in the modfile for UST / 15-inst indications.
144 For example, if an effect 3xx is found, it is assumed that the song 142 For example, if an effect 3xx is found, it is assumed that the song
145 is 15-inst. If a 1xx effect has dat greater than 0x20, it is UST. 143 is 15-inst. If a 1xx effect has dat greater than 0x20, it is UST.
146 144
147 Returns: 0 indecisive; 1 = UST; 2 = 15-inst */ 145 Returns: 0 indecisive; 1 = UST; 2 = 15-inst */
148static int CheckPatternType(int numpat) 146static int CheckPatternType(int numpat)
@@ -152,7 +150,7 @@ static int CheckPatternType(int numpat)
152 150
153 for(t=0;t<numpat*(64U*4);t++) { 151 for(t=0;t<numpat*(64U*4);t++) {
154 /* Load the pattern into the temp buffer and scan it */ 152 /* Load the pattern into the temp buffer and scan it */
155 (void)_mm_read_UBYTE(modreader);(void)_mm_read_UBYTE(modreader); 153 _mm_skip_BYTE(modreader);_mm_skip_BYTE(modreader);
156 eff = _mm_read_UBYTE(modreader); 154 eff = _mm_read_UBYTE(modreader);
157 dat = _mm_read_UBYTE(modreader); 155 dat = _mm_read_UBYTE(modreader);
158 156
@@ -177,55 +175,56 @@ static int CheckPatternType(int numpat)
177static int M15_Test(void) 175static int M15_Test(void)
178{ 176{
179 int t, numpat; 177 int t, numpat;
180 MODULEHEADER mh; 178 MODULEHEADER h;
181 179
182 ust_loader = 0; 180 ust_loader = 0;
183 if(!LoadModuleHeader(&mh)) return 0; 181 memset(&h, 0, sizeof(MODULEHEADER));
182 if(!LoadModuleHeader(&h)) return 0;
184 183
185 /* reject other file types */ 184 /* reject other file types */
186 for(t=0;t<REJECT;t++) 185 for(t=0;t<REJECT;t++)
187 if(!memcmp(mh.songname,signatures[t],siglen[t])) return 0; 186 if(!memcmp(h.songname,signatures[t],siglen[t])) return 0;
188 187
189 if(mh.magic1>127) return 0; 188 if(h.magic1>127) return 0;
190 if((!mh.songlength)||(mh.songlength>mh.magic1)) return 0; 189 if((!h.songlength)||(h.songlength>h.magic1)) return 0;
191 190
192 for(t=0;t<15;t++) { 191 for(t=0;t<15;t++) {
193 /* all finetunes should be zero */ 192 /* all finetunes should be zero */
194 if(mh.samples[t].finetune) return 0; 193 if(h.samples[t].finetune) return 0;
195 194
196 /* all volumes should be <= 64 */ 195 /* all volumes should be <= 64 */
197 if(mh.samples[t].volume>64) return 0; 196 if(h.samples[t].volume>64) return 0;
198 197
199 /* all instrument names should begin with s, st-, or a number */ 198 /* all instrument names should begin with s, st-, or a number */
200 if((mh.samples[t].samplename[0]=='s')|| 199 if((h.samples[t].samplename[0]=='s')||
201 (mh.samples[t].samplename[0]=='S')) { 200 (h.samples[t].samplename[0]=='S')) {
202 if((memcmp(mh.samples[t].samplename,"st-",3)) && 201 if((memcmp(h.samples[t].samplename,"st-",3)) &&
203 (memcmp(mh.samples[t].samplename,"ST-",3)) && 202 (memcmp(h.samples[t].samplename,"ST-",3)) &&
204 (*mh.samples[t].samplename)) 203 (*h.samples[t].samplename))
205 ust_loader = 1; 204 ust_loader = 1;
206 } else 205 } else
207 if(!isdigit((int)mh.samples[t].samplename[0])) 206 if(!isdigit((int)h.samples[t].samplename[0]))
208 ust_loader = 1; 207 ust_loader = 1;
209 208
210 if(mh.samples[t].length>4999||mh.samples[t].reppos>9999) { 209 if(h.samples[t].length>4999||h.samples[t].reppos>9999) {
211 ust_loader = 0; 210 ust_loader = 0;
212 if(mh.samples[t].length>32768) return 0; 211 if(h.samples[t].length>32768) return 0;
213 } 212 }
214 213
215 /* if loop information is incorrect as words, but correct as bytes, 214 /* if loop information is incorrect as words, but correct as bytes,
216 this is likely to be an ust-style module */ 215 this is likely to be an ust-style module */
217 if((mh.samples[t].reppos+mh.samples[t].replen>mh.samples[t].length)&& 216 if((h.samples[t].reppos+h.samples[t].replen>h.samples[t].length)&&
218 (mh.samples[t].reppos+mh.samples[t].replen<(mh.samples[t].length<<1))){ 217 (h.samples[t].reppos+h.samples[t].replen<(h.samples[t].length<<1))) {
219 ust_loader = 1; 218 ust_loader = 1;
220 return 1; 219 return 1;
221 } 220 }
222 221
223 if(!ust_loader) return 1; 222 if(!ust_loader) return 1;
224 } 223 }
225 224
226 for(numpat=0,t=0;t<mh.songlength;t++) 225 for(numpat=0,t=0;t<h.songlength;t++)
227 if(mh.positions[t]>numpat) 226 if(h.positions[t]>numpat)
228 numpat = mh.positions[t]; 227 numpat = h.positions[t];
229 numpat++; 228 numpat++;
230 switch(CheckPatternType(numpat)) { 229 switch(CheckPatternType(numpat)) {
231 case 0: /* indecisive, so check more clues... */ 230 case 0: /* indecisive, so check more clues... */
@@ -242,7 +241,7 @@ static int M15_Test(void)
242 241
243static int M15_Init(void) 242static int M15_Init(void)
244{ 243{
245 if(!(mh=(MODULEHEADER*)MikMod_malloc(sizeof(MODULEHEADER)))) return 0; 244 if(!(mh=(MODULEHEADER*)MikMod_calloc(1,sizeof(MODULEHEADER)))) return 0;
246 return 1; 245 return 1;
247} 246}
248 247
@@ -250,6 +249,8 @@ static void M15_Cleanup(void)
250{ 249{
251 MikMod_free(mh); 250 MikMod_free(mh);
252 MikMod_free(patbuf); 251 MikMod_free(patbuf);
252 mh=NULL;
253 patbuf=NULL;
253} 254}
254 255
255/* 256/*
@@ -331,7 +332,7 @@ static UBYTE M15_ConvertNote(MODNOTE* n, UBYTE lasteffect)
331 case 1: 332 case 1:
332 UniPTEffect(0,effdat); 333 UniPTEffect(0,effdat);
333 break; 334 break;
334 case 2: 335 case 2:
335 if(effdat&0xf) UniPTEffect(1,effdat&0xf); 336 if(effdat&0xf) UniPTEffect(1,effdat&0xf);
336 else if(effdat>>2) UniPTEffect(2,effdat>>2); 337 else if(effdat>>2) UniPTEffect(2,effdat>>2);
337 break; 338 break;
@@ -351,7 +352,7 @@ static UBYTE M15_ConvertNote(MODNOTE* n, UBYTE lasteffect)
351 } 352 }
352 if (effect == 8) 353 if (effect == 8)
353 of.flags |= UF_PANNING; 354 of.flags |= UF_PANNING;
354 355
355 return effect; 356 return effect;
356} 357}
357 358
@@ -372,8 +373,7 @@ static UBYTE *M15_ConvertTrack(MODNOTE* n)
372/* Loads all patterns of a modfile and converts them into the 3 byte format. */ 373/* Loads all patterns of a modfile and converts them into the 3 byte format. */
373static int M15_LoadPatterns(void) 374static int M15_LoadPatterns(void)
374{ 375{
375 int t,tracks=0; 376 unsigned int t,s,tracks=0;
376 unsigned int s;
377 377
378 if(!AllocPatterns()) return 0; 378 if(!AllocPatterns()) return 0;
379 if(!AllocTracks()) return 0; 379 if(!AllocTracks()) return 0;
@@ -398,9 +398,10 @@ static int M15_LoadPatterns(void)
398 398
399static int M15_Load(int curious) 399static int M15_Load(int curious)
400{ 400{
401 int t,scan; 401 unsigned int t,scan;
402 SAMPLE *q; 402 SAMPLE *q;
403 MSAMPINFO *s; 403 MSAMPINFO *s;
404 (void)curious;
404 405
405 /* try to read module header */ 406 /* try to read module header */
406 if(!LoadModuleHeader(mh)) { 407 if(!LoadModuleHeader(mh)) {
@@ -409,14 +410,14 @@ static int M15_Load(int curious)
409 } 410 }
410 411
411 if(ust_loader) 412 if(ust_loader)
412 of.modtype = StrDup("Ultimate Soundtracker"); 413 of.modtype = MikMod_strdup("Ultimate Soundtracker");
413 else 414 else
414 of.modtype = StrDup("Soundtracker"); 415 of.modtype = MikMod_strdup("Soundtracker");
415 416
416 /* set module variables */ 417 /* set module variables */
417 of.initspeed = 6; 418 of.initspeed = 6;
418 of.inittempo = 125; 419 of.inittempo = 125;
419 of.numchn = 4; 420 of.numchn = 4;
420 of.songname = DupStr(mh->songname,21,1); 421 of.songname = DupStr(mh->songname,21,1);
421 of.numpos = mh->songlength; 422 of.numpos = mh->songlength;
422 of.reppos = 0; 423 of.reppos = 0;
@@ -467,7 +468,7 @@ static int M15_Load(int curious)
467 q->length = s->length<<1; 468 q->length = s->length<<1;
468 469
469 q->flags = SF_SIGNED; 470 q->flags = SF_SIGNED;
470 if(ust_loader) q->flags |= SF_UST_LOOP; 471 if(ust_loader) q->flags |= SF_UST_LOOP;
471 if(s->replen>2) q->flags |= SF_LOOP; 472 if(s->replen>2) q->flags |= SF_LOOP;
472 473
473 s++; 474 s++;