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