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 | ␉/***** data ****/␊ |
596 | ␉else if (!strncmp(tagName, kXMLTagData, sizeof(kXMLTagData)))␊ |
597 | ␉{␊ |
598 | ␉␉length = ParseTagData(buffer + pos, tag);␊ |
599 | ␉}␊ |
600 | ␉else if (!strncmp(tagName, kXMLTagData " ", strlen(kXMLTagData " ")))␊ |
601 | ␉{␊ |
602 | ␉␉length = ParseTagData(buffer + pos, tag);␊ |
603 | ␉}␊ |
604 | ␊ |
605 | ␉else if (!strncmp(tagName, kXMLTagDate, sizeof(kXMLTagDate)))␊ |
606 | ␉{␊ |
607 | ␉␉length = ParseTagDate(buffer + pos, tag);␊ |
608 | ␉}␊ |
609 | ␊ |
610 | ␉/***** date ****/␊ |
611 | ␉else if (!strncmp(tagName, kXMLTagDate " ", strlen(kXMLTagDate " ")))␊ |
612 | ␉{␊ |
613 | ␉␉length = ParseTagDate(buffer + pos, tag);␊ |
614 | ␉}␊ |
615 | ␊ |
616 | ␉/***** false ****/␊ |
617 | ␉else if (!strncmp(tagName, kXMLTagFalse, sizeof(kXMLTagFalse)))␊ |
618 | ␉{␊ |
619 | ␉␉length = ParseTagBoolean(buffer + pos, tag, kTagTypeFalse);␊ |
620 | ␉}␊ |
621 | ␊ |
622 | ␉/***** true ****/␊ |
623 | ␉else if (!strncmp(tagName, kXMLTagTrue, sizeof(kXMLTagTrue)))␊ |
624 | ␉{␊ |
625 | ␉␉length = ParseTagBoolean(buffer + pos, tag, kTagTypeTrue);␊ |
626 | ␉}␊ |
627 | ␊ |
628 | ␉/***** array ****/␊ |
629 | ␉else if (!strncmp(tagName, kXMLTagArray, sizeof(kXMLTagArray) ))␊ |
630 | ␉{␊ |
631 | ␉␉length = ParseTagList(buffer + pos, tag, kTagTypeArray, 0);␊ |
632 | ␉}␊ |
633 | ␉else if (!strncmp(tagName, kXMLTagArray " ", strlen(kXMLTagArray " ")))␊ |
634 | ␉{␊ |
635 | ␉␉length = ParseTagList(buffer + pos, tag, kTagTypeArray, 0);␊ |
636 | ␉}␊ |
637 | ␉else if (!strncmp(tagName, kXMLTagArray "/", strlen(kXMLTagArray "/")))␊ |
638 | ␉{␊ |
639 | ␉␉length = ParseTagList(buffer + pos, tag, kTagTypeArray, 1);␊ |
640 | ␉}␊ |
641 | ␊ |
642 | ␉/***** unknown ****/␊ |
643 | ␉else␊ |
644 | ␉{␊ |
645 | ␉␉// it wasn't parsed so we consumed no additional characters␊ |
646 | ␉␉*tag = NULL;␊ |
647 | ␉␉length = 0;␊ |
648 | ␉}␊ |
649 | ␊ |
650 | ␉if (length == -1)␊ |
651 | ␉{␊ |
652 | ␉␉return -1;␊ |
653 | ␉}␊ |
654 | ␊ |
655 | ␉return pos + length;␊ |
656 | }␊ |
657 | ␊ |
658 | //==========================================================================␊ |
659 | // ParseTagList␊ |
660 | static long ParseTagList( char *buffer, TagPtr *tag, long type, long empty )␊ |
661 | {␊ |
662 | ␉long␉␉pos = 0;␊ |
663 | ␉long␉␉length = 0;␊ |
664 | ␉TagPtr␉␉tagList = NULL;␊ |
665 | ␉TagPtr␉␉tmpTag;␊ |
666 | ␊ |
667 | ␊ |
668 | ␉if (!empty)␊ |
669 | ␉{␊ |
670 | ␉␉while (1)␊ |
671 | ␉␉{␊ |
672 | ␉␉␉length = XMLParseNextTag(buffer + pos, &tmpTag);␊ |
673 | ␉␉␉if (length == -1)␊ |
674 | ␉␉␉{␊ |
675 | ␉␉␉␉break;␊ |
676 | ␉␉␉}␊ |
677 | ␊ |
678 | ␉␉␉pos += length;␊ |
679 | ␊ |
680 | ␉␉␉// detect end of list␊ |
681 | ␉␉␉if (tmpTag == NULL)␊ |
682 | ␉␉␉{␊ |
683 | ␉␉␉␉break;␊ |
684 | ␉␉␉}␊ |
685 | ␉␉␉tmpTag->tagNext = tagList;␊ |
686 | ␉␉␉tagList = tmpTag;␊ |
687 | ␉␉}␊ |
688 | ␊ |
689 | ␉␉if (length == -1)␊ |
690 | ␉␉{␊ |
691 | ␉␉␉XMLFreeTag(tagList);␊ |
692 | ␉␉␉return -1;␊ |
693 | ␉␉}␊ |
694 | ␉}␊ |
695 | ␊ |
696 | ␉tmpTag = NewTag();␊ |
697 | ␉if (tmpTag == NULL)␊ |
698 | ␉{␊ |
699 | ␉␉XMLFreeTag(tagList);␊ |
700 | ␉␉return -1;␊ |
701 | ␉}␊ |
702 | ␊ |
703 | ␉tmpTag->type = type;␊ |
704 | ␉tmpTag->string = 0;␊ |
705 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start : 0;␊ |
706 | ␉tmpTag->tag = tagList;␊ |
707 | ␉tmpTag->tagNext = 0;␊ |
708 | ␊ |
709 | ␉*tag = tmpTag;␊ |
710 | ␊ |
711 | ␉return pos;␊ |
712 | }␊ |
713 | ␊ |
714 | //==========================================================================␊ |
715 | // ParseTagKey␊ |
716 | static long ParseTagKey( char *buffer, TagPtr *tag )␊ |
717 | {␊ |
718 | ␉long␉␉length = 0;␊ |
719 | ␉long␉␉length2 = 0;␊ |
720 | ␉char␉␉*string;␊ |
721 | ␉TagPtr␉␉tmpTag;␊ |
722 | ␉TagPtr␉␉subTag;␊ |
723 | ␊ |
724 | ␉length = FixDataMatchingTag(buffer, kXMLTagKey);␊ |
725 | ␉if (length == -1)␊ |
726 | ␉{␊ |
727 | ␉␉return -1;␊ |
728 | ␉}␊ |
729 | ␊ |
730 | ␉length2 = XMLParseNextTag(buffer + length, &subTag);␊ |
731 | ␉if (length2 == -1)␊ |
732 | ␉{␊ |
733 | ␉␉return -1;␊ |
734 | ␉}␊ |
735 | ␊ |
736 | ␉tmpTag = NewTag();␊ |
737 | ␉if (tmpTag == NULL)␊ |
738 | ␉{␊ |
739 | ␉␉XMLFreeTag(subTag);␊ |
740 | ␉␉return -1;␊ |
741 | ␉}␊ |
742 | ␊ |
743 | ␉string = NewSymbol(buffer);␊ |
744 | ␉if (string == NULL)␊ |
745 | ␉{␊ |
746 | ␉␉XMLFreeTag(subTag);␊ |
747 | ␉␉XMLFreeTag(tmpTag);␊ |
748 | ␉␉return -1;␊ |
749 | ␉}␊ |
750 | ␊ |
751 | ␉tmpTag->type = kTagTypeKey;␊ |
752 | ␉tmpTag->string = string;␊ |
753 | ␉tmpTag->tag = subTag;␊ |
754 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
755 | ␉tmpTag->tagNext = 0;␊ |
756 | ␊ |
757 | ␉*tag = tmpTag;␊ |
758 | ␊ |
759 | ␉return length + length2;␊ |
760 | }␊ |
761 | ␊ |
762 | //==========================================================================␊ |
763 | // ParseTagString␊ |
764 | static long ParseTagString( char *buffer, TagPtr *tag )␊ |
765 | {␊ |
766 | ␉long␉␉length = 0;␊ |
767 | ␉char␉␉*string;␊ |
768 | ␊ |
769 | ␉length = FixDataMatchingTag(buffer, kXMLTagString);␊ |
770 | ␉if (length == -1)␊ |
771 | ␉{␊ |
772 | ␉␉return -1;␊ |
773 | ␉}␊ |
774 | ␊ |
775 | ␉TagPtr tmpTag = NewTag();␊ |
776 | ␉if (tmpTag == NULL)␊ |
777 | ␉{␊ |
778 | ␉␉return -1;␊ |
779 | ␉}␊ |
780 | ␊ |
781 | ␉string = NewSymbol(buffer);␊ |
782 | ␉if (string == NULL)␊ |
783 | ␉{␊ |
784 | ␉␉XMLFreeTag(tmpTag);␊ |
785 | ␉␉return -1;␊ |
786 | ␉}␊ |
787 | ␊ |
788 | ␉tmpTag->type = kTagTypeString;␊ |
789 | ␉tmpTag->string = string;␊ |
790 | ␉tmpTag->tag = NULL;␊ |
791 | ␉tmpTag->tagNext = NULL;␊ |
792 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
793 | ␊ |
794 | ␉*tag = tmpTag;␊ |
795 | ␉return length;␊ |
796 | }␊ |
797 | ␊ |
798 | //==========================================================================␊ |
799 | // ParseTagInteger␊ |
800 | static long ParseTagInteger( char *buffer, TagPtr *tag )␊ |
801 | {␊ |
802 | ␉long length, integer;␊ |
803 | ␉bool negative = false;␊ |
804 | ␉TagPtr tmpTag;␊ |
805 | ␉char* val = buffer;␊ |
806 | ␉int size;␊ |
807 | ␊ |
808 | ␉if(buffer[0] == '<')␊ |
809 | ␉{␊ |
810 | ␉␉printf("Warning integer is non existant\n");␊ |
811 | ␉␉getchar();␊ |
812 | ␉␉tmpTag = NewTag();␊ |
813 | ␉␉tmpTag->type = kTagTypeInteger;␊ |
814 | ␉␉tmpTag->string = 0;␊ |
815 | ␉␉tmpTag->tag = 0;␊ |
816 | ␉␉tmpTag->offset = 0;␊ |
817 | ␉␉tmpTag->tagNext = 0;␊ |
818 | ␉␉␊ |
819 | ␉␉*tag = tmpTag;␊ |
820 | ␉␉␊ |
821 | ␉␉return 0;␊ |
822 | ␉}␊ |
823 | ␊ |
824 | ␉size = length = FixDataMatchingTag(buffer, kXMLTagInteger);␊ |
825 | ␉if (length == -1)␊ |
826 | ␉{␊ |
827 | ␉␉return -1;␊ |
828 | ␉}␊ |
829 | ␊ |
830 | ␉tmpTag = NewTag();␊ |
831 | ␊ |
832 | ␉if (tmpTag == 0)␊ |
833 | ␉{␊ |
834 | ␉␉return -1;␊ |
835 | ␉}␊ |
836 | ␉integer = 0;␊ |
837 | ␊ |
838 | ␉if(size > 1 && (val[1] == 'x' || val[1] == 'X'))␉// Hex value␊ |
839 | ␉{␊ |
840 | ␉␉val += 2;␊ |
841 | ␉␉while(*val)␊ |
842 | ␉␉{␊ |
843 | ␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
844 | ␉␉␉{␊ |
845 | ␉␉␉␉integer = (integer * 16) + (*val++ - '0');␊ |
846 | ␉␉␉}␊ |
847 | ␉␉␉else if ((*val >= 'a' && *val <= 'f'))␉// a - f␊ |
848 | ␉␉␉{␊ |
849 | ␉␉␉␉integer = (integer * 16) + (*val++ - 'a' + 10);␊ |
850 | ␉␉␉}␊ |
851 | ␉␉␉else if ((*val >= 'A' && *val <= 'F'))␉// A - F␊ |
852 | ␉␉␉{␊ |
853 | ␉␉␉␉integer = (integer * 16) + (*val++ - 'A' + 10);␊ |
854 | ␉␉␉}␊ |
855 | ␉␉␉else␊ |
856 | ␉␉␉{␊ |
857 | ␉␉␉␉printf("ParseTagInteger hex error (0x%x) in buffer %s\n", *val, buffer);␊ |
858 | ␉␉␉␉getchar();␊ |
859 | ␉␉␉␉XMLFreeTag(tmpTag);␊ |
860 | ␉␉␉␉return -1;␊ |
861 | ␉␉␉}␊ |
862 | ␉␉}␊ |
863 | ␉}␊ |
864 | ␉else if ( size )␉// Decimal value␊ |
865 | ␉{␊ |
866 | ␉␉if (*val == '-')␊ |
867 | ␉␉{␊ |
868 | ␉␉␉negative = true;␊ |
869 | ␉␉␉val++;␊ |
870 | ␉␉␉size--;␊ |
871 | ␉␉}␊ |
872 | ␉␉␊ |
873 | ␉␉for (integer = 0; size > 0; size--)␊ |
874 | ␉␉{␊ |
875 | ␉␉␉if(*val) // UGLY HACK, fix me.␊ |
876 | ␉␉␉{␊ |
877 | ␉␉␉␉if (*val < '0' || *val > '9')␊ |
878 | ␉␉␉␉{␊ |
879 | ␉␉␉␉␉printf("ParseTagInteger decimal error (0x%x) in buffer %s\n", *val, buffer);␊ |
880 | ␉␉␉␉␉getchar();␊ |
881 | ␉␉␉␉␉return -1;␊ |
882 | ␉␉␉␉}␊ |
883 | ␊ |
884 | ␉␉␉␉integer = (integer * 10) + (*val++ - '0');␊ |
885 | ␉␉␉}␊ |
886 | ␉␉}␊ |
887 | ␉␉␊ |
888 | ␉␉if (negative) {␊ |
889 | ␉␉␉integer = -integer;␊ |
890 | ␉␉}␊ |
891 | ␉}␊ |
892 | ␊ |
893 | ␉tmpTag->type = kTagTypeInteger;␊ |
894 | ␉tmpTag->string = (char *)integer;␊ |
895 | ␉tmpTag->tag = NULL;␊ |
896 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
897 | ␉tmpTag->tagNext = NULL;␊ |
898 | ␊ |
899 | ␉*tag = tmpTag;␊ |
900 | ␊ |
901 | ␉return length;␊ |
902 | }␊ |
903 | ␊ |
904 | //==========================================================================␊ |
905 | // ParseTagData␊ |
906 | static long ParseTagData( char *buffer, TagPtr *tag)␊ |
907 | {␊ |
908 | ␉//int␉␉actuallen = 0;␊ |
909 | ␉int ␉len = 0;␊ |
910 | ␉long ␉length = 0;␊ |
911 | ␉TagPtr␉␉tmpTag;␊ |
912 | ␉char␉␉*tmpString;␊ |
913 | ␊ |
914 | ␉length = FixDataMatchingTag(buffer, kXMLTagData);␊ |
915 | ␉if (length == -1)␊ |
916 | ␉{␊ |
917 | ␉␉return -1;␊ |
918 | ␉}␊ |
919 | ␊ |
920 | ␉tmpTag = NewTag();␊ |
921 | ␉if (tmpTag == NULL)␊ |
922 | ␉{␊ |
923 | ␉␉return -1;␊ |
924 | ␉}␊ |
925 | ␉tmpString = NewSymbol(buffer);␊ |
926 | ␉tmpTag->type = kTagTypeData;␊ |
927 | ␉tmpTag->string = tmpString;␊ |
928 | ␉tmpTag->data = (UInt8 *)BASE64Decode(buffer, strlen(buffer), &len);␊ |
929 | ␉tmpTag->dataLen = len;␊ |
930 | ␊ |
931 | ␉tmpTag->tag = NULL;␊ |
932 | ␉tmpTag->offset = /* actuallen; */ buffer_start ? buffer - buffer_start: 0;␊ |
933 | ␉tmpTag->tagNext = NULL;␊ |
934 | ␊ |
935 | ␉*tag = tmpTag;␊ |
936 | ␊ |
937 | ␉return length;␊ |
938 | }␊ |
939 | ␊ |
940 | //==========================================================================␊ |
941 | // ParseTagDate␊ |
942 | static long ParseTagDate( char *buffer, TagPtr *tag )␊ |
943 | {␊ |
944 | ␉long␉␉length = 0;␊ |
945 | ␉TagPtr␉␉tmpTag;␊ |
946 | ␊ |
947 | ␉length = FixDataMatchingTag(buffer, kXMLTagDate);␊ |
948 | ␉if (length == -1)␊ |
949 | ␉{␊ |
950 | ␉␉return -1;␊ |
951 | ␉}␊ |
952 | ␊ |
953 | ␉tmpTag = NewTag();␊ |
954 | ␉if (tmpTag == NULL)␊ |
955 | ␉{␊ |
956 | ␉␉return -1;␊ |
957 | ␉}␊ |
958 | ␊ |
959 | //␉printf("ParseTagDate unimplimented\n");␊ |
960 | //␉getchar();␊ |
961 | ␊ |
962 | ␉tmpTag->type = kTagTypeDate;␊ |
963 | ␉tmpTag->string = NULL;␊ |
964 | ␉tmpTag->tag = NULL;␊ |
965 | ␉tmpTag->tagNext = NULL;␊ |
966 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
967 | ␊ |
968 | ␉*tag = tmpTag;␊ |
969 | ␊ |
970 | ␉return length;␊ |
971 | }␊ |
972 | ␊ |
973 | //==========================================================================␊ |
974 | // ParseTagBoolean␊ |
975 | long ParseTagBoolean( char *buffer, TagPtr *tag, long type )␊ |
976 | {␊ |
977 | ␉TagPtr tmpTag;␊ |
978 | ␊ |
979 | ␉tmpTag = NewTag();␊ |
980 | ␉if (tmpTag == NULL)␊ |
981 | ␉{␊ |
982 | ␉␉return -1;␊ |
983 | ␉}␊ |
984 | ␊ |
985 | ␉tmpTag->type = type;␊ |
986 | ␉tmpTag->string = NULL;␊ |
987 | ␉tmpTag->tag = NULL;␊ |
988 | ␉tmpTag->tagNext = NULL;␊ |
989 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
990 | ␊ |
991 | ␊ |
992 | ␉*tag = tmpTag;␊ |
993 | ␊ |
994 | ␉return 0;␊ |
995 | }␊ |
996 | ␊ |
997 | //==========================================================================␊ |
998 | // GetNextTag␊ |
999 | static long GetNextTag( char *buffer, char **tag, long *start )␊ |
1000 | {␊ |
1001 | ␉long␉cnt;␊ |
1002 | ␉long␉cnt2;␊ |
1003 | ␊ |
1004 | ␉if (tag == NULL)␊ |
1005 | ␉{␊ |
1006 | ␉␉return -1;␊ |
1007 | ␉}␊ |
1008 | ␊ |
1009 | ␉// Find the start of the tag.␊ |
1010 | ␉cnt = 0;␊ |
1011 | ␉while ((buffer[cnt] != '\0') && (buffer[cnt] != '<'))␊ |
1012 | ␉{␊ |
1013 | ␉␉cnt++;␊ |
1014 | ␉}␊ |
1015 | ␊ |
1016 | ␉if (buffer[cnt] == '\0')␊ |
1017 | ␉{␊ |
1018 | ␉␉return -1;␊ |
1019 | ␉}␊ |
1020 | ␉// Find the end of the tag.␊ |
1021 | ␉cnt2 = cnt + 1;␊ |
1022 | ␉while ((buffer[cnt2] != '\0') && (buffer[cnt2] != '>'))␊ |
1023 | ␉{␊ |
1024 | ␉␉cnt2++;␊ |
1025 | ␉}␊ |
1026 | ␊ |
1027 | ␉if (buffer[cnt2] == '\0')␊ |
1028 | ␉{␊ |
1029 | ␉␉return -1;␊ |
1030 | ␉}␊ |
1031 | ␊ |
1032 | ␉// Fix the tag data.␊ |
1033 | ␉*tag = buffer + cnt + 1;␊ |
1034 | ␉buffer[cnt2] = '\0';␊ |
1035 | ␉if (start)␊ |
1036 | ␉{␊ |
1037 | ␉␉*start = cnt;␊ |
1038 | ␉}␊ |
1039 | ␊ |
1040 | ␉return cnt2 + 1;␊ |
1041 | }␊ |
1042 | ␊ |
1043 | //==========================================================================␊ |
1044 | // FixDataMatchingTag␊ |
1045 | // Modifies 'buffer' to add a '\0' at the end of the tag matching 'tag'.␊ |
1046 | // Returns the length of the data found, counting the end tag,␊ |
1047 | // or -1 if the end tag was not found.␊ |
1048 | static long FixDataMatchingTag( char *buffer, char *tag )␊ |
1049 | {␊ |
1050 | ␉long␉length;␊ |
1051 | ␉long␉start;␊ |
1052 | ␉long␉stop;␊ |
1053 | ␉char␉*endTag;␊ |
1054 | ␊ |
1055 | ␉start = 0;␊ |
1056 | ␉while (1)␊ |
1057 | ␉{␊ |
1058 | ␉␉length = GetNextTag(buffer + start, &endTag, &stop);␊ |
1059 | ␉␉if (length == -1)␊ |
1060 | ␉␉{␊ |
1061 | ␉␉␉return -1;␊ |
1062 | ␉␉}␊ |
1063 | ␊ |
1064 | ␉␉if ((*endTag == '/') && !strcmp(endTag + 1, tag))␊ |
1065 | ␉␉{␊ |
1066 | ␉␉␉break;␊ |
1067 | ␉␉}␊ |
1068 | ␊ |
1069 | ␉␉start += length;␊ |
1070 | ␉}␊ |
1071 | ␊ |
1072 | ␉buffer[start + stop] = '\0';␊ |
1073 | ␊ |
1074 | ␉return start + length;␊ |
1075 | }␊ |
1076 | ␊ |
1077 | //==========================================================================␊ |
1078 | // NewTag␊ |
1079 | #define kTagsPerBlock (0x1000)␊ |
1080 | ␊ |
1081 | static TagPtr gTagsFree␉= NULL;␊ |
1082 | static TagPtr NewTag( void )␊ |
1083 | {␊ |
1084 | ␉long␉␉cnt;␊ |
1085 | ␉TagPtr␉␉tag;␊ |
1086 | ␊ |
1087 | ␉if (gTagsFree == NULL)␊ |
1088 | ␉{␊ |
1089 | ␉␉tag = (TagPtr)malloc(kTagsPerBlock *sizeof(Tag));␊ |
1090 | ␉␉if (tag == NULL)␊ |
1091 | ␉␉{␊ |
1092 | ␉␉␉return NULL;␊ |
1093 | ␉␉}␊ |
1094 | ␊ |
1095 | ␉␉// Initalize the new tags.␊ |
1096 | ␉␉for (cnt = 0; cnt < kTagsPerBlock; cnt++)␊ |
1097 | ␉␉{␊ |
1098 | ␉␉␉tag[cnt].type = kTagTypeNone;␊ |
1099 | ␉␉␉tag[cnt].string = 0;␊ |
1100 | ␉␉␉tag[cnt].data = 0;␊ |
1101 | ␉␉␉tag[cnt].dataLen = 0;␊ |
1102 | ␉␉␉tag[cnt].tag = 0;␊ |
1103 | ␉␉␉tag[cnt].tagNext = tag + cnt + 1;␊ |
1104 | ␉␉}␊ |
1105 | ␉␉tag[kTagsPerBlock - 1].tagNext = 0;␊ |
1106 | ␊ |
1107 | ␉␉gTagsFree = tag;␊ |
1108 | ␉}␊ |
1109 | ␊ |
1110 | ␉tag = gTagsFree;␊ |
1111 | ␉gTagsFree = tag->tagNext;␊ |
1112 | ␊ |
1113 | ␉return tag;␊ |
1114 | }␊ |
1115 | ␊ |
1116 | //==========================================================================␊ |
1117 | // XMLFreeTag␊ |
1118 | void XMLFreeTag( TagPtr tag )␊ |
1119 | {␊ |
1120 | #if DOFREE␊ |
1121 | ␉if (tag == NULL)␊ |
1122 | ␉{␊ |
1123 | ␉␉return;␊ |
1124 | ␉}␊ |
1125 | ␊ |
1126 | ␉if (!XMLIsInteger(tag) && tag->string)␊ |
1127 | ␉{␊ |
1128 | ␉␉FreeSymbol(tag->string);␊ |
1129 | ␉}␊ |
1130 | ␊ |
1131 | ␉XMLFreeTag(tag->tag);␊ |
1132 | ␉XMLFreeTag(tag->tagNext);␊ |
1133 | ␊ |
1134 | ␉// Clear and free the tag.␊ |
1135 | ␉tag->type = kTagTypeNone;␊ |
1136 | ␉tag->string = NULL;␊ |
1137 | ␉tag->data = NULL;␊ |
1138 | ␉tag->dataLen = 0;␊ |
1139 | ␉tag->tag = NULL;␊ |
1140 | ␉tag->offset = 0;␊ |
1141 | ␉tag->tagNext = gTagsFree;␊ |
1142 | ␉gTagsFree = tag;␊ |
1143 | #else␊ |
1144 | ␉return;␊ |
1145 | #endif␊ |
1146 | }␊ |
1147 | ␊ |
1148 | //==========================================================================␊ |
1149 | // Symbol object.␊ |
1150 | struct Symbol␊ |
1151 | {␊ |
1152 | ␉long refCount;␊ |
1153 | ␉struct Symbol *next;␊ |
1154 | ␉char string[];␊ |
1155 | };␊ |
1156 | typedef struct Symbol Symbol, *SymbolPtr;␊ |
1157 | ␊ |
1158 | static SymbolPtr FindSymbol(char *string, SymbolPtr *prevSymbol);␊ |
1159 | ␊ |
1160 | static SymbolPtr gSymbolsHead␉= NULL;␊ |
1161 | ␊ |
1162 | //==========================================================================␊ |
1163 | // NewSymbol␊ |
1164 | static char *NewSymbol( char *string )␊ |
1165 | {␊ |
1166 | ␉static SymbolPtr lastGuy = 0;␊ |
1167 | ␊ |
1168 | ␉SymbolPtr␉symbol;␊ |
1169 | ␊ |
1170 | ␉// Look for string in the list of symbols.␊ |
1171 | ␉symbol = FindSymbol(string, 0);␊ |
1172 | ␊ |
1173 | ␉// Add the new symbol.␊ |
1174 | ␉if (symbol == NULL)␊ |
1175 | ␉{␊ |
1176 | ␉␉symbol = (SymbolPtr)malloc(sizeof(Symbol) + 1 + strlen(string));␊ |
1177 | ␉␉if (symbol == NULL)␊ |
1178 | ␉␉{␊ |
1179 | ␉␉␉stop("NULL symbol!");␊ |
1180 | ␉␉␉return NULL;␊ |
1181 | ␉␉}␊ |
1182 | ␊ |
1183 | ␉␉// Set the symbol's data.␊ |
1184 | ␉␉symbol->refCount = 0;␊ |
1185 | ␉␉strcpy(symbol->string, string);␊ |
1186 | ␊ |
1187 | ␉␉// Add the symbol to the list.␊ |
1188 | ␉␉symbol->next = gSymbolsHead;␊ |
1189 | ␉␉gSymbolsHead = symbol;␊ |
1190 | ␉}␊ |
1191 | ␊ |
1192 | ␉// Update the refCount and return the string.␊ |
1193 | ␉symbol->refCount++;␊ |
1194 | ␊ |
1195 | ␉if (lastGuy && lastGuy->next != 0)␊ |
1196 | ␉{␊ |
1197 | ␉␉stop("last guy not last!");␊ |
1198 | ␉}␊ |
1199 | ␊ |
1200 | ␉return symbol->string;␊ |
1201 | }␊ |
1202 | ␊ |
1203 | //==========================================================================␊ |
1204 | // FreeSymbol␊ |
1205 | #if DOFREE␊ |
1206 | static void FreeSymbol( char *string )␊ |
1207 | {␊ |
1208 | ␉SymbolPtr symbol, prev;␊ |
1209 | ␉prev = NULL;␊ |
1210 | ␊ |
1211 | ␉// Look for string in the list of symbols.␊ |
1212 | ␉symbol = FindSymbol(string, &prev);␊ |
1213 | ␉if (symbol == NULL)␊ |
1214 | ␉{␊ |
1215 | ␉␉return;␊ |
1216 | ␉}␊ |
1217 | ␊ |
1218 | ␉// Update the refCount.␊ |
1219 | ␉symbol->refCount--;␊ |
1220 | ␊ |
1221 | ␉if (symbol->refCount != 0)␊ |
1222 | ␉{␊ |
1223 | ␉␉return;␊ |
1224 | ␉}␊ |
1225 | ␊ |
1226 | ␉// Remove the symbol from the list.␊ |
1227 | ␉if (prev != NULL)␊ |
1228 | ␉{␊ |
1229 | ␉␉prev->next = symbol->next;␊ |
1230 | ␉}␊ |
1231 | ␉else␊ |
1232 | ␉{␊ |
1233 | ␉␉gSymbolsHead = symbol->next;␊ |
1234 | ␉}␊ |
1235 | ␊ |
1236 | ␉// Free the symbol's memory.␊ |
1237 | ␉free(symbol);␊ |
1238 | }␊ |
1239 | #endif␊ |
1240 | ␊ |
1241 | //==========================================================================␊ |
1242 | // FindSymbol␊ |
1243 | static SymbolPtr FindSymbol( char *string, SymbolPtr *prevSymbol )␊ |
1244 | {␊ |
1245 | ␉SymbolPtr symbol, prev;␊ |
1246 | ␊ |
1247 | ␉if (string == NULL)␊ |
1248 | ␉{␊ |
1249 | ␉␉return NULL;␊ |
1250 | ␉}␊ |
1251 | ␊ |
1252 | ␉symbol = gSymbolsHead;␊ |
1253 | ␉prev = NULL;␊ |
1254 | ␊ |
1255 | ␉while (symbol != NULL)␊ |
1256 | ␉{␊ |
1257 | ␉␉if (!strcmp(symbol->string, string))␊ |
1258 | ␉␉{␊ |
1259 | ␉␉␉break;␊ |
1260 | ␉␉}␊ |
1261 | ␊ |
1262 | ␉␉prev = symbol;␊ |
1263 | ␉␉symbol = symbol->next;␊ |
1264 | ␉}␊ |
1265 | ␊ |
1266 | ␉if ((symbol != NULL) && (prevSymbol != NULL))␊ |
1267 | ␉{␊ |
1268 | ␉␉*prevSymbol = prev;␊ |
1269 | ␉}␊ |
1270 | ␊ |
1271 | ␉return symbol;␊ |
1272 | }␊ |
1273 | ␊ |
1274 | bool XMLIsType(TagPtr dict, enum xmltype type)␊ |
1275 | {␊ |
1276 | ␉if(!dict)␊ |
1277 | ␉{␊ |
1278 | ␉␉return (type == kTagTypeNone);␊ |
1279 | ␉}␊ |
1280 | ␉return (dict->type == type);␊ |
1281 | }␊ |
1282 | ␊ |
1283 | /*** Cast functions ***/␊ |
1284 | bool XMLIsArray(TagPtr entry)␊ |
1285 | {␊ |
1286 | ␉return entry && (entry->type == kTagTypeArray);␊ |
1287 | }␊ |
1288 | ␊ |
1289 | TagPtr XMLCastArray(TagPtr dict)␊ |
1290 | {␊ |
1291 | ␉if(!dict)␊ |
1292 | ␉{␊ |
1293 | ␉␉return NULL;␊ |
1294 | ␉}␊ |
1295 | ␊ |
1296 | ␉if(dict->type == kTagTypeArray)␊ |
1297 | ␉{␊ |
1298 | ␉␉return dict;␊ |
1299 | ␉} else {␊ |
1300 | ␉␉return NULL;␊ |
1301 | ␉}␊ |
1302 | }␊ |
1303 | ␊ |
1304 | bool XMLIsDict(TagPtr entry)␊ |
1305 | {␊ |
1306 | return entry && (entry->type == kTagTypeDict);␊ |
1307 | }␊ |
1308 | ␊ |
1309 | bool XMLIsData(TagPtr entry)␊ |
1310 | {␊ |
1311 | ␉return entry && (entry->type == kTagTypeData);␊ |
1312 | }␊ |
1313 | ␊ |
1314 | TagPtr XMLCastDict(TagPtr dict)␊ |
1315 | {␊ |
1316 | ␉if(!dict)␊ |
1317 | ␉{␊ |
1318 | ␉␉return NULL;␊ |
1319 | ␉}␊ |
1320 | ␊ |
1321 | ␉if(dict->type == kTagTypeDict)␊ |
1322 | ␉{␊ |
1323 | ␉␉return dict;␊ |
1324 | ␉}␊ |
1325 | ␉else␊ |
1326 | ␉{␊ |
1327 | ␉␉return NULL;␊ |
1328 | ␉}␊ |
1329 | }␊ |
1330 | ␊ |
1331 | bool XMLIsString(TagPtr entry)␊ |
1332 | {␊ |
1333 | return entry && ((entry->type == kTagTypeString) || (entry->type == kTagTypeKey));␊ |
1334 | }␊ |
1335 | ␊ |
1336 | char *XMLCastString(TagPtr dict)␊ |
1337 | {␊ |
1338 | ␉if(!dict)␊ |
1339 | ␉{␊ |
1340 | ␉␉return NULL;␊ |
1341 | ␉}␊ |
1342 | ␊ |
1343 | ␉if((dict->type == kTagTypeString) || (dict->type == kTagTypeKey))␊ |
1344 | ␉{␊ |
1345 | ␉␉return dict->string;␊ |
1346 | ␉}␊ |
1347 | ␊ |
1348 | ␉return NULL;␊ |
1349 | }␊ |
1350 | ␊ |
1351 | char *XMLCastData(TagPtr dict, int* length)␊ |
1352 | {␊ |
1353 | ␉if(!dict)␊ |
1354 | ␉{␊ |
1355 | ␉␉return NULL;␊ |
1356 | ␉}␊ |
1357 | ␊ |
1358 | ␉if((dict->type == kTagTypeData) || (dict->type == kTagTypeKey))␊ |
1359 | ␉{␊ |
1360 | ␉␉*length = dict->offset;␊ |
1361 | ␉␉return dict->string;␊ |
1362 | ␉}␊ |
1363 | ␉*length = 0;␊ |
1364 | ␉return NULL;␊ |
1365 | }␊ |
1366 | ␊ |
1367 | ␊ |
1368 | long XMLCastStringOffset(TagPtr dict)␊ |
1369 | {␊ |
1370 | ␉if(dict && ((dict->type == kTagTypeString) || (dict->type == kTagTypeKey)))␊ |
1371 | ␉{␊ |
1372 | ␉␉return dict->offset;␊ |
1373 | ␉}␊ |
1374 | ␉else␊ |
1375 | ␉{␊ |
1376 | ␉␉return -1;␊ |
1377 | ␉}␊ |
1378 | }␊ |
1379 | ␊ |
1380 | bool XMLIsBoolean(TagPtr entry)␊ |
1381 | {␊ |
1382 | ␉return entry && ((entry->type == kTagTypeTrue) || (entry->type == kTagTypeFalse));␊ |
1383 | }␊ |
1384 | ␊ |
1385 | bool XMLCastBoolean(TagPtr dict)␊ |
1386 | {␊ |
1387 | ␉if(!dict)␊ |
1388 | ␉{␊ |
1389 | ␉␉return false;␊ |
1390 | ␉}␊ |
1391 | ␊ |
1392 | ␉if(dict->type == kTagTypeTrue)␊ |
1393 | ␉{␊ |
1394 | ␉␉return true;␊ |
1395 | ␉}␊ |
1396 | ␉return false;␊ |
1397 | }␊ |
1398 | ␊ |
1399 | bool XMLIsInteger(TagPtr entry)␊ |
1400 | {␊ |
1401 | return entry && (entry->type == kTagTypeInteger);␊ |
1402 | }␊ |
1403 | ␊ |
1404 | int XMLCastInteger(TagPtr dict)␊ |
1405 | {␊ |
1406 | ␉if(!dict)␊ |
1407 | ␉{␊ |
1408 | ␉␉//printf("XMLCastInteger: null dict\n");␊ |
1409 | ␉␉return 0;␊ |
1410 | ␉}␊ |
1411 | ␊ |
1412 | ␉if(dict->type == kTagTypeInteger)␊ |
1413 | ␉{␊ |
1414 | ␉␉return (int)(dict->string);␊ |
1415 | ␉}␊ |
1416 | ␉return 0;␊ |
1417 | }␊ |
1418 | ␊ |
1419 | bool XMLAddTagToDictionary(TagPtr dict, char *key, TagPtr value)␊ |
1420 | {␊ |
1421 | ␉if (!dict || dict->type != kTagTypeDict)␊ |
1422 | ␉{␊ |
1423 | ␉␉return false;␊ |
1424 | ␉}␊ |
1425 | ␊ |
1426 | ␉TagPtr tmpTag;␊ |
1427 | ␉char* string;␊ |
1428 | ␊ |
1429 | ␉tmpTag = NewTag();␊ |
1430 | ␉if (tmpTag == 0)␊ |
1431 | ␉{␊ |
1432 | ␉␉return false;␊ |
1433 | ␉}␊ |
1434 | ␊ |
1435 | ␉string = NewSymbol(key);␊ |
1436 | ␉if (string == 0)␊ |
1437 | ␉{␊ |
1438 | ␉␉XMLFreeTag(tmpTag);␊ |
1439 | ␉␉return false;␊ |
1440 | ␉}␊ |
1441 | ␊ |
1442 | ␉tmpTag->type = kTagTypeKey;␊ |
1443 | ␉tmpTag->string = string;␊ |
1444 | ␉tmpTag->tag = value;␊ |
1445 | ␉tmpTag->offset = 0;␊ |
1446 | ␉tmpTag->tagNext = 0;␊ |
1447 | ␊ |
1448 | ␉TagPtr tagList = dict->tag;␊ |
1449 | ␉if(!tagList)␊ |
1450 | ␉{␊ |
1451 | ␉␉// First tag␊ |
1452 | ␉␉dict->tag = tmpTag;␊ |
1453 | ␉␉return true;␊ |
1454 | ␉}␊ |
1455 | ␉while(tagList && tagList->tagNext) tagList = tagList->tagNext;␊ |
1456 | ␉if(tagList)␊ |
1457 | ␉{␊ |
1458 | ␉␉tagList->tagNext = tmpTag;␊ |
1459 | ␉␉return true;␊ |
1460 | ␉}␊ |
1461 | ␊ |
1462 | ␉XMLFreeTag(tmpTag);␊ |
1463 | ␉return false;␊ |
1464 | }␊ |
1465 | //==========================================================================␊ |
1466 | // Micky1979␊ |
1467 | // XMLConvertTagPtrToXMLString␊ |
1468 | // convert back a TagPtr to a xml representation␊ |
1469 | // Here is still w/o header and footer␊ |
1470 | char *XMLConvertTagPtrToXMLString(TagPtr aDict, long parentType, int indent)␊ |
1471 | {␊ |
1472 | ␉indent ++;␊ |
1473 | ␉// TODO check if we can use a buffer for the booter stuff already reserved somewhere␊ |
1474 | ␉char *string = "";␊ |
1475 | ␉char *ind = "";␊ |
1476 | ␉for (int i = 0; i < indent; i++)␊ |
1477 | ␉{␊ |
1478 | ␉␉char *ind2 = malloc(strlen(ind) + strlen("\t") +1);␊ |
1479 | ␉␉strcpy(ind2, ind);␊ |
1480 | ␉␉ind2[strlen(ind)] = '\t';␊ |
1481 | ␉␉ind2[strlen(ind) +1] = '\0';␊ |
1482 | ␉␉ind = ind2;␊ |
1483 | ␉}␊ |
1484 | ␉unsigned long length = 0;␊ |
1485 | ␉int count = XMLTagCount(aDict);␊ |
1486 | ␊ |
1487 | ␉while(count)␊ |
1488 | ␉{␊ |
1489 | ␉␉// parsing only supported tags␊ |
1490 | ␉␉char *key = NULL;␊ |
1491 | ␉␉TagPtr sub = NULL;␊ |
1492 | ␉␉if (!XMLIsArray(aDict))␊ |
1493 | ␉␉{␊ |
1494 | ␉␉␉key = XMLCastString(XMLGetKey(aDict, count));␊ |
1495 | ␉␉␉sub = XMLGetProperty(aDict, key);␊ |
1496 | ␉␉}␊ |
1497 | ␉␉else␊ |
1498 | ␉␉{␊ |
1499 | ␉␉␉key = "";␊ |
1500 | ␉␉␉sub = XMLGetElement( aDict, count-1);␊ |
1501 | ␉␉}␊ |
1502 | ␊ |
1503 | ␉␉if(sub)␊ |
1504 | ␉␉{␊ |
1505 | ␉␉␉if(XMLIsData(sub))␊ |
1506 | ␉␉␉{␊ |
1507 | ␉␉␉␉char *temp;␊ |
1508 | ␉␉␉␉if (parentType == kTagTypeArray)␊ |
1509 | ␉␉␉␉{␊ |
1510 | ␉␉␉␉␉length = (strlen(ind) +␊ |
1511 | ␉␉␉␉␉␉strlen(sub->string ? sub->string : "") +␊ |
1512 | ␉␉␉␉␉␉strlen("<data>") +␊ |
1513 | ␉␉␉␉␉␉strlen("</data>\n") +␊ |
1514 | ␉␉␉␉␉␉1);␊ |
1515 | ␉␉␉␉␉temp = malloc(strlen(string) + length);␊ |
1516 | ␉␉␉␉␉snprintf(temp, length+strlen(string), "%s%s<data>%s</data>\n", string, ind, sub->string ? sub->string : "");␊ |
1517 | ␉␉␉␉} else {␊ |
1518 | ␉␉␉␉␉length = (strlen(ind) +␊ |
1519 | ␉␉␉␉␉␉strlen(key) +␊ |
1520 | ␉␉␉␉␉␉strlen("<key>") +␊ |
1521 | ␉␉␉␉␉␉strlen("</key>\n") +␊ |
1522 | ␉␉␉␉␉␉strlen(ind) +␊ |
1523 | ␉␉␉␉␉␉strlen(sub->string ? sub->string : "") +␊ |
1524 | ␉␉␉␉␉␉strlen("<data>") +␊ |
1525 | ␉␉␉␉␉␉strlen("</data>\n") +␊ |
1526 | ␉␉␉␉␉␉1);␊ |
1527 | ␉␉␉␉␉temp = malloc(strlen(string) + length);␊ |
1528 | ␉␉␉␉␉snprintf(temp, length+strlen(string), "%s%s<key>%s</key>\n%s<data>%s</data>\n",␊ |
1529 | ␉␉␉␉␉␉string, ind, key, ind, sub->string ? sub->string : "");␊ |
1530 | ␉␉␉␉}␊ |
1531 | ␊ |
1532 | ␉␉␉␉temp[strlen(string) + length] = '\0';␊ |
1533 | ␉␉␉␉string = temp;␊ |
1534 | ␉␉␉}␊ |
1535 | ␉␉␉else if(XMLIsString(sub))␊ |
1536 | ␉␉␉{␊ |
1537 | ␉␉␉␉char *temp;␊ |
1538 | ␉␉␉␉if (parentType == kTagTypeArray)␊ |
1539 | ␉␉␉␉{␊ |
1540 | ␉␉␉␉␉length = (strlen(ind) +␊ |
1541 | ␉␉␉␉␉␉strlen("<string>") +␊ |
1542 | ␉␉␉␉␉␉strlen(XMLCastString(sub) ? XMLCastString(sub) : "") +␊ |
1543 | ␉␉␉␉␉␉strlen("</string>\n") +␊ |
1544 | ␉␉␉␉␉␉1);␊ |
1545 | ␉␉␉␉␉temp = malloc(strlen(string) + length);␊ |
1546 | ␉␉␉␉␉snprintf(temp, length+strlen(string), "%s%s<string>%s</string>\n",␊ |
1547 | ␉␉␉␉␉␉string, ind, XMLCastString(sub) ? XMLCastString(sub) : "");␊ |
1548 | ␉␉␉␉} else {␊ |
1549 | ␉␉␉␉␉length = (strlen(ind) +␊ |
1550 | ␉␉␉␉␉␉strlen(key) +␊ |
1551 | ␉␉␉␉␉␉strlen("<key>") +␊ |
1552 | ␉␉␉␉␉␉strlen("</key>\n") +␊ |
1553 | ␉␉␉␉␉␉strlen(ind) +␊ |
1554 | ␉␉␉␉␉␉strlen("<string>") +␊ |
1555 | ␉␉␉␉␉␉strlen(XMLCastString(sub) ? XMLCastString(sub) : "") +␊ |
1556 | ␉␉␉␉␉␉strlen("</string>\n") +␊ |
1557 | ␉␉␉␉␉␉1);␊ |
1558 | ␉␉␉␉␉temp = malloc(strlen(string) + length);␊ |
1559 | ␉␉␉␉␉snprintf(temp, length+strlen(string), "%s%s<key>%s</key>\n%s<string>%s</string>\n",␊ |
1560 | ␉␉␉␉␉␉string, ind, key, ind, XMLCastString(sub) ? XMLCastString(sub) : "");␊ |
1561 | ␉␉␉␉}␊ |
1562 | ␊ |
1563 | ␉␉␉␉temp[strlen(string) + length] = '\0';␊ |
1564 | ␉␉␉␉string = temp;␊ |
1565 | ␉␉␉}␊ |
1566 | ␉␉␉else if(XMLIsInteger(sub))␊ |
1567 | ␉␉␉{␊ |
1568 | ␉␉␉␉// 'real' (float/dowble) cannot be parsed. only integers␊ |
1569 | ␉␉␉␉int x = XMLCastInteger(sub);␊ |
1570 | ␉␉␉␉char xBuf[snprintf( NULL, 0, "%d", x )];␊ |
1571 | ␉␉␉␉snprintf( xBuf, snprintf( NULL, 0, "%d", x ) +1, "%d", x );␊ |
1572 | ␉␉␉␉char *temp;␊ |
1573 | ␉␉␉␉if (parentType == kTagTypeArray)␊ |
1574 | ␉␉␉␉{␊ |
1575 | ␉␉␉␉␉length = (strlen(ind) +␊ |
1576 | ␉␉␉␉␉␉strlen("<integer>") +␊ |
1577 | ␉␉␉␉␉␉sizeof(xBuf) +␊ |
1578 | ␉␉␉␉␉␉strlen("</integer>\n") +␊ |
1579 | ␉␉␉␉␉␉1);␊ |
1580 | ␉␉␉␉␉temp = malloc(strlen(string) + length);␊ |
1581 | ␉␉␉␉␉snprintf(temp, length+strlen(string), "%s%s<integer>%s</integer>\n", string, ind, (char *)xBuf);␊ |
1582 | ␉␉␉␉} else {␊ |
1583 | ␉␉␉␉␉length = (strlen(ind) +␊ |
1584 | ␉␉␉␉␉␉strlen(key) +␊ |
1585 | ␉␉␉␉␉␉strlen("<key>") +␊ |
1586 | ␉␉␉␉␉␉strlen("</key>\n") +␊ |
1587 | ␉␉␉␉␉␉strlen(ind) +␊ |
1588 | ␉␉␉␉␉␉strlen("<integer>") +␊ |
1589 | ␉␉␉␉␉␉sizeof(xBuf) +␊ |
1590 | ␉␉␉␉␉␉strlen("</integer>\n") +␊ |
1591 | ␉␉␉␉␉␉1);␊ |
1592 | ␉␉␉␉␉temp = malloc(strlen(string) + length);␊ |
1593 | ␉␉␉␉␉snprintf(temp, length+strlen(string), "%s%s<key>%s</key>\n%s<integer>%s</integer>\n",␊ |
1594 | ␉␉␉␉␉␉string, ind, key, ind, (char *)xBuf);␊ |
1595 | ␉␉␉␉}␊ |
1596 | ␊ |
1597 | ␉␉␉␉temp[strlen(string) + length] = '\0';␊ |
1598 | ␉␉␉␉string = temp;␊ |
1599 | ␉␉␉}␊ |
1600 | ␉␉␉else if(XMLIsBoolean(sub))␊ |
1601 | ␉␉␉{␊ |
1602 | ␉␉␉␉char *temp;␊ |
1603 | ␉␉␉␉int v = XMLCastBoolean(sub);␊ |
1604 | ␊ |
1605 | ␉␉␉␉if (parentType == kTagTypeArray)␊ |
1606 | ␉␉␉␉{␊ |
1607 | ␉␉␉␉␉length = (strlen(ind) +␊ |
1608 | ␉␉␉␉␉␉strlen(v ? "<true/>\n" : "<false/>\n") +␊ |
1609 | ␉␉␉␉␉␉1);␊ |
1610 | ␉␉␉␉␉temp = malloc(strlen(string) + length);␊ |
1611 | ␉␉␉␉␉snprintf(temp, length+strlen(string), "%s%s%s", string, ind, v ? "<true/>\n" : "<false/>\n");␊ |
1612 | ␉␉␉␉} else {␊ |
1613 | ␉␉␉␉␉length = (strlen(ind) +␊ |
1614 | ␉␉␉␉␉␉strlen(key) +␊ |
1615 | ␉␉␉␉␉␉strlen("<key>") +␊ |
1616 | ␉␉␉␉␉␉strlen("</key>\n") +␊ |
1617 | ␉␉␉␉␉␉strlen(ind) +␊ |
1618 | ␉␉␉␉␉␉strlen(v ? "<true/>\n" : "<false/>\n") +␊ |
1619 | ␉␉␉␉␉␉1);␊ |
1620 | ␉␉␉␉␉temp = malloc(strlen(string) + length);␊ |
1621 | ␉␉␉␉␉snprintf(temp, length+strlen(string), "%s%s<key>%s</key>\n%s%s",␊ |
1622 | ␉␉␉␉␉␉string, ind, key, ind, v ? "<true/>\n" : "<false/>\n");␊ |
1623 | ␉␉␉␉}␊ |
1624 | ␊ |
1625 | ␉␉␉␉temp[strlen(string) + length] = '\0';␊ |
1626 | ␉␉␉␉string = temp;␊ |
1627 | ␉␉␉}␊ |
1628 | ␉␉␉else if (XMLIsDict(sub) || XMLIsArray(sub)) // its a contenitor..␊ |
1629 | ␉␉␉{␊ |
1630 | ␉␉␉␉int subCount = XMLTagCount(sub);␊ |
1631 | ␉␉␉␉char *otag = subCount <=0 ? "" : XMLIsDict(sub) ? "<dict>\n" : "<array>\n";␊ |
1632 | ␉␉␉␉char *ctag = subCount <=0 ? XMLIsDict(sub) ? "<dict/>\n" :␊ |
1633 | ␉␉␉␉␉"<array/>\n" : XMLIsDict(sub) ? "</dict>\n" : "</array>\n";␊ |
1634 | ␉␉␉␉char *dict = NULL;␊ |
1635 | ␉␉␉␉dict = NULL;␊ |
1636 | ␉␉␉␉dict = XMLConvertTagPtrToXMLString(sub, sub->type, indent);␊ |
1637 | ␊ |
1638 | ␉␉␉␉length = (strlen(ind) +␊ |
1639 | ␉␉␉␉␉strlen(key) +␊ |
1640 | ␉␉␉␉␉strlen("<key>") +␊ |
1641 | ␉␉␉␉␉strlen("</key>\n") +␊ |
1642 | ␉␉␉␉␉strlen(ind) +␊ |
1643 | ␉␉␉␉␉strlen(otag) +␊ |
1644 | ␉␉␉␉␉strlen(dict) +␊ |
1645 | ␉␉␉␉␉strlen(ind) +␊ |
1646 | ␉␉␉␉␉strlen(ctag) +␊ |
1647 | ␉␉␉␉␉1);␊ |
1648 | ␊ |
1649 | ␉␉␉␉char *temp = malloc(strlen(string) + length);␊ |
1650 | ␉␉␉␉snprintf(temp, length+strlen(string), "%s%s<key>%s</key>\n%s%s%s%s%s\n",␊ |
1651 | ␉␉␉␉␉string, ind, key, ind, otag, dict, ind, ctag);␊ |
1652 | ␊ |
1653 | ␉␉␉␉temp[strlen(string) + length] = '\0';␊ |
1654 | ␉␉␉␉string = temp;␊ |
1655 | ␉␉␉}␊ |
1656 | ␉␉}␊ |
1657 | ␉count--;␊ |
1658 | ␉}␊ |
1659 | ␊ |
1660 | ␉if(strlen(ind))␊ |
1661 | ␉{␊ |
1662 | ␉␉free(ind);␊ |
1663 | ␉} // not mallocated if ""␊ |
1664 | ␊ |
1665 | ␉return string;␊ |
1666 | }␊ |
1667 | ␊ |
1668 | // XMLConvertTagPtrToPropertyList_v1␊ |
1669 | // convert back a TagPtr to a xml representation␊ |
1670 | // with header and footer␊ |
1671 | char *XMLConvertTagPtrToPropertyList_v1(TagPtr aDict)␊ |
1672 | {␊ |
1673 | ␉if (!aDict)␊ |
1674 | ␉{␊ |
1675 | ␉␉return NULL;␊ |
1676 | ␉}␊ |
1677 | ␊ |
1678 | ␉char *s = XMLConvertTagPtrToXMLString(aDict, aDict->type, 1);␊ |
1679 | ␊ |
1680 | ␉if (s == NULL)␊ |
1681 | ␉{␊ |
1682 | ␉␉return NULL;␊ |
1683 | ␉}␊ |
1684 | ␊ |
1685 | ␉unsigned long length = ((strlen(kXMLv1) -1 /* -1 for %s included */) +␊ |
1686 | ␉␉strlen(s) +␊ |
1687 | ␉␉1);␊ |
1688 | ␊ |
1689 | ␉char *propertyList = malloc(length);␊ |
1690 | ␉snprintf(propertyList, length, kXMLv1, s);␊ |
1691 | ␊ |
1692 | ␉if (strlen(s))␊ |
1693 | ␉{␊ |
1694 | ␉␉free(s);␊ |
1695 | ␉}␊ |
1696 | ␊ |
1697 | ␉return (char *)propertyList;␊ |
1698 | }␊ |
1699 | ␊ |
1700 | // XMLGenerateKextInjectorFromTag␊ |
1701 | // generate an full Info.plist to create a kext injector␊ |
1702 | // sDict should contains all the contents of IOKitPersonalities␊ |
1703 | // personalityName is a funcy name. Must not be a real kext name␊ |
1704 | ␊ |
1705 | // OSBundleRequired should be the same of the real kext you want to hack (Root, Safe Boot, Network-Root etc.)␊ |
1706 | // can also be NULL or "" assuming the target kext does not have this key.␊ |
1707 | char * XMLGenerateKextInjectorFromTag(TagPtr aDict, char *personalityName, char *OSBundleRequired)␊ |
1708 | {␊ |
1709 | ␉if (!aDict || personalityName == NULL)␊ |
1710 | ␉{␊ |
1711 | ␉␉return NULL;␊ |
1712 | ␉}␊ |
1713 | ␊ |
1714 | ␉char *IOKitPersonalities = XMLConvertTagPtrToXMLString(aDict, aDict->type, 1);␊ |
1715 | ␊ |
1716 | ␉if (IOKitPersonalities == NULL)␊ |
1717 | ␉{␊ |
1718 | ␉␉return NULL;␊ |
1719 | ␉}␊ |
1720 | ␊ |
1721 | ␉char *OBRkey = OSBundleRequired == NULL ? "" : OSBundleRequired;␊ |
1722 | ␉char *propertyList;␊ |
1723 | ␉unsigned long length = 0;␊ |
1724 | ␊ |
1725 | ␉if (strlen(OBRkey))␊ |
1726 | ␉{␊ |
1727 | ␉␉length = (strlen("\t<key>OSBundleRequired</key>\n\t<string>") +␊ |
1728 | ␉␉␉strlen(OBRkey) +␊ |
1729 | ␉␉␉strlen("</string>\n") +␊ |
1730 | ␉␉␉1);␊ |
1731 | ␉␉char temp[length];␊ |
1732 | ␉␉snprintf(temp, length, "\t<key>OSBundleRequired</key>\n\t<string>%s</string>\n", OBRkey);␊ |
1733 | ␉␉unsigned long mlength = ((strlen(kFakeInjectorKext) -8 /* %s x 8 */) +␊ |
1734 | ␉␉␉strlen(personalityName) +␊ |
1735 | ␉␉␉strlen(personalityName) +␊ |
1736 | ␉␉␉strlen(IOKitPersonalities) +␊ |
1737 | ␉␉␉length +␊ |
1738 | ␉␉␉1);␊ |
1739 | ␉␉propertyList = malloc(mlength);␊ |
1740 | ␉␉snprintf(propertyList, mlength, kFakeInjectorKext, personalityName, personalityName, IOKitPersonalities, temp);␊ |
1741 | ␉}␊ |
1742 | ␉else␊ |
1743 | ␉{␊ |
1744 | ␉␉unsigned long mlength = ((strlen(kFakeInjectorKext) -8 /* %s x 8 */) +␊ |
1745 | ␉␉␉strlen(personalityName) +␊ |
1746 | ␉␉␉strlen(personalityName) +␊ |
1747 | ␉␉␉strlen(IOKitPersonalities) +␊ |
1748 | ␉␉␉1);␊ |
1749 | ␉␉propertyList = malloc(mlength);␊ |
1750 | ␉␉snprintf(propertyList, mlength, kFakeInjectorKext, personalityName, personalityName, IOKitPersonalities, "");␊ |
1751 | ␉}␊ |
1752 | ␊ |
1753 | ␉if (strlen(IOKitPersonalities))␊ |
1754 | ␉{␊ |
1755 | ␉␉free(IOKitPersonalities);␊ |
1756 | ␉}␊ |
1757 | ␊ |
1758 | ␉return propertyList;␊ |
1759 | ␊ |
1760 | }␊ |
1761 | |