-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsf_player.c
139 lines (111 loc) · 2.86 KB
/
xsf_player.c
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
//
// Created by cepheus on 3/10/19.
//
#include "xsf_player.h"
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "color.h"
#include "common.h"
#include "vio2sf/vio2sf.h"
static uint32 size; // size of buffer
/* ao_get_lib: called to load secondary files */
int xsf_get_lib(char *filename, void **buffer, unsigned int *length)
{
uint8 *filebuf;
unsigned int size;
FILE *auxfile;
struct stat st;
auxfile = fopen(filename, "rb");
if (!auxfile)
{
RED();
printf("Unable to find auxiliary file %s\n", filename);
NORMAL();
return AO_FAIL;
}
fstat(fileno(auxfile), &st);
if (st.st_size > UINT_MAX) {
fclose(auxfile);
RED();
printf("ERROR: file size of %zu bytes is larger than maximum supported value of %u\n", st.st_size, UINT_MAX);
NORMAL();
return AO_FAIL;
}
size = (unsigned int)st.st_size;
filebuf = malloc((size_t)size);
if (!filebuf)
{
fclose(auxfile);
RED();
printf("ERROR: could not allocate %d bytes of memory\n", size);
NORMAL();
return AO_FAIL;
}
fread(filebuf, size, 1, auxfile);
fclose(auxfile);
*buffer = filebuf;
*length = size;
return AO_SUCCESS;
}
extern void do_frame(uint32 size, int16 *buffer)
{
xsf_gen(buffer, size);
}
// load and set up a 2sf file
int read_in_file(char *name) {
FILE *file;
uint32 filesig;
uint8 *filedata;
uint64 file_len;
unsigned int size;
struct stat st;
c = NULL;
file = fopen(name, "rb");
if (!file) {
RED();
printf("ERROR: could not open file %s\n", name);
NORMAL();
return -1;
}
// get the length of the file by using fstat
fstat(fileno(file), &st);
if (st.st_size > UINT_MAX) {
fclose(file);
RED();
printf("ERROR: file size of %zu bytes is larger than maximum supported value of %u\n", st.st_size, UINT_MAX);
NORMAL();
return -1;
}
size = (unsigned int) st.st_size;
buffer = malloc((size_t) size);
if (!buffer) {
fclose(file);
RED();
printf("ERROR: could not allocate %d bytes of memory\n", size);
NORMAL();
return -1;
}
// read the file
fread(buffer, size, 1, file);
fclose(file);
// init our *SF engine so we can get tags
if (corlett_decode(buffer, size, &filedata, &file_len, &c) != AO_SUCCESS) {
RED();
printf("ERROR: Tag format unreadable in file ");
MAGENTA();
printf("%s\n", name);
NORMAL();
return -1;
}
free(filedata); // we don't use this
if (xsf_start(buffer, size) != XSF_TRUE) {
RED();
printf("ERROR: vio2sf failed to load file \n");
MAGENTA();
printf("%s\n", name);
NORMAL();
return -1;
}
return 0;
}