2 * Copyright (c) 1999-2019 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 131071
68 #define MAX_REFED_OBJECTS 65535
70 #define YYSTYPE object_t *
71 #define YYPARSE_PARAM state
72 #define YYLEX_PARAM (parser_state_t *)state
74 // this is the internal struct used to hold objects on parser stack
75 // it represents objects both before and after they have been created
76 typedef struct object {
79 struct object *elements;
81 OSSymbol *key; // for dictionary
83 void *data; // for data
84 char *string; // for string & symbol
85 long long number; // for number
89 // this code is reentrant, this structure contains all
90 // state information for the parsing of a single buffer
91 typedef struct parser_state {
92 const char *parseBuffer; // start of text to be parsed
93 int parseBufferIndex; // current index into text
94 int lineNumber; // current line number
95 object_t *objects; // internal objects in use
96 object_t *freeObjects; // internal objects that are free
97 OSDictionary *tags; // used to remember "ID" tags
98 OSString **errorString; // parse error with line
99 OSObject *parsedObject; // resultant object of parsed text
100 int parsedObjectCount;
101 int retrievedObjectCount;
104 #define STATE ((parser_state_t *)state)
107 #define yyerror(s) OSUnserializeerror(STATE, (s))
108 static int OSUnserializeerror(parser_state_t *state, const char *s);
110 static int yylex(YYSTYPE *lvalp, parser_state_t *state);
112 static object_t *newObject(parser_state_t *state);
113 static void freeObject(parser_state_t *state, object_t *o);
114 static void rememberObject(parser_state_t *state, int tag, OSObject *o);
115 static object_t *retrieveObject(parser_state_t *state, int tag);
116 static void cleanupObjects(parser_state_t *state);
118 static object_t *buildDictionary(parser_state_t *state, object_t *o);
119 static object_t *buildArray(parser_state_t *state, object_t *o);
120 static object_t *buildSet(parser_state_t *state, object_t *o);
121 static object_t *buildString(parser_state_t *state, object_t *o);
122 static object_t *buildSymbol(parser_state_t *state, object_t *o);
123 static object_t *buildData(parser_state_t *state, object_t *o);
124 static object_t *buildNumber(parser_state_t *state, object_t *o);
125 static object_t *buildBoolean(parser_state_t *state, object_t *o);
128 #include <kern/kalloc.h>
131 #define malloc(size) malloc_impl(size)
133 malloc_impl(size_t size)
138 return kheap_alloc_tag_bt(KHEAP_DEFAULT, size,
139 (zalloc_flags_t) (Z_WAITOK | Z_ZERO),
140 VM_KERN_MEMORY_LIBKERN);
143 #define free(addr) free_impl(addr)
145 free_impl(void *addr)
147 kheap_free_addr(KHEAP_DEFAULT, addr);
150 safe_free(void *addr, size_t size)
154 kheap_free(KHEAP_DEFAULT, addr, size);
158 #define realloc(addr, osize, nsize) realloc_impl(addr, osize, nsize)
160 realloc_impl(void *addr, size_t osize, size_t nsize)
163 return malloc(nsize);
165 if (nsize == osize) {
168 void *nmem = malloc(nsize);
170 safe_free(addr, osize);
173 (void)memcpy(nmem, addr, (nsize > osize) ? osize : nsize);
174 safe_free(addr, osize);
190 %% /* Grammar rules and actions follow */
192 input: /* empty */ { yyerror("unexpected end of buffer");
195 | object { STATE->parsedObject = $1->object;
197 freeObject(STATE, $1);
200 | SYNTAX_ERROR { yyerror("syntax error");
205 object: dict { $$ = buildDictionary(STATE, $1);
207 if (!yyval->object) {
208 yyerror("buildDictionary");
211 STATE->parsedObjectCount++;
212 if (STATE->parsedObjectCount > MAX_OBJECTS) {
213 yyerror("maximum object count");
217 | array { $$ = buildArray(STATE, $1);
219 if (!yyval->object) {
220 yyerror("buildArray");
223 STATE->parsedObjectCount++;
224 if (STATE->parsedObjectCount > MAX_OBJECTS) {
225 yyerror("maximum object count");
229 | set { $$ = buildSet(STATE, $1);
231 if (!yyval->object) {
235 STATE->parsedObjectCount++;
236 if (STATE->parsedObjectCount > MAX_OBJECTS) {
237 yyerror("maximum object count");
241 | string { $$ = buildString(STATE, $1);
243 if (!yyval->object) {
244 yyerror("buildString");
247 STATE->parsedObjectCount++;
248 if (STATE->parsedObjectCount > MAX_OBJECTS) {
249 yyerror("maximum object count");
253 | data { $$ = buildData(STATE, $1);
255 if (!yyval->object) {
256 yyerror("buildData");
259 STATE->parsedObjectCount++;
260 if (STATE->parsedObjectCount > MAX_OBJECTS) {
261 yyerror("maximum object count");
265 | number { $$ = buildNumber(STATE, $1);
267 if (!yyval->object) {
268 yyerror("buildNumber");
271 STATE->parsedObjectCount++;
272 if (STATE->parsedObjectCount > MAX_OBJECTS) {
273 yyerror("maximum object count");
277 | boolean { $$ = buildBoolean(STATE, $1);
279 if (!yyval->object) {
280 yyerror("buildBoolean");
283 STATE->parsedObjectCount++;
284 if (STATE->parsedObjectCount > MAX_OBJECTS) {
285 yyerror("maximum object count");
289 | idref { $$ = retrieveObject(STATE, $1->idref);
291 STATE->retrievedObjectCount++;
292 $$->object->retain();
293 if (STATE->retrievedObjectCount > MAX_REFED_OBJECTS) {
294 yyerror("maximum object reference count");
298 yyerror("forward reference detected");
301 freeObject(STATE, $1);
303 STATE->parsedObjectCount++;
304 if (STATE->parsedObjectCount > MAX_OBJECTS) {
305 yyerror("maximum object count");
311 //------------------------------------------------------------------------------
313 dict: '{' '}' { $$ = $1;
316 | '{' pairs '}' { $$ = $1;
323 | pairs pair { $$ = $2;
329 if (o->key == $$->key) {
330 yyerror("duplicate dictionary key");
338 pair: key object { $$ = $1;
339 $$->key = (OSSymbol *)$$->object;
340 $$->object = $2->object;
343 freeObject(STATE, $2);
347 key: KEY { $$ = buildSymbol(STATE, $1);
349 // STATE->parsedObjectCount++;
350 // if (STATE->parsedObjectCount > MAX_OBJECTS) {
351 // yyerror("maximum object count");
357 //------------------------------------------------------------------------------
359 array: '(' ')' { $$ = $1;
362 | '(' elements ')' { $$ = $1;
368 set: '[' ']' { $$ = $1;
371 | '[' elements ']' { $$ = $1;
377 elements: object { $$ = $1;
380 | elements object { $$ = $2;
385 //------------------------------------------------------------------------------
405 OSUnserializeerror(parser_state_t * state, const char *s) /* Called by yyparse on errors */
407 if (state->errorString) {
408 char tempString[128];
409 snprintf(tempString, 128, "OSUnserializeXML: %s near line %d\n", s, state->lineNumber);
410 *(state->errorString) = OSString::withCString(tempString);
416 #define TAG_MAX_LENGTH 32
417 #define TAG_MAX_ATTRIBUTES 32
424 #define currentChar() (state->parseBuffer[state->parseBufferIndex])
425 #define nextChar() (state->parseBuffer[++state->parseBufferIndex])
426 #define prevChar() (state->parseBuffer[state->parseBufferIndex - 1])
428 #define isSpace(c) ((c) == ' ' || (c) == '\t')
429 #define isAlpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
430 #define isDigit(c) ((c) >= '0' && (c) <= '9')
431 #define isAlphaDigit(c) ((c) >= 'a' && (c) <= 'f')
432 #define isHexDigit(c) (isDigit(c) || isAlphaDigit(c))
433 #define isAlphaNumeric(c) (isAlpha(c) || isDigit(c) || ((c) == '-'))
436 getTag(parser_state_t *state,
437 char tag[TAG_MAX_LENGTH],
439 char attributes[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH],
440 char values[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH] )
443 int c = currentChar();
444 int tagType = TAG_START;
451 c = nextChar(); // skip '<'
454 // <!TAG declarations >
458 bool isComment = (c == '-') && ((c = nextChar()) != 0) && (c == '-');
459 if (!isComment && !isAlpha(c)) {
460 return TAG_BAD; // <!1, <!-A, <!eos
462 while (c && (c = nextChar()) != 0) {
486 // <? Processing Instructions ?>
488 while ((c = nextChar()) != 0) {
508 c = nextChar(); // skip '/'
515 /* find end of tag while copying it */
516 while (isAlphaNumeric(c)) {
519 if (length >= (TAG_MAX_LENGTH - 1)) {
526 // printf("tag %s, type %d\n", tag, tagType);
528 // look for attributes of the form attribute = "value" ...
529 while ((c != '>') && (c != '/')) {
535 while (isAlphaNumeric(c)) {
536 attributes[*attributeCount][length++] = c;
537 if (length >= (TAG_MAX_LENGTH - 1)) {
542 attributes[*attributeCount][length] = 0;
563 values[*attributeCount][length++] = c;
564 if (length >= (TAG_MAX_LENGTH - 1)) {
572 values[*attributeCount][length] = 0;
574 c = nextChar(); // skip closing quote
576 // printf(" attribute '%s' = '%s', nextchar = '%c'\n",
577 // attributes[*attributeCount], values[*attributeCount], c);
580 if (*attributeCount >= TAG_MAX_ATTRIBUTES) {
586 c = nextChar(); // skip '/'
592 c = nextChar(); // skip '>'
598 getString(parser_state_t *state, int *alloc_lengthp)
600 int c = currentChar();
601 int start, length, i, j;
604 start = state->parseBufferIndex;
605 /* find end of string */
621 length = state->parseBufferIndex - start;
623 /* copy to null terminated buffer */
624 tempString = (char *)malloc(length + 1);
625 if (tempString == NULL) {
626 printf("OSUnserializeXML: can't alloc temp memory\n");
629 if (alloc_lengthp != NULL) {
630 *alloc_lengthp = length + 1;
633 // copy out string in tempString
634 // "&" -> '&', "<" -> '<', ">" -> '>'
638 c = state->parseBuffer[start + i++];
642 if ((i + 3) > length) {
645 c = state->parseBuffer[start + i++];
647 if (state->parseBuffer[start + i++] != 't') {
650 if (state->parseBuffer[start + i++] != ';') {
653 tempString[j++] = '<';
657 if (state->parseBuffer[start + i++] != 't') {
660 if (state->parseBuffer[start + i++] != ';') {
663 tempString[j++] = '>';
666 if ((i + 3) > length) {
670 if (state->parseBuffer[start + i++] != 'm') {
673 if (state->parseBuffer[start + i++] != 'p') {
676 if (state->parseBuffer[start + i++] != ';') {
679 tempString[j++] = '&';
687 // printf("string %s\n", tempString);
693 safe_free(tempString, length + 1);
694 if (alloc_lengthp != NULL) {
702 getNumber(parser_state_t *state)
704 unsigned long long n = 0;
707 int c = currentChar();
722 n = (n * base + c - '0');
726 n = (unsigned long long)((long long)n * (long long)-1);
729 while (isHexDigit(c)) {
731 n = (n * base + c - '0');
733 n = (n * base + 0xa + c - 'a');
738 // printf("number 0x%x\n", (unsigned long)n);
742 // taken from CFXMLParsing/CFPropertyList.c
744 static const signed char __CFPLDataDecodeTable[128] = {
745 /* 000 */ -1, -1, -1, -1, -1, -1, -1, -1,
746 /* 010 */ -1, -1, -1, -1, -1, -1, -1, -1,
747 /* 020 */ -1, -1, -1, -1, -1, -1, -1, -1,
748 /* 030 */ -1, -1, -1, -1, -1, -1, -1, -1,
749 /* ' ' */ -1, -1, -1, -1, -1, -1, -1, -1,
750 /* '(' */ -1, -1, -1, 62, -1, -1, -1, 63,
751 /* '0' */ 52, 53, 54, 55, 56, 57, 58, 59,
752 /* '8' */ 60, 61, -1, -1, -1, 0, -1, -1,
753 /* '@' */ -1, 0, 1, 2, 3, 4, 5, 6,
754 /* 'H' */ 7, 8, 9, 10, 11, 12, 13, 14,
755 /* 'P' */ 15, 16, 17, 18, 19, 20, 21, 22,
756 /* 'X' */ 23, 24, 25, -1, -1, -1, -1, -1,
757 /* '`' */ -1, 26, 27, 28, 29, 30, 31, 32,
758 /* 'h' */ 33, 34, 35, 36, 37, 38, 39, 40,
759 /* 'p' */ 41, 42, 43, 44, 45, 46, 47, 48,
760 /* 'x' */ 49, 50, 51, -1, -1, -1, -1, -1
763 #define DATA_ALLOC_SIZE 4096
766 getCFEncodedData(parser_state_t *state, unsigned int *size)
768 int numeq = 0, cntr = 0;
769 unsigned int acc = 0;
771 size_t tmpbuflen = DATA_ALLOC_SIZE;
772 unsigned char *tmpbuf = (unsigned char *)malloc(tmpbuflen);
774 int c = currentChar();
780 safe_free(tmpbuf, tmpbuflen);
791 if (__CFPLDataDecodeTable[c] < 0) {
797 acc += __CFPLDataDecodeTable[c];
798 if (0 == (cntr & 0x3)) {
799 if (tmpbuflen <= tmpbufpos + 2) {
800 size_t oldsize = tmpbuflen;
801 tmpbuflen += DATA_ALLOC_SIZE;
802 tmpbuf = (unsigned char *)realloc(tmpbuf, oldsize, tmpbuflen);
804 tmpbuf[tmpbufpos++] = (acc >> 16) & 0xff;
806 tmpbuf[tmpbufpos++] = (acc >> 8) & 0xff;
809 tmpbuf[tmpbufpos++] = acc & 0xff;
816 safe_free(tmpbuf, tmpbuflen);
823 getHexData(parser_state_t *state, unsigned int *size)
826 unsigned char *d, *start, *lastStart;
828 size_t buflen = DATA_ALLOC_SIZE;
829 start = lastStart = d = (unsigned char *)malloc(buflen);
834 while ((c = nextChar()) != 0 && isSpace(c)) {
847 } else if (isAlphaDigit(c)) {
848 *d = (0xa + (c - 'a')) << 4;
857 } else if (isAlphaDigit(c)) {
858 *d |= 0xa + (c - 'a');
864 if ((d - lastStart) >= DATA_ALLOC_SIZE) {
865 int oldsize = d - start;
866 assert(oldsize == buflen);
867 buflen += DATA_ALLOC_SIZE;
868 start = (unsigned char *)realloc(start, oldsize, buflen);
869 d = lastStart = start + oldsize;
880 safe_free(start, buflen);
885 yylex(YYSTYPE *lvalp, parser_state_t *state)
889 char tag[TAG_MAX_LENGTH];
891 char attributes[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH];
892 char values[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH];
899 /* skip white space */
901 while ((c = nextChar()) != 0 && isSpace(c)) {
906 /* keep track of line number, don't return \n's */
913 // end of the buffer?
918 tagType = getTag(STATE, tag, &attributeCount, attributes, values);
919 if (tagType == TAG_BAD) {
922 if (tagType == TAG_IGNORE) {
926 // handle allocation and check for "ID" and "IDREF" tags up front
927 *lvalp = object = newObject(STATE);
929 for (i = 0; i < attributeCount; i++) {
930 if (attributes[i][0] == 'I' && attributes[i][1] == 'D') {
931 // check for idref's, note: we ignore the tag, for
932 // this to work correctly, all idrefs must be unique
933 // across the whole serialization
934 if (attributes[i][2] == 'R' && attributes[i][3] == 'E' &&
935 attributes[i][4] == 'F' && !attributes[i][5]) {
936 if (tagType != TAG_EMPTY) {
939 object->idref = strtol(values[i], NULL, 0);
943 if (!attributes[i][2]) {
944 object->idref = strtol(values[i], NULL, 0);
953 if (!strcmp(tag, "array")) {
954 if (tagType == TAG_EMPTY) {
955 object->elements = NULL;
958 return (tagType == TAG_START) ? '(' : ')';
962 if (!strcmp(tag, "dict")) {
963 if (tagType == TAG_EMPTY) {
964 object->elements = NULL;
967 return (tagType == TAG_START) ? '{' : '}';
969 if (!strcmp(tag, "data")) {
971 if (tagType == TAG_EMPTY) {
977 bool isHexFormat = false;
978 for (i = 0; i < attributeCount; i++) {
979 if (!strcmp(attributes[i], "format") && !strcmp(values[i], "hex")) {
984 // CF encoded is the default form
986 object->data = getHexData(STATE, &size);
988 object->data = getCFEncodedData(STATE, &size);
991 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END) || strcmp(tag, "data")) {
998 if (!strcmp(tag, "false")) {
999 if (tagType == TAG_EMPTY) {
1006 if (!strcmp(tag, "integer")) {
1007 object->size = 64; // default
1008 for (i = 0; i < attributeCount; i++) {
1009 if (!strcmp(attributes[i], "size")) {
1010 object->size = strtoul(values[i], NULL, 0);
1013 if (tagType == TAG_EMPTY) {
1017 object->number = getNumber(STATE);
1018 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END) || strcmp(tag, "integer")) {
1019 return SYNTAX_ERROR;
1025 if (!strcmp(tag, "key")) {
1026 if (tagType == TAG_EMPTY) {
1027 return SYNTAX_ERROR;
1029 object->string = getString(STATE, &alloc_length);
1030 if (!object->string) {
1031 return SYNTAX_ERROR;
1033 object->string_alloc_length = alloc_length;
1034 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END)
1035 || strcmp(tag, "key")) {
1036 return SYNTAX_ERROR;
1042 if (!strcmp(tag, "plist")) {
1043 freeObject(STATE, object);
1048 if (!strcmp(tag, "string")) {
1049 if (tagType == TAG_EMPTY) {
1050 object->string = (char *)malloc(1);
1051 object->string[0] = 0;
1052 object->string_alloc_length = 1;
1055 object->string = getString(STATE, &alloc_length);
1056 if (!object->string) {
1057 return SYNTAX_ERROR;
1059 object->string_alloc_length = alloc_length;
1060 if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END)
1061 || strcmp(tag, "string")) {
1062 return SYNTAX_ERROR;
1066 if (!strcmp(tag, "set")) {
1067 if (tagType == TAG_EMPTY) {
1068 object->elements = NULL;
1071 if (tagType == TAG_START) {
1079 if (!strcmp(tag, "true")) {
1080 if (tagType == TAG_EMPTY) {
1088 return SYNTAX_ERROR;
1091 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1092 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1093 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1095 // "java" like allocation, if this code hits a syntax error in the
1096 // the middle of the parsed string we just bail with pointers hanging
1097 // all over place, this code helps keeps it all together
1099 //static int object_count = 0;
1102 newObject(parser_state_t *state)
1106 if (state->freeObjects) {
1107 o = state->freeObjects;
1108 state->freeObjects = state->freeObjects->next;
1110 o = (object_t *)malloc(sizeof(object_t));
1112 o->free = state->objects;
1120 freeObject(parser_state_t * state, object_t *o)
1122 o->next = state->freeObjects;
1123 state->freeObjects = o;
1127 cleanupObjects(parser_state_t *state)
1129 object_t *t, *o = state->objects;
1133 // printf("OSUnserializeXML: releasing object o=%x object=%x\n", (int)o, (int)o->object);
1134 o->object->release();
1137 // printf("OSUnserializeXML: freeing object o=%x data=%x\n", (int)o, (int)o->data);
1141 // printf("OSUnserializeXML: releasing object o=%x key=%x\n", (int)o, (int)o->key);
1145 // printf("OSUnserializeXML: freeing object o=%x string=%x\n", (int)o, (int)o->string);
1151 safe_free(t, sizeof(object_t));
1154 // printf("object_count = %d\n", object_count);
1157 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1158 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1159 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1162 rememberObject(parser_state_t *state, int tag, OSObject *o)
1165 snprintf(key, 16, "%u", tag);
1167 // printf("remember key %s\n", key);
1169 state->tags->setObject(key, o);
1173 retrieveObject(parser_state_t *state, int tag)
1178 snprintf(key, 16, "%u", tag);
1180 // printf("retrieve key '%s'\n", key);
1182 ref = state->tags->getObject(key);
1187 o = newObject(state);
1192 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1193 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1194 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1197 buildDictionary(parser_state_t *state, object_t * header)
1203 // get count and reverse order
1204 o = header->elements;
1205 header->elements = 0;
1211 t->next = header->elements;
1212 header->elements = t;
1215 dict = OSDictionary::withCapacity(count);
1216 if (header->idref >= 0) {
1217 rememberObject(state, header->idref, dict);
1220 o = header->elements;
1222 dict->setObject(o->key, o->object);
1225 o->object->release();
1231 freeObject(state, t);
1239 buildArray(parser_state_t *state, object_t * header)
1245 // get count and reverse order
1246 o = header->elements;
1247 header->elements = 0;
1253 t->next = header->elements;
1254 header->elements = t;
1257 array = OSArray::withCapacity(count);
1258 if (header->idref >= 0) {
1259 rememberObject(state, header->idref, array);
1262 o = header->elements;
1264 array->setObject(o->object);
1266 o->object->release();
1271 freeObject(state, t);
1279 buildSet(parser_state_t *state, object_t *header)
1281 object_t *o = buildArray(state, header);
1283 OSArray *array = (OSArray *)o->object;
1284 OSSet *set = OSSet::withArray(array, array->getCapacity());
1286 // write over the reference created in buildArray
1287 if (header->idref >= 0) {
1288 rememberObject(state, header->idref, set);
1297 buildString(parser_state_t *state, object_t *o)
1301 string = OSString::withCString(o->string);
1302 if (o->idref >= 0) {
1303 rememberObject(state, o->idref, string);
1314 buildSymbol(parser_state_t *state, object_t *o)
1318 symbol = const_cast < OSSymbol * > (OSSymbol::withCString(o->string));
1319 if (o->idref >= 0) {
1320 rememberObject(state, o->idref, symbol);
1323 safe_free(o->string, strlen(o->string) + 1);
1331 buildData(parser_state_t *state, object_t *o)
1336 data = OSData::withBytes(o->data, o->size);
1338 data = OSData::withCapacity(0);
1340 if (o->idref >= 0) {
1341 rememberObject(state, o->idref, data);
1353 buildNumber(parser_state_t *state, object_t *o)
1355 OSNumber *number = OSNumber::withNumber(o->number, o->size);
1357 if (o->idref >= 0) {
1358 rememberObject(state, o->idref, number);
1366 buildBoolean(parser_state_t *state __unused, object_t *o)
1368 o->object = ((o->number == 0) ? kOSBooleanFalse : kOSBooleanTrue);
1369 o->object->retain();
1374 OSUnserializeXML(const char *buffer, OSString **errorString)
1381 parser_state_t *state = (parser_state_t *)malloc(sizeof(parser_state_t));
1388 *errorString = NULL;
1391 state->parseBuffer = buffer;
1392 state->parseBufferIndex = 0;
1393 state->lineNumber = 1;
1395 state->freeObjects = 0;
1396 state->tags = OSDictionary::withCapacity(128);
1397 state->errorString = errorString;
1398 state->parsedObject = 0;
1399 state->parsedObjectCount = 0;
1400 state->retrievedObjectCount = 0;
1402 (void)yyparse((void *)state);
1404 object = state->parsedObject;
1406 cleanupObjects(state);
1407 state->tags->release();
1408 safe_free(state, sizeof(parser_state_t));
1413 #include <libkern/OSSerializeBinary.h>
1416 OSUnserializeXML(const char *buffer, size_t bufferSize, OSString **errorString)
1421 if (bufferSize < sizeof(kOSSerializeBinarySignature)) {
1425 if (!strcmp(kOSSerializeBinarySignature, buffer)
1426 || (kOSSerializeIndexedBinarySignature == (uint8_t)buffer[0])) {
1427 return OSUnserializeBinary(buffer, bufferSize, errorString);
1430 // XML must be null terminated
1431 if (buffer[bufferSize - 1]) {
1435 return OSUnserializeXML(buffer, errorString);
1444 // DO NOT EDIT OSUnserializeXML.cpp!