1 /* Extended Module Player
2  * Copyright (C) 1996-2021 Claudio Matsuoka and Hipolito Carraro Jr
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef LIBXMP_CORE_DISABLE_IT
24 
25 #include "loader.h"
26 #include "it.h"
27 #include "period.h"
28 
29 #define MAGIC_IMPM	MAGIC4('I','M','P','M')
30 #define MAGIC_IMPI	MAGIC4('I','M','P','I')
31 #define MAGIC_IMPS	MAGIC4('I','M','P','S')
32 
33 
34 static int it_test(HIO_HANDLE *, char *, const int);
35 static int it_load(struct module_data *, HIO_HANDLE *, const int);
36 
37 const struct format_loader libxmp_loader_it = {
38 	"Impulse Tracker",
39 	it_test,
40 	it_load
41 };
42 
it_test(HIO_HANDLE * f,char * t,const int start)43 static int it_test(HIO_HANDLE *f, char *t, const int start)
44 {
45 	if (hio_read32b(f) != MAGIC_IMPM)
46 		return -1;
47 
48 	libxmp_read_title(f, t, 26);
49 
50 	return 0;
51 }
52 
53 
54 #define	FX_NONE	0xff
55 #define FX_XTND 0xfe
56 #define L_CHANNELS 64
57 
58 
59 static const uint8 fx[32] = {
60 	/*   */ FX_NONE,
61 	/* A */ FX_S3M_SPEED,
62 	/* B */ FX_JUMP,
63 	/* C */ FX_IT_BREAK,
64 	/* D */ FX_VOLSLIDE,
65 	/* E */ FX_PORTA_DN,
66 	/* F */ FX_PORTA_UP,
67 	/* G */ FX_TONEPORTA,
68 	/* H */ FX_VIBRATO,
69 	/* I */ FX_TREMOR,
70 	/* J */ FX_S3M_ARPEGGIO,
71 	/* K */ FX_VIBRA_VSLIDE,
72 	/* L */ FX_TONE_VSLIDE,
73 	/* M */ FX_TRK_VOL,
74 	/* N */ FX_TRK_VSLIDE,
75 	/* O */ FX_OFFSET,
76 	/* P */ FX_IT_PANSLIDE,
77 	/* Q */ FX_MULTI_RETRIG,
78 	/* R */ FX_TREMOLO,
79 	/* S */ FX_XTND,
80 	/* T */ FX_IT_BPM,
81 	/* U */ FX_FINE_VIBRATO,
82 	/* V */ FX_GLOBALVOL,
83 	/* W */ FX_GVOL_SLIDE,
84 	/* X */ FX_SETPAN,
85 	/* Y */ FX_PANBRELLO,
86 	/* Z */ FX_FLT_CUTOFF,
87 	/* ? */ FX_NONE,
88 	/* ? */ FX_NONE,
89 	/* ? */ FX_NONE,
90 	/* ? */ FX_NONE,
91 	/* ? */ FX_NONE
92 };
93 
94 
95 int itsex_decompress8 (HIO_HANDLE *, void *, int, int);
96 int itsex_decompress16 (HIO_HANDLE *, void *, int, int);
97 
98 
xlat_fx(int c,struct xmp_event * e,uint8 * last_fxp,int new_fx)99 static void xlat_fx(int c, struct xmp_event *e, uint8 *last_fxp, int new_fx)
100 {
101 	uint8 h = MSN(e->fxp), l = LSN(e->fxp);
102 
103 	switch (e->fxt = fx[e->fxt]) {
104 	case FX_XTND:		/* Extended effect */
105 		e->fxt = FX_EXTENDED;
106 
107 		if (h == 0 && e->fxp == 0) {
108 			e->fxp = last_fxp[c];
109 			h = MSN(e->fxp);
110 			l = LSN(e->fxp);
111 		} else {
112 			last_fxp[c] = e->fxp;
113 		}
114 
115 		switch (h) {
116 		case 0x1:	/* Glissando */
117 			e->fxp = 0x30 | l;
118 			break;
119 		case 0x2:	/* Finetune -- not supported */
120 			e->fxt = e->fxp = 0;
121 			break;
122 		case 0x3:	/* Vibrato wave */
123 			e->fxp = 0x40 | l;
124 			break;
125 		case 0x4:	/* Tremolo wave */
126 			e->fxp = 0x70 | l;
127 			break;
128 		case 0x5:	/* Panbrello wave */
129 			if (l <= 3) {
130 				e->fxt = FX_PANBRELLO_WF;
131 				e->fxp = l;
132 			} else {
133 				e->fxt = e->fxp = 0;
134 			}
135 			break;
136 		case 0x6:	/* Pattern delay */
137 			e->fxp = 0xe0 | l;
138 			break;
139 		case 0x7:	/* Instrument functions */
140 			e->fxt = FX_IT_INSTFUNC;
141 			e->fxp &= 0x0f;
142 			break;
143 		case 0x8:	/* Set pan position */
144 			e->fxt = FX_SETPAN;
145 			e->fxp = l << 4;
146 			break;
147 		case 0x9:	/* 0x91 = set surround */
148 			e->fxt = FX_SURROUND;
149 			e->fxp = l;
150 			break;
151 		case 0xa:	/* High offset */
152 			e->fxt = FX_HIOFFSET;
153 			e->fxp = l;
154 			break;
155 		case 0xb:	/* Pattern loop */
156 			e->fxp = 0x60 | l;
157 			break;
158 		case 0xc:	/* Note cut */
159 		case 0xd:	/* Note delay */
160 			if ((e->fxp = l) == 0)
161 				e->fxp++;  /* SD0 and SC0 become SD1 and SC1 */
162 			e->fxp |= h << 4;
163 			break;
164 		case 0xe:	/* Pattern row delay */
165 			e->fxt = FX_IT_ROWDELAY;
166 			e->fxp = l;
167 			break;
168 		default:
169 			e->fxt = e->fxp = 0;
170 		}
171 		break;
172 	case FX_FLT_CUTOFF:
173 		if (e->fxp > 0x7f && e->fxp < 0x90) {	/* Resonance */
174 			e->fxt = FX_FLT_RESN;
175 			e->fxp = (e->fxp - 0x80) * 16;
176 		} else {	/* Cutoff */
177 			e->fxp *= 2;
178 		}
179 		break;
180 	case FX_TREMOR:
181 		if (!new_fx && e->fxp != 0) {
182 			e->fxp = ((MSN(e->fxp) + 1) << 4) | (LSN(e->fxp) + 1);
183 		}
184 		break;
185 	case FX_GLOBALVOL:
186 		if (e->fxp > 0x80) {	/* See storlek test 16 */
187 			e->fxt = e->fxp = 0;
188 		}
189 		break;
190 	case FX_NONE:		/* No effect */
191 		e->fxt = e->fxp = 0;
192 		break;
193 	}
194 }
195 
196 
xlat_volfx(struct xmp_event * event)197 static void xlat_volfx(struct xmp_event *event)
198 {
199 	int b;
200 
201 	b = event->vol;
202 	event->vol = 0;
203 
204 	if (b <= 0x40) {
205 		event->vol = b + 1;
206 	} else if (b >= 65 && b <= 74) {	/* A */
207 		event->f2t = FX_F_VSLIDE_UP_2;
208 		event->f2p = b - 65;
209 	} else if (b >= 75 && b <= 84) {	/* B */
210 		event->f2t = FX_F_VSLIDE_DN_2;
211 		event->f2p = b - 75;
212 	} else if (b >= 85 && b <= 94) {	/* C */
213 		event->f2t = FX_VSLIDE_UP_2;
214 		event->f2p = b - 85;
215 	} else if (b >= 95 && b <= 104) {	/* D */
216 		event->f2t = FX_VSLIDE_DN_2;
217 		event->f2p = b - 95;
218 	} else if (b >= 105 && b <= 114) {	/* E */
219 		event->f2t = FX_PORTA_DN;
220 		event->f2p = (b - 105) << 2;
221 	} else if (b >= 115 && b <= 124) {	/* F */
222 		event->f2t = FX_PORTA_UP;
223 		event->f2p = (b - 115) << 2;
224 	} else if (b >= 128 && b <= 192) {	/* pan */
225 		if (b == 192) {
226 			event->f2p = 0xff;
227 		} else {
228 			event->f2p = (b - 128) << 2;
229 		}
230 		event->f2t = FX_SETPAN;
231 	} else if (b >= 193 && b <= 202) {	/* G */
232 		uint8 val[10] = {
233 			0x00, 0x01, 0x04, 0x08, 0x10,
234 			0x20, 0x40, 0x60, 0x80, 0xff
235 		};
236 		event->f2t = FX_TONEPORTA;
237 		event->f2p = val[b - 193];
238 	} else if (b >= 203 && b <= 212) {	/* H */
239 		event->f2t = FX_VIBRATO;
240 		event->f2p = b - 203;
241 	}
242 }
243 
244 
fix_name(uint8 * s,int l)245 static void fix_name(uint8 *s, int l)
246 {
247 	int i;
248 
249 	/* IT names can have 0 at start of data, replace with space */
250 	for (l--, i = 0; i < l; i++) {
251 		if (s[i] == 0)
252 			s[i] = ' ';
253 	}
254 	for (i--; i >= 0 && s[i] == ' '; i--) {
255 		if (s[i] == ' ')
256 			s[i] = 0;
257 	}
258 }
259 
260 
read_envelope(struct xmp_envelope * ei,struct it_envelope * env,HIO_HANDLE * f)261 static int read_envelope(struct xmp_envelope *ei, struct it_envelope *env,
262 			  HIO_HANDLE *f)
263 {
264 	int i;
265 	uint8 buf[82];
266 
267 	if (hio_read(buf, 1, 82, f) != 82) {
268 		return -1;
269 	}
270 
271 	env->flg = buf[0];
272 	env->num = MIN(buf[1], 25); /* Clamp to IT max */
273 
274 	env->lpb = buf[2];
275 	env->lpe = buf[3];
276 	env->slb = buf[4];
277 	env->sle = buf[5];
278 
279 	for (i = 0; i < 25; i++) {
280 		env->node[i].y = buf[6 + i * 3];
281 		env->node[i].x = readmem16l(buf + 7 + i * 3);
282 	}
283 
284 	ei->flg = env->flg & IT_ENV_ON ? XMP_ENVELOPE_ON : 0;
285 
286 	if (env->flg & IT_ENV_LOOP) {
287 		ei->flg |= XMP_ENVELOPE_LOOP;
288 	}
289 
290 	if (env->flg & IT_ENV_SLOOP) {
291 		ei->flg |= XMP_ENVELOPE_SUS | XMP_ENVELOPE_SLOOP;
292 	}
293 
294 	if (env->flg & IT_ENV_CARRY) {
295 		ei->flg |= XMP_ENVELOPE_CARRY;
296 	}
297 
298 	ei->npt = env->num;
299 	ei->sus = env->slb;
300 	ei->sue = env->sle;
301 	ei->lps = env->lpb;
302 	ei->lpe = env->lpe;
303 
304 	if (ei->npt > 0 && ei->npt <= 25 /* XMP_MAX_ENV_POINTS */) {
305 		for (i = 0; i < ei->npt; i++) {
306 			ei->data[i * 2] = env->node[i].x;
307 			ei->data[i * 2 + 1] = env->node[i].y;
308 		}
309 	} else {
310 		ei->flg &= ~XMP_ENVELOPE_ON;
311 	}
312 
313 	return 0;
314 }
315 
identify_tracker(struct module_data * m,struct it_file_header * ifh)316 static void identify_tracker(struct module_data *m, struct it_file_header *ifh)
317 {
318 #ifndef LIBXMP_CORE_PLAYER
319 	char tracker_name[40];
320 	int sample_mode = ~ifh->flags & IT_USE_INST;
321 
322 	switch (ifh->cwt >> 8) {
323 	case 0x00:
324 		strcpy(tracker_name, "unmo3");
325 		break;
326 	case 0x01:
327 	case 0x02:		/* test from Schism Tracker sources */
328 		if (ifh->cmwt == 0x0200 && ifh->cwt == 0x0214
329 		    && ifh->flags == 9 && ifh->special == 0
330 		    && ifh->hilite_maj == 0 && ifh->hilite_min == 0
331 		    && ifh->insnum == 0 && ifh->patnum + 1 == ifh->ordnum
332 		    && ifh->gv == 128 && ifh->mv == 100 && ifh->is == 1
333 		    && ifh->sep == 128 && ifh->pwd == 0
334 		    && ifh->msglen == 0 && ifh->msgofs == 0 && ifh->rsvd == 0) {
335 			strcpy(tracker_name, "OpenSPC conversion");
336 		} else if (ifh->cmwt == 0x0200 && ifh->cwt == 0x0217) {
337 			strcpy(tracker_name, "ModPlug Tracker 1.16");
338 			/* ModPlug Tracker files aren't really IMPM 2.00 */
339 			ifh->cmwt = sample_mode ? 0x100 : 0x214;
340 		} else if (ifh->cwt == 0x0216) {
341 			strcpy(tracker_name, "Impulse Tracker 2.14v3");
342 		} else if (ifh->cwt == 0x0217) {
343 			strcpy(tracker_name, "Impulse Tracker 2.14v5");
344 		} else if (ifh->cwt == 0x0214 && !memcmp(&ifh->rsvd, "CHBI", 4)) {
345 			strcpy(tracker_name, "Chibi Tracker");
346 		} else {
347 			snprintf(tracker_name, 40, "Impulse Tracker %d.%02x",
348 				 (ifh->cwt & 0x0f00) >> 8, ifh->cwt & 0xff);
349 		}
350 		break;
351 	case 0x08:
352 	case 0x7f:
353 		if (ifh->cwt == 0x0888) {
354 			strcpy(tracker_name, "OpenMPT 1.17");
355 		} else if (ifh->cwt == 0x7fff) {
356 			strcpy(tracker_name, "munch.py");
357 		} else {
358 			snprintf(tracker_name, 40, "unknown (%04x)", ifh->cwt);
359 		}
360 		break;
361 	default:
362 		switch (ifh->cwt >> 12) {
363 		case 0x1:
364 			libxmp_schism_tracker_string(tracker_name, 40,
365 				(ifh->cwt & 0x0fff), ifh->rsvd);
366 			break;
367 		case 0x5:
368 			snprintf(tracker_name, 40, "OpenMPT %d.%02x",
369 				 (ifh->cwt & 0x0f00) >> 8, ifh->cwt & 0xff);
370 			if (memcmp(&ifh->rsvd, "OMPT", 4))
371 				strncat(tracker_name, " (compat.)", 39);
372 			break;
373 		case 0x06:
374 			snprintf(tracker_name, 40, "BeRoTracker %d.%02x",
375 				 (ifh->cwt & 0x0f00) >> 8, ifh->cwt & 0xff);
376 			break;
377 		default:
378 			snprintf(tracker_name, 40, "unknown (%04x)", ifh->cwt);
379 		}
380 	}
381 
382 	libxmp_set_type(m, "%s IT %d.%02x", tracker_name, ifh->cmwt >> 8,
383 						ifh->cmwt & 0xff);
384 #else
385 	libxmp_set_type(m, "Impulse Tracker");
386 #endif
387 }
388 
load_old_it_instrument(struct xmp_instrument * xxi,HIO_HANDLE * f)389 static int load_old_it_instrument(struct xmp_instrument *xxi, HIO_HANDLE *f)
390 {
391 	int inst_map[120], inst_rmap[XMP_MAX_KEYS];
392 	struct it_instrument1_header i1h;
393 	int c, k, j;
394 	uint8 buf[64];
395 
396 	if (hio_read(buf, 1, 64, f) != 64) {
397 		return -1;
398 	}
399 
400 	i1h.magic = readmem32b(buf);
401 	if (i1h.magic != MAGIC_IMPI) {
402 		D_(D_CRIT "bad instrument magic");
403 		return -1;
404 	}
405 	memcpy(i1h.dosname, buf + 4, 12);
406 	i1h.zero = buf[16];
407 	i1h.flags = buf[17];
408 	i1h.vls = buf[18];
409 	i1h.vle = buf[19];
410 	i1h.sls = buf[20];
411 	i1h.sle = buf[21];
412 	i1h.fadeout = readmem16l(buf + 24);
413 	i1h.nna = buf[26];
414 	i1h.dnc = buf[27];
415 	i1h.trkvers = readmem16l(buf + 28);
416 	i1h.nos = buf[30];
417 
418 	memcpy(i1h.name, buf + 32, 26);
419 	fix_name(i1h.name, 26);
420 
421 	if (hio_read(i1h.keys, 1, 240, f) != 240) {
422 		return -1;
423 	}
424 	if (hio_read(i1h.epoint, 1, 200, f) != 200) {
425 		return -1;
426 	}
427 	if (hio_read(i1h.enode, 1, 50, f) != 50) {
428 		return -1;
429 	}
430 
431 	libxmp_copy_adjust(xxi->name, i1h.name, 25);
432 
433 	xxi->rls = i1h.fadeout << 7;
434 
435 	xxi->aei.flg = 0;
436 	if (i1h.flags & IT_ENV_ON) {
437 		xxi->aei.flg |= XMP_ENVELOPE_ON;
438 	}
439 	if (i1h.flags & IT_ENV_LOOP) {
440 		xxi->aei.flg |= XMP_ENVELOPE_LOOP;
441 	}
442 	if (i1h.flags & IT_ENV_SLOOP) {
443 		xxi->aei.flg |= XMP_ENVELOPE_SUS | XMP_ENVELOPE_SLOOP;
444 	}
445 	if (i1h.flags & IT_ENV_CARRY) {
446 		xxi->aei.flg |= XMP_ENVELOPE_SUS | XMP_ENVELOPE_CARRY;
447 	}
448 	xxi->aei.lps = i1h.vls;
449 	xxi->aei.lpe = i1h.vle;
450 	xxi->aei.sus = i1h.sls;
451 	xxi->aei.sue = i1h.sle;
452 
453 	for (k = 0; k < 25 && i1h.enode[k * 2] != 0xff; k++) ;
454 
455 	/* Sanity check */
456 	if (k >= 25 || i1h.enode[k * 2] != 0xff) {
457 		return -1;
458 	}
459 
460 	for (xxi->aei.npt = k; k--;) {
461 		xxi->aei.data[k * 2] = i1h.enode[k * 2];
462 		xxi->aei.data[k * 2 + 1] = i1h.enode[k * 2 + 1];
463 	}
464 
465 	/* See how many different instruments we have */
466 	for (j = 0; j < 120; j++)
467 		inst_map[j] = -1;
468 
469 	for (k = j = 0; j < XMP_MAX_KEYS; j++) {
470 		c = j < 120 ? i1h.keys[j * 2 + 1] - 1 : -1;
471 		if (c < 0 || c >= 120) {
472 			xxi->map[j].ins = 0;
473 			xxi->map[j].xpo = 0;
474 			continue;
475 		}
476 		if (inst_map[c] == -1) {
477 			inst_map[c] = k;
478 			inst_rmap[k] = c;
479 			k++;
480 		}
481 		xxi->map[j].ins = inst_map[c];
482 		xxi->map[j].xpo = i1h.keys[j * 2] - j;
483 	}
484 
485 	xxi->nsm = k;
486 	xxi->vol = 0x40;
487 
488 	if (k) {
489 		xxi->sub = calloc(sizeof(struct xmp_subinstrument), k);
490 		if (xxi->sub == NULL) {
491 			return -1;
492 		}
493 
494 		for (j = 0; j < k; j++) {
495 			struct xmp_subinstrument *sub = &xxi->sub[j];
496 
497 			sub->sid = inst_rmap[j];
498 			sub->nna = i1h.nna;
499 			sub->dct =
500 			    i1h.dnc ? XMP_INST_DCT_NOTE : XMP_INST_DCT_OFF;
501 			sub->dca = XMP_INST_DCA_CUT;
502 			sub->pan = -1;
503 		}
504 	}
505 
506 	D_(D_INFO "[  ] %-26.26s %d %-4.4s %4d %2d %c%c%c %3d",
507 	   /*i,*/ i1h.name,
508 	   i1h.nna,
509 	   i1h.dnc ? "on" : "off",
510 	   i1h.fadeout,
511 	   xxi->aei.npt,
512 	   xxi->aei.flg & XMP_ENVELOPE_ON ? 'V' : '-',
513 	   xxi->aei.flg & XMP_ENVELOPE_LOOP ? 'L' : '-',
514 	   xxi->aei.flg & XMP_ENVELOPE_SUS ? 'S' : '-', xxi->nsm);
515 
516 	return 0;
517 }
518 
load_new_it_instrument(struct xmp_instrument * xxi,HIO_HANDLE * f)519 static int load_new_it_instrument(struct xmp_instrument *xxi, HIO_HANDLE *f)
520 {
521 	int inst_map[120], inst_rmap[XMP_MAX_KEYS];
522 	struct it_instrument2_header i2h;
523 	struct it_envelope env;
524 	int dca2nna[] = { 0, 2, 3, 3 /* Northern Sky (cj-north.it) has this... */ };
525 	int c, k, j;
526 	uint8 buf[64];
527 
528 	if (hio_read(buf, 1, 64, f) != 64) {
529 		return -1;
530 	}
531 
532 	i2h.magic = readmem32b(buf);
533 	if (i2h.magic != MAGIC_IMPI) {
534 		D_(D_CRIT "bad instrument magic");
535 		return -1;
536 	}
537 	memcpy(i2h.dosname, buf + 4, 12);
538 	i2h.zero = buf[16];
539 	i2h.nna = buf[17];
540 	i2h.dct = buf[18];
541 	i2h.dca = buf[19];
542 
543 	/* Sanity check */
544 	if (i2h.dca > 3) {
545 		/* Northern Sky has an instrument with DCA 3 */
546 		D_(D_WARN "bad instrument dca: %d", i2h.dca);
547 		i2h.dca = 0;
548 	}
549 
550 	i2h.fadeout = readmem16l(buf + 20);
551 	i2h.pps = buf[22];
552 	i2h.ppc = buf[23];
553 	i2h.gbv = buf[24];
554 	i2h.dfp = buf[25];
555 	i2h.rv = buf[26];
556 	i2h.rp = buf[27];
557 	i2h.trkvers = readmem16l(buf + 28);
558 	i2h.nos = buf[30];
559 
560 	memcpy(i2h.name, buf + 32, 26);
561 	fix_name(i2h.name, 26);
562 
563 	i2h.ifc = buf[58];
564 	i2h.ifr = buf[59];
565 	i2h.mch = buf[60];
566 	i2h.mpr = buf[61];
567 	i2h.mbnk = readmem16l(buf + 62);
568 
569 	if (hio_read(i2h.keys, 1, 240, f) != 240) {
570 		D_(D_CRIT "key map read error");
571 		return -1;
572 	}
573 
574 	libxmp_copy_adjust(xxi->name, i2h.name, 25);
575 	xxi->rls = i2h.fadeout << 6;
576 
577 	/* Envelopes */
578 
579 	if (read_envelope(&xxi->aei, &env, f) < 0) {
580 		return -1;
581 	}
582 	if (read_envelope(&xxi->pei, &env, f) < 0) {
583 		return -1;
584 	}
585 	if (read_envelope(&xxi->fei, &env, f) < 0) {
586 		return -1;
587 	}
588 
589 	if (xxi->pei.flg & XMP_ENVELOPE_ON) {
590 		for (j = 0; j < xxi->pei.npt; j++)
591 			xxi->pei.data[j * 2 + 1] += 32;
592 	}
593 
594 	if (xxi->aei.flg & XMP_ENVELOPE_ON && xxi->aei.npt == 0) {
595 		xxi->aei.npt = 1;
596 	}
597 	if (xxi->pei.flg & XMP_ENVELOPE_ON && xxi->pei.npt == 0) {
598 		xxi->pei.npt = 1;
599 	}
600 	if (xxi->fei.flg & XMP_ENVELOPE_ON && xxi->fei.npt == 0) {
601 		xxi->fei.npt = 1;
602 	}
603 
604 	if (env.flg & IT_ENV_FILTER) {
605 		xxi->fei.flg |= XMP_ENVELOPE_FLT;
606 		for (j = 0; j < env.num; j++) {
607 			xxi->fei.data[j * 2 + 1] += 32;
608 			xxi->fei.data[j * 2 + 1] *= 4;
609 		}
610 	} else {
611 		/* Pitch envelope is *50 to get fine interpolation */
612 		for (j = 0; j < env.num; j++)
613 			xxi->fei.data[j * 2 + 1] *= 50;
614 	}
615 
616 	/* See how many different instruments we have */
617 	for (j = 0; j < 120; j++)
618 		inst_map[j] = -1;
619 
620 	for (k = j = 0; j < 120; j++) {
621 		c = i2h.keys[j * 2 + 1] - 1;
622 		if (c < 0 || c >= 120) {
623 			xxi->map[j].ins = 0xff;	/* No sample */
624 			xxi->map[j].xpo = 0;
625 			continue;
626 		}
627 		if (inst_map[c] == -1) {
628 			inst_map[c] = k;
629 			inst_rmap[k] = c;
630 			k++;
631 		}
632 		xxi->map[j].ins = inst_map[c];
633 		xxi->map[j].xpo = i2h.keys[j * 2] - j;
634 	}
635 
636 	xxi->nsm = k;
637 	xxi->vol = i2h.gbv >> 1;
638 
639 	if (k) {
640 		xxi->sub = calloc(sizeof(struct xmp_subinstrument), k);
641 		if (xxi->sub == NULL)
642 			return -1;
643 
644 		for (j = 0; j < k; j++) {
645 			struct xmp_subinstrument *sub = &xxi->sub[j];
646 
647 			sub->sid = inst_rmap[j];
648 			sub->nna = i2h.nna;
649 			sub->dct = i2h.dct;
650 			sub->dca = dca2nna[i2h.dca];
651 			sub->pan = i2h.dfp & 0x80 ? -1 : i2h.dfp * 4;
652 			sub->ifc = i2h.ifc;
653 			sub->ifr = i2h.ifr;
654 			sub->rvv = ((int)i2h.rp << 8) | i2h.rv;
655 		}
656 	}
657 
658 	D_(D_INFO "[  ] %-26.26s %d %d %d %4d %4d  %2x "
659 	   "%02x %c%c%c %3d %02x %02x",
660 	   /*i,*/ i2h.name,
661 	   i2h.nna, i2h.dct, i2h.dca,
662 	   i2h.fadeout,
663 	   i2h.gbv,
664 	   i2h.dfp & 0x80 ? 0x80 : i2h.dfp * 4,
665 	   i2h.rv,
666 	   xxi->aei.flg & XMP_ENVELOPE_ON ? 'V' : '-',
667 	   xxi->pei.flg & XMP_ENVELOPE_ON ? 'P' : '-',
668 	   env.flg & 0x01 ? env.flg & 0x80 ? 'F' : 'P' : '-',
669 	   xxi->nsm, i2h.ifc, i2h.ifr);
670 
671 	return 0;
672 }
673 
force_sample_length(struct xmp_sample * xxs,int len)674 static void force_sample_length(struct xmp_sample *xxs, int len)
675 {
676 	xxs->len = len;
677 
678 	if (xxs->lpe > xxs->len)
679 		xxs->lpe = xxs->len;
680 
681 	if (xxs->lps >= xxs->len)
682 		xxs->flg &= ~XMP_SAMPLE_LOOP;
683 }
684 
load_it_sample(struct module_data * m,int i,int start,int sample_mode,HIO_HANDLE * f)685 static int load_it_sample(struct module_data *m, int i, int start,
686 			  int sample_mode, HIO_HANDLE *f)
687 {
688 	struct it_sample_header ish;
689 	struct xmp_module *mod = &m->mod;
690 	struct xmp_sample *xxs, *xsmp;
691 	int j, k;
692 	uint8 buf[80];
693 
694 	if (sample_mode) {
695 		mod->xxi[i].sub = calloc(sizeof(struct xmp_subinstrument), 1);
696 		if (mod->xxi[i].sub == NULL) {
697 			return -1;
698 		}
699 	}
700 
701 	if (hio_read(buf, 1, 80, f) != 80) {
702 		return -1;
703 	}
704 
705 	ish.magic = readmem32b(buf);
706 	/* Changed to continue to allow use-brdg.it and use-funk.it to
707 	 * load correctly (both IT 2.04)
708 	 */
709 	if (ish.magic != MAGIC_IMPS) {
710 		return 0;
711 	}
712 
713 	xxs = &mod->xxs[i];
714 	xsmp = &m->xsmp[i];
715 
716 	memcpy(ish.dosname, buf + 4, 12);
717 	ish.zero = buf[16];
718 	ish.gvl = buf[17];
719 	ish.flags = buf[18];
720 	ish.vol = buf[19];
721 
722 	memcpy(ish.name, buf + 20, 26);
723 	fix_name(ish.name, 26);
724 
725 	ish.convert = buf[46];
726 	ish.dfp = buf[47];
727 	ish.length = readmem32l(buf + 48);
728 	ish.loopbeg = readmem32l(buf + 52);
729 	ish.loopend = readmem32l(buf + 56);
730 	ish.c5spd = readmem32l(buf + 60);
731 	ish.sloopbeg = readmem32l(buf + 64);
732 	ish.sloopend = readmem32l(buf + 68);
733 	ish.sample_ptr = readmem32l(buf + 72);
734 	ish.vis = buf[76];
735 	ish.vid = buf[77];
736 	ish.vir = buf[78];
737 	ish.vit = buf[79];
738 
739 	if (ish.flags & IT_SMP_16BIT) {
740 		xxs->flg = XMP_SAMPLE_16BIT;
741 	}
742 	xxs->len = ish.length;
743 
744 	xxs->lps = ish.loopbeg;
745 	xxs->lpe = ish.loopend;
746 	xxs->flg |= ish.flags & IT_SMP_LOOP ? XMP_SAMPLE_LOOP : 0;
747 	xxs->flg |= ish.flags & IT_SMP_BLOOP ? XMP_SAMPLE_LOOP_BIDIR : 0;
748 	xxs->flg |= ish.flags & IT_SMP_SLOOP ? XMP_SAMPLE_SLOOP : 0;
749 	xxs->flg |= ish.flags & IT_SMP_BSLOOP ? XMP_SAMPLE_SLOOP_BIDIR : 0;
750 
751 	if (ish.flags & IT_SMP_SLOOP) {
752 		memcpy(xsmp, xxs, sizeof (struct xmp_sample));
753 		xsmp->lps = ish.sloopbeg;
754 		xsmp->lpe = ish.sloopend;
755 		xsmp->flg |= XMP_SAMPLE_LOOP;
756 		xsmp->flg &= ~XMP_SAMPLE_LOOP_BIDIR;
757 		if (ish.flags & IT_SMP_BSLOOP) {
758 			xsmp->flg |= XMP_SAMPLE_LOOP_BIDIR;
759 		}
760 	}
761 
762 	if (sample_mode) {
763 		/* Create an instrument for each sample */
764 		mod->xxi[i].vol = 64;
765 		mod->xxi[i].sub[0].vol = ish.vol;
766 		mod->xxi[i].sub[0].pan = 0x80;
767 		mod->xxi[i].sub[0].sid = i;
768 		mod->xxi[i].nsm = !!(xxs->len);
769 		libxmp_instrument_name(mod, i, ish.name, 25);
770 	} else {
771 		libxmp_copy_adjust(xxs->name, ish.name, 25);
772 	}
773 
774 	D_(D_INFO "\n[%2X] %-26.26s %05x%c%05x %05x %05x %05x "
775 	   "%02x%02x %02x%02x %5d ",
776 	   i, sample_mode ? xxs->name : mod->xxs[i].name,
777 	   xxs->len,
778 	   ish.flags & IT_SMP_16BIT ? '+' : ' ',
779 	   MIN(xxs->lps, 0xfffff), MIN(xxs->lpe, 0xfffff),
780 	   MIN(ish.sloopbeg, 0xfffff), MIN(ish.sloopend, 0xfffff),
781 	   ish.flags, ish.convert, ish.vol, ish.gvl, ish.c5spd);
782 
783 	/* Convert C5SPD to relnote/finetune
784 	 *
785 	 * In IT we can have a sample associated with two or more
786 	 * instruments, but c5spd is a sample attribute -- so we must
787 	 * scan all xmp instruments to set the correct transposition
788 	 */
789 
790 	for (j = 0; j < mod->ins; j++) {
791 		for (k = 0; k < mod->xxi[j].nsm; k++) {
792 			struct xmp_subinstrument *sub = &mod->xxi[j].sub[k];
793 			if (sub->sid == i) {
794 				sub->vol = ish.vol;
795 				sub->gvl = ish.gvl;
796 				sub->vra = ish.vis;	/* sample to sub-instrument vibrato */
797 				sub->vde = ish.vid << 1;
798 				sub->vwf = ish.vit;
799 				sub->vsw = (0xff - ish.vir) >> 1;
800 
801 				libxmp_c2spd_to_note(ish.c5spd,
802 					      &mod->xxi[j].sub[k].xpo,
803 					      &mod->xxi[j].sub[k].fin);
804 
805 				/* Set sample pan (overrides subinstrument) */
806 				if (ish.dfp & 0x80) {
807 					sub->pan = (ish.dfp & 0x7f) * 4;
808 				} else if (sample_mode) {
809 					sub->pan = -1;
810 				}
811 			}
812 		}
813 	}
814 
815 	if (ish.flags & IT_SMP_SAMPLE && xxs->len > 1) {
816 		int cvt = 0;
817 
818 		/* Sanity check - some modules may have invalid sizes on
819 		 * unused samples so only check this if the sample flag is set. */
820 		if (xxs->len > MAX_SAMPLE_SIZE) {
821 			return -1;
822 		}
823 
824 		if (0 != hio_seek(f, start + ish.sample_ptr, SEEK_SET))
825 			return -1;
826 
827 		if (xxs->lpe > xxs->len || xxs->lps >= xxs->lpe)
828 			xxs->flg &= ~XMP_SAMPLE_LOOP;
829 
830 		if (~ish.convert & IT_CVT_SIGNED)
831 			cvt |= SAMPLE_FLAG_UNS;
832 
833 		/* compressed samples */
834 		if (ish.flags & IT_SMP_COMP) {
835 			long min_size, file_len, left;
836 			uint8 *buf;
837 			int ret;
838 
839 			/* Sanity check - the lower bound on IT compressed
840 			 * sample size (in bytes) is a little over 1/8th of the
841 			 * number of SAMPLES in the sample.
842 			 */
843 			file_len = hio_size(f);
844 			min_size = xxs->len >> 3;
845 			left = file_len - (long)ish.sample_ptr;
846 			/* No data to read at all? Just skip it... */
847 			if (left <= 0)
848 				return 0;
849 
850 			if ((file_len > 0) && (left < min_size)) {
851 				D_(D_WARN "sample %X failed minimum size check "
852 				   "(len=%d, needs >=%ld bytes, %ld available): "
853 				   "resizing to %ld",
854 				   i, xxs->len, min_size, left, left << 3);
855 
856 				force_sample_length(xxs, left << 3);
857 				if (ish.flags & IT_SMP_SLOOP)
858 					force_sample_length(xsmp, left << 3);
859 			}
860 
861 			buf = calloc(1, xxs->len * 2);
862 			if (buf == NULL)
863 				return -1;
864 
865 			if (ish.flags & IT_SMP_16BIT) {
866 				itsex_decompress16(f, buf, xxs->len,
867 						   ish.convert & IT_CVT_DIFF);
868 
869 #ifdef WORDS_BIGENDIAN
870 				/* decompression generates native-endian
871 				 * samples, but we want little-endian
872 				 */
873 				cvt |= SAMPLE_FLAG_BIGEND;
874 #endif
875 			} else {
876 				itsex_decompress8(f, buf, xxs->len,
877 						  ish.convert & IT_CVT_DIFF);
878 			}
879 
880 			if (ish.flags & IT_SMP_SLOOP) {
881 				long pos = hio_tell(f);
882 				if (pos < 0) {
883 					free(buf);
884 					return -1;
885 				}
886 				ret = libxmp_load_sample(m, NULL, SAMPLE_FLAG_NOLOAD |
887 							cvt, &m->xsmp[i], buf);
888 				if (ret < 0) {
889 					free(buf);
890 					return -1;
891 				}
892 				hio_seek(f, pos, SEEK_SET);
893 			}
894 
895 			ret = libxmp_load_sample(m, NULL, SAMPLE_FLAG_NOLOAD | cvt,
896 					  &mod->xxs[i], buf);
897 			if (ret < 0) {
898 				free(buf);
899 				return -1;
900 			}
901 
902 			free(buf);
903 		} else {
904 			if (ish.flags & IT_SMP_SLOOP) {
905 				long pos = hio_tell(f);
906 				if (pos < 0) {
907 					return -1;
908 				}
909 				if (libxmp_load_sample(m, f, cvt, &m->xsmp[i], NULL) < 0)
910 					return -1;
911 				hio_seek(f, pos, SEEK_SET);
912 			}
913 
914 			if (libxmp_load_sample(m, f, cvt, &mod->xxs[i], NULL) < 0)
915 				return -1;
916 		}
917 	}
918 
919 	return 0;
920 }
921 
load_it_pattern(struct module_data * m,int i,int new_fx,HIO_HANDLE * f)922 static int load_it_pattern(struct module_data *m, int i, int new_fx,
923 			   HIO_HANDLE *f)
924 {
925 	struct xmp_module *mod = &m->mod;
926 	struct xmp_event *event, dummy, lastevent[L_CHANNELS];
927 	uint8 mask[L_CHANNELS];
928 	uint8 last_fxp[64];
929 
930 	int r, c, pat_len;
931 	uint8 b;
932 
933 	r = 0;
934 
935 	memset(last_fxp, 0, sizeof(last_fxp));
936 	memset(lastevent, 0, L_CHANNELS * sizeof(struct xmp_event));
937 	memset(&dummy, 0, sizeof(struct xmp_event));
938 
939 	pat_len = hio_read16l(f) /* - 4 */ ;
940 	mod->xxp[i]->rows = hio_read16l(f);
941 
942 	if (libxmp_alloc_tracks_in_pattern(mod, i) < 0) {
943 		return -1;
944 	}
945 
946 	memset(mask, 0, L_CHANNELS);
947 	hio_read16l(f);
948 	hio_read16l(f);
949 
950 	while (--pat_len >= 0) {
951 		b = hio_read8(f);
952 		if (hio_error(f)) {
953 			return -1;
954 		}
955 		if (!b) {
956 			r++;
957 			continue;
958 		}
959 		c = (b - 1) & 63;
960 
961 		if (b & 0x80) {
962 			mask[c] = hio_read8(f);
963 			pat_len--;
964 		}
965 		/*
966 		 * WARNING: we IGNORE events in disabled channels. Disabled
967 		 * channels should be muted only, but we don't know the
968 		 * real number of channels before loading the patterns and
969 		 * we don't want to set it to 64 channels.
970 		 */
971 		if (c >= mod->chn || r >= mod->xxp[i]->rows) {
972 			event = &dummy;
973 		} else {
974 			event = &EVENT(i, c, r);
975 		}
976 
977 		if (mask[c] & 0x01) {
978 			b = hio_read8(f);
979 
980 			/* From ittech.txt:
981 			 * Note ranges from 0->119 (C-0 -> B-9)
982 			 * 255 = note off, 254 = notecut
983 			 * Others = note fade (already programmed into IT's player
984 			 *                     but not available in the editor)
985 			 */
986 			switch (b) {
987 			case 0xff:	/* key off */
988 				b = XMP_KEY_OFF;
989 				break;
990 			case 0xfe:	/* cut */
991 				b = XMP_KEY_CUT;
992 				break;
993 			default:
994 				if (b > 119) {	/* fade */
995 					b = XMP_KEY_FADE;
996 				} else {
997 					b++;	/* note */
998 				}
999 			}
1000 			lastevent[c].note = event->note = b;
1001 			pat_len--;
1002 		}
1003 		if (mask[c] & 0x02) {
1004 			b = hio_read8(f);
1005 			lastevent[c].ins = event->ins = b;
1006 			pat_len--;
1007 		}
1008 		if (mask[c] & 0x04) {
1009 			b = hio_read8(f);
1010 			lastevent[c].vol = event->vol = b;
1011 			xlat_volfx(event);
1012 			pat_len--;
1013 		}
1014 		if (mask[c] & 0x08) {
1015 			b = hio_read8(f);
1016 			if (b >= ARRAY_SIZE(fx)) {
1017 				D_(D_WARN "invalid effect %#02x", b);
1018 				hio_read8(f);
1019 
1020 			} else {
1021 				event->fxt = b;
1022 				event->fxp = hio_read8(f);
1023 
1024 				xlat_fx(c, event, last_fxp, new_fx);
1025 				lastevent[c].fxt = event->fxt;
1026 				lastevent[c].fxp = event->fxp;
1027 			}
1028 			pat_len -= 2;
1029 		}
1030 		if (mask[c] & 0x10) {
1031 			event->note = lastevent[c].note;
1032 		}
1033 		if (mask[c] & 0x20) {
1034 			event->ins = lastevent[c].ins;
1035 		}
1036 		if (mask[c] & 0x40) {
1037 			event->vol = lastevent[c].vol;
1038 			xlat_volfx(event);
1039 		}
1040 		if (mask[c] & 0x80) {
1041 			event->fxt = lastevent[c].fxt;
1042 			event->fxp = lastevent[c].fxp;
1043 		}
1044 	}
1045 
1046 	return 0;
1047 }
1048 
it_load(struct module_data * m,HIO_HANDLE * f,const int start)1049 static int it_load(struct module_data *m, HIO_HANDLE *f, const int start)
1050 {
1051 	struct xmp_module *mod = &m->mod;
1052 	int c, i, j;
1053 	struct it_file_header ifh;
1054 	int max_ch;
1055 	uint32 *pp_ins;		/* Pointers to instruments */
1056 	uint32 *pp_smp;		/* Pointers to samples */
1057 	uint32 *pp_pat;		/* Pointers to patterns */
1058 	int new_fx, sample_mode;
1059 
1060 	LOAD_INIT();
1061 
1062 	/* Load and convert header */
1063 	ifh.magic = hio_read32b(f);
1064 	if (ifh.magic != MAGIC_IMPM) {
1065 		return -1;
1066 	}
1067 
1068 	hio_read(ifh.name, 26, 1, f);
1069 	ifh.hilite_min = hio_read8(f);
1070 	ifh.hilite_maj = hio_read8(f);
1071 
1072 	ifh.ordnum = hio_read16l(f);
1073 	ifh.insnum = hio_read16l(f);
1074 	ifh.smpnum = hio_read16l(f);
1075 	ifh.patnum = hio_read16l(f);
1076 
1077 	ifh.cwt = hio_read16l(f);
1078 	ifh.cmwt = hio_read16l(f);
1079 	ifh.flags = hio_read16l(f);
1080 	ifh.special = hio_read16l(f);
1081 
1082 	ifh.gv = hio_read8(f);
1083 	ifh.mv = hio_read8(f);
1084 	ifh.is = hio_read8(f);
1085 	ifh.it = hio_read8(f);
1086 	ifh.sep = hio_read8(f);
1087 	ifh.pwd = hio_read8(f);
1088 
1089 	/* Sanity check */
1090 	if (ifh.gv > 0x80 || ifh.mv > 0x80) {
1091 		D_(D_CRIT "invalid gv (%u) or mv (%u)", ifh.gv, ifh.mv);
1092 		goto err;
1093 	}
1094 
1095 	ifh.msglen = hio_read16l(f);
1096 	ifh.msgofs = hio_read32l(f);
1097 	ifh.rsvd = hio_read32l(f);
1098 
1099 	hio_read(ifh.chpan, 64, 1, f);
1100 	hio_read(ifh.chvol, 64, 1, f);
1101 
1102 	memcpy(mod->name, ifh.name, sizeof(ifh.name));
1103 	/* sizeof(ifh.name) == 26, sizeof(mod->name) == 64. */
1104 	mod->name[sizeof(ifh.name)] = '\0';
1105 	mod->len = ifh.ordnum;
1106 	mod->ins = ifh.insnum;
1107 	mod->smp = ifh.smpnum;
1108 	mod->pat = ifh.patnum;
1109 
1110 	/* Sanity check */
1111 	if (mod->ins > 255 || mod->smp > 255 || mod->pat > 255) {
1112 		D_(D_CRIT "invalid ins (%u), smp (%u), or pat (%u)",
1113 		   mod->ins, mod->smp, mod->pat);
1114 		goto err;
1115 	}
1116 
1117 	if (mod->ins) {
1118 		pp_ins = calloc(4, mod->ins);
1119 		if (pp_ins == NULL)
1120 			goto err;
1121 	} else {
1122 		pp_ins = NULL;
1123 	}
1124 
1125 	pp_smp = calloc(4, mod->smp);
1126 	if (pp_smp == NULL)
1127 		goto err2;
1128 
1129 	pp_pat = calloc(4, mod->pat);
1130 	if (pp_pat == NULL)
1131 		goto err3;
1132 
1133 	mod->spd = ifh.is;
1134 	mod->bpm = ifh.it;
1135 
1136 	sample_mode = ~ifh.flags & IT_USE_INST;
1137 
1138 	if (ifh.flags & IT_LINEAR_FREQ) {
1139 		m->period_type = PERIOD_LINEAR;
1140 	}
1141 
1142 	for (i = 0; i < 64; i++) {
1143 		struct xmp_channel *xxc = &mod->xxc[i];
1144 
1145 		if (ifh.chpan[i] == 100) {	/* Surround -> center */
1146 			xxc->flg |= XMP_CHANNEL_SURROUND;
1147 		}
1148 
1149 		if (ifh.chpan[i] & 0x80) {	/* Channel mute */
1150 			xxc->flg |= XMP_CHANNEL_MUTE;
1151 		}
1152 
1153 		if (ifh.flags & IT_STEREO) {
1154 			xxc->pan = (int)ifh.chpan[i] * 0x80 >> 5;
1155 			if (xxc->pan > 0xff)
1156 				xxc->pan = 0xff;
1157 		} else {
1158 			xxc->pan = 0x80;
1159 		}
1160 
1161 		xxc->vol = ifh.chvol[i];
1162 	}
1163 
1164 	if (mod->len <= XMP_MAX_MOD_LENGTH) {
1165 		hio_read(mod->xxo, 1, mod->len, f);
1166 	} else {
1167 		hio_read(mod->xxo, 1, XMP_MAX_MOD_LENGTH, f);
1168 		hio_seek(f, mod->len - XMP_MAX_MOD_LENGTH, SEEK_CUR);
1169 		mod->len = XMP_MAX_MOD_LENGTH;
1170 	}
1171 
1172 	new_fx = ifh.flags & IT_OLD_FX ? 0 : 1;
1173 
1174 	for (i = 0; i < mod->ins; i++)
1175 		pp_ins[i] = hio_read32l(f);
1176 	for (i = 0; i < mod->smp; i++)
1177 		pp_smp[i] = hio_read32l(f);
1178 	for (i = 0; i < mod->pat; i++)
1179 		pp_pat[i] = hio_read32l(f);
1180 
1181 	m->c4rate = C4_NTSC_RATE;
1182 
1183 	identify_tracker(m, &ifh);
1184 
1185 	MODULE_INFO();
1186 
1187 	D_(D_INFO "Instrument/FX mode: %s/%s",
1188 	   sample_mode ? "sample" : ifh.cmwt >= 0x200 ?
1189 	   "new" : "old", ifh.flags & IT_OLD_FX ? "old" : "IT");
1190 
1191 	if (sample_mode)
1192 		mod->ins = mod->smp;
1193 
1194 	if (libxmp_init_instrument(m) < 0)
1195 		goto err4;
1196 
1197 	/* Alloc extra samples for sustain loop */
1198 	if (mod->smp > 0) {
1199 		m->xsmp = calloc(sizeof (struct xmp_sample), mod->smp);
1200 		if (m->xsmp == NULL) {
1201 			goto err4;
1202 		}
1203 	}
1204 
1205 	D_(D_INFO "Instruments: %d", mod->ins);
1206 
1207 	for (i = 0; i < mod->ins; i++) {
1208 		/*
1209 		 * IT files can have three different instrument types: 'New'
1210 		 * instruments, 'old' instruments or just samples. We need a
1211 		 * different loader for each of them.
1212 		 */
1213 
1214 		struct xmp_instrument *xxi = &mod->xxi[i];
1215 
1216 		if (!sample_mode && ifh.cmwt >= 0x200) {
1217 			/* New instrument format */
1218 			if (hio_seek(f, start + pp_ins[i], SEEK_SET) < 0) {
1219 				goto err4;
1220 			}
1221 
1222 			if (load_new_it_instrument(xxi, f) < 0) {
1223 				goto err4;
1224 			}
1225 
1226 		} else if (!sample_mode) {
1227 			/* Old instrument format */
1228 			if (hio_seek(f, start + pp_ins[i], SEEK_SET) < 0) {
1229 				goto err4;
1230 			}
1231 
1232 			if (load_old_it_instrument(xxi, f) < 0) {
1233 				goto err4;
1234 			}
1235 		}
1236 	}
1237 
1238 	D_(D_INFO "Stored Samples: %d", mod->smp);
1239 
1240 	for (i = 0; i < mod->smp; i++) {
1241 
1242 		if (hio_seek(f, start + pp_smp[i], SEEK_SET) < 0) {
1243 			goto err4;
1244 		}
1245 
1246 		if (load_it_sample(m, i, start, sample_mode, f) < 0) {
1247 			goto err4;
1248 		}
1249 	}
1250 	/* Reset any error status set by truncated samples. */
1251 	hio_error(f);
1252 
1253 	D_(D_INFO "Stored patterns: %d", mod->pat);
1254 
1255 	/* Effects in muted channels are processed, so scan patterns first to
1256 	 * see the real number of channels
1257 	 */
1258 	max_ch = 0;
1259 	for (i = 0; i < mod->pat; i++) {
1260 		uint8 mask[L_CHANNELS];
1261 		int pat_len;
1262 
1263 		/* If the offset to a pattern is 0, the pattern is empty */
1264 		if (pp_pat[i] == 0)
1265 			continue;
1266 
1267 		hio_seek(f, start + pp_pat[i], SEEK_SET);
1268 		pat_len = hio_read16l(f) /* - 4 */ ;
1269 		hio_read16l(f);
1270 		memset(mask, 0, L_CHANNELS);
1271 		hio_read16l(f);
1272 		hio_read16l(f);
1273 
1274 		while (--pat_len >= 0) {
1275 			int b = hio_read8(f);
1276 			if (hio_error(f)) {
1277 				D_(D_CRIT "error scanning pattern %d", i);
1278 				goto err4;
1279 			}
1280 			if (b == 0)
1281 				continue;
1282 
1283 			c = (b - 1) & 63;
1284 
1285 			if (c > max_ch)
1286 				max_ch = c;
1287 
1288 			if (b & 0x80) {
1289 				mask[c] = hio_read8(f);
1290 				pat_len--;
1291 			}
1292 
1293 			if (mask[c] & 0x01) {
1294 				hio_read8(f);
1295 				pat_len--;
1296 			}
1297 			if (mask[c] & 0x02) {
1298 				hio_read8(f);
1299 				pat_len--;
1300 			}
1301 			if (mask[c] & 0x04) {
1302 				hio_read8(f);
1303 				pat_len--;
1304 			}
1305 			if (mask[c] & 0x08) {
1306 				hio_read8(f);
1307 				hio_read8(f);
1308 				pat_len -= 2;
1309 			}
1310 		}
1311 	}
1312 
1313 	/* Set the number of channels actually used
1314 	 */
1315 	mod->chn = max_ch + 1;
1316 	mod->trk = mod->pat * mod->chn;
1317 
1318 	if (libxmp_init_pattern(mod) < 0) {
1319 		goto err4;
1320 	}
1321 
1322 	/* Read patterns */
1323 	for (i = 0; i < mod->pat; i++) {
1324 
1325 		if (libxmp_alloc_pattern(mod, i) < 0) {
1326 			goto err4;
1327 		}
1328 
1329 		/* If the offset to a pattern is 0, the pattern is empty */
1330 		if (pp_pat[i] == 0) {
1331 			mod->xxp[i]->rows = 64;
1332 			for (j = 0; j < mod->chn; j++) {
1333 				int tnum = i * mod->chn + j;
1334 				if (libxmp_alloc_track(mod, tnum, 64) < 0)
1335 					goto err4;
1336 				mod->xxp[i]->index[j] = tnum;
1337 			}
1338 			continue;
1339 		}
1340 
1341 		if (hio_seek(f, start + pp_pat[i], SEEK_SET) < 0) {
1342 			D_(D_CRIT "error seeking to %d", start + pp_pat[i]);
1343 			goto err4;
1344 		}
1345 
1346 		if (load_it_pattern(m, i, new_fx, f) < 0) {
1347 			D_(D_CRIT "error loading pattern %d", i);
1348 			goto err4;
1349 		}
1350 	}
1351 
1352 	free(pp_pat);
1353 	free(pp_smp);
1354 	free(pp_ins);
1355 
1356 	/* Song message */
1357 
1358 	if (ifh.special & IT_HAS_MSG) {
1359 		if ((m->comment = malloc(ifh.msglen)) != NULL) {
1360 			hio_seek(f, start + ifh.msgofs, SEEK_SET);
1361 
1362 			D_(D_INFO "Message length : %d", ifh.msglen);
1363 
1364 			for (j = 0; j + 1 < ifh.msglen; j++) {
1365 				int b = hio_read8(f);
1366 				if (b == '\r') {
1367 					b = '\n';
1368 				} else if ((b < 32 || b > 127) && b != '\n'
1369 					   && b != '\t') {
1370 					b = '.';
1371 				}
1372 				m->comment[j] = b;
1373 			}
1374 
1375 			if (ifh.msglen > 0) {
1376 				m->comment[j] = 0;
1377 			}
1378 		}
1379 	}
1380 
1381 	/* Format quirks */
1382 
1383 	m->quirk |= QUIRKS_IT | QUIRK_ARPMEM | QUIRK_INSVOL;
1384 
1385 	if (ifh.flags & IT_LINK_GXX) {
1386 		m->quirk |= QUIRK_PRENV;
1387 	} else {
1388 		m->quirk |= QUIRK_UNISLD;
1389 	}
1390 
1391 	if (new_fx) {
1392 		m->quirk |= QUIRK_VIBHALF | QUIRK_VIBINV;
1393 	} else {
1394 		m->quirk &= ~QUIRK_VIBALL;
1395 		m->quirk |= QUIRK_ITOLDFX;
1396 	}
1397 
1398 	if (sample_mode) {
1399 		m->quirk &= ~(QUIRK_VIRTUAL | QUIRK_RSTCHN);
1400 	}
1401 
1402 	m->gvolbase = 0x80;
1403 	m->gvol = ifh.gv;
1404 	m->read_event_type = READ_EVENT_IT;
1405 
1406 	return 0;
1407 
1408 err4:
1409 	free(pp_pat);
1410 err3:
1411 	free(pp_smp);
1412 err2:
1413 	free(pp_ins);
1414 err:
1415 	return -1;
1416 }
1417 
1418 #endif /* LIBXMP_CORE_DISABLE_IT */
1419