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