2 * Copyright (c) 1999-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * OSUnserializeXML.y created by rsulack on Tue Oct 12 1999
35 // parser for unserializing OSContainer objects serialized to XML
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
42 // when changing code check in both OSUnserializeXML.y and OSUnserializeXML.cpp
48 // DO NOT EDIT OSUnserializeXML.cpp!
63 #include <libkern/c++/OSMetaClass.h>
64 #include <libkern/c++/OSContainers.h>
65 #include <libkern/c++/OSLib.h>
67 #define MAX_OBJECTS 65535
69 #define YYSTYPE object_t *
70 #define YYPARSE_PARAM state
71 #define YYLEX_PARAM (parser_state_t *)state
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 {
78 struct object *elements;
80 OSSymbol *key; // for dictionary
82 void *data; // for data
83 char *string; // for string & symbol
84 long long number; // for number
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;
102 #define STATE ((parser_state_t *)state)
105 #define yyerror(s) OSUnserializeerror(STATE, (s))
106 static int OSUnserializeerror(parser_state_t *state, const char *s);
108 static int yylex(YYSTYPE *lvalp, parser_state_t *state);
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);
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);
126 extern void *kern_os_malloc(size_t size);
127 extern void *kern_os_realloc(void * addr, size_t size);
128 extern void kern_os_free(void * addr);
132 #define malloc(s) kern_os_malloc(s)
133 #define realloc(a, s) kern_os_realloc(a, s)
134 #define free(a) kern_os_free((void *)a)
147 %% /* Grammar rules and actions follow */
149 input: /* empty */ { yyerror("unexpected end of buffer");
152 | object { STATE->parsedObject = $1->object;
154 freeObject(STATE, $1);
157 | SYNTAX_ERROR { yyerror("syntax error");
162 object: dict { $$ = buildDictionary(STATE, $1);
164 if (!yyval->object) {
165 yyerror("buildDictionary");
168 STATE->parsedObjectCount++;
169 if (STATE->parsedObjectCount > MAX_OBJECTS) {
170 yyerror("maximum object count");
174 | array { $$ = buildArray(STATE, $1);
176 if (!yyval->object) {
177 yyerror("buildArray");
180 STATE->parsedObjectCount++;
181 if (STATE->parsedObjectCount > MAX_OBJECTS) {
182 yyerror("maximum object count");
186 | set { $$ = buildSet(STATE, $1);
188 if (!yyval->object) {
192 STATE->parsedObjectCount++;
193 if (STATE->parsedObjectCount > MAX_OBJECTS) {
194 yyerror("maximum object count");
198 | string { $$ = buildString(STATE, $1);
200 if (!yyval->object) {
201 yyerror("buildString");
204 STATE->parsedObjectCount++;
205 if (STATE->parsedObjectCount > MAX_OBJECTS) {
206 yyerror("maximum object count");
210 | data { $$ = buildData(STATE, $1);
212 if (!yyval->object) {
213 yyerror("buildData");
216 STATE->parsedObjectCount++;
217 if (STATE->parsedObjectCount > MAX_OBJECTS) {
218 yyerror("maximum object count");
222 | number { $$ = buildNumber(STATE, $1);
224 if (!yyval->object) {
225 yyerror("buildNumber");
228 STATE->parsedObjectCount++;
229 if (STATE->parsedObjectCount > MAX_OBJECTS) {
230 yyerror("maximum object count");
234 | boolean { $$ = buildBoolean(STATE, $1);
236 if (!yyval->object) {
237 yyerror("buildBoolean");
240 STATE->parsedObjectCount++;
241 if (STATE->parsedObjectCount > MAX_OBJECTS) {
242 yyerror("maximum object count");
246 | idref { $$ = retrieveObject(STATE, $1->idref);
248 $$->object->retain();
250 yyerror("forward reference detected");
253 freeObject(STATE, $1);
255 STATE->parsedObjectCount++;
256 if (STATE->parsedObjectCount > MAX_OBJECTS) {
257 yyerror("maximum object count");
263 //------------------------------------------------------------------------------
265 dict: '{' '}' { $$ = $1;
268 | '{' pairs '}' { $$ = $1;
275 | pairs pair { $$ = $2;
281 if (o->key == $$->key) {
282 yyerror("duplicate dictionary key");
290 pair: key object { $$ = $1;
291 $$->key = (OSSymbol *)$$->object;
292 $$->object = $2->object;
295 freeObject(STATE, $2);
299 key: KEY { $$ = buildSymbol(STATE, $1);
301 // STATE->parsedObjectCount++;
302 // if (STATE->parsedObjectCount > MAX_OBJECTS) {
303 // yyerror("maximum object count");
309 //------------------------------------------------------------------------------
311 array: '(' ')' { $$ = $1;
314 | '(' elements ')' { $$ = $1;
320 set: '[' ']' { $$ = $1;
323 | '[' elements ']' { $$ = $1;
329 elements: object { $$ = $1;
332 | elements object { $$ = $2;
337 //------------------------------------------------------------------------------
357 OSUnserializeerror(parser_state_t * state, const char *s) /* Called by yyparse on errors */
359 if (state->errorString) {
360 char tempString[128];
361 snprintf(tempString, 128, "OSUnserializeXML: %s near line %d\n", s, state->lineNumber);
362 *(state->errorString) = OSString::withCString(tempString);
368 #define TAG_MAX_LENGTH 32
369 #define TAG_MAX_ATTRIBUTES 32
376 #define currentChar() (state->parseBuffer[state->parseBufferIndex])
377 #define nextChar() (state->parseBuffer[++state->parseBufferIndex])
378 #define prevChar() (state->parseBuffer[state->parseBufferIndex - 1])
380 #define isSpace(c) ((c) == ' ' || (c) == '\t')
381 #define isAlpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
382 #define isDigit(c) ((c) >= '0' && (c) <= '9')
383 #define isAlphaDigit(c) ((c) >= 'a' && (c) <= 'f')
384 #define isHexDigit(c) (isDigit(c) || isAlphaDigit(c))
385 #define isAlphaNumeric(c) (isAlpha(c) || isDigit(c) || ((c) == '-'))
388 getTag(parser_state_t *state,
389 char tag[TAG_MAX_LENGTH],
391 char attributes[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH],
392 char values[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH] )
395 int c = currentChar();
396 int tagType = TAG_START;
400 if (c != '<') return TAG_BAD;
401 c = nextChar(); // skip '<'
404 // <!TAG declarations >
408 bool isComment = (c == '-') && ((c = nextChar()) != 0) && (c == '-');
409 if (!isComment && !isAlpha(c)) return TAG_BAD; // <!1, <!-A, <!eos
411 while (c && (c = nextChar()) != 0) {
412 if (c == '\n') state->lineNumber++;
414 if (c != '-') continue;
416 if (c != '-') continue;
423 if (isComment) break;
430 // <? Processing Instructions ?>
432 while ((c = nextChar()) != 0) {
433 if (c == '\n') state->lineNumber++;
434 if (c != '?') continue;
436 if (!c) return TAG_IGNORE;
449 c = nextChar(); // skip '/'
452 if (!isAlpha(c)) return TAG_BAD;
454 /* find end of tag while copying it */
455 while (isAlphaNumeric(c)) {
458 if (length >= (TAG_MAX_LENGTH - 1)) return TAG_BAD;
463 // printf("tag %s, type %d\n", tag, tagType);
465 // look for attributes of the form attribute = "value" ...
466 while ((c != '>') && (c != '/')) {
467 while (isSpace(c)) c = nextChar();
470 while (isAlphaNumeric(c)) {
471 attributes[*attributeCount][length++] = c;
472 if (length >= (TAG_MAX_LENGTH - 1)) return TAG_BAD;
475 attributes[*attributeCount][length] = 0;
477 while (isSpace(c)) c = nextChar();
479 if (c != '=') return TAG_BAD;
482 while (isSpace(c)) c = nextChar();
484 if (c != '"') return TAG_BAD;
488 values[*attributeCount][length++] = c;
489 if (length >= (TAG_MAX_LENGTH - 1)) return TAG_BAD;
491 if (!c) return TAG_BAD;
493 values[*attributeCount][length] = 0;
495 c = nextChar(); // skip closing quote
497 // printf(" attribute '%s' = '%s', nextchar = '%c'\n",
498 // attributes[*attributeCount], values[*attributeCount], c);
501 if (*attributeCount >= TAG_MAX_ATTRIBUTES) return TAG_BAD;
505 c = nextChar(); // skip '/'
508 if (c != '>') return TAG_BAD;
509 c = nextChar(); // skip '>'
515 getString(parser_state_t *state)
517 int c = currentChar();
518 int start, length, i, j;
521 start = state->parseBufferIndex;
522 /* find end of string */
525 if (c == '\n') state->lineNumber++;
532 if (c != '<') return 0;
534 length = state->parseBufferIndex - start;
536 /* copy to null terminated buffer */
537 tempString = (char *)malloc(length + 1);
538 if (tempString == 0) {
539 printf("OSUnserializeXML: can't alloc temp memory\n");
543 // copy out string in tempString
544 // "&" -> '&', "<" -> '<', ">" -> '>'
548 c = state->parseBuffer[start + i++];
552 if ((i+3) > length) goto error;
553 c = state->parseBuffer[start + i++];
555 if (state->parseBuffer[start + i++] != 't') goto error;
556 if (state->parseBuffer[start + i++] != ';') goto error;
557 tempString[j++] = '<';
561 if (state->parseBuffer[start + i++] != 't') goto error;
562 if (state->parseBuffer[start + i++] != ';') goto error;
563 tempString[j++] = '>';
566 if ((i+3) > length) goto error;
568 if (state->parseBuffer[start + i++] != 'm') goto error;
569 if (state->parseBuffer[start + i++] != 'p') goto error;
570 if (state->parseBuffer[start + i++] != ';') goto error;
571 tempString[j++] = '&';
579 // printf("string %s\n", tempString);
584 if (tempString) free(tempString);
589 getNumber(parser_state_t *state)
591 unsigned long long n = 0;
594 int c = currentChar();
609 n = (n * base + c - '0');
613 n = (unsigned long long)((long long)n * (long long)-1);
616 while(isHexDigit(c)) {
618 n = (n * base + c - '0');
620 n = (n * base + 0xa + c - 'a');
625 // printf("number 0x%x\n", (unsigned long)n);
629 // taken from CFXMLParsing/CFPropertyList.c
631 static const signed char __CFPLDataDecodeTable[128] = {
632 /* 000 */ -1, -1, -1, -1, -1, -1, -1, -1,
633 /* 010 */ -1, -1, -1, -1, -1, -1, -1, -1,
634 /* 020 */ -1, -1, -1, -1, -1, -1, -1, -1,
635 /* 030 */ -1, -1, -1, -1, -1, -1, -1, -1,
636 /* ' ' */ -1, -1, -1, -1, -1, -1, -1, -1,
637 /* '(' */ -1, -1, -1, 62, -1, -1, -1, 63,
638 /* '0' */ 52, 53, 54, 55, 56, 57, 58, 59,
639 /* '8' */ 60, 61, -1, -1, -1, 0, -1, -1,
640 /* '@' */ -1, 0, 1, 2, 3, 4, 5, 6,
641 /* 'H' */ 7, 8, 9, 10, 11, 12, 13, 14,
642 /* 'P' */ 15, 16, 17, 18, 19, 20, 21, 22,
643 /* 'X' */ 23, 24, 25, -1, -1, -1, -1, -1,
644 /* '`' */ -1, 26, 27, 28, 29, 30, 31, 32,
645 /* 'h' */ 33, 34, 35, 36, 37, 38, 39, 40,
646 /* 'p' */ 41, 42, 43, 44, 45, 46, 47, 48,
647 /* 'x' */ 49, 50, 51, -1, -1, -1, -1, -1
650 #define DATA_ALLOC_SIZE 4096
653 getCFEncodedData(parser_state_t *state, unsigned int *size)
655 int numeq = 0, acc = 0, cntr = 0;
656 int tmpbufpos = 0, tmpbuflen = 0;
657 unsigned char *tmpbuf = (unsigned char *)malloc(DATA_ALLOC_SIZE);
659 int c = currentChar();
668 if (c == '=') numeq++; else numeq = 0;
669 if (c == '\n') state->lineNumber++;
670 if (__CFPLDataDecodeTable[c] < 0) {
676 acc += __CFPLDataDecodeTable[c];
677 if (0 == (cntr & 0x3)) {
678 if (tmpbuflen <= tmpbufpos + 2) {
679 tmpbuflen += DATA_ALLOC_SIZE;
680 tmpbuf = (unsigned char *)realloc(tmpbuf, tmpbuflen);
682 tmpbuf[tmpbufpos++] = (acc >> 16) & 0xff;
684 tmpbuf[tmpbufpos++] = (acc >> 8) & 0xff;
686 tmpbuf[tmpbufpos++] = acc & 0xff;
699 getHexData(parser_state_t *state, unsigned int *size)
702 unsigned char *d, *start, *lastStart;
704 start = lastStart = d = (unsigned char *)malloc(DATA_ALLOC_SIZE);
709 if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
719 } else if (isAlphaDigit(c)) {
720 *d = (0xa + (c - 'a')) << 4;
729 } else if (isAlphaDigit(c)) {
730 *d |= 0xa + (c - 'a');
736 if ((d - lastStart) >= DATA_ALLOC_SIZE) {
737 int oldsize = d - start;
738 start = (unsigned char *)realloc(start, oldsize + DATA_ALLOC_SIZE);
739 d = lastStart = start + oldsize;
755 yylex(YYSTYPE *lvalp, parser_state_t *state)
759 char tag[TAG_MAX_LENGTH];
761 char attributes[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH];
762 char values[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH];
768 /* skip white space */
769 if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
771 /* keep track of line number, don't return \n's */
778 // end of the buffer?
781 tagType = getTag(STATE, tag, &attributeCount, attributes, values);
782 if (tagType == TAG_BAD) return SYNTAX_ERROR;
783 if (tagType == TAG_IGNORE) goto top;
785 // handle allocation and check for "ID" and "IDREF" tags up front
786 *lvalp = object = newObject(STATE);
788 for (i=0; i < attributeCount; i++) {
789 if (attributes[i][0] == 'I' && attributes[i][1] == 'D') {
790 // check for idref's, note: we ignore the tag, for
791 // this to work correctly, all idrefs must be unique
792 // across the whole serialization
793 if (attributes[i][2] == 'R' && attributes[i][3] == 'E' &&
794 attributes[i][4] == 'F' && !attributes[i][5]) {
795 if (tagType != TAG_EMPTY) return SYNTAX_ERROR;
796 object->idref = strtol(values[i], NULL, 0);
800 if (!attributes[i][2]) {
801 object->idref = strtol(values[i], NULL, 0);
810 if (!strcmp(tag, "array")) {
811 if (tagType == TAG_EMPTY) {
812 object->elements = NULL;
815 return (tagType == TAG_START) ? '(' : ')';
819 if (!strcmp(tag, "dict")) {
820 if (tagType == TAG_EMPTY) {
821 object->elements = NULL;
824 return (tagType == TAG_START) ? '{' : '}';
826 if (!strcmp(tag, "data")) {
828 if (tagType == TAG_EMPTY) {
834 bool isHexFormat = false;
835 for (i=0; i < attributeCount; i++) {
836 if (!strcmp(attributes[i], "format") && !strcmp(values[i], "hex")) {
841 // CF encoded is the default form
843 object->data = getHexData(STATE, &size);
845 object->data = getCFEncodedData(STATE, &size);
848 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END) || strcmp(tag, "data")) {
855 if (!strcmp(tag, "false")) {
856 if (tagType == TAG_EMPTY) {
863 if (!strcmp(tag, "integer")) {
864 object->size = 64; // default
865 for (i=0; i < attributeCount; i++) {
866 if (!strcmp(attributes[i], "size")) {
867 object->size = strtoul(values[i], NULL, 0);
870 if (tagType == TAG_EMPTY) {
874 object->number = getNumber(STATE);
875 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END) || strcmp(tag, "integer")) {
882 if (!strcmp(tag, "key")) {
883 if (tagType == TAG_EMPTY) return SYNTAX_ERROR;
884 object->string = getString(STATE);
885 if (!object->string) {
888 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END)
889 || strcmp(tag, "key")) {
896 if (!strcmp(tag, "plist")) {
897 freeObject(STATE, object);
902 if (!strcmp(tag, "string")) {
903 if (tagType == TAG_EMPTY) {
904 object->string = (char *)malloc(1);
905 object->string[0] = 0;
908 object->string = getString(STATE);
909 if (!object->string) {
912 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END)
913 || strcmp(tag, "string")) {
918 if (!strcmp(tag, "set")) {
919 if (tagType == TAG_EMPTY) {
920 object->elements = NULL;
923 if (tagType == TAG_START) {
931 if (!strcmp(tag, "true")) {
932 if (tagType == TAG_EMPTY) {
943 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
944 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
945 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
947 // "java" like allocation, if this code hits a syntax error in the
948 // the middle of the parsed string we just bail with pointers hanging
949 // all over place, this code helps keeps it all together
951 //static int object_count = 0;
954 newObject(parser_state_t *state)
958 if (state->freeObjects) {
959 o = state->freeObjects;
960 state->freeObjects = state->freeObjects->next;
962 o = (object_t *)malloc(sizeof(object_t));
964 bzero(o, sizeof(object_t));
965 o->free = state->objects;
973 freeObject(parser_state_t * state, object_t *o)
975 o->next = state->freeObjects;
976 state->freeObjects = o;
980 cleanupObjects(parser_state_t *state)
982 object_t *t, *o = state->objects;
986 // printf("OSUnserializeXML: releasing object o=%x object=%x\n", (int)o, (int)o->object);
987 o->object->release();
990 // printf("OSUnserializeXML: freeing object o=%x data=%x\n", (int)o, (int)o->data);
994 // printf("OSUnserializeXML: releasing object o=%x key=%x\n", (int)o, (int)o->key);
998 // printf("OSUnserializeXML: freeing object o=%x string=%x\n", (int)o, (int)o->string);
1007 // printf("object_count = %d\n", object_count);
1010 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1011 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1012 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1015 rememberObject(parser_state_t *state, int tag, OSObject *o)
1018 snprintf(key, 16, "%u", tag);
1020 // printf("remember key %s\n", key);
1022 state->tags->setObject(key, o);
1026 retrieveObject(parser_state_t *state, int tag)
1031 snprintf(key, 16, "%u", tag);
1033 // printf("retrieve key '%s'\n", key);
1035 ref = state->tags->getObject(key);
1038 o = newObject(state);
1043 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1044 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1045 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1048 buildDictionary(parser_state_t *state, object_t * header)
1054 // get count and reverse order
1055 o = header->elements;
1056 header->elements = 0;
1062 t->next = header->elements;
1063 header->elements = t;
1066 dict = OSDictionary::withCapacity(count);
1067 if (header->idref >= 0) rememberObject(state, header->idref, dict);
1069 o = header->elements;
1071 dict->setObject(o->key, o->object);
1074 o->object->release();
1080 freeObject(state, t);
1088 buildArray(parser_state_t *state, object_t * header)
1094 // get count and reverse order
1095 o = header->elements;
1096 header->elements = 0;
1102 t->next = header->elements;
1103 header->elements = t;
1106 array = OSArray::withCapacity(count);
1107 if (header->idref >= 0) rememberObject(state, header->idref, array);
1109 o = header->elements;
1111 array->setObject(o->object);
1113 o->object->release();
1118 freeObject(state, t);
1126 buildSet(parser_state_t *state, object_t *header)
1128 object_t *o = buildArray(state, header);
1130 OSArray *array = (OSArray *)o->object;
1131 OSSet *set = OSSet::withArray(array, array->getCapacity());
1133 // write over the reference created in buildArray
1134 if (header->idref >= 0) rememberObject(state, header->idref, set);
1142 buildString(parser_state_t *state, object_t *o)
1146 string = OSString::withCString(o->string);
1147 if (o->idref >= 0) rememberObject(state, o->idref, string);
1157 buildSymbol(parser_state_t *state, object_t *o)
1161 symbol = (OSSymbol *)OSSymbol::withCString(o->string);
1162 if (o->idref >= 0) rememberObject(state, o->idref, symbol);
1172 buildData(parser_state_t *state, object_t *o)
1177 data = OSData::withBytes(o->data, o->size);
1179 data = OSData::withCapacity(0);
1181 if (o->idref >= 0) rememberObject(state, o->idref, data);
1183 if (o->size) free(o->data);
1190 buildNumber(parser_state_t *state, object_t *o)
1192 OSNumber *number = OSNumber::withNumber(o->number, o->size);
1194 if (o->idref >= 0) rememberObject(state, o->idref, number);
1201 buildBoolean(parser_state_t *state __unused, object_t *o)
1203 o->object = ((o->number == 0) ? kOSBooleanFalse : kOSBooleanTrue);
1204 o->object->retain();
1209 OSUnserializeXML(const char *buffer, OSString **errorString)
1213 if (!buffer) return 0;
1214 parser_state_t *state = (parser_state_t *)malloc(sizeof(parser_state_t));
1215 if (!state) return 0;
1218 if (errorString) *errorString = NULL;
1220 state->parseBuffer = buffer;
1221 state->parseBufferIndex = 0;
1222 state->lineNumber = 1;
1224 state->freeObjects = 0;
1225 state->tags = OSDictionary::withCapacity(128);
1226 state->errorString = errorString;
1227 state->parsedObject = 0;
1228 state->parsedObjectCount = 0;
1230 (void)yyparse((void *)state);
1232 object = state->parsedObject;
1234 cleanupObjects(state);
1235 state->tags->release();
1241 #include <libkern/OSSerializeBinary.h>
1244 OSUnserializeXML(const char *buffer, size_t bufferSize, OSString **errorString)
1246 if (!buffer) return (0);
1247 if (bufferSize < sizeof(kOSSerializeBinarySignature)) return (0);
1249 if (!strcmp(kOSSerializeBinarySignature, buffer)) return OSUnserializeBinary(buffer, bufferSize, errorString);
1251 // XML must be null terminated
1252 if (buffer[bufferSize - 1]) return 0;
1254 return OSUnserializeXML(buffer, errorString);
1263 // DO NOT EDIT OSUnserializeXML.cpp!