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