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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "ayemu.h"
#define AYEMU_VTX_STRING_MAX 254
typedef const char * data_ptr_t;
/* LHA5 decoder, defined in lh5dec.c */
extern void lh5_decode(unsigned const char *inp,
unsigned char *outp,
unsigned long original_size,
unsigned long packed_size);
/* Read 8-bit integer from file.
*/
static data_ptr_t read_byte(data_ptr_t pos, int *p)
{
const unsigned char *data = (const unsigned char*)pos;
*p = *data++;
return (data_ptr_t)data;
}
/* Read 16-bit little-endian 1234 integer from file.
*/
static data_ptr_t read_word16(data_ptr_t pos, int *p)
{
const unsigned char *data = (const unsigned char*)pos;
*p = *data++;
*p += *data++ << 8;
return (data_ptr_t)data;
}
/* read 32-bit integer from file.
*/
static data_ptr_t read_word32(data_ptr_t pos, int *p)
{
const unsigned char *data = (const unsigned char*)pos;
*p = *data++;
*p += *data++ << 8;
*p += *data++ << 16;
*p += *data++ << 24;
return (data_ptr_t)data;
}
/* read_string: max 254 chars len (+1 for null terminator).
*/
static data_ptr_t read_string(data_ptr_t pos, char **res)
{
int len;
if (pos == NULL)
return NULL;
len = strlen(pos);
if (len > AYEMU_VTX_STRING_MAX) {
fprintf(stderr, "Error: string len more than %d (=%d)\n", AYEMU_VTX_STRING_MAX, len);
return NULL;
}
*res = calloc(1, len + 1);
strcpy(*res, pos);
return pos + len + 1;
}
static data_ptr_t read_header(data_ptr_t buf, ayemu_vtx_t **target, size_t size)
{
char hdr[3];
ayemu_vtx_t *vtx;
hdr[0] = tolower(*buf++);
hdr[1] = tolower(*buf++);
hdr[2] = '\0';
if (size < 20) {
fprintf(stderr, "ayemu_vtx_open: file size < 20 bytes - it is impossible\n");
return NULL;
}
vtx = (ayemu_vtx_t*) calloc(1, sizeof(ayemu_vtx_t));
if (strncmp(hdr, "ay", 2) == 0)
vtx->chiptype = AYEMU_AY;
else if (strncmp (hdr, "ym", 2) == 0)
vtx->chiptype = AYEMU_YM;
else {
fprintf (stderr, "File is _not_ VORTEX format!\n"
"It not begins with 'ay' or 'ym' string.\n");
goto clean_and_ret_null;
}
buf = read_byte(buf, &vtx->stereo);
buf = read_word16(buf, &vtx->loop);
buf = read_word32(buf, &vtx->chipFreq);
buf = read_byte(buf, &vtx->playerFreq);
buf = read_word16(buf, &vtx->year);
buf = read_word32(buf, &vtx->regdata_size);
buf = read_string(buf, &vtx->title);
buf = read_string(buf, &vtx->author);
buf = read_string(buf, &vtx->from);
buf = read_string(buf, &vtx->tracker);
buf = read_string(buf, &vtx->comment);
*target = vtx;
return buf;
clean_and_ret_null:
ayemu_vtx_free(vtx);
return NULL;
}
ayemu_vtx_t * ayemu_vtx_header(data_ptr_t buf, size_t size)
{
ayemu_vtx_t *vtx;
const char *data;
data = read_header(buf, &vtx, size);
return vtx;
}
ayemu_vtx_t * ayemu_vtx_load(data_ptr_t buf, size_t size)
{
ayemu_vtx_t *vtx;
const char *data = read_header(buf, &vtx, size);
if (! data) {
fprintf(stderr, "ayemu_vtx_load: Cannot parse file header\n");
return NULL;
}
// unpack data
size -= (buf - data);
if ((vtx->regdata = (unsigned char *) malloc (vtx->regdata_size)) == NULL) {
fprintf (stderr, "ayemu_vtx_load_data: Can allocate %d bytes"
" for unpack register data\n", vtx->regdata_size);
return NULL;
}
lh5_decode ((unsigned char*)data, vtx->regdata, vtx->regdata_size, size);
vtx->frames = vtx->regdata_size / 14;
return vtx;
}
/** Get specified data frame by number.
*
*/
void ayemu_vtx_getframe(const ayemu_vtx_t *vtx, size_t frame_n, ayemu_ay_reg_frame_t regs)
{
int n;
if (frame_n >= vtx->frames)
return;
// calculate begin of data
unsigned char *p = vtx->regdata + frame_n;
// step is data size / 14
for(n = 0 ; n < 14 ; n++) {
regs[n] = *p;
p += vtx->frames;
}
}
/** Free all allocaded resources.
*
*/
void ayemu_vtx_free(ayemu_vtx_t *vtx)
{
#define FREE_PTR(x) if (x) { free(x); x = NULL; }
FREE_PTR(vtx->title);
FREE_PTR(vtx->author);
FREE_PTR(vtx->from);
FREE_PTR(vtx->tracker);
FREE_PTR(vtx->comment);
FREE_PTR(vtx->regdata);
}
ayemu_vtx_t * ayemu_vtx_header_from_file(const char *filename)
{
ayemu_vtx_t *ret;
size_t size;
const size_t page_size = (size_t) sysconf (_SC_PAGESIZE);
int fd;
struct stat st;
// printf("Page size is %d\n", page_size);
if (stat(filename, &st) != 0) {
fprintf(stderr, "Can't stat file %s: %s\n", filename, strerror(errno));
return NULL;
}
size = st.st_size;
fd = open(filename, O_RDONLY, 0);
if (fd == 0) {
fprintf(stderr, "Can't open file %s: %s\n", filename, strerror(errno));
return NULL;
}
size_t data_len = (size / page_size + 1) * page_size;
char *data = mmap(NULL, data_len, PROT_READ, MAP_PRIVATE, fd, 0);
if (data == (void*)(-1)) {
fprintf(stderr, "Can't mmap file %s: %s\n", filename, strerror(errno));
return NULL;
}
ret = ayemu_vtx_header(data, size);
if (munmap(data, data_len) != 0) {
fprintf(stderr, "Can't munmmap file %s: %s\n", filename, strerror(errno));
}
return ret;
}
ayemu_vtx_t * ayemu_vtx_load_from_file(const char *filename)
{
size_t size;
const size_t page_size = (size_t) sysconf (_SC_PAGESIZE);
int fd;
struct stat st;
ayemu_vtx_t *ret;
// printf("Page size is %d\n", page_size);
if (stat(filename, &st) != 0) {
fprintf(stderr, "Can't stat file %s: %s\n", filename, strerror(errno));
return NULL;
}
size = st.st_size;
fd = open(filename, O_RDONLY, 0);
if (fd == 0) {
fprintf(stderr, "Can't open file %s: %s\n", filename, strerror(errno));
return NULL;
}
size_t data_len = (size / page_size + 1) * page_size;
char *data = mmap(NULL, data_len, PROT_READ, MAP_PRIVATE, fd, 0);
if (data == (void*)(-1)) {
fprintf(stderr, "Can't mmap file %s: %s\n", filename, strerror(errno));
return NULL;
}
ret = ayemu_vtx_load(data, size);
if (munmap(data, data_len) != 0) {
fprintf(stderr, "Can't munmmap file %s: %s\n", filename, strerror(errno));
}
return ret;
}
|