forked from kode54/libupse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupse_module.c
134 lines (106 loc) · 3.17 KB
/
upse_module.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
/*
* UPSE: the unix playstation sound emulator.
*
* Filename: upse-module.h
* Purpose: libupse: Module loading and probing.
*
* Copyright (c) 2008 William Pitcock <[email protected]>
*
* UPSE is free software, released under the GNU General Public License,
* version 2.
*
* A copy of the GNU General Public License, version 2, is included in
* the UPSE source kit as COPYING.
*
* UPSE is offered without any warranty of any kind, explicit or implicit.
*/
#include "upse-internal.h"
static upse_loader_t *upse_loader_table_ = NULL;
upse_loader_func_t
upse_module_probe(void *fileptr, const upse_iofuncs_t *funcs)
{
int i, previous_offset, previous_length;
char *bytes = NULL;
if (!fileptr)
return NULL;
if (!upse_loader_table_)
upse_loader_table_ = upse_loader_prepare_table();
previous_length = 0;
previous_offset = upse_loader_table_[0].offset;
funcs->seek_impl(fileptr, previous_offset, SEEK_SET);
for (i = 0; upse_loader_table_[i].bytes != NULL; i++)
{
upse_loader_t *loader = &upse_loader_table_[i];
_DEBUG("trying loader with magic: %s length: %d", loader->bytes, loader->length);
if (loader->offset != previous_offset)
{
previous_length = 0;
funcs->seek_impl(fileptr, previous_offset, SEEK_SET);
}
if (loader->length != previous_length || bytes == NULL)
{
if (bytes)
free(bytes);
bytes = calloc(sizeof(char), loader->length);
funcs->seek_impl(fileptr, loader->offset, SEEK_SET);
funcs->read_impl(bytes, loader->length, 1, fileptr);
}
if (!memcmp(bytes, loader->bytes, loader->length))
{
free(bytes);
return loader->func;
}
}
return NULL;
}
int
upse_module_is_supported(void *fileptr, const upse_iofuncs_t *funcs)
{
return upse_module_probe(fileptr, funcs) != NULL;
}
int
upse_file_is_supported(char *file, const upse_iofuncs_t *funcs)
{
void *fileptr;
int ret;
fileptr = funcs->open_impl(file, "rb");
ret = upse_module_is_supported(fileptr, funcs);
funcs->close_impl(fileptr);
return ret;
}
upse_module_t *
upse_module_open(const char *file, const upse_iofuncs_t *funcs)
{
void *fileptr;
upse_module_t *ret;
upse_loader_func_t functor;
fileptr = funcs->open_impl(file, "rb");
if (!fileptr)
return NULL;
functor = upse_module_probe(fileptr, funcs);
if (!functor)
{
funcs->close_impl(fileptr);
return NULL;
}
funcs->seek_impl(fileptr, 0, SEEK_SET);
ret = functor(fileptr, file, funcs);
funcs->close_impl(fileptr);
return ret;
}
void
upse_module_close(upse_module_t *mod)
{
if (!mod)
return;
upse_free_psf_metadata(mod->metadata); /* XXX */
free(mod);
}
extern upse_module_t *upse_load_psf(void *fileptr, const char *path, const upse_iofuncs_t *funcs);
extern upse_module_t *upse_load_psf2(void *fileptr, const char *path, const upse_iofuncs_t *funcs);
void
upse_module_init(void)
{
upse_loader_add_magic("PSF\x01", 4, 0, upse_load_psf);
upse_loader_add_magic("PSF\x02", 4, 0, upse_load_psf2);
}