-
Notifications
You must be signed in to change notification settings - Fork 13
/
iniparse.c
438 lines (399 loc) · 11.1 KB
/
iniparse.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include "iniparse.h"
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#ifdef __GNUC__
#include <strings.h>
#define stricmp strcasecmp
#define strnicmp strncasecmp
#endif
static int find_next_char(const char* byteBuf, const int maxBytes, const char toFind)
{
if (byteBuf == NULL || maxBytes == 0)
return -1;
for (int i=0; i<maxBytes; i++)
{
if (byteBuf[i] == toFind)
return i;
}
return -1;
}
static bool is_space(const char theChar)
{
return (theChar == 0x20) || (theChar >= 0x09 && theChar <= 0x0d);
}
static int trim_trailing_whitespace(char* textBytes, const int maxLen)
{
if (textBytes == NULL || maxLen == 0)
return maxLen;
int realLen = maxLen;
for (int i=maxLen-1; i>=0; i--)
{
if (is_space(textBytes[i]))
{
textBytes[i] = 0;
realLen = i;
}
else
break;
}
return realLen;
}
static char* my_strdup(const char* srcString, AllocatorFunc allocator)
{
if (srcString == NULL)
return NULL;
size_t srcLen = strlen(srcString);
char* dstString = (char*)allocator(srcLen+1);
memcpy(dstString, srcString, srcLen+1);
return dstString;
}
IniParsedInfo_t parse_memloader_ini(char* iniBytes, const int numBytes, AllocatorFunc allocator, ErrPrintFunc printer)
{
IniParsedInfo_t out;
out.loads = NULL;
out.copies = NULL;
out.boots = NULL;
IniLoadSectionNode_t* currLoadNode = NULL;
IniCopySectionNode_t* currCopyNode = NULL;
IniBootSectionNode_t* currBootNode = NULL;
int currLine = -1;
int currPos = 0;
int bytesRemaining = numBytes;
while (bytesRemaining > 0)
{
char* const lineStart = &iniBytes[currPos];
char* currBytes = lineStart;
int lineLength = find_next_char(currBytes, bytesRemaining, '\n');
if (lineLength >= 0)
{
currBytes[lineLength] = 0;
bytesRemaining -= lineLength+1;
currPos += lineLength+1;
}
else
{
lineLength = (int)bytesRemaining;
bytesRemaining = 0;
currPos += lineLength;
}
currLine++;
//skip leading space
while (is_space(*currBytes) && lineLength > 0)
{
currBytes++;
lineLength--;
}
//trim comments
{
int semiColonPos = find_next_char(currBytes, lineLength, ';');
if (semiColonPos >= 0)
{
currBytes[semiColonPos] = 0;
lineLength = semiColonPos;
}
}
lineLength = trim_trailing_whitespace(currBytes, lineLength);
if (lineLength < 1)
continue;
//new section start
if (*currBytes == '[')
{
currBytes++;
lineLength--;
//skip leading space
while (is_space(*currBytes) && lineLength > 0)
{
currBytes++;
lineLength--;
}
int colonPos = find_next_char(currBytes, lineLength, ':');
if (colonPos < 0)
{
printer("Cannot find : separator in section name '%s' on line %d, skipping\n", currBytes, currLine);
continue;
}
currBytes[colonPos] = 0;
//left side processing
char* leftSide = currBytes;
int leftSideLen = colonPos;
leftSideLen = trim_trailing_whitespace(leftSide, leftSideLen);
//right side processing
char* rightSide = currBytes+colonPos+1;
int rightSideLen = lineLength-colonPos-1;
while (is_space(*rightSide) && rightSideLen > 0)
{
rightSide++;
rightSideLen--;
}
int rightSideClosingPos = find_next_char(rightSide, rightSideLen, ']');
if (rightSideClosingPos < 0)
printer("No closing ] found for section '%s' on line %d, behaving as if it was there\n", rightSide, currLine);
else
{
rightSide[rightSideClosingPos] = 0;
rightSideLen = rightSideClosingPos;
}
rightSideLen = trim_trailing_whitespace(rightSide, rightSideLen);
currLoadNode = NULL;
currCopyNode = NULL;
currBootNode = NULL;
if (strnicmp(leftSide, "load", leftSideLen) == 0)
{
if (out.loads == NULL)
{
out.loads = allocator(sizeof(IniLoadSectionNode_t));
currLoadNode = out.loads;
memset(currLoadNode, 0, sizeof(IniLoadSectionNode_t));
}
else
{
IniLoadSectionNode_t* currNode = out.loads;
while (currNode->next != NULL)
{
if (stricmp(currNode->curr.sectname, rightSide) == 0)
{
currLoadNode = currNode;
break;
}
currNode = currNode->next;
}
if (currLoadNode == NULL)
{
currNode->next = allocator(sizeof(IniLoadSectionNode_t));
currLoadNode = currNode->next;
memset(currLoadNode, 0, sizeof(IniLoadSectionNode_t));
}
}
if (currLoadNode->curr.sectname == NULL)
currLoadNode->curr.sectname = my_strdup(rightSide, allocator);
}
else if (strnicmp(leftSide, "copy", leftSideLen) == 0)
{
if (out.copies == NULL)
{
out.copies = allocator(sizeof(IniCopySectionNode_t));
currCopyNode = out.copies;
memset(currCopyNode, 0, sizeof(IniCopySectionNode_t));
}
else
{
IniCopySectionNode_t* currNode = out.copies;
while (currNode->next != NULL)
{
if (stricmp(currNode->curr.sectname, rightSide) == 0)
{
currCopyNode = currNode;
break;
}
currNode = currNode->next;
}
if (currCopyNode == NULL)
{
currNode->next = allocator(sizeof(IniCopySectionNode_t));
currCopyNode = currNode->next;
memset(currCopyNode, 0, sizeof(IniCopySectionNode_t));
}
}
if (currCopyNode->curr.sectname == NULL)
currCopyNode->curr.sectname = my_strdup(rightSide, allocator);
}
else if (strnicmp(leftSide, "boot", leftSideLen) == 0)
{
if (out.boots == NULL)
{
out.boots = allocator(sizeof(IniBootSectionNode_t));
currBootNode = out.boots;
memset(currBootNode, 0, sizeof(IniBootSectionNode_t));
}
else
{
IniBootSectionNode_t* currNode = out.boots;
while (currNode->next != NULL)
{
if (stricmp(currNode->curr.sectname, rightSide) == 0)
{
currBootNode = currNode;
break;
}
currNode = currNode->next;
}
if (currBootNode == NULL)
{
currNode->next = allocator(sizeof(IniBootSectionNode_t));
currBootNode = currNode->next;
memset(currBootNode, 0, sizeof(IniBootSectionNode_t));
}
}
if (currBootNode->curr.sectname == NULL)
currBootNode->curr.sectname = my_strdup(rightSide, allocator);
}
else
{
printer("Unknown section type '%s' on line %d\n", rightSide, currLine);
continue;
}
}
else //key=value
{
int equalsPos = find_next_char(currBytes, lineLength, '=');
if (equalsPos < 0)
{
printer("Cannot find = separator in kv pair '%s' on line %d, skipping\n", currBytes, currLine);
continue;
}
currBytes[equalsPos] = 0;
//left side processing
char* leftSide = currBytes;
int leftSideLen = equalsPos;
leftSideLen = trim_trailing_whitespace(leftSide, leftSideLen);
//right side processing
char* rightSide = currBytes+equalsPos+1;
int rightSideLen = lineLength-equalsPos-1;
while (is_space(*rightSide) && rightSideLen > 0)
{
rightSide++;
rightSideLen--;
}
if (currLoadNode != NULL)
{
enum { KEY_INPUTFILE, KEY_SKIPBYTES, KEY_COUNTBYTES, KEY_DSTADDR, KEY_COUNT };
static const char* keyNames[KEY_COUNT] = { "if", "skip", "count", "dst" };
int currKey;
for (currKey=0; currKey<KEY_COUNT; currKey++)
{
if (strnicmp(leftSide, keyNames[currKey], leftSideLen) == 0)
break;
}
if (currKey == KEY_COUNT)
{
printer("Unknown key '%s' for LOAD section on line %d, skipping\n", leftSide, currLine);
continue;
}
else if (currKey == KEY_INPUTFILE)
currLoadNode->curr.filename = my_strdup(rightSide,allocator);
else
{
char* outPos = NULL;
uint32_t theValue = strtoul(rightSide, &outPos, 0);
if (outPos == NULL || outPos == rightSide)
printer("Invalid value '%s' for LOAD section key '%s' on line %d, skipping\n", rightSide, leftSide, currLine);
else if (currKey == KEY_SKIPBYTES)
currLoadNode->curr.skip = theValue;
else if (currKey == KEY_COUNTBYTES)
currLoadNode->curr.count = theValue;
else if (currKey == KEY_DSTADDR)
currLoadNode->curr.dst = theValue;
}
}
else if (currCopyNode != NULL)
{
enum { KEY_COMPTYPE, KEY_SRCADDR, KEY_SRCLEN, KEY_DSTADDR, KEY_DSTLEN, KEY_COUNT };
static const char* keyNames[KEY_COUNT] ={ "type", "src", "srclen", "dst", "dstlen" };
int currKey;
for (currKey=0; currKey<KEY_COUNT; currKey++)
{
if (strnicmp(leftSide, keyNames[currKey], leftSideLen) == 0)
break;
}
if (currKey == KEY_COUNT)
{
printer("Unknown key '%s' for COPY section on line %d, skipping\n", leftSide, currLine);
continue;
}
else
{
char* outPos = NULL;
uint32_t theValue = strtoul(rightSide, &outPos, 0);
if (outPos == NULL || outPos == rightSide)
printer("Invalid value '%s' for COPY section key '%s' on line %d, skipping\n", rightSide, leftSide, currLine);
else if (currKey == KEY_COMPTYPE)
currCopyNode->curr.compType = theValue;
else if (currKey == KEY_SRCADDR)
currCopyNode->curr.src = theValue;
else if (currKey == KEY_SRCLEN)
currCopyNode->curr.srclen = theValue;
else if (currKey == KEY_DSTADDR)
currCopyNode->curr.dst = theValue;
else if (currKey == KEY_DSTLEN)
currCopyNode->curr.dstlen = theValue;
}
}
else if (currBootNode != NULL)
{
enum { KEY_PCADDR, KEY_COUNT };
static const char* keyNames[KEY_COUNT] ={ "pc" };
int currKey;
for (currKey=0; currKey<KEY_COUNT; currKey++)
{
if (strnicmp(leftSide, keyNames[currKey], leftSideLen) == 0)
break;
}
if (currKey == KEY_COUNT)
{
printer("Unknown key '%s' for BOOT section on line %d, skipping\n", leftSide, currLine);
continue;
}
else
{
char* outPos = NULL;
uint32_t theValue = strtoul(rightSide, &outPos, 0);
if (outPos == NULL || outPos == rightSide)
printer("Invalid value '%s' for BOOT section key '%s' on line %d, skipping\n", rightSide, leftSide, currLine);
else if (currKey == KEY_PCADDR)
currBootNode->curr.pc = theValue;
}
}
else
{
printer("Key '%s' outside of recognized section on line %d, skipping\n", leftSide, currLine);
continue;
}
}
}
return out;
}
void free_memloader_info(IniParsedInfo_t* infoPtr, DeallocatorFunc deallocator)
{
if (infoPtr->loads != NULL)
{
IniLoadSectionNode_t* currNode = infoPtr->loads;
while (currNode != NULL)
{
IniLoadSectionNode_t* freeMe = currNode;
currNode = currNode->next;
if (freeMe->curr.sectname != NULL)
deallocator(freeMe->curr.sectname);
if (freeMe->curr.filename != NULL)
deallocator(freeMe->curr.filename);
deallocator(freeMe);
}
infoPtr->loads = NULL;
}
if (infoPtr->copies != NULL)
{
IniCopySectionNode_t* currNode = infoPtr->copies;
while (currNode != NULL)
{
IniCopySectionNode_t* freeMe = currNode;
currNode = currNode->next;
if (freeMe->curr.sectname != NULL)
deallocator(freeMe->curr.sectname);
deallocator(freeMe);
}
infoPtr->copies = NULL;
}
if (infoPtr->boots != NULL)
{
IniBootSectionNode_t* currNode = infoPtr->boots;
while (currNode != NULL)
{
IniBootSectionNode_t* freeMe = currNode;
currNode = currNode->next;
if (freeMe->curr.sectname != NULL)
deallocator(freeMe->curr.sectname);
deallocator(freeMe);
}
infoPtr->boots = NULL;
}
}