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