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