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