]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSUnserializeXML.y
xnu-792.22.5.tar.gz
[apple/xnu.git] / libkern / c++ / OSUnserializeXML.y
1 /*
2 * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * HISTORY
31 *
32 * OSUnserializeXML.y created by rsulack on Tue Oct 12 1999
33 */
34
35 // parser for unserializing OSContainer objects serialized to XML
36 //
37 // to build :
38 // bison -p OSUnserializeXML OSUnserializeXML.y
39 // head -50 OSUnserializeXML.y > OSUnserializeXML.cpp
40 // sed -e "s/#include <stdio.h>//" < OSUnserializeXML.tab.c >> OSUnserializeXML.cpp
41 //
42 // when changing code check in both OSUnserializeXML.y and OSUnserializeXML.cpp
43 //
44 //
45 //
46 //
47 //
48 // DO NOT EDIT OSUnserializeXML.cpp!
49 //
50 // this means you!
51 //
52 //
53 //
54 //
55 //
56 //
57
58
59 %pure_parser
60
61 %{
62 #include <string.h>
63 #include <libkern/c++/OSMetaClass.h>
64 #include <libkern/c++/OSContainers.h>
65 #include <libkern/c++/OSLib.h>
66
67 #define YYSTYPE object_t *
68 #define YYPARSE_PARAM state
69 #define YYLEX_PARAM (parser_state_t *)state
70
71 // this is the internal struct used to hold objects on parser stack
72 // it represents objects both before and after they have been created
73 typedef struct object {
74 struct object *next;
75 struct object *free;
76 struct object *elements;
77 OSObject *object;
78 OSString *key; // for dictionary
79 int size;
80 void *data; // for data
81 char *string; // for string & symbol
82 long long number; // for number
83 int idref;
84 } object_t;
85
86 // this code is reentrant, this structure contains all
87 // state information for the parsing of a single buffer
88 typedef struct parser_state {
89 const char *parseBuffer; // start of text to be parsed
90 int parseBufferIndex; // current index into text
91 int lineNumber; // current line number
92 object_t *objects; // internal objects in use
93 object_t *freeObjects; // internal objects that are free
94 OSDictionary *tags; // used to remember "ID" tags
95 OSString **errorString; // parse error with line
96 OSObject *parsedObject; // resultant object of parsed text
97 } parser_state_t;
98
99 #define STATE ((parser_state_t *)state)
100
101 #undef yyerror
102 #define yyerror(s) OSUnserializeerror(STATE, (s))
103 static int OSUnserializeerror(parser_state_t *state, char *s);
104
105 static int yylex(YYSTYPE *lvalp, parser_state_t *state);
106 static int yyparse(void * state);
107
108 static object_t *newObject(parser_state_t *state);
109 static void freeObject(parser_state_t *state, object_t *o);
110 static void rememberObject(parser_state_t *state, int tag, OSObject *o);
111 static object_t *retrieveObject(parser_state_t *state, int tag);
112 static void cleanupObjects(parser_state_t *state);
113
114 static object_t *buildDictionary(parser_state_t *state, object_t *o);
115 static object_t *buildArray(parser_state_t *state, object_t *o);
116 static object_t *buildSet(parser_state_t *state, object_t *o);
117 static object_t *buildString(parser_state_t *state, object_t *o);
118 static object_t *buildData(parser_state_t *state, object_t *o);
119 static object_t *buildNumber(parser_state_t *state, object_t *o);
120 static object_t *buildBoolean(parser_state_t *state, object_t *o);
121
122 extern "C" {
123 extern void *kern_os_malloc(size_t size);
124 extern void *kern_os_realloc(void * addr, size_t size);
125 extern void kern_os_free(void * addr);
126
127 //XXX shouldn't have to define these
128 extern long strtol(const char *, char **, int);
129 extern unsigned long strtoul(const char *, char **, int);
130
131 } /* extern "C" */
132
133 #define malloc(s) kern_os_malloc(s)
134 #define realloc(a, s) kern_os_realloc(a, s)
135 #define free(a) kern_os_free((void *)a)
136
137 %}
138 %token ARRAY
139 %token BOOLEAN
140 %token DATA
141 %token DICTIONARY
142 %token IDREF
143 %token KEY
144 %token NUMBER
145 %token SET
146 %token STRING
147 %token SYNTAX_ERROR
148 %% /* Grammar rules and actions follow */
149
150 input: /* empty */ { yyerror("unexpected end of buffer");
151 YYERROR;
152 }
153 | object { STATE->parsedObject = $1->object;
154 $1->object = 0;
155 freeObject(STATE, $1);
156 YYACCEPT;
157 }
158 | SYNTAX_ERROR { yyerror("syntax error");
159 YYERROR;
160 }
161 ;
162
163 object: dict { $$ = buildDictionary(STATE, $1); }
164 | array { $$ = buildArray(STATE, $1); }
165 | set { $$ = buildSet(STATE, $1); }
166 | string { $$ = buildString(STATE, $1); }
167 | data { $$ = buildData(STATE, $1); }
168 | number { $$ = buildNumber(STATE, $1); }
169 | boolean { $$ = buildBoolean(STATE, $1); }
170 | idref { $$ = retrieveObject(STATE, $1->idref);
171 if ($$) {
172 $$->object->retain();
173 } else {
174 yyerror("forward reference detected");
175 YYERROR;
176 }
177 freeObject(STATE, $1);
178 }
179 ;
180
181 //------------------------------------------------------------------------------
182
183 dict: '{' '}' { $$ = $1;
184 $$->elements = NULL;
185 }
186 | '{' pairs '}' { $$ = $1;
187 $$->elements = $2;
188 }
189 | DICTIONARY
190 ;
191
192 pairs: pair
193 | pairs pair { $$ = $2;
194 $$->next = $1;
195 }
196 ;
197
198 pair: key object { $$ = $1;
199 $$->key = (OSString *)$$->object;
200 $$->object = $2->object;
201 $$->next = NULL;
202 $2->object = 0;
203 freeObject(STATE, $2);
204 }
205 ;
206
207 key: KEY { $$ = buildString(STATE, $1); }
208 ;
209
210 //------------------------------------------------------------------------------
211
212 array: '(' ')' { $$ = $1;
213 $$->elements = NULL;
214 }
215 | '(' elements ')' { $$ = $1;
216 $$->elements = $2;
217 }
218 | ARRAY
219 ;
220
221 set: '[' ']' { $$ = $1;
222 $$->elements = NULL;
223 }
224 | '[' elements ']' { $$ = $1;
225 $$->elements = $2;
226 }
227 | SET
228 ;
229
230 elements: object { $$ = $1;
231 $$->next = NULL;
232 }
233 | elements object { $$ = $2;
234 $$->next = $1;
235 }
236 ;
237
238 //------------------------------------------------------------------------------
239
240 boolean: BOOLEAN
241 ;
242
243 data: DATA
244 ;
245
246 idref: IDREF
247 ;
248
249 number: NUMBER
250 ;
251
252 string: STRING
253 ;
254
255 %%
256
257 int
258 OSUnserializeerror(parser_state_t * state, char *s) /* Called by yyparse on errors */
259 {
260 char tempString[128];
261
262 if (state->errorString) {
263 snprintf(tempString, 128, "OSUnserializeXML: %s near line %d\n", s, state->lineNumber);
264 *(state->errorString) = OSString::withCString(tempString);
265 }
266
267 return 0;
268 }
269
270 #define TAG_MAX_LENGTH 32
271 #define TAG_MAX_ATTRIBUTES 32
272 #define TAG_BAD 0
273 #define TAG_START 1
274 #define TAG_END 2
275 #define TAG_EMPTY 3
276 #define TAG_COMMENT 4
277
278 #define currentChar() (state->parseBuffer[state->parseBufferIndex])
279 #define nextChar() (state->parseBuffer[++state->parseBufferIndex])
280 #define prevChar() (state->parseBuffer[state->parseBufferIndex - 1])
281
282 #define isSpace(c) ((c) == ' ' || (c) == '\t')
283 #define isAlpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
284 #define isDigit(c) ((c) >= '0' && (c) <= '9')
285 #define isAlphaDigit(c) ((c) >= 'a' && (c) <= 'f')
286 #define isHexDigit(c) (isDigit(c) || isAlphaDigit(c))
287 #define isAlphaNumeric(c) (isAlpha(c) || isDigit(c) || ((c) == '-'))
288
289 static int
290 getTag(parser_state_t *state,
291 char tag[TAG_MAX_LENGTH],
292 int *attributeCount,
293 char attributes[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH],
294 char values[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH] )
295 {
296 int length = 0;
297 int c = currentChar();
298 int tagType = TAG_START;
299
300 *attributeCount = 0;
301
302 if (c != '<') return TAG_BAD;
303 c = nextChar(); // skip '<'
304
305 if (c == '?' || c == '!') {
306 while ((c = nextChar()) != 0) {
307 if (c == '\n') state->lineNumber++;
308 if (c == '>') {
309 (void)nextChar();
310 return TAG_COMMENT;
311 }
312 }
313 }
314
315 if (c == '/') {
316 c = nextChar(); // skip '/'
317 tagType = TAG_END;
318 }
319 if (!isAlpha(c)) return TAG_BAD;
320
321 /* find end of tag while copying it */
322 while (isAlphaNumeric(c)) {
323 tag[length++] = c;
324 c = nextChar();
325 if (length >= (TAG_MAX_LENGTH - 1)) return TAG_BAD;
326 }
327
328 tag[length] = 0;
329
330 // printf("tag %s, type %d\n", tag, tagType);
331
332 // look for attributes of the form attribute = "value" ...
333 while ((c != '>') && (c != '/')) {
334 while (isSpace(c)) c = nextChar();
335
336 length = 0;
337 while (isAlphaNumeric(c)) {
338 attributes[*attributeCount][length++] = c;
339 if (length >= (TAG_MAX_LENGTH - 1)) return TAG_BAD;
340 c = nextChar();
341 }
342 attributes[*attributeCount][length] = 0;
343
344 while (isSpace(c)) c = nextChar();
345
346 if (c != '=') return TAG_BAD;
347 c = nextChar();
348
349 while (isSpace(c)) c = nextChar();
350
351 if (c != '"') return TAG_BAD;
352 c = nextChar();
353 length = 0;
354 while (c != '"') {
355 values[*attributeCount][length++] = c;
356 if (length >= (TAG_MAX_LENGTH - 1)) return TAG_BAD;
357 c = nextChar();
358 }
359 values[*attributeCount][length] = 0;
360
361 c = nextChar(); // skip closing quote
362
363 // printf(" attribute '%s' = '%s', nextchar = '%c'\n",
364 // attributes[*attributeCount], values[*attributeCount], c);
365
366 (*attributeCount)++;
367 if (*attributeCount >= TAG_MAX_ATTRIBUTES) return TAG_BAD;
368 }
369
370 if (c == '/') {
371 c = nextChar(); // skip '/'
372 tagType = TAG_EMPTY;
373 }
374 if (c != '>') return TAG_BAD;
375 c = nextChar(); // skip '>'
376
377 return tagType;
378 }
379
380 static char *
381 getString(parser_state_t *state)
382 {
383 int c = currentChar();
384 int start, length, i, j;
385 char * tempString;
386
387 start = state->parseBufferIndex;
388 /* find end of string */
389
390 while (c != 0) {
391 if (c == '\n') state->lineNumber++;
392 if (c == '<') {
393 break;
394 }
395 c = nextChar();
396 }
397
398 if (c != '<') return 0;
399
400 length = state->parseBufferIndex - start;
401
402 /* copy to null terminated buffer */
403 tempString = (char *)malloc(length + 1);
404 if (tempString == 0) {
405 printf("OSUnserializeXML: can't alloc temp memory\n");
406 goto error;
407 }
408
409 // copy out string in tempString
410 // "&amp;" -> '&', "&lt;" -> '<', "&gt;" -> '>'
411
412 i = j = 0;
413 while (i < length) {
414 c = state->parseBuffer[start + i++];
415 if (c != '&') {
416 tempString[j++] = c;
417 } else {
418 if ((i+3) > length) goto error;
419 c = state->parseBuffer[start + i++];
420 if (c == 'l') {
421 if (state->parseBuffer[start + i++] != 't') goto error;
422 if (state->parseBuffer[start + i++] != ';') goto error;
423 tempString[j++] = '<';
424 continue;
425 }
426 if (c == 'g') {
427 if (state->parseBuffer[start + i++] != 't') goto error;
428 if (state->parseBuffer[start + i++] != ';') goto error;
429 tempString[j++] = '>';
430 continue;
431 }
432 if ((i+3) > length) goto error;
433 if (c == 'a') {
434 if (state->parseBuffer[start + i++] != 'm') goto error;
435 if (state->parseBuffer[start + i++] != 'p') goto error;
436 if (state->parseBuffer[start + i++] != ';') goto error;
437 tempString[j++] = '&';
438 continue;
439 }
440 goto error;
441 }
442 }
443 tempString[j] = 0;
444
445 // printf("string %s\n", tempString);
446
447 return tempString;
448
449 error:
450 if (tempString) free(tempString);
451 return 0;
452 }
453
454 static long long
455 getNumber(parser_state_t *state)
456 {
457 unsigned long long n = 0;
458 int base = 10;
459 bool negate = false;
460 int c = currentChar();
461
462 if (c == '0') {
463 c = nextChar();
464 if (c == 'x') {
465 base = 16;
466 c = nextChar();
467 }
468 }
469 if (base == 10) {
470 if (c == '-') {
471 negate = true;
472 c = nextChar();
473 }
474 while(isDigit(c)) {
475 n = (n * base + c - '0');
476 c = nextChar();
477 }
478 if (negate) {
479 n = (unsigned long long)((long long)n * (long long)-1);
480 }
481 } else {
482 while(isHexDigit(c)) {
483 if (isDigit(c)) {
484 n = (n * base + c - '0');
485 } else {
486 n = (n * base + 0xa + c - 'a');
487 }
488 c = nextChar();
489 }
490 }
491 // printf("number 0x%x\n", (unsigned long)n);
492 return n;
493 }
494
495 // taken from CFXMLParsing/CFPropertyList.c
496
497 static const signed char __CFPLDataDecodeTable[128] = {
498 /* 000 */ -1, -1, -1, -1, -1, -1, -1, -1,
499 /* 010 */ -1, -1, -1, -1, -1, -1, -1, -1,
500 /* 020 */ -1, -1, -1, -1, -1, -1, -1, -1,
501 /* 030 */ -1, -1, -1, -1, -1, -1, -1, -1,
502 /* ' ' */ -1, -1, -1, -1, -1, -1, -1, -1,
503 /* '(' */ -1, -1, -1, 62, -1, -1, -1, 63,
504 /* '0' */ 52, 53, 54, 55, 56, 57, 58, 59,
505 /* '8' */ 60, 61, -1, -1, -1, 0, -1, -1,
506 /* '@' */ -1, 0, 1, 2, 3, 4, 5, 6,
507 /* 'H' */ 7, 8, 9, 10, 11, 12, 13, 14,
508 /* 'P' */ 15, 16, 17, 18, 19, 20, 21, 22,
509 /* 'X' */ 23, 24, 25, -1, -1, -1, -1, -1,
510 /* '`' */ -1, 26, 27, 28, 29, 30, 31, 32,
511 /* 'h' */ 33, 34, 35, 36, 37, 38, 39, 40,
512 /* 'p' */ 41, 42, 43, 44, 45, 46, 47, 48,
513 /* 'x' */ 49, 50, 51, -1, -1, -1, -1, -1
514 };
515
516 #define DATA_ALLOC_SIZE 4096
517
518 static void *
519 getCFEncodedData(parser_state_t *state, unsigned int *size)
520 {
521 int numeq = 0, acc = 0, cntr = 0;
522 int tmpbufpos = 0, tmpbuflen = 0;
523 unsigned char *tmpbuf = (unsigned char *)malloc(DATA_ALLOC_SIZE);
524
525 int c = currentChar();
526 *size = 0;
527
528 while (c != '<') {
529 c &= 0x7f;
530 if (c == 0) {
531 free(tmpbuf);
532 return 0;
533 }
534 if (c == '=') numeq++; else numeq = 0;
535 if (c == '\n') state->lineNumber++;
536 if (__CFPLDataDecodeTable[c] < 0) {
537 c = nextChar();
538 continue;
539 }
540 cntr++;
541 acc <<= 6;
542 acc += __CFPLDataDecodeTable[c];
543 if (0 == (cntr & 0x3)) {
544 if (tmpbuflen <= tmpbufpos + 2) {
545 tmpbuflen += DATA_ALLOC_SIZE;
546 tmpbuf = (unsigned char *)realloc(tmpbuf, tmpbuflen);
547 }
548 tmpbuf[tmpbufpos++] = (acc >> 16) & 0xff;
549 if (numeq < 2)
550 tmpbuf[tmpbufpos++] = (acc >> 8) & 0xff;
551 if (numeq < 1)
552 tmpbuf[tmpbufpos++] = acc & 0xff;
553 }
554 c = nextChar();
555 }
556 *size = tmpbufpos;
557 if (*size == 0) {
558 free(tmpbuf);
559 return 0;
560 }
561 return tmpbuf;
562 }
563
564 static void *
565 getHexData(parser_state_t *state, unsigned int *size)
566 {
567 int c;
568 unsigned char *d, *start, *lastStart;
569
570 start = lastStart = d = (unsigned char *)malloc(DATA_ALLOC_SIZE);
571 c = currentChar();
572
573 while (c != '<') {
574
575 if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
576 if (c == '\n') {
577 state->lineNumber++;
578 c = nextChar();
579 continue;
580 }
581
582 // get high nibble
583 if (isDigit(c)) {
584 *d = (c - '0') << 4;
585 } else if (isAlphaDigit(c)) {
586 *d = (0xa + (c - 'a')) << 4;
587 } else {
588 goto error;
589 }
590
591 // get low nibble
592 c = nextChar();
593 if (isDigit(c)) {
594 *d |= c - '0';
595 } else if (isAlphaDigit(c)) {
596 *d |= 0xa + (c - 'a');
597 } else {
598 goto error;
599 }
600
601 d++;
602 if ((d - lastStart) >= DATA_ALLOC_SIZE) {
603 int oldsize = d - start;
604 start = (unsigned char *)realloc(start, oldsize + DATA_ALLOC_SIZE);
605 d = lastStart = start + oldsize;
606 }
607 c = nextChar();
608 }
609
610 *size = d - start;
611 return start;
612
613 error:
614
615 *size = 0;
616 free(start);
617 return 0;
618 }
619
620 static int
621 yylex(YYSTYPE *lvalp, parser_state_t *state)
622 {
623 int c, i;
624 int tagType;
625 char tag[TAG_MAX_LENGTH];
626 int attributeCount;
627 char attributes[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH];
628 char values[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH];
629 object_t *object;
630
631 top:
632 c = currentChar();
633
634 /* skip white space */
635 if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
636
637 /* keep track of line number, don't return \n's */
638 if (c == '\n') {
639 STATE->lineNumber++;
640 (void)nextChar();
641 goto top;
642 }
643
644 // end of the buffer?
645 if (!c) return 0;
646
647 tagType = getTag(STATE, tag, &attributeCount, attributes, values);
648 if (tagType == TAG_BAD) return SYNTAX_ERROR;
649 if (tagType == TAG_COMMENT) goto top;
650
651 // handle allocation and check for "ID" and "IDREF" tags up front
652 *lvalp = object = newObject(STATE);
653 object->idref = -1;
654 for (i=0; i < attributeCount; i++) {
655 if (attributes[i][0] == 'I' && attributes[i][1] == 'D') {
656 // check for idref's, note: we ignore the tag, for
657 // this to work correctly, all idrefs must be unique
658 // across the whole serialization
659 if (attributes[i][2] == 'R' && attributes[i][3] == 'E' &&
660 attributes[i][4] == 'F' && !attributes[i][5]) {
661 if (tagType != TAG_EMPTY) return SYNTAX_ERROR;
662 object->idref = strtol(values[i], NULL, 0);
663 return IDREF;
664 }
665 // check for id's
666 if (!attributes[i][2]) {
667 object->idref = strtol(values[i], NULL, 0);
668 } else {
669 return SYNTAX_ERROR;
670 }
671 }
672 }
673
674 switch (*tag) {
675 case 'a':
676 if (!strcmp(tag, "array")) {
677 if (tagType == TAG_EMPTY) {
678 object->elements = NULL;
679 return ARRAY;
680 }
681 return (tagType == TAG_START) ? '(' : ')';
682 }
683 break;
684 case 'd':
685 if (!strcmp(tag, "dict")) {
686 if (tagType == TAG_EMPTY) {
687 object->elements = NULL;
688 return DICTIONARY;
689 }
690 return (tagType == TAG_START) ? '{' : '}';
691 }
692 if (!strcmp(tag, "data")) {
693 unsigned int size;
694 if (tagType == TAG_EMPTY) {
695 object->data = NULL;
696 object->size = 0;
697 return DATA;
698 }
699
700 bool isHexFormat = false;
701 for (int i=0; i < attributeCount; i++) {
702 if (!strcmp(attributes[i], "format") && !strcmp(values[i], "hex")) {
703 isHexFormat = true;
704 break;
705 }
706 }
707 // CF encoded is the default form
708 if (isHexFormat) {
709 object->data = getHexData(STATE, &size);
710 } else {
711 object->data = getCFEncodedData(STATE, &size);
712 }
713 object->size = size;
714 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END) || strcmp(tag, "data")) {
715 return SYNTAX_ERROR;
716 }
717 return DATA;
718 }
719 break;
720 case 'f':
721 if (!strcmp(tag, "false")) {
722 if (tagType == TAG_EMPTY) {
723 object->number = 0;
724 return BOOLEAN;
725 }
726 }
727 break;
728 case 'i':
729 if (!strcmp(tag, "integer")) {
730 object->size = 64; // default
731 for (i=0; i < attributeCount; i++) {
732 if (!strcmp(attributes[i], "size")) {
733 object->size = strtoul(values[i], NULL, 0);
734 }
735 }
736 if (tagType == TAG_EMPTY) {
737 object->number = 0;
738 return NUMBER;
739 }
740 object->number = getNumber(STATE);
741 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END) || strcmp(tag, "integer")) {
742 return SYNTAX_ERROR;
743 }
744 return NUMBER;
745 }
746 break;
747 case 'k':
748 if (!strcmp(tag, "key")) {
749 if (tagType == TAG_EMPTY) return SYNTAX_ERROR;
750 object->string = getString(STATE);
751 if (!object->string) {
752 return SYNTAX_ERROR;
753 }
754 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END)
755 || strcmp(tag, "key")) {
756 return SYNTAX_ERROR;
757 }
758 return KEY;
759 }
760 break;
761 case 'p':
762 if (!strcmp(tag, "plist")) {
763 freeObject(STATE, object);
764 goto top;
765 }
766 break;
767 case 's':
768 if (!strcmp(tag, "string")) {
769 if (tagType == TAG_EMPTY) {
770 object->string = (char *)malloc(1);
771 object->string[0] = 0;
772 return STRING;
773 }
774 object->string = getString(STATE);
775 if (!object->string) {
776 return SYNTAX_ERROR;
777 }
778 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END)
779 || strcmp(tag, "string")) {
780 return SYNTAX_ERROR;
781 }
782 return STRING;
783 }
784 if (!strcmp(tag, "set")) {
785 if (tagType == TAG_EMPTY) {
786 object->elements = NULL;
787 return SET;;
788 }
789 if (tagType == TAG_START) {
790 return '[';
791 } else {
792 return ']';
793 }
794 }
795 break;
796 case 't':
797 if (!strcmp(tag, "true")) {
798 if (tagType == TAG_EMPTY) {
799 object->number = 1;
800 return BOOLEAN;
801 }
802 }
803 break;
804 }
805
806 return SYNTAX_ERROR;
807 }
808
809 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
810 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
811 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
812
813 // "java" like allocation, if this code hits a syntax error in the
814 // the middle of the parsed string we just bail with pointers hanging
815 // all over place, this code helps keeps it all together
816
817 //static int object_count = 0;
818
819 object_t *
820 newObject(parser_state_t *state)
821 {
822 object_t *o;
823
824 if (state->freeObjects) {
825 o = state->freeObjects;
826 state->freeObjects = state->freeObjects->next;
827 } else {
828 o = (object_t *)malloc(sizeof(object_t));
829 // object_count++;
830 bzero(o, sizeof(object_t));
831 o->free = state->objects;
832 state->objects = o;
833 }
834
835 return o;
836 }
837
838 void
839 freeObject(parser_state_t * state, object_t *o)
840 {
841 o->next = state->freeObjects;
842 state->freeObjects = o;
843 }
844
845 void
846 cleanupObjects(parser_state_t *state)
847 {
848 object_t *t, *o = state->objects;
849
850 while (o) {
851 if (o->object) {
852 // printf("OSUnserializeXML: releasing object o=%x object=%x\n", (int)o, (int)o->object);
853 o->object->release();
854 }
855 if (o->data) {
856 // printf("OSUnserializeXML: freeing object o=%x data=%x\n", (int)o, (int)o->data);
857 free(o->data);
858 }
859 if (o->key) {
860 // printf("OSUnserializeXML: releasing object o=%x key=%x\n", (int)o, (int)o->key);
861 o->key->release();
862 }
863 if (o->string) {
864 // printf("OSUnserializeXML: freeing object o=%x string=%x\n", (int)o, (int)o->string);
865 free(o->string);
866 }
867
868 t = o;
869 o = o->free;
870 free(t);
871 // object_count--;
872 }
873 // printf("object_count = %d\n", object_count);
874 }
875
876 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
877 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
878 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
879
880 static void
881 rememberObject(parser_state_t *state, int tag, OSObject *o)
882 {
883 char key[16];
884 snprintf(key, 16, "%u", tag);
885
886 // printf("remember key %s\n", key);
887
888 state->tags->setObject(key, o);
889 }
890
891 static object_t *
892 retrieveObject(parser_state_t *state, int tag)
893 {
894 OSObject *ref;
895 object_t *o;
896 char key[16];
897 snprintf(key, 16, "%u", tag);
898
899 // printf("retrieve key '%s'\n", key);
900
901 ref = state->tags->getObject(key);
902 if (!ref) return 0;
903
904 o = newObject(state);
905 o->object = ref;
906 return o;
907 }
908
909 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
910 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
911 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
912
913 object_t *
914 buildDictionary(parser_state_t *state, object_t * header)
915 {
916 object_t *o, *t;
917 int count = 0;
918 OSDictionary *dict;
919
920 // get count and reverse order
921 o = header->elements;
922 header->elements = 0;
923 while (o) {
924 count++;
925 t = o;
926 o = o->next;
927
928 t->next = header->elements;
929 header->elements = t;
930 }
931
932 dict = OSDictionary::withCapacity(count);
933 if (header->idref >= 0) rememberObject(state, header->idref, dict);
934
935 o = header->elements;
936 while (o) {
937 dict->setObject(o->key, o->object);
938
939 o->key->release();
940 o->object->release();
941 o->key = 0;
942 o->object = 0;
943
944 t = o;
945 o = o->next;
946 freeObject(state, t);
947 }
948 o = header;
949 o->object = dict;
950 return o;
951 };
952
953 object_t *
954 buildArray(parser_state_t *state, object_t * header)
955 {
956 object_t *o, *t;
957 int count = 0;
958 OSArray *array;
959
960 // get count and reverse order
961 o = header->elements;
962 header->elements = 0;
963 while (o) {
964 count++;
965 t = o;
966 o = o->next;
967
968 t->next = header->elements;
969 header->elements = t;
970 }
971
972 array = OSArray::withCapacity(count);
973 if (header->idref >= 0) rememberObject(state, header->idref, array);
974
975 o = header->elements;
976 while (o) {
977 array->setObject(o->object);
978
979 o->object->release();
980 o->object = 0;
981
982 t = o;
983 o = o->next;
984 freeObject(state, t);
985 }
986 o = header;
987 o->object = array;
988 return o;
989 };
990
991 object_t *
992 buildSet(parser_state_t *state, object_t *header)
993 {
994 object_t *o = buildArray(state, header);
995
996 OSArray *array = (OSArray *)o->object;
997 OSSet *set = OSSet::withArray(array, array->getCapacity());
998
999 // write over the reference created in buildArray
1000 if (header->idref >= 0) rememberObject(state, header->idref, set);
1001
1002 array->release();
1003 o->object = set;
1004 return o;
1005 };
1006
1007 object_t *
1008 buildString(parser_state_t *state, object_t *o)
1009 {
1010 OSString *string;
1011
1012 string = OSString::withCString(o->string);
1013 if (o->idref >= 0) rememberObject(state, o->idref, string);
1014
1015 free(o->string);
1016 o->string = 0;
1017 o->object = string;
1018
1019 return o;
1020 };
1021
1022 object_t *
1023 buildData(parser_state_t *state, object_t *o)
1024 {
1025 OSData *data;
1026
1027 if (o->size) {
1028 data = OSData::withBytes(o->data, o->size);
1029 } else {
1030 data = OSData::withCapacity(0);
1031 }
1032 if (o->idref >= 0) rememberObject(state, o->idref, data);
1033
1034 if (o->size) free(o->data);
1035 o->data = 0;
1036 o->object = data;
1037 return o;
1038 };
1039
1040 object_t *
1041 buildNumber(parser_state_t *state, object_t *o)
1042 {
1043 OSNumber *number = OSNumber::withNumber(o->number, o->size);
1044
1045 if (o->idref >= 0) rememberObject(state, o->idref, number);
1046
1047 o->object = number;
1048 return o;
1049 };
1050
1051 object_t *
1052 buildBoolean(parser_state_t *state, object_t *o)
1053 {
1054 o->object = ((o->number == 0) ? kOSBooleanFalse : kOSBooleanTrue);
1055 o->object->retain();
1056 return o;
1057 };
1058
1059 OSObject*
1060 OSUnserializeXML(const char *buffer, OSString **errorString)
1061 {
1062 OSObject *object;
1063 parser_state_t *state = (parser_state_t *)malloc(sizeof(parser_state_t));
1064
1065 if ((!state) || (!buffer)) return 0;
1066
1067 // just in case
1068 if (errorString) *errorString = NULL;
1069
1070 state->parseBuffer = buffer;
1071 state->parseBufferIndex = 0;
1072 state->lineNumber = 1;
1073 state->objects = 0;
1074 state->freeObjects = 0;
1075 state->tags = OSDictionary::withCapacity(128);
1076 state->errorString = errorString;
1077 state->parsedObject = 0;
1078
1079 (void)yyparse((void *)state);
1080
1081 object = state->parsedObject;
1082
1083 cleanupObjects(state);
1084 state->tags->release();
1085 free(state);
1086
1087 return object;
1088 }
1089
1090
1091 //
1092 //
1093 //
1094 //
1095 //
1096 // DO NOT EDIT OSUnserializeXML.cpp!
1097 //
1098 // this means you!
1099 //
1100 //
1101 //
1102 //
1103 //