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