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