summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/nds/libnds/source/common/gbfs.c
blob: 95fbc729a53988e7a854b1952c7811f208c90634 (plain) (blame)
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
/*---------------------------------------------------------------------------------
	access object in a GBFS file

	Copyright 2002-2005 Damian Yerrick

	Additional code Dave Murphy

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
	BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
	AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
	OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
	IN THE SOFTWARE.

---------------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------------
	This code assumes a LITTLE ENDIAN target.  It'll need a boatload
	of itohs and itohl calls if converted to run on Sega Genesis.  It
	also assumes that the target uses 16-bit short and 32-bit longs.
---------------------------------------------------------------------------------*/

#include <stdlib.h>
#include <string.h>
#include <nds/jtypes.h>
#include "gbfs.h"

/*---------------------------------------------------------------------------------
	change this to the end of your ROM,
	or to 0x02040000 for gba multiboot
	or to 0x24000000 for nds main ram
---------------------------------------------------------------------------------*/

static const u32 * GBFS_1ST_SEARCH_LIMIT = (const u32 *)0x02400000;
static const u32 * GBFS_2ND_SEARCH_START = (const u32 *)0x08000000;
static const u32 * GBFS_2ND_SEARCH_LIMIT = (const u32 *)0x0a000000;
/*---------------------------------------------------------------------------------
	a power of two, less than or equal to the argument passed to
	padbin.  Increasing the stride makes find_first_gbfs_file()
	faster at the cost of a slightly larger binary.
---------------------------------------------------------------------------------*/
static int GBFS_STRIDE = 256;

/*---------------------------------------------------------------------------------
	Set the search limits and stride for searching
---------------------------------------------------------------------------------*/
void gbfs_search_range(
	u32 gbfs_1st_limit,
	u32 gbfs_2nd_start, u32 gbfs_2nd_limit,
	u32 gbfs_stride
) {
//---------------------------------------------------------------------------------
	if ( NULL != gbfs_1st_limit ) GBFS_1ST_SEARCH_LIMIT = (u32 *)gbfs_1st_limit;
	if ( NULL != gbfs_2nd_limit ) GBFS_2ND_SEARCH_LIMIT = (u32 *)gbfs_2nd_limit;
	if ( NULL != gbfs_2nd_start ) GBFS_2ND_SEARCH_START = (u32 *)gbfs_2nd_start;
	if ( NULL != gbfs_stride ) GBFS_STRIDE = gbfs_stride;

}

//---------------------------------------------------------------------------------
const GBFS_FILE * find_first_gbfs_file(const void *start) {
//---------------------------------------------------------------------------------
	/* align the pointer */
	const u32 *here = (const u32 *)
											((unsigned long)start & (-GBFS_STRIDE));
	const char rest_of_magic[] = "ightGBFS\r\n\x1a\n";

	/* Linear-search first in multiboot space. */
	while(here < GBFS_1ST_SEARCH_LIMIT)
	{
		/*	We have to keep the magic code in two pieces; otherwise,
			this function may find itself and think it's a GBFS file.
			This obviously won't work if your compiler stores this
			numeric literal just before the literal string, but Devkit
			Advance R4 and R5 seem to keep numeric constant pools
			separate enough from string pools for this to work.
		*/
		if(*here == 0x456e6950) { /* ASCII code for little endian "PinE" */
			/*	We've matched the first four bytes.
				If the rest of the magic matches, then we've found a file. */
			if(!memcmp(here + 1, rest_of_magic, 12))
				return (const GBFS_FILE *)here;
		}
		here += GBFS_STRIDE / sizeof(here);
 }

	/* Now search in ROM space. */
	if(here < GBFS_2ND_SEARCH_START)
		here = GBFS_2ND_SEARCH_START;
	while(here < GBFS_2ND_SEARCH_LIMIT) {
		/* Search loop same as above. */
		if(*here == 0x456e6950) {  /* ASCII code for little endian "PinE" */
			if(!memcmp(here + 1, rest_of_magic, 12))
				return (const GBFS_FILE *)here;
		}
		here += GBFS_STRIDE / sizeof(*here);
	}
	return 0;
}


//---------------------------------------------------------------------------------
const void *skip_gbfs_file(const GBFS_FILE *file) {
//---------------------------------------------------------------------------------
	return ((char *)file + file->total_len);
}


//---------------------------------------------------------------------------------
static int namecmp(const void *a, const void *b) {
//---------------------------------------------------------------------------------
	return memcmp(a, b, 24);
}


//---------------------------------------------------------------------------------
const void *gbfs_get_obj(	const GBFS_FILE *file,
							const char *name,
							u32 *len) {
//---------------------------------------------------------------------------------
	char key[24] = {0};

	const GBFS_ENTRY *dirbase = (const GBFS_ENTRY *)((const char *)file + file->dir_off);
	size_t n_entries = file->dir_nmemb;
	const GBFS_ENTRY *here;

	strncpy(key, name, 24);

	here = bsearch(	key, dirbase,
					n_entries, sizeof(GBFS_ENTRY),
					 namecmp);
	if(!here)
		return NULL;

	if(len)
		*len = here->len;
	return (char *)file + here->data_offset;
}


//---------------------------------------------------------------------------------
const void *gbfs_get_nth_obj(	const GBFS_FILE *file,
							 	size_t n,
								char *name,
								u32 *len)
//---------------------------------------------------------------------------------
{
	const GBFS_ENTRY *dirbase = (const GBFS_ENTRY *)((const char *)file + file->dir_off);
	size_t n_entries = file->dir_nmemb;
	const GBFS_ENTRY *here = dirbase + n;

	if(n >= n_entries)
		return NULL;

	if(name) {
		strncpy(name, here->name, 24);
		name[24] = 0;
	}

	if(len)
		*len = here->len;

	return (char *)file + here->data_offset;
}


//---------------------------------------------------------------------------------
void *gbfs_copy_obj(	void *dst,
						const GBFS_FILE *file,
						const char *name) {
//---------------------------------------------------------------------------------
	u32 len;
	const void *src = gbfs_get_obj(file, name, &len);

	if(!src)
		return NULL;

	memcpy(dst, src, len);
	return dst;
}


//---------------------------------------------------------------------------------
size_t gbfs_count_objs(const GBFS_FILE *file) {
//---------------------------------------------------------------------------------
	return file ? file->dir_nmemb : 0;
}