-
Notifications
You must be signed in to change notification settings - Fork 15
/
resource_manager.c
325 lines (270 loc) · 8.68 KB
/
resource_manager.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* @file resource_manager.c
* @brief Embedded resource management
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
*
* 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) 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, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 2.4.4
**/
//Dependencies
#include <string.h>
#include "os_port.h"
#include "cpu_endian.h"
#include "resource_manager.h"
#include "debug.h"
//Resource data
extern const uint8_t res[];
error_t resGetData(const char_t *path, const uint8_t **data, size_t *length)
{
bool_t found;
bool_t match;
uint_t n;
uint_t dirLength;
ResEntry *resEntry;
//Point to the resource header
ResHeader *resHeader = (ResHeader *) res;
//Make sure the resource data is valid
if(letoh32(resHeader->totalSize) < sizeof(ResHeader))
return ERROR_INVALID_RESOURCE;
//Retrieve the length of the root directory
dirLength = letoh32(resHeader->rootEntry.dataLength);
//Point to the contents of the root directory
resEntry = (ResEntry *) (res + letoh32(resHeader->rootEntry.dataStart));
//Parse the entire path
for(found = FALSE; !found && path[0] != '\0'; path += n + 1)
{
//Search for the separator that terminates the current token
for(n = 0; path[n] != '\\' && path[n] != '/' && path[n] != '\0'; n++)
{
}
if(n == 0 && path[n] != '\0')
{
path++;
for(n = 0; path[n] != '\\' && path[n] != '/' && path[n] != '\0'; n++)
{
}
}
//Loop through the directory
for(match = FALSE; !match && dirLength > 0; )
{
//Check the number of remaining bytes
if(dirLength < sizeof(ResEntry))
return ERROR_INVALID_RESOURCE;
//Make sure the entry is valid
if(dirLength < (sizeof(ResEntry) + resEntry->nameLength))
return ERROR_INVALID_RESOURCE;
//Compare current entry name against the expected one
if(resEntry->nameLength == n && !strncasecmp(resEntry->name, path, n))
{
//Check the type of the entry
if(resEntry->type == RES_TYPE_DIR)
{
//Save the length of the directory
dirLength = letoh32(resEntry->dataLength);
//Point to the contents of the directory
resEntry = (ResEntry *) (res + letoh32(resEntry->dataStart));
}
else
{
//A file may only appear at the end of the path
if(path[n] != '\0')
return ERROR_NOT_FOUND;
//The search process is complete
found = TRUE;
}
//The current entry matches the specified path
match = TRUE;
}
else
{
//Remaining bytes to process
dirLength -= sizeof(ResEntry) + resEntry->nameLength;
//Point to the next entry
resEntry = (ResEntry *) ((uint8_t *) resEntry + sizeof(ResEntry) +
resEntry->nameLength);
}
}
//Unable to find the specified file?
if(!match)
return ERROR_NOT_FOUND;
}
//Unable to find the specified file?
if(!found)
return ERROR_NOT_FOUND;
//Enforce the entry type
if(resEntry->type != RES_TYPE_FILE)
return ERROR_NOT_FOUND;
//Return the location of the specified resource
*data = res + letoh32(resEntry->dataStart);
//Return the length of the resource
*length = letoh32(resEntry->dataLength);
//Successful processing
return NO_ERROR;
}
error_t resSearchFile(const char_t *path, DirEntry *dirEntry)
{
bool_t found;
bool_t match;
uint_t n;
uint_t length;
ResEntry *resEntry;
//Point to the resource header
ResHeader *resHeader = (ResHeader *) res;
//Make sure the resource data is valid
if(letoh32(resHeader->totalSize) < sizeof(ResHeader))
return ERROR_INVALID_RESOURCE;
//Retrieve the length of the root directory
length = letoh32(resHeader->rootEntry.dataLength);
//Point to the contents of the root directory
resEntry = (ResEntry *) (res + letoh32(resHeader->rootEntry.dataStart));
//Parse the entire path
for(found = FALSE; !found && path[0] != '\0'; path += n + 1)
{
//Search for the separator that terminates the current token
for(n = 0; path[n] != '\\' && path[n] != '/' && path[n] != '\0'; n++)
{
}
if(n == 0 && path[n] != '\0')
{
path++;
for(n = 0; path[n] != '\\' && path[n] != '/' && path[n] != '\0'; n++)
{
}
}
//Loop through the directory
for(match = FALSE; !match && length > 0; )
{
//Check the number of remaining bytes
if(length < sizeof(ResEntry))
return ERROR_INVALID_RESOURCE;
//Make sure the entry is valid
if(length < (sizeof(ResEntry) + resEntry->nameLength))
return ERROR_INVALID_RESOURCE;
//Compare current entry name against the expected one
if(resEntry->nameLength == n && !strncasecmp(resEntry->name, path, n))
{
//Check the type of the entry
if(resEntry->type == RES_TYPE_DIR)
{
//Save the length of the directory
length = letoh32(resEntry->dataLength);
//Point to the contents of the directory
resEntry = (ResEntry *) (res + letoh32(resEntry->dataStart));
}
else
{
//A file may only appear at the end of the path
if(path[n] != '\0')
return ERROR_INVALID_PATH;
//The search process is complete
found = TRUE;
}
//The current entry matches the specified path
match = TRUE;
}
else
{
//Remaining bytes to process
length -= sizeof(ResEntry) + resEntry->nameLength;
//Point to the next entry
resEntry = (ResEntry *) ((uint8_t *) resEntry + sizeof(ResEntry) +
resEntry->nameLength);
}
}
//Unable to find the specified file?
if(!match)
return ERROR_NOT_FOUND;
}
//Unable to find the specified file?
if(!found)
return ERROR_NOT_FOUND;
//Return information about the file
dirEntry->type = resEntry->type;
dirEntry->volume = 0;
dirEntry->dataStart = letoh32(resEntry->dataStart);
dirEntry->dataLength = letoh32(resEntry->dataLength);
dirEntry->nameLength = 0; //resEntry->nameLength;
//Copy the filename
//osStrncpy(dirEntry->name, resEntry->name, dirEntry->nameLength);
//Properly terminate the filename
//dirEntry->name[dirEntry->nameLength] = '\0';
//Successful processing
return NO_ERROR;
}
#if 0
error_t resOpenFile(FsFile *file, const DirEntry *dirEntry, uint_t mode)
{
file->mode = mode;
file->offset = 0;
file->start = dirEntry->dataStart;
file->size = dirEntry->dataLength;
return NO_ERROR;
}
error_t resSeekFile(FsFile *file, uint32_t *position)
{
return ERROR_NOT_IMPLEMENTED;
}
uint_t resReadFile(FsFile *file, void *data, size_t length)
{
length = MIN(length, file->size - file->offset);
osMemcpy(data, res + file->start + file->offset, length);
file->offset += length;
return length;
}
FILE *fopen(const char_t *filename, const char_t *mode)
{
error_t error;
DirEntry dirEntry;
FsFile *file;
error = resSearchFile(filename, &dirEntry);
if(error)
return NULL;
file = osAllocMem(sizeof(FsFile));
if(!file)
return NULL;
error = resOpenFile(file, &dirEntry, MODE_BINARY);
if(error)
{
osFreeMem(file);
return NULL;
}
return (FILE *) file;
}
size_t fread(void *ptr, size_t size, size_t count, FILE *stream)
{
uint_t n;
n = resReadFile((FsFile *) stream, ptr, size * count);
return n / size;
}
int_t fclose(FILE *stream)
{
osFreeMem(stream);
//The stream is successfully closed
return 0;
}
uint_t fileGetSize(FILE *stream)
{
uint_t n;
n = ((FsFile *) stream)->size;
return n;
}
#endif