1 | /*␊ |
2 | * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. ␊ |
8 | * The contents of this file constitute Original Code as defined in and␊ |
9 | * are subject to the Apple Public Source License Version 2.0 (the␊ |
10 | * "License"). You may not use this file except in compliance with the␊ |
11 | * License. Please obtain a copy of the License at␊ |
12 | * http://www.apple.com/publicsource and read it before using this file.␊ |
13 | * ␊ |
14 | * This Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | ␊ |
25 | #include "bootstruct.h"␊ |
26 | #include "libsaio.h"␊ |
27 | #include "sl.h"␊ |
28 | #include "xml.h"␊ |
29 | ␊ |
30 | #define ASSERT_DICT \␊ |
31 | if (dict == DEFAULT_BOOT_CONFIG_DICT) \␊ |
32 | dict = bootInfo->bootConfig.dictionary ; \␊ |
33 | else if (dict == DEFAULT_SYSTEM_CONFIG_DICT) \␊ |
34 | dict = bootInfo->SystemConfig.dictionary ; \␊ |
35 | else if (dict == DEFAULT_OVERRIDE_CONFIG_DICT) \␊ |
36 | dict = bootInfo->overrideConfig.dictionary ; \␊ |
37 | else if (dict == DEFAULT_SMBIOS_CONFIG_DICT) \␊ |
38 | dict = bootInfo->smbiosConfig.dictionary ; \␊ |
39 | else if (dict == DEFAULT_HELPER_CONFIG_DICT) \␊ |
40 | dict = bootInfo->helperConfig.dictionary ; ␊ |
41 | ␊ |
42 | static string_ref *ref_strings = NULL;␊ |
43 | ␊ |
44 | /// TODO: remove below␊ |
45 | static char* buffer_start = NULL;␊ |
46 | // TODO: redo the next two functions␊ |
47 | static int SaveRefString(char* string, int id)␊ |
48 | {␊ |
49 | ␉//printf("Adding Ref String %d (%s)\n", id, string);␊ |
50 | ␉string_ref* tmp = ref_strings;␊ |
51 | ␉while(tmp)␊ |
52 | ␉{␊ |
53 | ␉␉if(tmp->id == id)␊ |
54 | ␉␉{␊ |
55 | ␉␉␉tmp->string = calloc(strlen(string)+1, sizeof(char));␊ |
56 | if (!tmp->string) {␊ |
57 | return -1;␊ |
58 | }␊ |
59 | ␉␉␉snprintf(tmp->string, strlen(string)+1,"%s", string);␊ |
60 | ␉␉␉return 0;␊ |
61 | ␉␉}␊ |
62 | ␉␉tmp = tmp->next;␊ |
63 | ␉}␊ |
64 | ␉␊ |
65 | ␉string_ref* new_ref = malloc(sizeof(string_ref));␊ |
66 | if (!new_ref) {␊ |
67 | return -1;␊ |
68 | }␊ |
69 | ␉bzero(new_ref,sizeof(string_ref));␊ |
70 | ␉␊ |
71 | ␉new_ref->string = calloc(strlen(string)+1, sizeof(char));␊ |
72 | if (!new_ref->string) {␊ |
73 | free(new_ref);␊ |
74 | return -1;␊ |
75 | }␊ |
76 | ␉␊ |
77 | ␉snprintf(new_ref->string, (strlen(string)+1)* sizeof(char),"%s", string);␊ |
78 | ␉new_ref->id = id;␊ |
79 | ␉new_ref->next = ref_strings;␊ |
80 | ␉ref_strings = new_ref;␊ |
81 | return 0;␊ |
82 | }␊ |
83 | ␊ |
84 | static char* GetRefString(int id)␊ |
85 | {␊ |
86 | ␉string_ref* tmp = ref_strings;␊ |
87 | ␉while(tmp)␊ |
88 | ␉{␊ |
89 | ␉␉if(tmp->id == id) return tmp->string;␊ |
90 | ␉␉tmp = tmp->next;␊ |
91 | ␉}␊ |
92 | ␉//verbose("Unable to locate Ref String %d\n", id);␊ |
93 | ␉return "Unknown";␊ |
94 | }␊ |
95 | ␊ |
96 | #define USEMALLOC 1␊ |
97 | #define DOFREE 1␊ |
98 | ␊ |
99 | static long ParseTagList(char *buffer, TagPtr *tag, long type, long empty);␊ |
100 | static long ParseTagKey(char *buffer, TagPtr *tag);␊ |
101 | static long ParseTagString(char *buffer, TagPtr *tag);␊ |
102 | static long ParseTagInteger(char *buffer, TagPtr *tag);␊ |
103 | static long ParseTagData(char *buffer, TagPtr *tag);␊ |
104 | static long ParseTagDate(char *buffer, TagPtr *tag);␊ |
105 | static long ParseTagBoolean(char *buffer, TagPtr *tag, long type);␊ |
106 | static long GetNextTag(char *buffer, char **tag, long *start);␊ |
107 | static long FixDataMatchingTag(char *buffer, char *tag);␊ |
108 | static TagPtr NewTag(void);␊ |
109 | static char *NewSymbol(char *string);␊ |
110 | #if DOFREE␊ |
111 | static void FreeSymbol(char *string);␊ |
112 | #endif␊ |
113 | ␊ |
114 | ␊ |
115 | //==========================================================================␊ |
116 | // XMLGetProperty␊ |
117 | ␊ |
118 | TagPtr␊ |
119 | XMLGetProperty( TagPtr dict, const char * key )␊ |
120 | {␊ |
121 | TagPtr tagList, tag;␊ |
122 | ␊ |
123 | ASSERT_DICT␊ |
124 | ␊ |
125 | if (dict->type != kTagTypeDict) return 0;␊ |
126 | ␊ |
127 | tag = 0;␊ |
128 | tagList = dict->tag;␊ |
129 | while (tagList)␊ |
130 | {␊ |
131 | tag = tagList;␊ |
132 | tagList = tag->tagNext;␊ |
133 | ␊ |
134 | if ((tag->type != kTagTypeKey) || (tag->string == 0)) continue;␊ |
135 | ␊ |
136 | if (!strcmp(tag->string, key)) return tag->tag;␊ |
137 | }␊ |
138 | ␊ |
139 | return 0;␊ |
140 | }␊ |
141 | ␊ |
142 | ␊ |
143 | // XMLGetTag(int index)␊ |
144 | ␊ |
145 | // XMLTagCount( TagPtr dict )␊ |
146 | int XMLTagCount( TagPtr dict )␊ |
147 | {␊ |
148 | ␉int count = 0;␊ |
149 | ␉TagPtr tagList, tag;␊ |
150 | ␊ |
151 | ASSERT_DICT␊ |
152 | ␊ |
153 | if (dict->type != kTagTypeDict && dict->type != kTagTypeArray) return 0;␊ |
154 | ␉tag = 0;␊ |
155 | tagList = dict->tag;␊ |
156 | while (tagList)␊ |
157 | {␊ |
158 | ␉␉tag = tagList;␊ |
159 | tagList = tag->tagNext;␊ |
160 | ␉␉␊ |
161 | ␉␉if (((tag->type != kTagTypeKey) && ((tag->string == 0) || (tag->string[0] == 0)))␊ |
162 | ␉␉␉&& (dict->type != kTagTypeArray)␉// If we are an array, any element is valid␊ |
163 | ␉␉␉) continue;␊ |
164 | ␉␉␊ |
165 | ␉␉//if(tag->type == kTagTypeKey) printf("Located key %s\n", tag->string);␊ |
166 | ␊ |
167 | ␉␉count++;␊ |
168 | }␊ |
169 | ␉␊ |
170 | ␉return count;␊ |
171 | }␊ |
172 | ␊ |
173 | TagPtr XMLGetElement( TagPtr dict, int id )␊ |
174 | {␊ |
175 | ASSERT_DICT␊ |
176 | ␊ |
177 | ␉if(dict->type != kTagTypeArray) return 0;␊ |
178 | ␉␊ |
179 | ␉int element = 0;␊ |
180 | ␉TagPtr tmp = dict->tag;␊ |
181 | ␊ |
182 | ␉while(element < id)␊ |
183 | ␉{␊ |
184 | ␉␉element++;␊ |
185 | ␉␉tmp = tmp->tagNext;␊ |
186 | ␉}␊ |
187 | ␉␊ |
188 | ␉return tmp;␊ |
189 | }␊ |
190 | /* Function for basic XML character entities parsing */␊ |
191 | ␊ |
192 | typedef const struct XMLEntity {␊ |
193 | ␉const char* name;␊ |
194 | ␉size_t nameLen;␊ |
195 | ␉char value;␊ |
196 | } XMLEntity;␊ |
197 | ␊ |
198 | /* This is ugly, but better than specifying the lengths by hand */␊ |
199 | #define _e(str,c) {str,sizeof(str)-1,c}␊ |
200 | const XMLEntity ents[] = {␊ |
201 | ␉_e("quot;",'"'), _e("apos;",'\''),␊ |
202 | ␉_e("lt;", '<'), _e("gt;", '>'),␊ |
203 | ␉_e("amp;", '&')␊ |
204 | };␊ |
205 | ␊ |
206 | char*␊ |
207 | XMLDecode(const char* src)␊ |
208 | { ␊ |
209 | size_t len;␊ |
210 | const char *s;␊ |
211 | char *out, *o;␊ |
212 | ␊ |
213 | if ( !src || !(len = strlen(src)) || !(out = calloc(len+1,sizeof(char))) )␊ |
214 | return 0;␊ |
215 | ␊ |
216 | o = out;␊ |
217 | s = src;␊ |
218 | while (s <= src+len) /* Make sure the terminator is also copied */␊ |
219 | {␊ |
220 | if ( *s == '&' )␊ |
221 | {␊ |
222 | bool entFound = false;␊ |
223 | int i;␊ |
224 | ␊ |
225 | s++;␊ |
226 | for ( i = 0; (unsigned)i < (sizeof(ents)/sizeof(ents[0])); i++)␊ |
227 | {␊ |
228 | if ( strncmp(s, ents[i].name, ents[i].nameLen) == 0 )␊ |
229 | {␊ |
230 | entFound = true;␊ |
231 | break;␊ |
232 | }␊ |
233 | }␊ |
234 | if ( entFound )␊ |
235 | {␊ |
236 | *o++ = ents[i].value;␊ |
237 | s += ents[i].nameLen;␊ |
238 | continue;␊ |
239 | }␊ |
240 | }␊ |
241 | ␊ |
242 | *o++ = *s++;␊ |
243 | }␊ |
244 | ␊ |
245 | return out;␊ |
246 | } ␊ |
247 | ␊ |
248 | //==========================================================================␊ |
249 | // XMLParseFile␊ |
250 | // Expects to see one dictionary in the XML file, the final pos will be returned␊ |
251 | // If the pos is not equal to the strlen, then there are multiple dicts␊ |
252 | // Puts the first dictionary it finds in the␊ |
253 | // tag pointer and returns the end of the dic, or returns -1 if not found.␊ |
254 | //␊ |
255 | long␊ |
256 | XMLParseFile( char * buffer, TagPtr * dict )␊ |
257 | {␊ |
258 | long length, pos;␊ |
259 | TagPtr tag = 0;␊ |
260 | pos = 0;␊ |
261 | ␉char *configBuffer;␊ |
262 | ␉␊ |
263 | ␉␊ |
264 | ␉length = strlen(buffer) + 1;␊ |
265 | configBuffer = calloc(length, sizeof(char));␊ |
266 | if (!configBuffer) {␊ |
267 | return -1;␊ |
268 | }␊ |
269 | strlcpy(configBuffer, buffer, length);␊ |
270 | ␉␊ |
271 | ␉buffer_start = configBuffer;␊ |
272 | ␊ |
273 | while (1)␊ |
274 | {␊ |
275 | length = XMLParseNextTag(configBuffer + pos, &tag);␊ |
276 | if (length == -1) break;␊ |
277 | ␊ |
278 | pos += length;␊ |
279 | ␊ |
280 | if (tag == 0) continue;␊ |
281 | if (tag->type == kTagTypeDict) break;␊ |
282 | ␊ |
283 | XMLFreeTag(tag);␊ |
284 | }␊ |
285 | ␉free(configBuffer);␊ |
286 | ␉if (length < 0) {␊ |
287 | return -1;␊ |
288 | }␊ |
289 | *dict = tag;␊ |
290 | return pos;␊ |
291 | }␊ |
292 | ␊ |
293 | //==========================================================================␊ |
294 | // ParseNextTag␊ |
295 | // TODO: cleanup␊ |
296 | long␊ |
297 | XMLParseNextTag( char * buffer, TagPtr * tag )␊ |
298 | {␊ |
299 | ␉long length, pos;␊ |
300 | ␉char * tagName;␊ |
301 | ␊ |
302 | length = GetNextTag(buffer, &tagName, 0);␊ |
303 | if (length == -1) return -1;␊ |
304 | ␉␊ |
305 | ␉pos = length;␊ |
306 | if (!strncmp(tagName, kXMLTagPList, 6))␊ |
307 | {␊ |
308 | length = 0;␊ |
309 | }␊ |
310 | ␉/***** dict ****/␊ |
311 | else if (!strncmp(tagName, kXMLTagDict, sizeof(kXMLTagDict)))␊ |
312 | {␊ |
313 | length = ParseTagList(buffer + pos, tag, kTagTypeDict, 0);␊ |
314 | }␊ |
315 | else if (!strncmp(tagName, kXMLTagDict, strlen(kXMLTagDict)) && tagName[strlen(tagName)-1] == '/')␊ |
316 | {␊ |
317 | length = ParseTagList(buffer + pos, tag, kTagTypeDict, 1);␊ |
318 | }␊ |
319 | else if (!strncmp(tagName, kXMLTagDict " ", strlen(kXMLTagDict " ")))␊ |
320 | {␊ |
321 | length = ParseTagList(buffer + pos, tag, kTagTypeDict, 0);␊ |
322 | }␊ |
323 | ␉/***** key ****/␊ |
324 | else if (!strncmp(tagName, kXMLTagKey, sizeof(kXMLTagKey)))␊ |
325 | {␊ |
326 | length = ParseTagKey(buffer + pos, tag);␊ |
327 | }␊ |
328 | ␉␊ |
329 | ␉/***** string ****/␊ |
330 | else if (!strncmp(tagName, kXMLTagString, sizeof(kXMLTagString)))␊ |
331 | {␊ |
332 | length = ParseTagString(buffer + pos, tag);␊ |
333 | }␊ |
334 | else if (!strncmp(tagName, kXMLTagString " ", strlen(kXMLTagString " ")))␊ |
335 | {␊ |
336 | ␉␉// TODO: save tag if if found␊ |
337 | ␉␉if(!strncmp(tagName + strlen(kXMLTagString " "), kXMLStringID, strlen(kXMLStringID)))␊ |
338 | ␉␉{␊ |
339 | ␉␉␉// ID=␊ |
340 | ␉␉␉int id = 0;␊ |
341 | ␉␉␉int cnt = strlen(kXMLTagString " " kXMLStringID "\"") + 1;␊ |
342 | ␉␉␉while ((tagName[cnt] != '\0') && (tagName[cnt] != '"')) cnt++;␊ |
343 | ␉␉␉tagName[cnt] = 0;␊ |
344 | ␉␉␉char* val = tagName + strlen(kXMLTagString " " kXMLStringID "\"");␊ |
345 | ␉␉␉while(*val)␊ |
346 | ␉␉␉{␉␉␉␊ |
347 | ␉␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
348 | ␉␉␉␉{␊ |
349 | ␉␉␉␉␉id = (id * 10) + (*val++ - '0');␊ |
350 | ␉␉␉␉}␊ |
351 | ␉␉␉␉else␊ |
352 | ␉␉␉␉{␊ |
353 | ␉␉␉␉␉printf("ParseStringID error (0x%x)\n", *val);␊ |
354 | ␉␉␉␉␉getc();␊ |
355 | ␉␉␉␉␉return -1;␊ |
356 | ␉␉␉␉␉␊ |
357 | ␉␉␉␉}␊ |
358 | ␉␉␉␉␊ |
359 | ␉␉␉}␊ |
360 | ␉␉␉length = ParseTagString(buffer + pos, tag);␊ |
361 | ␊ |
362 | ␉␉␉if (SaveRefString(buffer + pos, id) != 0) return -1;␊ |
363 | ␉␉}␊ |
364 | ␉␉else if(!strncmp(tagName + strlen(kXMLTagString " "), kXMLStringIDRef, strlen(kXMLStringIDRef)))␊ |
365 | ␉␉{␊ |
366 | ␉␉␉// IDREF=␊ |
367 | ␉␉␉int id = 0;␊ |
368 | ␉␉␉int cnt = strlen(kXMLTagString " " kXMLStringIDRef "\"") + 1;␊ |
369 | ␉␉␉while ((tagName[cnt] != '\0') && (tagName[cnt] != '"')) cnt++;␊ |
370 | ␉␉␉tagName[cnt] = 0;␊ |
371 | ␉␉␉char* val = tagName + strlen(kXMLTagString " " kXMLStringIDRef "\"");␊ |
372 | ␉␉␉while(*val)␊ |
373 | ␉␉␉{␉␉␉␊ |
374 | ␉␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
375 | ␉␉␉␉{␊ |
376 | ␉␉␉␉␉id = (id * 10) + (*val++ - '0');␊ |
377 | ␉␉␉␉}␊ |
378 | ␉␉␉␉else␊ |
379 | ␉␉␉␉{␊ |
380 | ␉␉␉␉␉printf("ParseStringIDREF error (0x%x)\n", *val);␊ |
381 | ␉␉␉␉␉getc();␊ |
382 | ␉␉␉␉␉return -1;␊ |
383 | ␉␉␉␉␉␊ |
384 | ␉␉␉␉}␊ |
385 | ␉␉␉␉␊ |
386 | ␉␉␉}␊ |
387 | ␉␉␉char* str = GetRefString(id);␊ |
388 | ␊ |
389 | ␉␉␉TagPtr tmpTag = NewTag();␊ |
390 | ␉␉␉tmpTag->type = kTagTypeString;␊ |
391 | ␉␉␉tmpTag->string = str;␊ |
392 | ␉␉␉tmpTag->tag = 0;␊ |
393 | ␉␉␉tmpTag->tagNext = 0;␊ |
394 | ␉␉␉tmpTag->offset = buffer_start ? buffer - buffer_start + pos : 0;␊ |
395 | ␉␉␉*tag = tmpTag;␊ |
396 | ␉␉␉␊ |
397 | ␉␉␉length = 0;␊ |
398 | ␉␉␉//printf("Located IDREF, id = %d, string = %s\n", id, str);␊ |
399 | ␉␉}␊ |
400 | ␊ |
401 | }␊ |
402 | ␉␊ |
403 | ␉/***** integer ****/␊ |
404 | else if (!strncmp(tagName, kXMLTagInteger, sizeof(kXMLTagInteger)))␊ |
405 | {␊ |
406 | length = ParseTagInteger(buffer + pos, tag);␊ |
407 | }␊ |
408 | else if (!strncmp(tagName, kXMLTagInteger " ", strlen(kXMLTagInteger " ")))␊ |
409 | {␊ |
410 | ␉␉if(!strncmp(tagName + strlen(kXMLTagInteger " "), kXMLStringID, strlen(kXMLStringID)))␊ |
411 | ␉␉{␊ |
412 | ␉␉␉// ID=␊ |
413 | ␉␉␉int id = 0;␊ |
414 | ␉␉␉int cnt = strlen(kXMLTagInteger " " kXMLStringID "\"") + 1;␊ |
415 | ␉␉␉while ((tagName[cnt] != '\0') && (tagName[cnt] != '"')) cnt++;␊ |
416 | ␉␉␉tagName[cnt] = 0;␊ |
417 | ␉␉␉char* val = tagName + strlen(kXMLTagInteger " " kXMLStringID "\"");␊ |
418 | ␉␉␉while(*val)␊ |
419 | ␉␉␉{␉␉␉␊ |
420 | ␉␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
421 | ␉␉␉␉{␊ |
422 | ␉␉␉␉␉id = (id * 10) + (*val++ - '0');␊ |
423 | ␉␉␉␉}␊ |
424 | ␉␉␉␉else␊ |
425 | ␉␉␉␉{␊ |
426 | ␉␉␉␉␉printf("ParseIntegerID error (0x%x)\n", *val);␊ |
427 | ␉␉␉␉␉getc();␊ |
428 | ␉␉␉␉␉return -1;␊ |
429 | ␉␉␉␉␉␊ |
430 | ␉␉␉␉}␊ |
431 | ␉␉␉␉␊ |
432 | ␉␉␉}␊ |
433 | ␉␉␉length = ParseTagInteger(buffer + pos, tag);␊ |
434 | ␉␉␉␊ |
435 | ␉␉␉if (SaveRefString((*tag)->string, id) != 0) return -1;␊ |
436 | ␉␉}␊ |
437 | ␉␉else if(!strncmp(tagName + strlen(kXMLTagInteger " "), kXMLStringIDRef, strlen(kXMLStringIDRef)))␊ |
438 | ␉␉{␊ |
439 | ␉␉␉// IDREF=␊ |
440 | ␉␉␉int id = 0;␊ |
441 | ␉␉␉int cnt = strlen(kXMLTagInteger " " kXMLStringIDRef "\"") + 1;␊ |
442 | ␉␉␉while ((tagName[cnt] != '\0') && (tagName[cnt] != '"')) cnt++;␊ |
443 | ␉␉␉tagName[cnt] = 0;␊ |
444 | ␉␉␉char* val = tagName + strlen(kXMLTagInteger " " kXMLStringIDRef "\"");␊ |
445 | ␉␉␉while(*val)␊ |
446 | ␉␉␉{␉␉␉␊ |
447 | ␉␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
448 | ␉␉␉␉{␊ |
449 | ␉␉␉␉␉id = (id * 10) + (*val++ - '0');␊ |
450 | ␉␉␉␉}␊ |
451 | ␉␉␉␉else␊ |
452 | ␉␉␉␉{␊ |
453 | ␉␉␉␉␉printf("ParseStringIDREF error (0x%x)\n", *val);␊ |
454 | ␉␉␉␉␉getc();␊ |
455 | ␉␉␉␉␉return -1;␊ |
456 | ␉␉␉␉␉␊ |
457 | ␉␉␉␉}␊ |
458 | ␉␉␉␉␊ |
459 | ␉␉␉}␊ |
460 | ␉␉␉int integer = (int)GetRefString(id);␊ |
461 | ␉␉␉␊ |
462 | ␉␉␉TagPtr tmpTag = NewTag();␊ |
463 | if (tmpTag == 0) return -1;␊ |
464 | ␉␉␉␊ |
465 | ␉␉␉tmpTag->type = kTagTypeInteger;␊ |
466 | ␉␉␉tmpTag->string = (char*) integer;␊ |
467 | ␉␉␉tmpTag->tag = 0;␊ |
468 | ␉␉␉tmpTag->tagNext = 0;␊ |
469 | ␉␉␉tmpTag->offset = buffer_start ? buffer - buffer_start + pos : 0;␊ |
470 | ␊ |
471 | ␉␉␉*tag = tmpTag;␊ |
472 | ␉␉␉␊ |
473 | ␉␉␉length = 0;␊ |
474 | ␉␉␉//printf("Located IDREF, id = %d, string = %s\n", id, str);␊ |
475 | ␉␉}␉␉␊ |
476 | ␉␉else ␊ |
477 | ␉␉{␊ |
478 | ␉␉␉length = ParseTagInteger(buffer + pos, tag);␊ |
479 | ␉␉}␊ |
480 | ␉}␊ |
481 | ␉␊ |
482 | ␉/***** data ****/␊ |
483 | else if (!strncmp(tagName, kXMLTagData, sizeof(kXMLTagData)))␊ |
484 | {␊ |
485 | length = ParseTagData(buffer + pos, tag);␊ |
486 | }␊ |
487 | ␉else if (!strncmp(tagName, kXMLTagData " ", strlen(kXMLTagData " ")))␊ |
488 | {␊ |
489 | length = ParseTagData(buffer + pos, tag);␊ |
490 | }␉␊ |
491 | else if (!strncmp(tagName, kXMLTagDate, sizeof(kXMLTagDate)))␊ |
492 | {␊ |
493 | length = ParseTagDate(buffer + pos, tag);␊ |
494 | }␊ |
495 | ␉␊ |
496 | ␉/***** date ****/␊ |
497 | ␉else if (!strncmp(tagName, kXMLTagDate " ", strlen(kXMLTagDate " ")))␊ |
498 | {␊ |
499 | length = ParseTagDate(buffer + pos, tag);␊ |
500 | }␉␊ |
501 | ␉␊ |
502 | ␉/***** false ****/␊ |
503 | else if (!strncmp(tagName, kXMLTagFalse, sizeof(kXMLTagFalse)))␊ |
504 | {␊ |
505 | length = ParseTagBoolean(buffer + pos, tag, kTagTypeFalse);␊ |
506 | }␊ |
507 | ␉/***** true ****/␊ |
508 | else if (!strncmp(tagName, kXMLTagTrue, sizeof(kXMLTagTrue)))␊ |
509 | {␊ |
510 | length = ParseTagBoolean(buffer + pos, tag, kTagTypeTrue);␊ |
511 | }␊ |
512 | ␉␊ |
513 | ␉/***** array ****/␊ |
514 | else if (!strncmp(tagName, kXMLTagArray, sizeof(kXMLTagArray) ))␊ |
515 | {␊ |
516 | length = ParseTagList(buffer + pos, tag, kTagTypeArray, 0);␊ |
517 | }␊ |
518 | ␉else if (!strncmp(tagName, kXMLTagArray " ", strlen(kXMLTagArray " ")))␊ |
519 | {␊ |
520 | length = ParseTagList(buffer + pos, tag, kTagTypeArray, 0);␊ |
521 | }␉␉␊ |
522 | else if (!strncmp(tagName, kXMLTagArray "/", strlen(kXMLTagArray "/")))␊ |
523 | {␊ |
524 | length = ParseTagList(buffer + pos, tag, kTagTypeArray, 1);␊ |
525 | }␊ |
526 | ␉␊ |
527 | ␉/***** unknown ****/␊ |
528 | else␊ |
529 | {␊ |
530 | *tag = 0;␊ |
531 | length = 0;␊ |
532 | }␊ |
533 | ␊ |
534 | ␊ |
535 | if (length == -1) return -1;␊ |
536 | ␊ |
537 | return pos + length;␊ |
538 | }␊ |
539 | ␊ |
540 | //==========================================================================␊ |
541 | // ParseTagList␊ |
542 | ␊ |
543 | static long␊ |
544 | ParseTagList( char * buffer, TagPtr * tag, long type, long empty )␊ |
545 | {␊ |
546 | ␉long length, pos;␊ |
547 | ␉TagPtr tagList, tmpTag;␊ |
548 | ␊ |
549 | tagList = 0;␊ |
550 | tmpTag = 0;␊ |
551 | pos = 0;␊ |
552 | ␊ |
553 | if (!empty)␊ |
554 | {␊ |
555 | while (1)␊ |
556 | {␊ |
557 | length = XMLParseNextTag(buffer + pos, &tmpTag);␊ |
558 | if (length == -1) break;␊ |
559 | ␊ |
560 | pos += length;␊ |
561 | ␊ |
562 | if (tmpTag == 0) break;␊ |
563 | tmpTag->tagNext = tagList;␊ |
564 | tagList = tmpTag;␊ |
565 | }␊ |
566 | ␊ |
567 | if (length == -1)␊ |
568 | {␊ |
569 | XMLFreeTag(tagList);␊ |
570 | return -1;␊ |
571 | }␊ |
572 | }␊ |
573 | ␊ |
574 | tmpTag = NewTag();␊ |
575 | if (tmpTag == 0)␊ |
576 | {␊ |
577 | XMLFreeTag(tagList);␊ |
578 | return -1;␊ |
579 | }␊ |
580 | ␊ |
581 | tmpTag->type = type;␊ |
582 | tmpTag->string = 0;␊ |
583 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start : 0;␊ |
584 | tmpTag->tag = tagList;␊ |
585 | tmpTag->tagNext = 0;␊ |
586 | ␊ |
587 | *tag = tmpTag;␊ |
588 | ␊ |
589 | return pos;␊ |
590 | }␊ |
591 | ␊ |
592 | //==========================================================================␊ |
593 | // ParseTagKey␊ |
594 | ␊ |
595 | static long␊ |
596 | ParseTagKey( char * buffer, TagPtr * tag )␊ |
597 | {␊ |
598 | long length, length2;␊ |
599 | char *string;␊ |
600 | TagPtr tmpTag, subTag;␊ |
601 | ␊ |
602 | length = FixDataMatchingTag(buffer, kXMLTagKey);␊ |
603 | if (length == -1) return -1;␊ |
604 | ␊ |
605 | length2 = XMLParseNextTag(buffer + length, &subTag);␊ |
606 | if (length2 == -1) return -1;␊ |
607 | ␊ |
608 | tmpTag = NewTag();␊ |
609 | if (tmpTag == 0)␊ |
610 | {␊ |
611 | XMLFreeTag(subTag);␊ |
612 | return -1;␊ |
613 | }␊ |
614 | ␊ |
615 | string = NewSymbol(buffer);␊ |
616 | if (string == 0)␊ |
617 | {␊ |
618 | XMLFreeTag(subTag);␊ |
619 | XMLFreeTag(tmpTag);␊ |
620 | return -1;␊ |
621 | }␊ |
622 | ␊ |
623 | tmpTag->type = kTagTypeKey;␊ |
624 | tmpTag->string = string;␊ |
625 | tmpTag->tag = subTag;␊ |
626 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
627 | tmpTag->tagNext = 0;␊ |
628 | ␊ |
629 | *tag = tmpTag;␊ |
630 | ␊ |
631 | return length + length2;␊ |
632 | }␊ |
633 | ␊ |
634 | //==========================================================================␊ |
635 | // ParseTagString␊ |
636 | ␊ |
637 | static long␊ |
638 | ParseTagString( char * buffer, TagPtr * tag )␊ |
639 | {␊ |
640 | long length;␊ |
641 | char * string;␊ |
642 | TagPtr tmpTag;␊ |
643 | ␊ |
644 | length = FixDataMatchingTag(buffer, kXMLTagString);␊ |
645 | if (length == -1) return -1;␊ |
646 | ␊ |
647 | tmpTag = NewTag();␊ |
648 | if (tmpTag == 0) return -1;␊ |
649 | ␊ |
650 | string = NewSymbol(buffer);␊ |
651 | if (string == 0)␊ |
652 | {␊ |
653 | XMLFreeTag(tmpTag);␊ |
654 | return -1;␊ |
655 | }␊ |
656 | ␊ |
657 | tmpTag->type = kTagTypeString;␊ |
658 | tmpTag->string = string;␊ |
659 | tmpTag->tag = 0;␊ |
660 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
661 | tmpTag->tagNext = 0;␊ |
662 | ␊ |
663 | *tag = tmpTag;␊ |
664 | return length;␊ |
665 | }␊ |
666 | ␊ |
667 | //==========================================================================␊ |
668 | // ParseTagInteger␊ |
669 | ␊ |
670 | static long␊ |
671 | ParseTagInteger( char * buffer, TagPtr * tag )␊ |
672 | {␊ |
673 | long length, integer;␊ |
674 | ␉bool negative = false;␊ |
675 | TagPtr tmpTag;␊ |
676 | ␉char* val = buffer;␊ |
677 | int size;␊ |
678 | ␉␊ |
679 | tmpTag = NewTag();␊ |
680 | if (tmpTag == 0) return -1;␊ |
681 | ␉␊ |
682 | ␉if(buffer[0] == '<') ␊ |
683 | ␉{␊ |
684 | ␉␉printf("Warning integer is non existant\n");␊ |
685 | ␉␉getc();␊ |
686 | ␉␉tmpTag->type = kTagTypeInteger;␊ |
687 | ␉␉tmpTag->string = 0;␊ |
688 | ␉␉tmpTag->tag = 0;␊ |
689 | ␉␉tmpTag->offset = 0;␊ |
690 | ␉␉tmpTag->tagNext = 0;␊ |
691 | ␉␉␊ |
692 | ␉␉*tag = tmpTag;␊ |
693 | ␉␉␊ |
694 | ␉␉return 0;␊ |
695 | ␉}␊ |
696 | ␉␊ |
697 | size = length = FixDataMatchingTag(buffer, kXMLTagInteger);␊ |
698 | if (length == -1) return -1;␊ |
699 | ␉␊ |
700 | integer = 0;␊ |
701 | ␊ |
702 | ␉if(size > 1 && (val[1] == 'x' || val[1] == 'X'))␉// Hex value␊ |
703 | ␉{␊ |
704 | ␉␉val += 2;␊ |
705 | ␉␉while(*val)␊ |
706 | ␉␉{␉␉␉␊ |
707 | ␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
708 | ␉␉␉{␊ |
709 | ␉␉␉␉integer = (integer * 16) + (*val++ - '0');␊ |
710 | ␉␉␉}␊ |
711 | ␉␉␉else if ((*val >= 'a' && *val <= 'f'))␉// a - f␊ |
712 | ␉␉␉{␊ |
713 | ␉␉␉␉integer = (integer * 16) + (*val++ - 'a' + 10);␊ |
714 | ␉␉␉}␊ |
715 | ␉␉␉else if ((*val >= 'A' && *val <= 'F'))␉// A - F␊ |
716 | ␉␉␉{␊ |
717 | ␉␉␉␉integer = (integer * 16) + (*val++ - 'a' + 10);␊ |
718 | ␉␉␉}␊ |
719 | ␉␉␉else␊ |
720 | ␉␉␉{␊ |
721 | ␉␉␉␉printf("ParseTagInteger hex error (0x%x) in buffer %s\n", *val, buffer);␊ |
722 | ␉␉␉␉getc();␊ |
723 | ␉␉␉␉return -1;␊ |
724 | ␉␉␉␉␊ |
725 | ␉␉␉}␊ |
726 | ␊ |
727 | ␉␉}␊ |
728 | ␉}␊ |
729 | ␉else if ( size )␉// Decimal value␊ |
730 | ␉{␊ |
731 | ␉␉if (*val == '-')␊ |
732 | ␉␉{␊ |
733 | ␉␉␉negative = true;␊ |
734 | ␉␉␉val++;␊ |
735 | ␉␉␉size--;␊ |
736 | ␉␉}␊ |
737 | ␉␉␊ |
738 | ␉␉for (integer = 0; size > 0; size--)␊ |
739 | ␉␉{␊ |
740 | ␉␉␉if(*val) // UGLY HACK, fix me.␊ |
741 | ␉␉␉{␊ |
742 | ␉␉␉␉if (*val < '0' || *val > '9')␊ |
743 | ␉␉␉␉{␊ |
744 | ␉␉␉␉␉printf("ParseTagInteger decimal error (0x%x) in buffer %s\n", *val, buffer);␊ |
745 | ␉␉␉␉␉getc();␊ |
746 | ␉␉␉␉␉return -1;␊ |
747 | ␉␉␉␉}␊ |
748 | ␉␉␉␉␊ |
749 | ␉␉␉␉integer = (integer * 10) + (*val++ - '0');␊ |
750 | ␉␉␉}␊ |
751 | ␉␉}␊ |
752 | ␉␉␊ |
753 | ␉␉if (negative)␊ |
754 | ␉␉␉integer = -integer;␊ |
755 | ␉}␊ |
756 | ␊ |
757 | tmpTag->type = kTagTypeInteger;␊ |
758 | ␉tmpTag->string = (char *)integer;␊ |
759 | ␉tmpTag->tag = 0;␊ |
760 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
761 | tmpTag->tagNext = 0;␊ |
762 | ␊ |
763 | *tag = tmpTag;␊ |
764 | ␊ |
765 | return length;␊ |
766 | }␊ |
767 | ␊ |
768 | //==========================================================================␊ |
769 | // ParseTagData␊ |
770 | ␊ |
771 | static long␊ |
772 | ParseTagData( char * buffer, TagPtr * tag )␊ |
773 | {␊ |
774 | long length;␊ |
775 | TagPtr tmpTag;␊ |
776 | ␊ |
777 | length = FixDataMatchingTag(buffer, kXMLTagData);␊ |
778 | if (length == -1) return -1;␊ |
779 | ␊ |
780 | tmpTag = NewTag();␊ |
781 | if (tmpTag == 0) return -1;␊ |
782 | ␊ |
783 | ␉//printf("ParseTagData unimplimented\n");␊ |
784 | ␉//printf("Data: %s\n", buffer);␊ |
785 | ␉//␉getc();␊ |
786 | ␉␊ |
787 | ␉// TODO: base64 decode␊ |
788 | ␉␊ |
789 | ␉char* string = NewSymbol(buffer);␊ |
790 | tmpTag->type = kTagTypeData;␊ |
791 | tmpTag->string = string;␊ |
792 | tmpTag->tag = 0;␊ |
793 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
794 | tmpTag->tagNext = 0;␊ |
795 | ␊ |
796 | *tag = tmpTag;␊ |
797 | ␊ |
798 | return length;␊ |
799 | }␊ |
800 | ␊ |
801 | //==========================================================================␊ |
802 | // ParseTagDate␊ |
803 | ␊ |
804 | static long␊ |
805 | ParseTagDate( char * buffer, TagPtr * tag )␊ |
806 | {␊ |
807 | long length;␊ |
808 | TagPtr tmpTag;␊ |
809 | ␊ |
810 | length = FixDataMatchingTag(buffer, kXMLTagDate);␊ |
811 | if (length == -1) return -1;␊ |
812 | ␊ |
813 | tmpTag = NewTag();␊ |
814 | if (tmpTag == 0) return -1;␊ |
815 | ␊ |
816 | ␉printf("ParseTagDate unimplimented\n");␊ |
817 | ␉getc();␊ |
818 | ␉␊ |
819 | tmpTag->type = kTagTypeDate;␊ |
820 | tmpTag->string = 0;␊ |
821 | tmpTag->tag = 0;␊ |
822 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
823 | tmpTag->tagNext = 0;␊ |
824 | ␊ |
825 | *tag = tmpTag;␊ |
826 | ␊ |
827 | return length;␊ |
828 | }␊ |
829 | ␊ |
830 | //==========================================================================␊ |
831 | // ParseTagBoolean␊ |
832 | ␊ |
833 | static long␊ |
834 | ParseTagBoolean( char * buffer, TagPtr * tag, long type )␊ |
835 | {␊ |
836 | TagPtr tmpTag;␊ |
837 | ␊ |
838 | tmpTag = NewTag();␊ |
839 | if (tmpTag == 0) return -1;␊ |
840 | ␊ |
841 | tmpTag->type = type;␊ |
842 | tmpTag->string = 0;␊ |
843 | tmpTag->tag = 0;␊ |
844 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
845 | tmpTag->tagNext = 0;␊ |
846 | ␊ |
847 | *tag = tmpTag;␊ |
848 | ␊ |
849 | return 0;␊ |
850 | }␊ |
851 | ␊ |
852 | //==========================================================================␊ |
853 | // GetNextTag␊ |
854 | ␊ |
855 | static long␊ |
856 | GetNextTag( char * buffer, char ** tag, long * start )␊ |
857 | {␊ |
858 | long cnt, cnt2;␊ |
859 | ␊ |
860 | if (tag == 0) return -1;␊ |
861 | ␊ |
862 | // Find the start of the tag.␊ |
863 | cnt = 0;␊ |
864 | while ((buffer[cnt] != '\0') && (buffer[cnt] != '<')) cnt++;␊ |
865 | if (buffer[cnt] == '\0') return -1;␊ |
866 | ␊ |
867 | // Find the end of the tag.␊ |
868 | cnt2 = cnt + 1;␊ |
869 | while ((buffer[cnt2] != '\0') && (buffer[cnt2] != '>')) cnt2++;␊ |
870 | if (buffer[cnt2] == '\0') return -1;␊ |
871 | ␊ |
872 | // Fix the tag data.␊ |
873 | *tag = buffer + cnt + 1;␊ |
874 | buffer[cnt2] = '\0';␊ |
875 | if (start) *start = cnt;␊ |
876 | ␊ |
877 | return cnt2 + 1;␊ |
878 | }␊ |
879 | ␊ |
880 | //==========================================================================␊ |
881 | // FixDataMatchingTag␊ |
882 | // Modifies 'buffer' to add a '\0' at the end of the tag matching 'tag'.␊ |
883 | // Returns the length of the data found, counting the end tag,␊ |
884 | // or -1 if the end tag was not found.␊ |
885 | ␊ |
886 | static long␊ |
887 | FixDataMatchingTag( char * buffer, char * tag )␊ |
888 | {␊ |
889 | long length, start, stop;␊ |
890 | char * endTag;␊ |
891 | ␊ |
892 | start = 0;␊ |
893 | while (1)␊ |
894 | {␊ |
895 | length = GetNextTag(buffer + start, &endTag, &stop);␊ |
896 | if (length == -1) return -1;␊ |
897 | ␊ |
898 | if ((*endTag == '/') && !strcmp(endTag + 1, tag)) break;␊ |
899 | start += length;␊ |
900 | }␊ |
901 | ␊ |
902 | buffer[start + stop] = '\0';␊ |
903 | ␊ |
904 | return start + length;␊ |
905 | }␊ |
906 | ␊ |
907 | //==========================================================================␊ |
908 | // NewTag␊ |
909 | ␊ |
910 | #define kTagsPerBlock (0x1000)␊ |
911 | ␊ |
912 | static TagPtr gTagsFree;␊ |
913 | ␊ |
914 | static TagPtr␊ |
915 | NewTag( void )␊ |
916 | {␊ |
917 | ␉long cnt;␊ |
918 | ␉TagPtr tag = 0;␊ |
919 | ␊ |
920 | if (gTagsFree == 0)␊ |
921 | {␊ |
922 | #if USEMALLOC␊ |
923 | tag = (TagPtr)calloc(sizeof(Tag), kTagsPerBlock);␊ |
924 | #else␊ |
925 | tag = (TagPtr)AllocateBootXMemory(kTagsPerBlock * sizeof(Tag));␊ |
926 | #endif␊ |
927 | if (tag == 0) return 0;␊ |
928 | ␊ |
929 | // Initalize the new tags.␊ |
930 | for (cnt = 0; cnt < kTagsPerBlock; cnt++)␊ |
931 | {␊ |
932 | tag[cnt].type = kTagTypeNone;␊ |
933 | tag[cnt].string = 0;␊ |
934 | tag[cnt].tag = 0;␊ |
935 | tag[cnt].tagNext = tag + cnt + 1;␊ |
936 | }␊ |
937 | tag[kTagsPerBlock - 1].tagNext = 0;␊ |
938 | ␊ |
939 | gTagsFree = tag;␊ |
940 | }␊ |
941 | ␊ |
942 | tag = gTagsFree;␊ |
943 | gTagsFree = tag->tagNext;␊ |
944 | ␊ |
945 | return tag;␊ |
946 | }␊ |
947 | ␊ |
948 | //==========================================================================␊ |
949 | // XMLFreeTag␊ |
950 | ␊ |
951 | void␊ |
952 | XMLFreeTag( TagPtr tag )␊ |
953 | {␊ |
954 | #if DOFREE␊ |
955 | if (tag == 0) return;␊ |
956 | ␊ |
957 | if (tag->string) FreeSymbol(tag->string);␊ |
958 | ␊ |
959 | XMLFreeTag(tag->tag);␊ |
960 | XMLFreeTag(tag->tagNext);␊ |
961 | ␊ |
962 | // Clear and free the tag.␊ |
963 | tag->type = kTagTypeNone;␊ |
964 | tag->string = 0;␊ |
965 | tag->tag = 0;␊ |
966 | ␉tag->offset = 0;␊ |
967 | tag->tagNext = gTagsFree;␊ |
968 | gTagsFree = tag;␊ |
969 | #else␊ |
970 | return;␊ |
971 | #endif␊ |
972 | }␊ |
973 | ␊ |
974 | //==========================================================================␊ |
975 | // Symbol object.␊ |
976 | ␊ |
977 | struct Symbol␊ |
978 | {␊ |
979 | long refCount;␊ |
980 | struct Symbol *next;␊ |
981 | char *string; ␊ |
982 | };␊ |
983 | typedef struct Symbol Symbol, *SymbolPtr;␊ |
984 | ␊ |
985 | static SymbolPtr FindSymbol(char * string, SymbolPtr * prevSymbol);␊ |
986 | ␊ |
987 | static SymbolPtr gSymbolsHead;␊ |
988 | ␊ |
989 | //==========================================================================␊ |
990 | // NewSymbol␊ |
991 | ␊ |
992 | static char *␊ |
993 | NewSymbol( char * string )␊ |
994 | {␊ |
995 | static SymbolPtr lastGuy = 0;␊ |
996 | ␉SymbolPtr symbol;␊ |
997 | ␊ |
998 | // Look for string in the list of symbols.␊ |
999 | symbol = FindSymbol(string, 0);␊ |
1000 | ␊ |
1001 | // Add the new symbol.␊ |
1002 | if (symbol == 0)␊ |
1003 | {␊ |
1004 | int len;␊ |
1005 | #if USEMALLOC␊ |
1006 | symbol = (SymbolPtr)malloc(sizeof(Symbol) );␊ |
1007 | #else␊ |
1008 | symbol = (SymbolPtr)AllocateBootXMemory(sizeof(Symbol));␊ |
1009 | #endif␊ |
1010 | if (symbol == 0) { ␊ |
1011 | stop("NULL symbol!"); ␊ |
1012 | return 0;␊ |
1013 | }␊ |
1014 | ␉␉bzero(symbol,sizeof(Symbol) );␊ |
1015 | ␉␉␊ |
1016 | len = strlen(string) + 1;␊ |
1017 | symbol->string = (char*)calloc(len,sizeof(char));␊ |
1018 | if (symbol->string == 0) {␊ |
1019 | free(symbol);␊ |
1020 | stop("NULL symbol->string!");␊ |
1021 | return 0;␊ |
1022 | }␊ |
1023 | // Set the symbol's data.␊ |
1024 | symbol->refCount = 0;␊ |
1025 | strlcpy(symbol->string, string, len); ␊ |
1026 | ␊ |
1027 | // Add the symbol to the list.␊ |
1028 | symbol->next = gSymbolsHead;␊ |
1029 | gSymbolsHead = symbol;␊ |
1030 | }␊ |
1031 | ␊ |
1032 | // Update the refCount and return the string.␊ |
1033 | symbol->refCount++;␊ |
1034 | ␊ |
1035 | if (lastGuy && lastGuy->next != 0)␊ |
1036 | {␊ |
1037 | free(symbol);␊ |
1038 | stop("last guy not last!");␊ |
1039 | return 0;␊ |
1040 | }␊ |
1041 | return symbol->string;␊ |
1042 | }␊ |
1043 | ␊ |
1044 | //==========================================================================␊ |
1045 | // FreeSymbol␊ |
1046 | ␊ |
1047 | #if DOFREE␊ |
1048 | static void␊ |
1049 | FreeSymbol( char * string )␊ |
1050 | { ␊ |
1051 | SymbolPtr symbol, prev;␊ |
1052 | ␉prev = 0;␊ |
1053 | ␊ |
1054 | // Look for string in the list of symbols.␊ |
1055 | symbol = FindSymbol(string, &prev);␊ |
1056 | if (symbol == 0) return;␊ |
1057 | ␊ |
1058 | // Update the refCount.␊ |
1059 | symbol->refCount--;␊ |
1060 | ␊ |
1061 | if (symbol->refCount != 0) return;␊ |
1062 | ␊ |
1063 | // Remove the symbol from the list.␊ |
1064 | if (prev != 0) prev->next = symbol->next;␊ |
1065 | else gSymbolsHead = symbol->next;␊ |
1066 | ␊ |
1067 | // Free the symbol's memory.␊ |
1068 | if (symbol->string) free(symbol->string);␊ |
1069 | free(symbol);␊ |
1070 | }␊ |
1071 | #endif␊ |
1072 | ␊ |
1073 | //==========================================================================␊ |
1074 | // FindSymbol␊ |
1075 | ␊ |
1076 | static SymbolPtr␊ |
1077 | FindSymbol( char * string, SymbolPtr * prevSymbol )␊ |
1078 | {␊ |
1079 | SymbolPtr symbol, prev;␊ |
1080 | ␊ |
1081 | symbol = gSymbolsHead;␊ |
1082 | prev = 0;␊ |
1083 | ␊ |
1084 | while (symbol != 0) {␊ |
1085 | if (!strcmp(symbol->string, string)) break;␊ |
1086 | ␊ |
1087 | prev = symbol;␊ |
1088 | symbol = symbol->next;␊ |
1089 | }␊ |
1090 | ␊ |
1091 | if ((symbol != 0) && (prevSymbol != 0)) *prevSymbol = prev;␊ |
1092 | ␊ |
1093 | return symbol;␊ |
1094 | }␊ |
1095 | ␊ |
1096 | ␊ |
1097 | ␊ |
1098 | bool XMLIsType(TagPtr dict, enum xmltype type)␊ |
1099 | {␊ |
1100 | ␉if(!dict) return (type == kTagTypeNone);␊ |
1101 | ␉return (dict->type == (long)type);␊ |
1102 | }␊ |
1103 | ␊ |
1104 | ␊ |
1105 | /*** Cast functions ***/␊ |
1106 | TagPtr XMLCastArray(TagPtr dict)␊ |
1107 | {␊ |
1108 | ␉if(!dict) return NULL;␊ |
1109 | ␉if(dict->type == kTagTypeArray) return dict;␊ |
1110 | ␉else return NULL;␊ |
1111 | }␊ |
1112 | ␊ |
1113 | ␊ |
1114 | TagPtr XMLCastDict(TagPtr dict)␊ |
1115 | {␊ |
1116 | ␉if(!dict) return NULL;␊ |
1117 | ␉if(dict->type == kTagTypeDict) return dict;␊ |
1118 | ␉else return NULL;␊ |
1119 | }␊ |
1120 | ␊ |
1121 | char* XMLCastString(TagPtr dict)␊ |
1122 | {␊ |
1123 | ␉if(!dict) return NULL;␊ |
1124 | ␊ |
1125 | ␉if((dict->type == kTagTypeString) ||␊ |
1126 | ␉ (dict->type == kTagTypeKey)) return dict->string;␊ |
1127 | ␉␊ |
1128 | ␉return NULL;␊ |
1129 | }␊ |
1130 | ␊ |
1131 | long XMLCastStringOffset(TagPtr dict)␊ |
1132 | {␊ |
1133 | ␉if(dict &&␊ |
1134 | ␉ ((dict->type == kTagTypeString) ||␊ |
1135 | (dict->type == kTagTypeKey)))␊ |
1136 | ␉{␊ |
1137 | ␉␉return dict->offset;␊ |
1138 | ␉}␊ |
1139 | ␉else ␊ |
1140 | ␉{␊ |
1141 | ␉␉return -1;␊ |
1142 | ␉}␊ |
1143 | }␊ |
1144 | ␊ |
1145 | ␊ |
1146 | bool XMLCastBoolean(TagPtr dict)␊ |
1147 | {␊ |
1148 | ␉if(!dict) return false;␊ |
1149 | ␉if(dict->type == kTagTypeTrue) return true;␊ |
1150 | ␉return false;␊ |
1151 | }␊ |
1152 | ␊ |
1153 | int XMLCastInteger(TagPtr dict)␊ |
1154 | {␊ |
1155 | ␉if(!dict)␊ |
1156 | ␉{␊ |
1157 | ␉␉printf("XMLCastInteger: null dict\n");␊ |
1158 | ␉␉return 0;␊ |
1159 | ␉}␊ |
1160 | ␉if(dict->type == kTagTypeInteger) return (int)(dict->string);␊ |
1161 | ␉return 0;␊ |
1162 | }␊ |
1163 | |