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