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