-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMRipper.cpp
185 lines (158 loc) · 3.77 KB
/
XMRipper.cpp
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
/***********************************************************************
* Copyright 2007-2010 Michael Drueing <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#include <cstring>
#include <cstdio>
#include "GlobalDefs.h"
#include "XMRipper.h"
const char *XMRipper::s_name = "XM module ripper v1.0";
const HeaderStruct XMRipper::s_headers[] = {
{"Extended Module: ", 17},
{"", 0}
};
#pragma pack(push, 1)
struct XMHeader
{
char id[17];
char modname[20];
char eof;
char trackername[20];
uint16 trackerversion;
uint32 headersize;
uint16 songlen; // in patterns
uint16 restartpos;
uint16 channels;
uint16 numpatterns; // (< 256)
uint16 numinstruments; // <128
uint16 flags; // bit 0: linear instead of amiga freq. table
uint16 tempo;
uint16 bpm;
char pattern_table[256];
};
struct XMPattern
{
uint32 hdrlength;
char packing;
uint16 rows;
uint16 datasize;
};
struct XMInstrument
{
uint32 headersize;
char instrname[22];
char insttype;
uint16 numsamples;
uint32 sampleheader_size;
char sample_numbers[96];
char vol_envelope[48];
char pan_envelope[48];
char vol_points;
char pan_points;
char vol_sustain;
char vol_loop_start;
char vol_loop_end;
char pan_sustain;
char pan_loop_start;
char pan_loop_end;
char vol_type;
char pan_type;
char vibrato_type;
char vibrato_sweep;
char vibrato_depth;
char vibrato_rate;
uint16 vol_fadeout;
uint16 vol_reserved;
};
struct XMSample
{
uint32 length;
uint32 loop_start;
uint32 loop_end;
char volume;
char finetune;
char type;
char pan;
char note;
char reserved;
char name[22];
};
#pragma pack(pop)
static bool valid_ascii(char *x, int len)
{
for (int i = 0; i < len; i++)
{
if (x[i] == 0)
return true;
if (*x < 32)
return false;
}
return true;
}
bool XMRipper::checkLocation(unsigned char *pos, const HeaderStruct *header, FoundStruct *found)
{
XMHeader *hdr = (XMHeader *)pos;
XMPattern *phdr;
XMInstrument *ihdr;
XMSample *shdr;
uint32 smp_len;
unsigned char *pos2 = pos + sizeof(XMHeader);
if (hdr->eof != 0x1a)
return false;
if (!valid_ascii(hdr->modname, 20))
return false;
if (!valid_ascii(hdr->trackername, 20))
return false;
if ((hdr->numpatterns > 255) || (hdr->numinstruments > 127))
return false;
found->criterium = CRIT_STRONG;
strcpy(found->extension, "xm");
if (hdr->trackerversion != 0x0104)
found->criterium = CRIT_WEAK;
for (int i = 0; i < hdr->numpatterns; i++)
{
phdr = (XMPattern *)pos2;
if ((phdr->packing != 0) || (phdr->hdrlength != 9))
return false;
pos2 += sizeof(XMPattern);
pos2 += phdr->datasize;
}
for (int i = 0; i < hdr->numinstruments; i++)
{
ihdr = (XMInstrument *)pos2;
pos2 += ihdr->headersize;
smp_len = 0;
for (int j = 0; j < ihdr->numsamples; j++)
{
shdr = (XMSample *)pos2;
pos2 += ihdr->sampleheader_size;
smp_len += shdr->length;
}
pos2 += smp_len;
}
found->startoffset = pos;
found->length = pos2 - pos;
return true;
}
bool XMRipper::checkCompileAssertions()
{
if (sizeof(XMHeader) != 0x100 + 0x50)
return false;
if (sizeof(XMPattern) != 9)
return false;
if (sizeof(XMSample) != 0x28)
return false;
return true;
}