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