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