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