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