xref: /xnu-8796.121.2/libkern/c++/OSUnserializeXML.y (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 1999-2019 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions /*
30*c54f35caSApple OSS Distributions  * HISTORY
31*c54f35caSApple OSS Distributions  *
32*c54f35caSApple OSS Distributions  * OSUnserializeXML.y created by rsulack on Tue Oct 12 1999
33*c54f35caSApple OSS Distributions  */
34*c54f35caSApple OSS Distributions 
35*c54f35caSApple OSS Distributions // parser for unserializing OSContainer objects serialized to XML
36*c54f35caSApple OSS Distributions //
37*c54f35caSApple OSS Distributions // to build :
38*c54f35caSApple OSS Distributions //	bison -p OSUnserializeXML OSUnserializeXML.y
39*c54f35caSApple OSS Distributions //	head -50 OSUnserializeXML.y > ../libkern/c++/OSUnserializeXMLSharedImplementation.h
40*c54f35caSApple OSS Distributions //	sed -e "s/#include <stdio.h>//" < OSUnserializeXML.tab.c >> ../libkern/c++/OSUnserializeXMLSharedImplementation.h
41*c54f35caSApple OSS Distributions //
42*c54f35caSApple OSS Distributions //	when changing code check in both OSUnserializeXML.y and OSUnserializeXMLSharedImplementation.h
43*c54f35caSApple OSS Distributions //
44*c54f35caSApple OSS Distributions //
45*c54f35caSApple OSS Distributions //
46*c54f35caSApple OSS Distributions //
47*c54f35caSApple OSS Distributions //
48*c54f35caSApple OSS Distributions //		 DO NOT EDIT OSUnserializeXMLSharedImplementation.h!
49*c54f35caSApple OSS Distributions //
50*c54f35caSApple OSS Distributions //			this means you!
51*c54f35caSApple OSS Distributions //
52*c54f35caSApple OSS Distributions //
53*c54f35caSApple OSS Distributions //
54*c54f35caSApple OSS Distributions //
55*c54f35caSApple OSS Distributions //
56*c54f35caSApple OSS Distributions //
57*c54f35caSApple OSS Distributions 
58*c54f35caSApple OSS Distributions 
59*c54f35caSApple OSS Distributions %pure_parser
60*c54f35caSApple OSS Distributions 
61*c54f35caSApple OSS Distributions %{
62*c54f35caSApple OSS Distributions #include <string.h>
63*c54f35caSApple OSS Distributions #if KERNEL
64*c54f35caSApple OSS Distributions #include <libkern/c++/OSMetaClass.h>
65*c54f35caSApple OSS Distributions #include <libkern/c++/OSContainers.h>
66*c54f35caSApple OSS Distributions #include <libkern/c++/OSLib.h>
67*c54f35caSApple OSS Distributions #else /* !KERNEL */
68*c54f35caSApple OSS Distributions #include <DriverKit/DriverKit.h>
69*c54f35caSApple OSS Distributions #endif /* KERNEL */
70*c54f35caSApple OSS Distributions 
71*c54f35caSApple OSS Distributions 
72*c54f35caSApple OSS Distributions #define MAX_OBJECTS              131071
73*c54f35caSApple OSS Distributions #define MAX_REFED_OBJECTS        65535
74*c54f35caSApple OSS Distributions 
75*c54f35caSApple OSS Distributions #define YYSTYPE object_t *
76*c54f35caSApple OSS Distributions #define YYPARSE_PARAM   state
77*c54f35caSApple OSS Distributions #define YYLEX_PARAM     (parser_state_t *)state
78*c54f35caSApple OSS Distributions 
79*c54f35caSApple OSS Distributions // this is the internal struct used to hold objects on parser stack
80*c54f35caSApple OSS Distributions // it represents objects both before and after they have been created
81*c54f35caSApple OSS Distributions typedef struct object {
82*c54f35caSApple OSS Distributions 	struct object   *next;
83*c54f35caSApple OSS Distributions 	struct object   *free;
84*c54f35caSApple OSS Distributions 	struct object   *elements;
85*c54f35caSApple OSS Distributions 	OSObject        *object;
86*c54f35caSApple OSS Distributions 	OSSymbol        *key;                   // for dictionary
87*c54f35caSApple OSS Distributions 	int             size;
88*c54f35caSApple OSS Distributions 	void            *data;                  // for data
89*c54f35caSApple OSS Distributions 	char            *string;                // for string & symbol
90*c54f35caSApple OSS Distributions 	int             string_alloc_length;
91*c54f35caSApple OSS Distributions 	long long       number;                 // for number
92*c54f35caSApple OSS Distributions 	int             idref;
93*c54f35caSApple OSS Distributions } object_t;
94*c54f35caSApple OSS Distributions 
95*c54f35caSApple OSS Distributions // this code is reentrant, this structure contains all
96*c54f35caSApple OSS Distributions // state information for the parsing of a single buffer
97*c54f35caSApple OSS Distributions typedef struct parser_state {
98*c54f35caSApple OSS Distributions 	const char      *parseBuffer;           // start of text to be parsed
99*c54f35caSApple OSS Distributions 	int             parseBufferIndex;       // current index into text
100*c54f35caSApple OSS Distributions 	int             lineNumber;             // current line number
101*c54f35caSApple OSS Distributions 	object_t        *objects;               // internal objects in use
102*c54f35caSApple OSS Distributions 	object_t        *freeObjects;           // internal objects that are free
103*c54f35caSApple OSS Distributions 	OSDictionary    *tags;                  // used to remember "ID" tags
104*c54f35caSApple OSS Distributions 	OSString        **errorString;          // parse error with line
105*c54f35caSApple OSS Distributions 	OSObject        *parsedObject;          // resultant object of parsed text
106*c54f35caSApple OSS Distributions 	int             parsedObjectCount;
107*c54f35caSApple OSS Distributions 	int             retrievedObjectCount;
108*c54f35caSApple OSS Distributions } parser_state_t;
109*c54f35caSApple OSS Distributions 
110*c54f35caSApple OSS Distributions #define STATE           ((parser_state_t *)state)
111*c54f35caSApple OSS Distributions 
112*c54f35caSApple OSS Distributions #undef yyerror
113*c54f35caSApple OSS Distributions #define yyerror(s)      OSUnserializeerror(STATE, (s))
114*c54f35caSApple OSS Distributions static int              OSUnserializeerror(parser_state_t *state, const char *s);
115*c54f35caSApple OSS Distributions 
116*c54f35caSApple OSS Distributions static int              yylex(YYSTYPE *lvalp, parser_state_t *state);
117*c54f35caSApple OSS Distributions 
118*c54f35caSApple OSS Distributions static object_t         *newObject(parser_state_t *state);
119*c54f35caSApple OSS Distributions static void             freeObject(parser_state_t *state, object_t *o);
120*c54f35caSApple OSS Distributions static void             rememberObject(parser_state_t *state, int tag, OSObject *o);
121*c54f35caSApple OSS Distributions static object_t         *retrieveObject(parser_state_t *state, int tag);
122*c54f35caSApple OSS Distributions static void             cleanupObjects(parser_state_t *state);
123*c54f35caSApple OSS Distributions 
124*c54f35caSApple OSS Distributions static object_t         *buildDictionary(parser_state_t *state, object_t *o);
125*c54f35caSApple OSS Distributions static object_t         *buildArray(parser_state_t *state, object_t *o);
126*c54f35caSApple OSS Distributions static object_t         *buildSet(parser_state_t *state, object_t *o);
127*c54f35caSApple OSS Distributions static object_t         *buildString(parser_state_t *state, object_t *o);
128*c54f35caSApple OSS Distributions static object_t         *buildSymbol(parser_state_t *state, object_t *o);
129*c54f35caSApple OSS Distributions static object_t         *buildData(parser_state_t *state, object_t *o);
130*c54f35caSApple OSS Distributions static object_t         *buildNumber(parser_state_t *state, object_t *o);
131*c54f35caSApple OSS Distributions static object_t         *buildBoolean(parser_state_t *state, object_t *o);
132*c54f35caSApple OSS Distributions 
133*c54f35caSApple OSS Distributions #if KERNEL
134*c54f35caSApple OSS Distributions __BEGIN_DECLS
135*c54f35caSApple OSS Distributions #include <kern/kalloc.h>
136*c54f35caSApple OSS Distributions __END_DECLS
137*c54f35caSApple OSS Distributions 
138*c54f35caSApple OSS Distributions #define malloc(size)         malloc_impl(size)
139*c54f35caSApple OSS Distributions #define malloc_type(type)    kalloc_type(type, Z_WAITOK)
140*c54f35caSApple OSS Distributions static inline void *
malloc_impl(size_t size)141*c54f35caSApple OSS Distributions malloc_impl(size_t size)
142*c54f35caSApple OSS Distributions {
143*c54f35caSApple OSS Distributions 	if (size == 0) {
144*c54f35caSApple OSS Distributions 		return NULL;
145*c54f35caSApple OSS Distributions 	}
146*c54f35caSApple OSS Distributions 	return kheap_alloc_tag_bt(KHEAP_DATA_BUFFERS, size,
147*c54f35caSApple OSS Distributions 				(zalloc_flags_t) (Z_WAITOK | Z_ZERO),
148*c54f35caSApple OSS Distributions 				VM_KERN_MEMORY_LIBKERN);
149*c54f35caSApple OSS Distributions }
150*c54f35caSApple OSS Distributions 
151*c54f35caSApple OSS Distributions #define free(addr)             free_impl(addr)
152*c54f35caSApple OSS Distributions #define free_type(type, addr)  kfree_type(type, addr)
153*c54f35caSApple OSS Distributions static inline void
free_impl(void * addr)154*c54f35caSApple OSS Distributions free_impl(void *addr)
155*c54f35caSApple OSS Distributions {
156*c54f35caSApple OSS Distributions 	kheap_free_addr(KHEAP_DATA_BUFFERS, addr);
157*c54f35caSApple OSS Distributions }
158*c54f35caSApple OSS Distributions static inline void
safe_free(void * addr,size_t size)159*c54f35caSApple OSS Distributions safe_free(void *addr, size_t size)
160*c54f35caSApple OSS Distributions {
161*c54f35caSApple OSS Distributions   if(addr) {
162*c54f35caSApple OSS Distributions 	assert(size != 0);
163*c54f35caSApple OSS Distributions 	kheap_free(KHEAP_DATA_BUFFERS, addr, size);
164*c54f35caSApple OSS Distributions   }
165*c54f35caSApple OSS Distributions }
166*c54f35caSApple OSS Distributions 
167*c54f35caSApple OSS Distributions #define realloc(addr, osize, nsize) realloc_impl(addr, osize, nsize)
168*c54f35caSApple OSS Distributions static inline void *
realloc_impl(void * addr,size_t osize,size_t nsize)169*c54f35caSApple OSS Distributions realloc_impl(void *addr, size_t osize, size_t nsize)
170*c54f35caSApple OSS Distributions {
171*c54f35caSApple OSS Distributions 	if (!addr) {
172*c54f35caSApple OSS Distributions 		return malloc(nsize);
173*c54f35caSApple OSS Distributions 	}
174*c54f35caSApple OSS Distributions 	if (nsize == osize) {
175*c54f35caSApple OSS Distributions 		return addr;
176*c54f35caSApple OSS Distributions 	}
177*c54f35caSApple OSS Distributions 	void *nmem = malloc(nsize);
178*c54f35caSApple OSS Distributions 	if (!nmem) {
179*c54f35caSApple OSS Distributions 		safe_free(addr, osize);
180*c54f35caSApple OSS Distributions 		return NULL;
181*c54f35caSApple OSS Distributions 	}
182*c54f35caSApple OSS Distributions 	(void)memcpy(nmem, addr, (nsize > osize) ? osize : nsize);
183*c54f35caSApple OSS Distributions 	safe_free(addr, osize);
184*c54f35caSApple OSS Distributions 
185*c54f35caSApple OSS Distributions 	return nmem;
186*c54f35caSApple OSS Distributions }
187*c54f35caSApple OSS Distributions #else /* !KERNEL */
188*c54f35caSApple OSS Distributions #define malloc(size)      malloc_impl(size)
189*c54f35caSApple OSS Distributions #define malloc_type(type) (type *) calloc(1, sizeof(type))
190*c54f35caSApple OSS Distributions static inline void *
malloc_impl(size_t size)191*c54f35caSApple OSS Distributions malloc_impl(size_t size)
192*c54f35caSApple OSS Distributions {
193*c54f35caSApple OSS Distributions 	if (size == 0) {
194*c54f35caSApple OSS Distributions 		return NULL;
195*c54f35caSApple OSS Distributions 	}
196*c54f35caSApple OSS Distributions 	return calloc(1, size);
197*c54f35caSApple OSS Distributions }
198*c54f35caSApple OSS Distributions #define safe_free(addr, size)       free(addr)
199*c54f35caSApple OSS Distributions #define free_type(type, addr)       safe_free(addr, sizeof(type))
200*c54f35caSApple OSS Distributions #define realloc(addr, osize, nsize) realloc_impl(addr, osize, nsize)
201*c54f35caSApple OSS Distributions static inline void *
realloc_impl(void * addr,size_t osize,size_t nsize)202*c54f35caSApple OSS Distributions realloc_impl(void *addr, size_t osize, size_t nsize)
203*c54f35caSApple OSS Distributions {
204*c54f35caSApple OSS Distributions 	void * nmem;
205*c54f35caSApple OSS Distributions 
206*c54f35caSApple OSS Distributions 	if (!addr) {
207*c54f35caSApple OSS Distributions 		return malloc(nsize);
208*c54f35caSApple OSS Distributions 	}
209*c54f35caSApple OSS Distributions 	if (nsize == osize) {
210*c54f35caSApple OSS Distributions 		return addr;
211*c54f35caSApple OSS Distributions 	}
212*c54f35caSApple OSS Distributions 	nmem = (realloc)(addr, nsize);
213*c54f35caSApple OSS Distributions 	if (nmem && nsize > osize) {
214*c54f35caSApple OSS Distributions 		bzero((uint8_t *)nmem + osize, nsize - osize);
215*c54f35caSApple OSS Distributions 	}
216*c54f35caSApple OSS Distributions 
217*c54f35caSApple OSS Distributions 	return nmem;
218*c54f35caSApple OSS Distributions }
219*c54f35caSApple OSS Distributions #endif /* KERNEL */
220*c54f35caSApple OSS Distributions 
221*c54f35caSApple OSS Distributions %}
222*c54f35caSApple OSS Distributions %token ARRAY
223*c54f35caSApple OSS Distributions %token BOOLEAN
224*c54f35caSApple OSS Distributions %token DATA
225*c54f35caSApple OSS Distributions %token DICTIONARY
226*c54f35caSApple OSS Distributions %token IDREF
227*c54f35caSApple OSS Distributions %token KEY
228*c54f35caSApple OSS Distributions %token NUMBER
229*c54f35caSApple OSS Distributions %token SET
230*c54f35caSApple OSS Distributions %token STRING
231*c54f35caSApple OSS Distributions %token SYNTAX_ERROR
232*c54f35caSApple OSS Distributions %% /* Grammar rules and actions follow */
233*c54f35caSApple OSS Distributions 
234*c54f35caSApple OSS Distributions input:	  /* empty */		{ yyerror("unexpected end of buffer");
235*c54f35caSApple OSS Distributions 				  YYERROR;
236*c54f35caSApple OSS Distributions 				}
237*c54f35caSApple OSS Distributions 	| object		{ STATE->parsedObject = $1->object;
238*c54f35caSApple OSS Distributions 				  $1->object = 0;
239*c54f35caSApple OSS Distributions 				  freeObject(STATE, $1);
240*c54f35caSApple OSS Distributions 				  YYACCEPT;
241*c54f35caSApple OSS Distributions 				}
242*c54f35caSApple OSS Distributions 	| SYNTAX_ERROR		{ yyerror("syntax error");
243*c54f35caSApple OSS Distributions 				  YYERROR;
244*c54f35caSApple OSS Distributions 				}
245*c54f35caSApple OSS Distributions 	;
246*c54f35caSApple OSS Distributions 
247*c54f35caSApple OSS Distributions object:	  dict			{ $$ = buildDictionary(STATE, $1);
248*c54f35caSApple OSS Distributions 
249*c54f35caSApple OSS Distributions 				  if (!yyval->object) {
250*c54f35caSApple OSS Distributions 				    yyerror("buildDictionary");
251*c54f35caSApple OSS Distributions 				    YYERROR;
252*c54f35caSApple OSS Distributions 				  }
253*c54f35caSApple OSS Distributions 				  STATE->parsedObjectCount++;
254*c54f35caSApple OSS Distributions 				  if (STATE->parsedObjectCount > MAX_OBJECTS) {
255*c54f35caSApple OSS Distributions 				    yyerror("maximum object count");
256*c54f35caSApple OSS Distributions 				    YYERROR;
257*c54f35caSApple OSS Distributions 				  }
258*c54f35caSApple OSS Distributions 				}
259*c54f35caSApple OSS Distributions 	| array			{ $$ = buildArray(STATE, $1);
260*c54f35caSApple OSS Distributions 
261*c54f35caSApple OSS Distributions 				  if (!yyval->object) {
262*c54f35caSApple OSS Distributions 				    yyerror("buildArray");
263*c54f35caSApple OSS Distributions 				    YYERROR;
264*c54f35caSApple OSS Distributions 				  }
265*c54f35caSApple OSS Distributions 				  STATE->parsedObjectCount++;
266*c54f35caSApple OSS Distributions 				  if (STATE->parsedObjectCount > MAX_OBJECTS) {
267*c54f35caSApple OSS Distributions 				    yyerror("maximum object count");
268*c54f35caSApple OSS Distributions 				    YYERROR;
269*c54f35caSApple OSS Distributions 				  }
270*c54f35caSApple OSS Distributions 				}
271*c54f35caSApple OSS Distributions 	| set			{ $$ = buildSet(STATE, $1);
272*c54f35caSApple OSS Distributions 
273*c54f35caSApple OSS Distributions 				  if (!yyval->object) {
274*c54f35caSApple OSS Distributions 				    yyerror("buildSet");
275*c54f35caSApple OSS Distributions 				    YYERROR;
276*c54f35caSApple OSS Distributions 				  }
277*c54f35caSApple OSS Distributions 				  STATE->parsedObjectCount++;
278*c54f35caSApple OSS Distributions 				  if (STATE->parsedObjectCount > MAX_OBJECTS) {
279*c54f35caSApple OSS Distributions 				    yyerror("maximum object count");
280*c54f35caSApple OSS Distributions 				    YYERROR;
281*c54f35caSApple OSS Distributions 				  }
282*c54f35caSApple OSS Distributions 				}
283*c54f35caSApple OSS Distributions 	| string		{ $$ = buildString(STATE, $1);
284*c54f35caSApple OSS Distributions 
285*c54f35caSApple OSS Distributions 				  if (!yyval->object) {
286*c54f35caSApple OSS Distributions 				    yyerror("buildString");
287*c54f35caSApple OSS Distributions 				    YYERROR;
288*c54f35caSApple OSS Distributions 				  }
289*c54f35caSApple OSS Distributions 				  STATE->parsedObjectCount++;
290*c54f35caSApple OSS Distributions 				  if (STATE->parsedObjectCount > MAX_OBJECTS) {
291*c54f35caSApple OSS Distributions 				    yyerror("maximum object count");
292*c54f35caSApple OSS Distributions 				    YYERROR;
293*c54f35caSApple OSS Distributions 				  }
294*c54f35caSApple OSS Distributions 				}
295*c54f35caSApple OSS Distributions 	| data			{ $$ = buildData(STATE, $1);
296*c54f35caSApple OSS Distributions 
297*c54f35caSApple OSS Distributions 				  if (!yyval->object) {
298*c54f35caSApple OSS Distributions 				    yyerror("buildData");
299*c54f35caSApple OSS Distributions 				    YYERROR;
300*c54f35caSApple OSS Distributions 				  }
301*c54f35caSApple OSS Distributions 				  STATE->parsedObjectCount++;
302*c54f35caSApple OSS Distributions 				  if (STATE->parsedObjectCount > MAX_OBJECTS) {
303*c54f35caSApple OSS Distributions 				    yyerror("maximum object count");
304*c54f35caSApple OSS Distributions 				    YYERROR;
305*c54f35caSApple OSS Distributions 				  }
306*c54f35caSApple OSS Distributions 				}
307*c54f35caSApple OSS Distributions 	| number		{ $$ = buildNumber(STATE, $1);
308*c54f35caSApple OSS Distributions 
309*c54f35caSApple OSS Distributions 				  if (!yyval->object) {
310*c54f35caSApple OSS Distributions 				    yyerror("buildNumber");
311*c54f35caSApple OSS Distributions 				    YYERROR;
312*c54f35caSApple OSS Distributions 				  }
313*c54f35caSApple OSS Distributions 				  STATE->parsedObjectCount++;
314*c54f35caSApple OSS Distributions 				  if (STATE->parsedObjectCount > MAX_OBJECTS) {
315*c54f35caSApple OSS Distributions 				    yyerror("maximum object count");
316*c54f35caSApple OSS Distributions 				    YYERROR;
317*c54f35caSApple OSS Distributions 				  }
318*c54f35caSApple OSS Distributions 				}
319*c54f35caSApple OSS Distributions 	| boolean		{ $$ = buildBoolean(STATE, $1);
320*c54f35caSApple OSS Distributions 
321*c54f35caSApple OSS Distributions 				  if (!yyval->object) {
322*c54f35caSApple OSS Distributions 				    yyerror("buildBoolean");
323*c54f35caSApple OSS Distributions 				    YYERROR;
324*c54f35caSApple OSS Distributions 				  }
325*c54f35caSApple OSS Distributions 				  STATE->parsedObjectCount++;
326*c54f35caSApple OSS Distributions 				  if (STATE->parsedObjectCount > MAX_OBJECTS) {
327*c54f35caSApple OSS Distributions 				    yyerror("maximum object count");
328*c54f35caSApple OSS Distributions 				    YYERROR;
329*c54f35caSApple OSS Distributions 				  }
330*c54f35caSApple OSS Distributions 				}
331*c54f35caSApple OSS Distributions 	| idref			{ $$ = retrieveObject(STATE, $1->idref);
332*c54f35caSApple OSS Distributions 				  if ($$) {
333*c54f35caSApple OSS Distributions 				    STATE->retrievedObjectCount++;
334*c54f35caSApple OSS Distributions 				    $$->object->retain();
335*c54f35caSApple OSS Distributions 				    if (STATE->retrievedObjectCount > MAX_REFED_OBJECTS) {
336*c54f35caSApple OSS Distributions 				      yyerror("maximum object reference count");
337*c54f35caSApple OSS Distributions 				      YYERROR;
338*c54f35caSApple OSS Distributions 				    }
339*c54f35caSApple OSS Distributions 				  } else {
340*c54f35caSApple OSS Distributions 				    yyerror("forward reference detected");
341*c54f35caSApple OSS Distributions 				    YYERROR;
342*c54f35caSApple OSS Distributions 				  }
343*c54f35caSApple OSS Distributions 				  freeObject(STATE, $1);
344*c54f35caSApple OSS Distributions 
345*c54f35caSApple OSS Distributions 				  STATE->parsedObjectCount++;
346*c54f35caSApple OSS Distributions 				  if (STATE->parsedObjectCount > MAX_OBJECTS) {
347*c54f35caSApple OSS Distributions 				    yyerror("maximum object count");
348*c54f35caSApple OSS Distributions 				    YYERROR;
349*c54f35caSApple OSS Distributions 				  }
350*c54f35caSApple OSS Distributions 				}
351*c54f35caSApple OSS Distributions 	;
352*c54f35caSApple OSS Distributions 
353*c54f35caSApple OSS Distributions //------------------------------------------------------------------------------
354*c54f35caSApple OSS Distributions 
355*c54f35caSApple OSS Distributions dict:	  '{' '}'		{ $$ = $1;
356*c54f35caSApple OSS Distributions 				  $$->elements = NULL;
357*c54f35caSApple OSS Distributions 				}
358*c54f35caSApple OSS Distributions 	| '{' pairs '}'		{ $$ = $1;
359*c54f35caSApple OSS Distributions 				  $$->elements = $2;
360*c54f35caSApple OSS Distributions 				}
361*c54f35caSApple OSS Distributions 	| DICTIONARY
362*c54f35caSApple OSS Distributions 	;
363*c54f35caSApple OSS Distributions 
364*c54f35caSApple OSS Distributions pairs:	  pair
365*c54f35caSApple OSS Distributions 	| pairs pair		{ $$ = $2;
366*c54f35caSApple OSS Distributions 				  $$->next = $1;
367*c54f35caSApple OSS Distributions 
368*c54f35caSApple OSS Distributions 				  object_t *o;
369*c54f35caSApple OSS Distributions 				  o = $$->next;
370*c54f35caSApple OSS Distributions 				  while (o) {
371*c54f35caSApple OSS Distributions 				    if (o->key == $$->key) {
372*c54f35caSApple OSS Distributions 				      yyerror("duplicate dictionary key");
373*c54f35caSApple OSS Distributions 				      YYERROR;
374*c54f35caSApple OSS Distributions 				    }
375*c54f35caSApple OSS Distributions 				    o = o->next;
376*c54f35caSApple OSS Distributions 				  }
377*c54f35caSApple OSS Distributions 				}
378*c54f35caSApple OSS Distributions 	;
379*c54f35caSApple OSS Distributions 
380*c54f35caSApple OSS Distributions pair:	  key object		{ $$ = $1;
381*c54f35caSApple OSS Distributions 				  $$->key = (OSSymbol *)$$->object;
382*c54f35caSApple OSS Distributions 				  $$->object = $2->object;
383*c54f35caSApple OSS Distributions 				  $$->next = NULL;
384*c54f35caSApple OSS Distributions 				  $2->object = 0;
385*c54f35caSApple OSS Distributions 				  freeObject(STATE, $2);
386*c54f35caSApple OSS Distributions 				}
387*c54f35caSApple OSS Distributions 	;
388*c54f35caSApple OSS Distributions 
389*c54f35caSApple OSS Distributions key:	  KEY			{ $$ = buildSymbol(STATE, $1);
390*c54f35caSApple OSS Distributions 
391*c54f35caSApple OSS Distributions //				  STATE->parsedObjectCount++;
392*c54f35caSApple OSS Distributions //				  if (STATE->parsedObjectCount > MAX_OBJECTS) {
393*c54f35caSApple OSS Distributions //				    yyerror("maximum object count");
394*c54f35caSApple OSS Distributions //				    YYERROR;
395*c54f35caSApple OSS Distributions //				  }
396*c54f35caSApple OSS Distributions 				}
397*c54f35caSApple OSS Distributions 	;
398*c54f35caSApple OSS Distributions 
399*c54f35caSApple OSS Distributions //------------------------------------------------------------------------------
400*c54f35caSApple OSS Distributions 
401*c54f35caSApple OSS Distributions array:	  '(' ')'		{ $$ = $1;
402*c54f35caSApple OSS Distributions 				  $$->elements = NULL;
403*c54f35caSApple OSS Distributions 				}
404*c54f35caSApple OSS Distributions 	| '(' elements ')'	{ $$ = $1;
405*c54f35caSApple OSS Distributions 				  $$->elements = $2;
406*c54f35caSApple OSS Distributions 				}
407*c54f35caSApple OSS Distributions 	| ARRAY
408*c54f35caSApple OSS Distributions 	;
409*c54f35caSApple OSS Distributions 
410*c54f35caSApple OSS Distributions set:	  '[' ']'		{ $$ = $1;
411*c54f35caSApple OSS Distributions 				  $$->elements = NULL;
412*c54f35caSApple OSS Distributions 				}
413*c54f35caSApple OSS Distributions 	| '[' elements ']'	{ $$ = $1;
414*c54f35caSApple OSS Distributions 				  $$->elements = $2;
415*c54f35caSApple OSS Distributions 				}
416*c54f35caSApple OSS Distributions 	| SET
417*c54f35caSApple OSS Distributions 	;
418*c54f35caSApple OSS Distributions 
419*c54f35caSApple OSS Distributions elements: object		{ $$ = $1;
420*c54f35caSApple OSS Distributions 				  $$->next = NULL;
421*c54f35caSApple OSS Distributions 				}
422*c54f35caSApple OSS Distributions 	| elements object	{ $$ = $2;
423*c54f35caSApple OSS Distributions 				  $$->next = $1;
424*c54f35caSApple OSS Distributions 				}
425*c54f35caSApple OSS Distributions 	;
426*c54f35caSApple OSS Distributions 
427*c54f35caSApple OSS Distributions //------------------------------------------------------------------------------
428*c54f35caSApple OSS Distributions 
429*c54f35caSApple OSS Distributions boolean:  BOOLEAN
430*c54f35caSApple OSS Distributions 	;
431*c54f35caSApple OSS Distributions 
432*c54f35caSApple OSS Distributions data:	  DATA
433*c54f35caSApple OSS Distributions 	;
434*c54f35caSApple OSS Distributions 
435*c54f35caSApple OSS Distributions idref:	  IDREF
436*c54f35caSApple OSS Distributions 	;
437*c54f35caSApple OSS Distributions 
438*c54f35caSApple OSS Distributions number:	  NUMBER
439*c54f35caSApple OSS Distributions 	;
440*c54f35caSApple OSS Distributions 
441*c54f35caSApple OSS Distributions string:	  STRING
442*c54f35caSApple OSS Distributions 	;
443*c54f35caSApple OSS Distributions 
444*c54f35caSApple OSS Distributions %%
445*c54f35caSApple OSS Distributions 
446*c54f35caSApple OSS Distributions int
447*c54f35caSApple OSS Distributions OSUnserializeerror(parser_state_t * state, const char *s)  /* Called by yyparse on errors */
448*c54f35caSApple OSS Distributions {
449*c54f35caSApple OSS Distributions 	if (state->errorString) {
450*c54f35caSApple OSS Distributions 		char tempString[128];
451*c54f35caSApple OSS Distributions 		snprintf(tempString, 128, "OSUnserializeXML: %s near line %d\n", s, state->lineNumber);
452*c54f35caSApple OSS Distributions 		*(state->errorString) = OSString::withCString(tempString);
453*c54f35caSApple OSS Distributions 	}
454*c54f35caSApple OSS Distributions 
455*c54f35caSApple OSS Distributions 	return 0;
456*c54f35caSApple OSS Distributions }
457*c54f35caSApple OSS Distributions 
458*c54f35caSApple OSS Distributions #define TAG_MAX_LENGTH          32
459*c54f35caSApple OSS Distributions #define TAG_MAX_ATTRIBUTES      32
460*c54f35caSApple OSS Distributions #define TAG_BAD                 0
461*c54f35caSApple OSS Distributions #define TAG_START               1
462*c54f35caSApple OSS Distributions #define TAG_END                 2
463*c54f35caSApple OSS Distributions #define TAG_EMPTY               3
464*c54f35caSApple OSS Distributions #define TAG_IGNORE              4
465*c54f35caSApple OSS Distributions 
466*c54f35caSApple OSS Distributions #define currentChar()   (state->parseBuffer[state->parseBufferIndex])
467*c54f35caSApple OSS Distributions #define nextChar()      (state->parseBuffer[++state->parseBufferIndex])
468*c54f35caSApple OSS Distributions #define prevChar()      (state->parseBuffer[state->parseBufferIndex - 1])
469*c54f35caSApple OSS Distributions 
470*c54f35caSApple OSS Distributions #define isSpace(c)      ((c) == ' ' || (c) == '\t')
471*c54f35caSApple OSS Distributions #define isAlpha(c)      (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
472*c54f35caSApple OSS Distributions #define isDigit(c)      ((c) >= '0' && (c) <= '9')
473*c54f35caSApple OSS Distributions #define isAlphaDigit(c) ((c) >= 'a' && (c) <= 'f')
474*c54f35caSApple OSS Distributions #define isHexDigit(c)   (isDigit(c) || isAlphaDigit(c))
475*c54f35caSApple OSS Distributions #define isAlphaNumeric(c) (isAlpha(c) || isDigit(c) || ((c) == '-'))
476*c54f35caSApple OSS Distributions 
477*c54f35caSApple OSS Distributions static int
getTag(parser_state_t * state,char tag[TAG_MAX_LENGTH],int * attributeCount,char attributes[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH],char values[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH])478*c54f35caSApple OSS Distributions getTag(parser_state_t *state,
479*c54f35caSApple OSS Distributions     char tag[TAG_MAX_LENGTH],
480*c54f35caSApple OSS Distributions     int *attributeCount,
481*c54f35caSApple OSS Distributions     char attributes[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH],
482*c54f35caSApple OSS Distributions     char values[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH] )
483*c54f35caSApple OSS Distributions {
484*c54f35caSApple OSS Distributions 	int length = 0;
485*c54f35caSApple OSS Distributions 	int c = currentChar();
486*c54f35caSApple OSS Distributions 	int tagType = TAG_START;
487*c54f35caSApple OSS Distributions 
488*c54f35caSApple OSS Distributions 	*attributeCount = 0;
489*c54f35caSApple OSS Distributions 
490*c54f35caSApple OSS Distributions 	if (c != '<') {
491*c54f35caSApple OSS Distributions 		return TAG_BAD;
492*c54f35caSApple OSS Distributions 	}
493*c54f35caSApple OSS Distributions 	c = nextChar();         // skip '<'
494*c54f35caSApple OSS Distributions 
495*c54f35caSApple OSS Distributions 
496*c54f35caSApple OSS Distributions 	// <!TAG   declarations     >
497*c54f35caSApple OSS Distributions 	// <!--     comments      -->
498*c54f35caSApple OSS Distributions 	if (c == '!') {
499*c54f35caSApple OSS Distributions 		c = nextChar();
500*c54f35caSApple OSS Distributions 		bool isComment = (c == '-') && ((c = nextChar()) != 0) && (c == '-');
501*c54f35caSApple OSS Distributions 		if (!isComment && !isAlpha(c)) {
502*c54f35caSApple OSS Distributions 			return TAG_BAD;                      // <!1, <!-A, <!eos
503*c54f35caSApple OSS Distributions 		}
504*c54f35caSApple OSS Distributions 		while (c && (c = nextChar()) != 0) {
505*c54f35caSApple OSS Distributions 			if (c == '\n') {
506*c54f35caSApple OSS Distributions 				state->lineNumber++;
507*c54f35caSApple OSS Distributions 			}
508*c54f35caSApple OSS Distributions 			if (isComment) {
509*c54f35caSApple OSS Distributions 				if (c != '-') {
510*c54f35caSApple OSS Distributions 					continue;
511*c54f35caSApple OSS Distributions 				}
512*c54f35caSApple OSS Distributions 				c = nextChar();
513*c54f35caSApple OSS Distributions 				if (c != '-') {
514*c54f35caSApple OSS Distributions 					continue;
515*c54f35caSApple OSS Distributions 				}
516*c54f35caSApple OSS Distributions 				c = nextChar();
517*c54f35caSApple OSS Distributions 			}
518*c54f35caSApple OSS Distributions 			if (c == '>') {
519*c54f35caSApple OSS Distributions 				(void)nextChar();
520*c54f35caSApple OSS Distributions 				return TAG_IGNORE;
521*c54f35caSApple OSS Distributions 			}
522*c54f35caSApple OSS Distributions 			if (isComment) {
523*c54f35caSApple OSS Distributions 				break;
524*c54f35caSApple OSS Distributions 			}
525*c54f35caSApple OSS Distributions 		}
526*c54f35caSApple OSS Distributions 		return TAG_BAD;
527*c54f35caSApple OSS Distributions 	} else
528*c54f35caSApple OSS Distributions 	// <? Processing Instructions  ?>
529*c54f35caSApple OSS Distributions 	if (c == '?') {
530*c54f35caSApple OSS Distributions 		while ((c = nextChar()) != 0) {
531*c54f35caSApple OSS Distributions 			if (c == '\n') {
532*c54f35caSApple OSS Distributions 				state->lineNumber++;
533*c54f35caSApple OSS Distributions 			}
534*c54f35caSApple OSS Distributions 			if (c != '?') {
535*c54f35caSApple OSS Distributions 				continue;
536*c54f35caSApple OSS Distributions 			}
537*c54f35caSApple OSS Distributions 			c = nextChar();
538*c54f35caSApple OSS Distributions 			if (!c) {
539*c54f35caSApple OSS Distributions 				return TAG_IGNORE;
540*c54f35caSApple OSS Distributions 			}
541*c54f35caSApple OSS Distributions 			if (c == '>') {
542*c54f35caSApple OSS Distributions 				(void)nextChar();
543*c54f35caSApple OSS Distributions 				return TAG_IGNORE;
544*c54f35caSApple OSS Distributions 			}
545*c54f35caSApple OSS Distributions 		}
546*c54f35caSApple OSS Distributions 		return TAG_BAD;
547*c54f35caSApple OSS Distributions 	} else
548*c54f35caSApple OSS Distributions 	// </ end tag >
549*c54f35caSApple OSS Distributions 	if (c == '/') {
550*c54f35caSApple OSS Distributions 		c = nextChar();         // skip '/'
551*c54f35caSApple OSS Distributions 		tagType = TAG_END;
552*c54f35caSApple OSS Distributions 	}
553*c54f35caSApple OSS Distributions 	if (!isAlpha(c)) {
554*c54f35caSApple OSS Distributions 		return TAG_BAD;
555*c54f35caSApple OSS Distributions 	}
556*c54f35caSApple OSS Distributions 
557*c54f35caSApple OSS Distributions 	/* find end of tag while copying it */
558*c54f35caSApple OSS Distributions 	while (isAlphaNumeric(c)) {
559*c54f35caSApple OSS Distributions 		tag[length++] = c;
560*c54f35caSApple OSS Distributions 		c = nextChar();
561*c54f35caSApple OSS Distributions 		if (length >= (TAG_MAX_LENGTH - 1)) {
562*c54f35caSApple OSS Distributions 			return TAG_BAD;
563*c54f35caSApple OSS Distributions 		}
564*c54f35caSApple OSS Distributions 	}
565*c54f35caSApple OSS Distributions 
566*c54f35caSApple OSS Distributions 	tag[length] = 0;
567*c54f35caSApple OSS Distributions 
568*c54f35caSApple OSS Distributions //	printf("tag %s, type %d\n", tag, tagType);
569*c54f35caSApple OSS Distributions 
570*c54f35caSApple OSS Distributions 	// look for attributes of the form attribute = "value" ...
571*c54f35caSApple OSS Distributions 	while ((c != '>') && (c != '/')) {
572*c54f35caSApple OSS Distributions 		while (isSpace(c)) {
573*c54f35caSApple OSS Distributions 			c = nextChar();
574*c54f35caSApple OSS Distributions 		}
575*c54f35caSApple OSS Distributions 
576*c54f35caSApple OSS Distributions 		length = 0;
577*c54f35caSApple OSS Distributions 		while (isAlphaNumeric(c)) {
578*c54f35caSApple OSS Distributions 			attributes[*attributeCount][length++] = c;
579*c54f35caSApple OSS Distributions 			if (length >= (TAG_MAX_LENGTH - 1)) {
580*c54f35caSApple OSS Distributions 				return TAG_BAD;
581*c54f35caSApple OSS Distributions 			}
582*c54f35caSApple OSS Distributions 			c = nextChar();
583*c54f35caSApple OSS Distributions 		}
584*c54f35caSApple OSS Distributions 		attributes[*attributeCount][length] = 0;
585*c54f35caSApple OSS Distributions 
586*c54f35caSApple OSS Distributions 		while (isSpace(c)) {
587*c54f35caSApple OSS Distributions 			c = nextChar();
588*c54f35caSApple OSS Distributions 		}
589*c54f35caSApple OSS Distributions 
590*c54f35caSApple OSS Distributions 		if (c != '=') {
591*c54f35caSApple OSS Distributions 			return TAG_BAD;
592*c54f35caSApple OSS Distributions 		}
593*c54f35caSApple OSS Distributions 		c = nextChar();
594*c54f35caSApple OSS Distributions 
595*c54f35caSApple OSS Distributions 		while (isSpace(c)) {
596*c54f35caSApple OSS Distributions 			c = nextChar();
597*c54f35caSApple OSS Distributions 		}
598*c54f35caSApple OSS Distributions 
599*c54f35caSApple OSS Distributions 		if (c != '"') {
600*c54f35caSApple OSS Distributions 			return TAG_BAD;
601*c54f35caSApple OSS Distributions 		}
602*c54f35caSApple OSS Distributions 		c = nextChar();
603*c54f35caSApple OSS Distributions 		length = 0;
604*c54f35caSApple OSS Distributions 		while (c != '"') {
605*c54f35caSApple OSS Distributions 			values[*attributeCount][length++] = c;
606*c54f35caSApple OSS Distributions 			if (length >= (TAG_MAX_LENGTH - 1)) {
607*c54f35caSApple OSS Distributions 				return TAG_BAD;
608*c54f35caSApple OSS Distributions 			}
609*c54f35caSApple OSS Distributions 			c = nextChar();
610*c54f35caSApple OSS Distributions 			if (!c) {
611*c54f35caSApple OSS Distributions 				return TAG_BAD;
612*c54f35caSApple OSS Distributions 			}
613*c54f35caSApple OSS Distributions 		}
614*c54f35caSApple OSS Distributions 		values[*attributeCount][length] = 0;
615*c54f35caSApple OSS Distributions 
616*c54f35caSApple OSS Distributions 		c = nextChar(); // skip closing quote
617*c54f35caSApple OSS Distributions 
618*c54f35caSApple OSS Distributions //		printf("	attribute '%s' = '%s', nextchar = '%c'\n",
619*c54f35caSApple OSS Distributions //		       attributes[*attributeCount], values[*attributeCount], c);
620*c54f35caSApple OSS Distributions 
621*c54f35caSApple OSS Distributions 		(*attributeCount)++;
622*c54f35caSApple OSS Distributions 		if (*attributeCount >= TAG_MAX_ATTRIBUTES) {
623*c54f35caSApple OSS Distributions 			return TAG_BAD;
624*c54f35caSApple OSS Distributions 		}
625*c54f35caSApple OSS Distributions 	}
626*c54f35caSApple OSS Distributions 
627*c54f35caSApple OSS Distributions 	if (c == '/') {
628*c54f35caSApple OSS Distributions 		c = nextChar();         // skip '/'
629*c54f35caSApple OSS Distributions 		tagType = TAG_EMPTY;
630*c54f35caSApple OSS Distributions 	}
631*c54f35caSApple OSS Distributions 	if (c != '>') {
632*c54f35caSApple OSS Distributions 		return TAG_BAD;
633*c54f35caSApple OSS Distributions 	}
634*c54f35caSApple OSS Distributions 	c = nextChar();         // skip '>'
635*c54f35caSApple OSS Distributions 
636*c54f35caSApple OSS Distributions 	return tagType;
637*c54f35caSApple OSS Distributions }
638*c54f35caSApple OSS Distributions 
639*c54f35caSApple OSS Distributions static char *
getString(parser_state_t * state,int * alloc_lengthp)640*c54f35caSApple OSS Distributions getString(parser_state_t *state, int *alloc_lengthp)
641*c54f35caSApple OSS Distributions {
642*c54f35caSApple OSS Distributions 	int c = currentChar();
643*c54f35caSApple OSS Distributions 	int start, length, i, j;
644*c54f35caSApple OSS Distributions 	char * tempString;
645*c54f35caSApple OSS Distributions 
646*c54f35caSApple OSS Distributions 	start = state->parseBufferIndex;
647*c54f35caSApple OSS Distributions 	/* find end of string */
648*c54f35caSApple OSS Distributions 
649*c54f35caSApple OSS Distributions 	while (c != 0) {
650*c54f35caSApple OSS Distributions 		if (c == '\n') {
651*c54f35caSApple OSS Distributions 			state->lineNumber++;
652*c54f35caSApple OSS Distributions 		}
653*c54f35caSApple OSS Distributions 		if (c == '<') {
654*c54f35caSApple OSS Distributions 			break;
655*c54f35caSApple OSS Distributions 		}
656*c54f35caSApple OSS Distributions 		c = nextChar();
657*c54f35caSApple OSS Distributions 	}
658*c54f35caSApple OSS Distributions 
659*c54f35caSApple OSS Distributions 	if (c != '<') {
660*c54f35caSApple OSS Distributions 		return 0;
661*c54f35caSApple OSS Distributions 	}
662*c54f35caSApple OSS Distributions 
663*c54f35caSApple OSS Distributions 	length = state->parseBufferIndex - start;
664*c54f35caSApple OSS Distributions 
665*c54f35caSApple OSS Distributions 	/* copy to null terminated buffer */
666*c54f35caSApple OSS Distributions 	tempString = (char *)malloc(length + 1);
667*c54f35caSApple OSS Distributions 	if (tempString == NULL) {
668*c54f35caSApple OSS Distributions 		printf("OSUnserializeXML: can't alloc temp memory\n");
669*c54f35caSApple OSS Distributions 		goto error;
670*c54f35caSApple OSS Distributions 	}
671*c54f35caSApple OSS Distributions 	if (alloc_lengthp) {
672*c54f35caSApple OSS Distributions 		*alloc_lengthp = length + 1;
673*c54f35caSApple OSS Distributions 	}
674*c54f35caSApple OSS Distributions 
675*c54f35caSApple OSS Distributions 	// copy out string in tempString
676*c54f35caSApple OSS Distributions 	// "&amp;" -> '&', "&lt;" -> '<', "&gt;" -> '>'
677*c54f35caSApple OSS Distributions 
678*c54f35caSApple OSS Distributions 	i = j = 0;
679*c54f35caSApple OSS Distributions 	while (i < length) {
680*c54f35caSApple OSS Distributions 		c = state->parseBuffer[start + i++];
681*c54f35caSApple OSS Distributions 		if (c != '&') {
682*c54f35caSApple OSS Distributions 			tempString[j++] = c;
683*c54f35caSApple OSS Distributions 		} else {
684*c54f35caSApple OSS Distributions 			if ((i + 3) > length) {
685*c54f35caSApple OSS Distributions 				goto error;
686*c54f35caSApple OSS Distributions 			}
687*c54f35caSApple OSS Distributions 			c = state->parseBuffer[start + i++];
688*c54f35caSApple OSS Distributions 			if (c == 'l') {
689*c54f35caSApple OSS Distributions 				if (state->parseBuffer[start + i++] != 't') {
690*c54f35caSApple OSS Distributions 					goto error;
691*c54f35caSApple OSS Distributions 				}
692*c54f35caSApple OSS Distributions 				if (state->parseBuffer[start + i++] != ';') {
693*c54f35caSApple OSS Distributions 					goto error;
694*c54f35caSApple OSS Distributions 				}
695*c54f35caSApple OSS Distributions 				tempString[j++] = '<';
696*c54f35caSApple OSS Distributions 				continue;
697*c54f35caSApple OSS Distributions 			}
698*c54f35caSApple OSS Distributions 			if (c == 'g') {
699*c54f35caSApple OSS Distributions 				if (state->parseBuffer[start + i++] != 't') {
700*c54f35caSApple OSS Distributions 					goto error;
701*c54f35caSApple OSS Distributions 				}
702*c54f35caSApple OSS Distributions 				if (state->parseBuffer[start + i++] != ';') {
703*c54f35caSApple OSS Distributions 					goto error;
704*c54f35caSApple OSS Distributions 				}
705*c54f35caSApple OSS Distributions 				tempString[j++] = '>';
706*c54f35caSApple OSS Distributions 				continue;
707*c54f35caSApple OSS Distributions 			}
708*c54f35caSApple OSS Distributions 			if ((i + 3) > length) {
709*c54f35caSApple OSS Distributions 				goto error;
710*c54f35caSApple OSS Distributions 			}
711*c54f35caSApple OSS Distributions 			if (c == 'a') {
712*c54f35caSApple OSS Distributions 				if (state->parseBuffer[start + i++] != 'm') {
713*c54f35caSApple OSS Distributions 					goto error;
714*c54f35caSApple OSS Distributions 				}
715*c54f35caSApple OSS Distributions 				if (state->parseBuffer[start + i++] != 'p') {
716*c54f35caSApple OSS Distributions 					goto error;
717*c54f35caSApple OSS Distributions 				}
718*c54f35caSApple OSS Distributions 				if (state->parseBuffer[start + i++] != ';') {
719*c54f35caSApple OSS Distributions 					goto error;
720*c54f35caSApple OSS Distributions 				}
721*c54f35caSApple OSS Distributions 				tempString[j++] = '&';
722*c54f35caSApple OSS Distributions 				continue;
723*c54f35caSApple OSS Distributions 			}
724*c54f35caSApple OSS Distributions 			goto error;
725*c54f35caSApple OSS Distributions 		}
726*c54f35caSApple OSS Distributions 	}
727*c54f35caSApple OSS Distributions 	tempString[j] = 0;
728*c54f35caSApple OSS Distributions 
729*c54f35caSApple OSS Distributions //	printf("string %s\n", tempString);
730*c54f35caSApple OSS Distributions 
731*c54f35caSApple OSS Distributions 	return tempString;
732*c54f35caSApple OSS Distributions 
733*c54f35caSApple OSS Distributions error:
734*c54f35caSApple OSS Distributions 	if (tempString) {
735*c54f35caSApple OSS Distributions 		safe_free(tempString, length + 1);
736*c54f35caSApple OSS Distributions 		if (alloc_lengthp) {
737*c54f35caSApple OSS Distributions 			*alloc_lengthp = 0;
738*c54f35caSApple OSS Distributions 		}
739*c54f35caSApple OSS Distributions 	}
740*c54f35caSApple OSS Distributions 	return 0;
741*c54f35caSApple OSS Distributions }
742*c54f35caSApple OSS Distributions 
743*c54f35caSApple OSS Distributions static long long
getNumber(parser_state_t * state)744*c54f35caSApple OSS Distributions getNumber(parser_state_t *state)
745*c54f35caSApple OSS Distributions {
746*c54f35caSApple OSS Distributions 	unsigned long long n = 0;
747*c54f35caSApple OSS Distributions 	int base = 10;
748*c54f35caSApple OSS Distributions 	bool negate = false;
749*c54f35caSApple OSS Distributions 	int c = currentChar();
750*c54f35caSApple OSS Distributions 
751*c54f35caSApple OSS Distributions 	if (c == '0') {
752*c54f35caSApple OSS Distributions 		c = nextChar();
753*c54f35caSApple OSS Distributions 		if (c == 'x') {
754*c54f35caSApple OSS Distributions 			base = 16;
755*c54f35caSApple OSS Distributions 			c = nextChar();
756*c54f35caSApple OSS Distributions 		}
757*c54f35caSApple OSS Distributions 	}
758*c54f35caSApple OSS Distributions 	if (base == 10) {
759*c54f35caSApple OSS Distributions 		if (c == '-') {
760*c54f35caSApple OSS Distributions 			negate = true;
761*c54f35caSApple OSS Distributions 			c = nextChar();
762*c54f35caSApple OSS Distributions 		}
763*c54f35caSApple OSS Distributions 		while (isDigit(c)) {
764*c54f35caSApple OSS Distributions 			n = (n * base + c - '0');
765*c54f35caSApple OSS Distributions 			c = nextChar();
766*c54f35caSApple OSS Distributions 		}
767*c54f35caSApple OSS Distributions 		if (negate) {
768*c54f35caSApple OSS Distributions 			n = (unsigned long long)((long long)n * (long long)-1);
769*c54f35caSApple OSS Distributions 		}
770*c54f35caSApple OSS Distributions 	} else {
771*c54f35caSApple OSS Distributions 		while (isHexDigit(c)) {
772*c54f35caSApple OSS Distributions 			if (isDigit(c)) {
773*c54f35caSApple OSS Distributions 				n = (n * base + c - '0');
774*c54f35caSApple OSS Distributions 			} else {
775*c54f35caSApple OSS Distributions 				n = (n * base + 0xa + c - 'a');
776*c54f35caSApple OSS Distributions 			}
777*c54f35caSApple OSS Distributions 			c = nextChar();
778*c54f35caSApple OSS Distributions 		}
779*c54f35caSApple OSS Distributions 	}
780*c54f35caSApple OSS Distributions //	printf("number 0x%x\n", (unsigned long)n);
781*c54f35caSApple OSS Distributions 	return n;
782*c54f35caSApple OSS Distributions }
783*c54f35caSApple OSS Distributions 
784*c54f35caSApple OSS Distributions // taken from CFXMLParsing/CFPropertyList.c
785*c54f35caSApple OSS Distributions 
786*c54f35caSApple OSS Distributions static const signed char __CFPLDataDecodeTable[128] = {
787*c54f35caSApple OSS Distributions 	/* 000 */ -1, -1, -1, -1, -1, -1, -1, -1,
788*c54f35caSApple OSS Distributions 	/* 010 */ -1, -1, -1, -1, -1, -1, -1, -1,
789*c54f35caSApple OSS Distributions 	/* 020 */ -1, -1, -1, -1, -1, -1, -1, -1,
790*c54f35caSApple OSS Distributions 	/* 030 */ -1, -1, -1, -1, -1, -1, -1, -1,
791*c54f35caSApple OSS Distributions 	/* ' ' */ -1, -1, -1, -1, -1, -1, -1, -1,
792*c54f35caSApple OSS Distributions 	/* '(' */ -1, -1, -1, 62, -1, -1, -1, 63,
793*c54f35caSApple OSS Distributions 	/* '0' */ 52, 53, 54, 55, 56, 57, 58, 59,
794*c54f35caSApple OSS Distributions 	/* '8' */ 60, 61, -1, -1, -1, 0, -1, -1,
795*c54f35caSApple OSS Distributions 	/* '@' */ -1, 0, 1, 2, 3, 4, 5, 6,
796*c54f35caSApple OSS Distributions 	/* 'H' */ 7, 8, 9, 10, 11, 12, 13, 14,
797*c54f35caSApple OSS Distributions 	/* 'P' */ 15, 16, 17, 18, 19, 20, 21, 22,
798*c54f35caSApple OSS Distributions 	/* 'X' */ 23, 24, 25, -1, -1, -1, -1, -1,
799*c54f35caSApple OSS Distributions 	/* '`' */ -1, 26, 27, 28, 29, 30, 31, 32,
800*c54f35caSApple OSS Distributions 	/* 'h' */ 33, 34, 35, 36, 37, 38, 39, 40,
801*c54f35caSApple OSS Distributions 	/* 'p' */ 41, 42, 43, 44, 45, 46, 47, 48,
802*c54f35caSApple OSS Distributions 	/* 'x' */ 49, 50, 51, -1, -1, -1, -1, -1
803*c54f35caSApple OSS Distributions };
804*c54f35caSApple OSS Distributions 
805*c54f35caSApple OSS Distributions #define DATA_ALLOC_SIZE 4096
806*c54f35caSApple OSS Distributions 
807*c54f35caSApple OSS Distributions static void *
getCFEncodedData(parser_state_t * state,unsigned int * size)808*c54f35caSApple OSS Distributions getCFEncodedData(parser_state_t *state, unsigned int *size)
809*c54f35caSApple OSS Distributions {
810*c54f35caSApple OSS Distributions 	int numeq = 0, cntr = 0;
811*c54f35caSApple OSS Distributions 	unsigned int acc = 0;
812*c54f35caSApple OSS Distributions 	int tmpbufpos = 0;
813*c54f35caSApple OSS Distributions 	size_t tmpbuflen = DATA_ALLOC_SIZE;
814*c54f35caSApple OSS Distributions 	unsigned char *tmpbuf = (unsigned char *)malloc(tmpbuflen);
815*c54f35caSApple OSS Distributions 
816*c54f35caSApple OSS Distributions 	int c = currentChar();
817*c54f35caSApple OSS Distributions 	*size = 0;
818*c54f35caSApple OSS Distributions 
819*c54f35caSApple OSS Distributions 	while (c != '<') {
820*c54f35caSApple OSS Distributions 		c &= 0x7f;
821*c54f35caSApple OSS Distributions 		if (c == 0) {
822*c54f35caSApple OSS Distributions 			safe_free(tmpbuf, tmpbuflen);
823*c54f35caSApple OSS Distributions 			return 0;
824*c54f35caSApple OSS Distributions 		}
825*c54f35caSApple OSS Distributions 		if (c == '=') {
826*c54f35caSApple OSS Distributions 			numeq++;
827*c54f35caSApple OSS Distributions 		} else {
828*c54f35caSApple OSS Distributions 			numeq = 0;
829*c54f35caSApple OSS Distributions 		}
830*c54f35caSApple OSS Distributions 		if (c == '\n') {
831*c54f35caSApple OSS Distributions 			state->lineNumber++;
832*c54f35caSApple OSS Distributions 		}
833*c54f35caSApple OSS Distributions 		if (__CFPLDataDecodeTable[c] < 0) {
834*c54f35caSApple OSS Distributions 			c = nextChar();
835*c54f35caSApple OSS Distributions 			continue;
836*c54f35caSApple OSS Distributions 		}
837*c54f35caSApple OSS Distributions 		cntr++;
838*c54f35caSApple OSS Distributions 		acc <<= 6;
839*c54f35caSApple OSS Distributions 		acc += __CFPLDataDecodeTable[c];
840*c54f35caSApple OSS Distributions 		if (0 == (cntr & 0x3)) {
841*c54f35caSApple OSS Distributions 			if (tmpbuflen <= tmpbufpos + 2) {
842*c54f35caSApple OSS Distributions 				size_t oldsize = tmpbuflen;
843*c54f35caSApple OSS Distributions 				tmpbuflen *= 2;
844*c54f35caSApple OSS Distributions 				tmpbuf = (unsigned char *)realloc(tmpbuf, oldsize, tmpbuflen);
845*c54f35caSApple OSS Distributions 			}
846*c54f35caSApple OSS Distributions 			tmpbuf[tmpbufpos++] = (acc >> 16) & 0xff;
847*c54f35caSApple OSS Distributions 			if (numeq < 2) {
848*c54f35caSApple OSS Distributions 				tmpbuf[tmpbufpos++] = (acc >> 8) & 0xff;
849*c54f35caSApple OSS Distributions 			}
850*c54f35caSApple OSS Distributions 			if (numeq < 1) {
851*c54f35caSApple OSS Distributions 				tmpbuf[tmpbufpos++] = acc & 0xff;
852*c54f35caSApple OSS Distributions 			}
853*c54f35caSApple OSS Distributions 		}
854*c54f35caSApple OSS Distributions 		c = nextChar();
855*c54f35caSApple OSS Distributions 	}
856*c54f35caSApple OSS Distributions 	*size = tmpbufpos;
857*c54f35caSApple OSS Distributions 	if (*size == 0) {
858*c54f35caSApple OSS Distributions 		safe_free(tmpbuf, tmpbuflen);
859*c54f35caSApple OSS Distributions 		return 0;
860*c54f35caSApple OSS Distributions 	}
861*c54f35caSApple OSS Distributions 	return tmpbuf;
862*c54f35caSApple OSS Distributions }
863*c54f35caSApple OSS Distributions 
864*c54f35caSApple OSS Distributions static void *
getHexData(parser_state_t * state,unsigned int * size)865*c54f35caSApple OSS Distributions getHexData(parser_state_t *state, unsigned int *size)
866*c54f35caSApple OSS Distributions {
867*c54f35caSApple OSS Distributions 	int c;
868*c54f35caSApple OSS Distributions 	unsigned char *d, *start;
869*c54f35caSApple OSS Distributions 
870*c54f35caSApple OSS Distributions 	size_t buflen = DATA_ALLOC_SIZE; // initial buffer size
871*c54f35caSApple OSS Distributions 	start = d = (unsigned char *)malloc(buflen);
872*c54f35caSApple OSS Distributions 	c = currentChar();
873*c54f35caSApple OSS Distributions 
874*c54f35caSApple OSS Distributions 	while (c != '<') {
875*c54f35caSApple OSS Distributions 		if (isSpace(c)) {
876*c54f35caSApple OSS Distributions 			while ((c = nextChar()) != 0 && isSpace(c)) {
877*c54f35caSApple OSS Distributions 			}
878*c54f35caSApple OSS Distributions 		}
879*c54f35caSApple OSS Distributions 		;
880*c54f35caSApple OSS Distributions 		if (c == '\n') {
881*c54f35caSApple OSS Distributions 			state->lineNumber++;
882*c54f35caSApple OSS Distributions 			c = nextChar();
883*c54f35caSApple OSS Distributions 			continue;
884*c54f35caSApple OSS Distributions 		}
885*c54f35caSApple OSS Distributions 
886*c54f35caSApple OSS Distributions 		// get high nibble
887*c54f35caSApple OSS Distributions 		if (isDigit(c)) {
888*c54f35caSApple OSS Distributions 			*d = (c - '0') << 4;
889*c54f35caSApple OSS Distributions 		} else if (isAlphaDigit(c)) {
890*c54f35caSApple OSS Distributions 			*d =  (0xa + (c - 'a')) << 4;
891*c54f35caSApple OSS Distributions 		} else {
892*c54f35caSApple OSS Distributions 			goto error;
893*c54f35caSApple OSS Distributions 		}
894*c54f35caSApple OSS Distributions 
895*c54f35caSApple OSS Distributions 		// get low nibble
896*c54f35caSApple OSS Distributions 		c = nextChar();
897*c54f35caSApple OSS Distributions 		if (isDigit(c)) {
898*c54f35caSApple OSS Distributions 			*d |= c - '0';
899*c54f35caSApple OSS Distributions 		} else if (isAlphaDigit(c)) {
900*c54f35caSApple OSS Distributions 			*d |= 0xa + (c - 'a');
901*c54f35caSApple OSS Distributions 		} else {
902*c54f35caSApple OSS Distributions 			goto error;
903*c54f35caSApple OSS Distributions 		}
904*c54f35caSApple OSS Distributions 
905*c54f35caSApple OSS Distributions 		d++;
906*c54f35caSApple OSS Distributions 		size_t oldsize = d - start;
907*c54f35caSApple OSS Distributions 		if (oldsize >= buflen) {
908*c54f35caSApple OSS Distributions 			assert(oldsize == buflen);
909*c54f35caSApple OSS Distributions 			buflen *= 2;
910*c54f35caSApple OSS Distributions 			start = (unsigned char *)realloc(start, oldsize, buflen);
911*c54f35caSApple OSS Distributions 			d = start + oldsize;
912*c54f35caSApple OSS Distributions 		}
913*c54f35caSApple OSS Distributions 		c = nextChar();
914*c54f35caSApple OSS Distributions 	}
915*c54f35caSApple OSS Distributions 
916*c54f35caSApple OSS Distributions 	*size = d - start;
917*c54f35caSApple OSS Distributions 	return start;
918*c54f35caSApple OSS Distributions 
919*c54f35caSApple OSS Distributions error:
920*c54f35caSApple OSS Distributions 
921*c54f35caSApple OSS Distributions 	*size = 0;
922*c54f35caSApple OSS Distributions 	safe_free(start, buflen);
923*c54f35caSApple OSS Distributions 	return 0;
924*c54f35caSApple OSS Distributions }
925*c54f35caSApple OSS Distributions 
926*c54f35caSApple OSS Distributions static int
yylex(YYSTYPE * lvalp,parser_state_t * state)927*c54f35caSApple OSS Distributions yylex(YYSTYPE *lvalp, parser_state_t *state)
928*c54f35caSApple OSS Distributions {
929*c54f35caSApple OSS Distributions 	int c, i;
930*c54f35caSApple OSS Distributions 	int tagType;
931*c54f35caSApple OSS Distributions 	char tag[TAG_MAX_LENGTH];
932*c54f35caSApple OSS Distributions 	int attributeCount;
933*c54f35caSApple OSS Distributions 	char attributes[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH];
934*c54f35caSApple OSS Distributions 	char values[TAG_MAX_ATTRIBUTES][TAG_MAX_LENGTH];
935*c54f35caSApple OSS Distributions 	object_t *object;
936*c54f35caSApple OSS Distributions 	int alloc_length;
937*c54f35caSApple OSS Distributions top:
938*c54f35caSApple OSS Distributions 	c = currentChar();
939*c54f35caSApple OSS Distributions 
940*c54f35caSApple OSS Distributions 	/* skip white space  */
941*c54f35caSApple OSS Distributions 	if (isSpace(c)) {
942*c54f35caSApple OSS Distributions 		while ((c = nextChar()) != 0 && isSpace(c)) {
943*c54f35caSApple OSS Distributions 		}
944*c54f35caSApple OSS Distributions 	}
945*c54f35caSApple OSS Distributions 	;
946*c54f35caSApple OSS Distributions 
947*c54f35caSApple OSS Distributions 	/* keep track of line number, don't return \n's */
948*c54f35caSApple OSS Distributions 	if (c == '\n') {
949*c54f35caSApple OSS Distributions 		STATE->lineNumber++;
950*c54f35caSApple OSS Distributions 		(void)nextChar();
951*c54f35caSApple OSS Distributions 		goto top;
952*c54f35caSApple OSS Distributions 	}
953*c54f35caSApple OSS Distributions 
954*c54f35caSApple OSS Distributions 	// end of the buffer?
955*c54f35caSApple OSS Distributions 	if (!c) {
956*c54f35caSApple OSS Distributions 		return 0;
957*c54f35caSApple OSS Distributions 	}
958*c54f35caSApple OSS Distributions 
959*c54f35caSApple OSS Distributions 	tagType = getTag(STATE, tag, &attributeCount, attributes, values);
960*c54f35caSApple OSS Distributions 	if (tagType == TAG_BAD) {
961*c54f35caSApple OSS Distributions 		return SYNTAX_ERROR;
962*c54f35caSApple OSS Distributions 	}
963*c54f35caSApple OSS Distributions 	if (tagType == TAG_IGNORE) {
964*c54f35caSApple OSS Distributions 		goto top;
965*c54f35caSApple OSS Distributions 	}
966*c54f35caSApple OSS Distributions 
967*c54f35caSApple OSS Distributions 	// handle allocation and check for "ID" and "IDREF" tags up front
968*c54f35caSApple OSS Distributions 	*lvalp = object = newObject(STATE);
969*c54f35caSApple OSS Distributions 	object->idref = -1;
970*c54f35caSApple OSS Distributions 	for (i = 0; i < attributeCount; i++) {
971*c54f35caSApple OSS Distributions 		if (attributes[i][0] == 'I' && attributes[i][1] == 'D') {
972*c54f35caSApple OSS Distributions 			// check for idref's, note: we ignore the tag, for
973*c54f35caSApple OSS Distributions 			// this to work correctly, all idrefs must be unique
974*c54f35caSApple OSS Distributions 			// across the whole serialization
975*c54f35caSApple OSS Distributions 			if (attributes[i][2] == 'R' && attributes[i][3] == 'E' &&
976*c54f35caSApple OSS Distributions 			    attributes[i][4] == 'F' && !attributes[i][5]) {
977*c54f35caSApple OSS Distributions 				if (tagType != TAG_EMPTY) {
978*c54f35caSApple OSS Distributions 					return SYNTAX_ERROR;
979*c54f35caSApple OSS Distributions 				}
980*c54f35caSApple OSS Distributions 				object->idref = strtol(values[i], NULL, 0);
981*c54f35caSApple OSS Distributions 				return IDREF;
982*c54f35caSApple OSS Distributions 			}
983*c54f35caSApple OSS Distributions 			// check for id's
984*c54f35caSApple OSS Distributions 			if (!attributes[i][2]) {
985*c54f35caSApple OSS Distributions 				object->idref = strtol(values[i], NULL, 0);
986*c54f35caSApple OSS Distributions 			} else {
987*c54f35caSApple OSS Distributions 				return SYNTAX_ERROR;
988*c54f35caSApple OSS Distributions 			}
989*c54f35caSApple OSS Distributions 		}
990*c54f35caSApple OSS Distributions 	}
991*c54f35caSApple OSS Distributions 
992*c54f35caSApple OSS Distributions 	switch (*tag) {
993*c54f35caSApple OSS Distributions 	case 'a':
994*c54f35caSApple OSS Distributions 		if (!strcmp(tag, "array")) {
995*c54f35caSApple OSS Distributions 			if (tagType == TAG_EMPTY) {
996*c54f35caSApple OSS Distributions 				object->elements = NULL;
997*c54f35caSApple OSS Distributions 				return ARRAY;
998*c54f35caSApple OSS Distributions 			}
999*c54f35caSApple OSS Distributions 			return (tagType == TAG_START) ? '(' : ')';
1000*c54f35caSApple OSS Distributions 		}
1001*c54f35caSApple OSS Distributions 		break;
1002*c54f35caSApple OSS Distributions 	case 'd':
1003*c54f35caSApple OSS Distributions 		if (!strcmp(tag, "dict")) {
1004*c54f35caSApple OSS Distributions 			if (tagType == TAG_EMPTY) {
1005*c54f35caSApple OSS Distributions 				object->elements = NULL;
1006*c54f35caSApple OSS Distributions 				return DICTIONARY;
1007*c54f35caSApple OSS Distributions 			}
1008*c54f35caSApple OSS Distributions 			return (tagType == TAG_START) ? '{' : '}';
1009*c54f35caSApple OSS Distributions 		}
1010*c54f35caSApple OSS Distributions 		if (!strcmp(tag, "data")) {
1011*c54f35caSApple OSS Distributions 			unsigned int size;
1012*c54f35caSApple OSS Distributions 			if (tagType == TAG_EMPTY) {
1013*c54f35caSApple OSS Distributions 				object->data = NULL;
1014*c54f35caSApple OSS Distributions 				object->size = 0;
1015*c54f35caSApple OSS Distributions 				return DATA;
1016*c54f35caSApple OSS Distributions 			}
1017*c54f35caSApple OSS Distributions 
1018*c54f35caSApple OSS Distributions 			bool isHexFormat = false;
1019*c54f35caSApple OSS Distributions 			for (i = 0; i < attributeCount; i++) {
1020*c54f35caSApple OSS Distributions 				if (!strcmp(attributes[i], "format") && !strcmp(values[i], "hex")) {
1021*c54f35caSApple OSS Distributions 					isHexFormat = true;
1022*c54f35caSApple OSS Distributions 					break;
1023*c54f35caSApple OSS Distributions 				}
1024*c54f35caSApple OSS Distributions 			}
1025*c54f35caSApple OSS Distributions 			// CF encoded is the default form
1026*c54f35caSApple OSS Distributions 			if (isHexFormat) {
1027*c54f35caSApple OSS Distributions 				object->data = getHexData(STATE, &size);
1028*c54f35caSApple OSS Distributions 			} else {
1029*c54f35caSApple OSS Distributions 				object->data = getCFEncodedData(STATE, &size);
1030*c54f35caSApple OSS Distributions 			}
1031*c54f35caSApple OSS Distributions 			object->size = size;
1032*c54f35caSApple OSS Distributions 			if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END) || strcmp(tag, "data")) {
1033*c54f35caSApple OSS Distributions 				return SYNTAX_ERROR;
1034*c54f35caSApple OSS Distributions 			}
1035*c54f35caSApple OSS Distributions 			return DATA;
1036*c54f35caSApple OSS Distributions 		}
1037*c54f35caSApple OSS Distributions 		break;
1038*c54f35caSApple OSS Distributions 	case 'f':
1039*c54f35caSApple OSS Distributions 		if (!strcmp(tag, "false")) {
1040*c54f35caSApple OSS Distributions 			if (tagType == TAG_EMPTY) {
1041*c54f35caSApple OSS Distributions 				object->number = 0;
1042*c54f35caSApple OSS Distributions 				return BOOLEAN;
1043*c54f35caSApple OSS Distributions 			}
1044*c54f35caSApple OSS Distributions 		}
1045*c54f35caSApple OSS Distributions 		break;
1046*c54f35caSApple OSS Distributions 	case 'i':
1047*c54f35caSApple OSS Distributions 		if (!strcmp(tag, "integer")) {
1048*c54f35caSApple OSS Distributions 			object->size = 64;      // default
1049*c54f35caSApple OSS Distributions 			for (i = 0; i < attributeCount; i++) {
1050*c54f35caSApple OSS Distributions 				if (!strcmp(attributes[i], "size")) {
1051*c54f35caSApple OSS Distributions 					object->size = strtoul(values[i], NULL, 0);
1052*c54f35caSApple OSS Distributions 				}
1053*c54f35caSApple OSS Distributions 			}
1054*c54f35caSApple OSS Distributions 			if (tagType == TAG_EMPTY) {
1055*c54f35caSApple OSS Distributions 				object->number = 0;
1056*c54f35caSApple OSS Distributions 				return NUMBER;
1057*c54f35caSApple OSS Distributions 			}
1058*c54f35caSApple OSS Distributions 			object->number = getNumber(STATE);
1059*c54f35caSApple OSS Distributions 			if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END) || strcmp(tag, "integer")) {
1060*c54f35caSApple OSS Distributions 				return SYNTAX_ERROR;
1061*c54f35caSApple OSS Distributions 			}
1062*c54f35caSApple OSS Distributions 			return NUMBER;
1063*c54f35caSApple OSS Distributions 		}
1064*c54f35caSApple OSS Distributions 		break;
1065*c54f35caSApple OSS Distributions 	case 'k':
1066*c54f35caSApple OSS Distributions 		if (!strcmp(tag, "key")) {
1067*c54f35caSApple OSS Distributions 			if (tagType == TAG_EMPTY) {
1068*c54f35caSApple OSS Distributions 				return SYNTAX_ERROR;
1069*c54f35caSApple OSS Distributions 			}
1070*c54f35caSApple OSS Distributions 			object->string = getString(STATE, &alloc_length);
1071*c54f35caSApple OSS Distributions 			if (!object->string) {
1072*c54f35caSApple OSS Distributions 				return SYNTAX_ERROR;
1073*c54f35caSApple OSS Distributions 			}
1074*c54f35caSApple OSS Distributions 			object->string_alloc_length = alloc_length;
1075*c54f35caSApple OSS Distributions 			if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END)
1076*c54f35caSApple OSS Distributions 			    || strcmp(tag, "key")) {
1077*c54f35caSApple OSS Distributions 				return SYNTAX_ERROR;
1078*c54f35caSApple OSS Distributions 			}
1079*c54f35caSApple OSS Distributions 			return KEY;
1080*c54f35caSApple OSS Distributions 		}
1081*c54f35caSApple OSS Distributions 		break;
1082*c54f35caSApple OSS Distributions 	case 'p':
1083*c54f35caSApple OSS Distributions 		if (!strcmp(tag, "plist")) {
1084*c54f35caSApple OSS Distributions 			freeObject(STATE, object);
1085*c54f35caSApple OSS Distributions 			goto top;
1086*c54f35caSApple OSS Distributions 		}
1087*c54f35caSApple OSS Distributions 		break;
1088*c54f35caSApple OSS Distributions 	case 's':
1089*c54f35caSApple OSS Distributions 		if (!strcmp(tag, "string")) {
1090*c54f35caSApple OSS Distributions 			if (tagType == TAG_EMPTY) {
1091*c54f35caSApple OSS Distributions 				object->string = (char *)malloc(1);
1092*c54f35caSApple OSS Distributions 				object->string_alloc_length = 1;
1093*c54f35caSApple OSS Distributions 				object->string[0] = 0;
1094*c54f35caSApple OSS Distributions 				return STRING;
1095*c54f35caSApple OSS Distributions 			}
1096*c54f35caSApple OSS Distributions 			object->string = getString(STATE, &alloc_length);
1097*c54f35caSApple OSS Distributions 			if (!object->string) {
1098*c54f35caSApple OSS Distributions 				return SYNTAX_ERROR;
1099*c54f35caSApple OSS Distributions 			}
1100*c54f35caSApple OSS Distributions 			object->string_alloc_length = alloc_length;
1101*c54f35caSApple OSS Distributions 			if ((getTag(STATE, tag, &attributeCount, attributes, values) != TAG_END)
1102*c54f35caSApple OSS Distributions 			    || strcmp(tag, "string")) {
1103*c54f35caSApple OSS Distributions 				return SYNTAX_ERROR;
1104*c54f35caSApple OSS Distributions 			}
1105*c54f35caSApple OSS Distributions 			return STRING;
1106*c54f35caSApple OSS Distributions 		}
1107*c54f35caSApple OSS Distributions 		if (!strcmp(tag, "set")) {
1108*c54f35caSApple OSS Distributions 			if (tagType == TAG_EMPTY) {
1109*c54f35caSApple OSS Distributions 				object->elements = NULL;
1110*c54f35caSApple OSS Distributions 				return SET;
1111*c54f35caSApple OSS Distributions 			}
1112*c54f35caSApple OSS Distributions 			if (tagType == TAG_START) {
1113*c54f35caSApple OSS Distributions 				return '[';
1114*c54f35caSApple OSS Distributions 			} else {
1115*c54f35caSApple OSS Distributions 				return ']';
1116*c54f35caSApple OSS Distributions 			}
1117*c54f35caSApple OSS Distributions 		}
1118*c54f35caSApple OSS Distributions 		break;
1119*c54f35caSApple OSS Distributions 	case 't':
1120*c54f35caSApple OSS Distributions 		if (!strcmp(tag, "true")) {
1121*c54f35caSApple OSS Distributions 			if (tagType == TAG_EMPTY) {
1122*c54f35caSApple OSS Distributions 				object->number = 1;
1123*c54f35caSApple OSS Distributions 				return BOOLEAN;
1124*c54f35caSApple OSS Distributions 			}
1125*c54f35caSApple OSS Distributions 		}
1126*c54f35caSApple OSS Distributions 		break;
1127*c54f35caSApple OSS Distributions 	}
1128*c54f35caSApple OSS Distributions 
1129*c54f35caSApple OSS Distributions 	return SYNTAX_ERROR;
1130*c54f35caSApple OSS Distributions }
1131*c54f35caSApple OSS Distributions 
1132*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1133*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1134*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1135*c54f35caSApple OSS Distributions 
1136*c54f35caSApple OSS Distributions // "java" like allocation, if this code hits a syntax error in the
1137*c54f35caSApple OSS Distributions // the middle of the parsed string we just bail with pointers hanging
1138*c54f35caSApple OSS Distributions // all over place, this code helps keeps it all together
1139*c54f35caSApple OSS Distributions 
1140*c54f35caSApple OSS Distributions //static int object_count = 0;
1141*c54f35caSApple OSS Distributions 
1142*c54f35caSApple OSS Distributions object_t *
newObject(parser_state_t * state)1143*c54f35caSApple OSS Distributions newObject(parser_state_t *state)
1144*c54f35caSApple OSS Distributions {
1145*c54f35caSApple OSS Distributions 	object_t *o;
1146*c54f35caSApple OSS Distributions 
1147*c54f35caSApple OSS Distributions 	if (state->freeObjects) {
1148*c54f35caSApple OSS Distributions 		o = state->freeObjects;
1149*c54f35caSApple OSS Distributions 		state->freeObjects = state->freeObjects->next;
1150*c54f35caSApple OSS Distributions 	} else {
1151*c54f35caSApple OSS Distributions 		o = malloc_type(object_t);
1152*c54f35caSApple OSS Distributions //		object_count++;
1153*c54f35caSApple OSS Distributions 		o->free = state->objects;
1154*c54f35caSApple OSS Distributions 		state->objects = o;
1155*c54f35caSApple OSS Distributions 	}
1156*c54f35caSApple OSS Distributions 
1157*c54f35caSApple OSS Distributions 	return o;
1158*c54f35caSApple OSS Distributions }
1159*c54f35caSApple OSS Distributions 
1160*c54f35caSApple OSS Distributions void
freeObject(parser_state_t * state,object_t * o)1161*c54f35caSApple OSS Distributions freeObject(parser_state_t * state, object_t *o)
1162*c54f35caSApple OSS Distributions {
1163*c54f35caSApple OSS Distributions 	o->next = state->freeObjects;
1164*c54f35caSApple OSS Distributions 	state->freeObjects = o;
1165*c54f35caSApple OSS Distributions }
1166*c54f35caSApple OSS Distributions 
1167*c54f35caSApple OSS Distributions void
cleanupObjects(parser_state_t * state)1168*c54f35caSApple OSS Distributions cleanupObjects(parser_state_t *state)
1169*c54f35caSApple OSS Distributions {
1170*c54f35caSApple OSS Distributions 	object_t *t, *o = state->objects;
1171*c54f35caSApple OSS Distributions 
1172*c54f35caSApple OSS Distributions 	while (o) {
1173*c54f35caSApple OSS Distributions 		if (o->object) {
1174*c54f35caSApple OSS Distributions //			printf("OSUnserializeXML: releasing object o=%x object=%x\n", (int)o, (int)o->object);
1175*c54f35caSApple OSS Distributions 			o->object->release();
1176*c54f35caSApple OSS Distributions 		}
1177*c54f35caSApple OSS Distributions 		if (o->data) {
1178*c54f35caSApple OSS Distributions //			printf("OSUnserializeXML: freeing   object o=%x data=%x\n", (int)o, (int)o->data);
1179*c54f35caSApple OSS Distributions 			free(o->data);
1180*c54f35caSApple OSS Distributions 		}
1181*c54f35caSApple OSS Distributions 		if (o->key) {
1182*c54f35caSApple OSS Distributions //			printf("OSUnserializeXML: releasing object o=%x key=%x\n", (int)o, (int)o->key);
1183*c54f35caSApple OSS Distributions 			o->key->release();
1184*c54f35caSApple OSS Distributions 		}
1185*c54f35caSApple OSS Distributions 		if (o->string) {
1186*c54f35caSApple OSS Distributions //			printf("OSUnserializeXML: freeing   object o=%x string=%x\n", (int)o, (int)o->string);
1187*c54f35caSApple OSS Distributions 			free(o->string);
1188*c54f35caSApple OSS Distributions 		}
1189*c54f35caSApple OSS Distributions 
1190*c54f35caSApple OSS Distributions 		t = o;
1191*c54f35caSApple OSS Distributions 		o = o->free;
1192*c54f35caSApple OSS Distributions 		free_type(object_t, t);
1193*c54f35caSApple OSS Distributions //		object_count--;
1194*c54f35caSApple OSS Distributions 	}
1195*c54f35caSApple OSS Distributions //	printf("object_count = %d\n", object_count);
1196*c54f35caSApple OSS Distributions }
1197*c54f35caSApple OSS Distributions 
1198*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1199*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1200*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1201*c54f35caSApple OSS Distributions 
1202*c54f35caSApple OSS Distributions static void
rememberObject(parser_state_t * state,int tag,OSObject * o)1203*c54f35caSApple OSS Distributions rememberObject(parser_state_t *state, int tag, OSObject *o)
1204*c54f35caSApple OSS Distributions {
1205*c54f35caSApple OSS Distributions 	char key[16];
1206*c54f35caSApple OSS Distributions 	snprintf(key, 16, "%u", tag);
1207*c54f35caSApple OSS Distributions 
1208*c54f35caSApple OSS Distributions //	printf("remember key %s\n", key);
1209*c54f35caSApple OSS Distributions 
1210*c54f35caSApple OSS Distributions 	state->tags->setObject(key, o);
1211*c54f35caSApple OSS Distributions }
1212*c54f35caSApple OSS Distributions 
1213*c54f35caSApple OSS Distributions static object_t *
retrieveObject(parser_state_t * state,int tag)1214*c54f35caSApple OSS Distributions retrieveObject(parser_state_t *state, int tag)
1215*c54f35caSApple OSS Distributions {
1216*c54f35caSApple OSS Distributions 	OSObject *ref;
1217*c54f35caSApple OSS Distributions 	object_t *o;
1218*c54f35caSApple OSS Distributions 	char key[16];
1219*c54f35caSApple OSS Distributions 	snprintf(key, 16, "%u", tag);
1220*c54f35caSApple OSS Distributions 
1221*c54f35caSApple OSS Distributions //	printf("retrieve key '%s'\n", key);
1222*c54f35caSApple OSS Distributions 
1223*c54f35caSApple OSS Distributions 	ref = state->tags->getObject(key);
1224*c54f35caSApple OSS Distributions 	if (!ref) {
1225*c54f35caSApple OSS Distributions 		return 0;
1226*c54f35caSApple OSS Distributions 	}
1227*c54f35caSApple OSS Distributions 
1228*c54f35caSApple OSS Distributions 	o = newObject(state);
1229*c54f35caSApple OSS Distributions 	o->object = ref;
1230*c54f35caSApple OSS Distributions 	return o;
1231*c54f35caSApple OSS Distributions }
1232*c54f35caSApple OSS Distributions 
1233*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1234*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1235*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
1236*c54f35caSApple OSS Distributions 
1237*c54f35caSApple OSS Distributions object_t *
buildDictionary(parser_state_t * state,object_t * header)1238*c54f35caSApple OSS Distributions buildDictionary(parser_state_t *state, object_t * header)
1239*c54f35caSApple OSS Distributions {
1240*c54f35caSApple OSS Distributions 	object_t *o, *t;
1241*c54f35caSApple OSS Distributions 	int count = 0;
1242*c54f35caSApple OSS Distributions 	OSDictionary *dict;
1243*c54f35caSApple OSS Distributions 
1244*c54f35caSApple OSS Distributions 	// get count and reverse order
1245*c54f35caSApple OSS Distributions 	o = header->elements;
1246*c54f35caSApple OSS Distributions 	header->elements = 0;
1247*c54f35caSApple OSS Distributions 	while (o) {
1248*c54f35caSApple OSS Distributions 		count++;
1249*c54f35caSApple OSS Distributions 		t = o;
1250*c54f35caSApple OSS Distributions 		o = o->next;
1251*c54f35caSApple OSS Distributions 
1252*c54f35caSApple OSS Distributions 		t->next = header->elements;
1253*c54f35caSApple OSS Distributions 		header->elements = t;
1254*c54f35caSApple OSS Distributions 	}
1255*c54f35caSApple OSS Distributions 
1256*c54f35caSApple OSS Distributions 	dict = OSDictionary::withCapacity(count);
1257*c54f35caSApple OSS Distributions 	if (header->idref >= 0) {
1258*c54f35caSApple OSS Distributions 		rememberObject(state, header->idref, dict);
1259*c54f35caSApple OSS Distributions 	}
1260*c54f35caSApple OSS Distributions 
1261*c54f35caSApple OSS Distributions 	o = header->elements;
1262*c54f35caSApple OSS Distributions 	while (o) {
1263*c54f35caSApple OSS Distributions 		dict->setObject(o->key, o->object);
1264*c54f35caSApple OSS Distributions 
1265*c54f35caSApple OSS Distributions 		o->key->release();
1266*c54f35caSApple OSS Distributions 		o->object->release();
1267*c54f35caSApple OSS Distributions 		o->key = 0;
1268*c54f35caSApple OSS Distributions 		o->object = 0;
1269*c54f35caSApple OSS Distributions 
1270*c54f35caSApple OSS Distributions 		t = o;
1271*c54f35caSApple OSS Distributions 		o = o->next;
1272*c54f35caSApple OSS Distributions 		freeObject(state, t);
1273*c54f35caSApple OSS Distributions 	}
1274*c54f35caSApple OSS Distributions 	o = header;
1275*c54f35caSApple OSS Distributions 	o->object = dict;
1276*c54f35caSApple OSS Distributions 	return o;
1277*c54f35caSApple OSS Distributions };
1278*c54f35caSApple OSS Distributions 
1279*c54f35caSApple OSS Distributions object_t *
buildArray(parser_state_t * state,object_t * header)1280*c54f35caSApple OSS Distributions buildArray(parser_state_t *state, object_t * header)
1281*c54f35caSApple OSS Distributions {
1282*c54f35caSApple OSS Distributions 	object_t *o, *t;
1283*c54f35caSApple OSS Distributions 	int count = 0;
1284*c54f35caSApple OSS Distributions 	OSArray *array;
1285*c54f35caSApple OSS Distributions 
1286*c54f35caSApple OSS Distributions 	// get count and reverse order
1287*c54f35caSApple OSS Distributions 	o = header->elements;
1288*c54f35caSApple OSS Distributions 	header->elements = 0;
1289*c54f35caSApple OSS Distributions 	while (o) {
1290*c54f35caSApple OSS Distributions 		count++;
1291*c54f35caSApple OSS Distributions 		t = o;
1292*c54f35caSApple OSS Distributions 		o = o->next;
1293*c54f35caSApple OSS Distributions 
1294*c54f35caSApple OSS Distributions 		t->next = header->elements;
1295*c54f35caSApple OSS Distributions 		header->elements = t;
1296*c54f35caSApple OSS Distributions 	}
1297*c54f35caSApple OSS Distributions 
1298*c54f35caSApple OSS Distributions 	array = OSArray::withCapacity(count);
1299*c54f35caSApple OSS Distributions 	if (header->idref >= 0) {
1300*c54f35caSApple OSS Distributions 		rememberObject(state, header->idref, array);
1301*c54f35caSApple OSS Distributions 	}
1302*c54f35caSApple OSS Distributions 
1303*c54f35caSApple OSS Distributions 	o = header->elements;
1304*c54f35caSApple OSS Distributions 	while (o) {
1305*c54f35caSApple OSS Distributions 		array->setObject(o->object);
1306*c54f35caSApple OSS Distributions 
1307*c54f35caSApple OSS Distributions 		o->object->release();
1308*c54f35caSApple OSS Distributions 		o->object = 0;
1309*c54f35caSApple OSS Distributions 
1310*c54f35caSApple OSS Distributions 		t = o;
1311*c54f35caSApple OSS Distributions 		o = o->next;
1312*c54f35caSApple OSS Distributions 		freeObject(state, t);
1313*c54f35caSApple OSS Distributions 	}
1314*c54f35caSApple OSS Distributions 	o = header;
1315*c54f35caSApple OSS Distributions 	o->object = array;
1316*c54f35caSApple OSS Distributions 	return o;
1317*c54f35caSApple OSS Distributions };
1318*c54f35caSApple OSS Distributions 
1319*c54f35caSApple OSS Distributions object_t *
buildSet(parser_state_t * state,object_t * header)1320*c54f35caSApple OSS Distributions buildSet(parser_state_t *state, object_t *header)
1321*c54f35caSApple OSS Distributions {
1322*c54f35caSApple OSS Distributions 	object_t *o = buildArray(state, header);
1323*c54f35caSApple OSS Distributions 
1324*c54f35caSApple OSS Distributions #if KERNEL
1325*c54f35caSApple OSS Distributions 	OSArray *array = (OSArray *)o->object;
1326*c54f35caSApple OSS Distributions 	OSSet *set = OSSet::withArray(array, array->getCapacity());
1327*c54f35caSApple OSS Distributions 
1328*c54f35caSApple OSS Distributions 	// write over the reference created in buildArray
1329*c54f35caSApple OSS Distributions 	if (header->idref >= 0) {
1330*c54f35caSApple OSS Distributions 		rememberObject(state, header->idref, set);
1331*c54f35caSApple OSS Distributions 	}
1332*c54f35caSApple OSS Distributions 
1333*c54f35caSApple OSS Distributions 	array->release();
1334*c54f35caSApple OSS Distributions 	o->object = set;
1335*c54f35caSApple OSS Distributions #endif /* KERNEL */
1336*c54f35caSApple OSS Distributions 	return o;
1337*c54f35caSApple OSS Distributions };
1338*c54f35caSApple OSS Distributions 
1339*c54f35caSApple OSS Distributions object_t *
buildString(parser_state_t * state,object_t * o)1340*c54f35caSApple OSS Distributions buildString(parser_state_t *state, object_t *o)
1341*c54f35caSApple OSS Distributions {
1342*c54f35caSApple OSS Distributions 	OSString *string;
1343*c54f35caSApple OSS Distributions 
1344*c54f35caSApple OSS Distributions 	string = OSString::withCString(o->string);
1345*c54f35caSApple OSS Distributions 	if (o->idref >= 0) {
1346*c54f35caSApple OSS Distributions 		rememberObject(state, o->idref, string);
1347*c54f35caSApple OSS Distributions 	}
1348*c54f35caSApple OSS Distributions 
1349*c54f35caSApple OSS Distributions 	free(o->string);
1350*c54f35caSApple OSS Distributions 	o->string = 0;
1351*c54f35caSApple OSS Distributions 	o->object = string;
1352*c54f35caSApple OSS Distributions 
1353*c54f35caSApple OSS Distributions 	return o;
1354*c54f35caSApple OSS Distributions };
1355*c54f35caSApple OSS Distributions 
1356*c54f35caSApple OSS Distributions object_t *
buildSymbol(parser_state_t * state,object_t * o)1357*c54f35caSApple OSS Distributions buildSymbol(parser_state_t *state, object_t *o)
1358*c54f35caSApple OSS Distributions {
1359*c54f35caSApple OSS Distributions 	OSSymbol *symbol;
1360*c54f35caSApple OSS Distributions 
1361*c54f35caSApple OSS Distributions 	symbol = const_cast < OSSymbol * > (OSSymbol::withCString(o->string));
1362*c54f35caSApple OSS Distributions 	if (o->idref >= 0) {
1363*c54f35caSApple OSS Distributions 		rememberObject(state, o->idref, symbol);
1364*c54f35caSApple OSS Distributions 	}
1365*c54f35caSApple OSS Distributions 
1366*c54f35caSApple OSS Distributions 	safe_free(o->string, o->string_alloc_length);
1367*c54f35caSApple OSS Distributions 	o->string = 0;
1368*c54f35caSApple OSS Distributions 	o->object = symbol;
1369*c54f35caSApple OSS Distributions 
1370*c54f35caSApple OSS Distributions 	return o;
1371*c54f35caSApple OSS Distributions };
1372*c54f35caSApple OSS Distributions 
1373*c54f35caSApple OSS Distributions object_t *
buildData(parser_state_t * state,object_t * o)1374*c54f35caSApple OSS Distributions buildData(parser_state_t *state, object_t *o)
1375*c54f35caSApple OSS Distributions {
1376*c54f35caSApple OSS Distributions 	OSData *data;
1377*c54f35caSApple OSS Distributions 
1378*c54f35caSApple OSS Distributions 	if (o->size) {
1379*c54f35caSApple OSS Distributions 		data = OSData::withBytes(o->data, o->size);
1380*c54f35caSApple OSS Distributions 	} else {
1381*c54f35caSApple OSS Distributions 		data = OSData::withCapacity(0);
1382*c54f35caSApple OSS Distributions 	}
1383*c54f35caSApple OSS Distributions 	if (o->idref >= 0) {
1384*c54f35caSApple OSS Distributions 		rememberObject(state, o->idref, data);
1385*c54f35caSApple OSS Distributions 	}
1386*c54f35caSApple OSS Distributions 
1387*c54f35caSApple OSS Distributions 	if (o->size) {
1388*c54f35caSApple OSS Distributions 		free(o->data);
1389*c54f35caSApple OSS Distributions 	}
1390*c54f35caSApple OSS Distributions 	o->data = 0;
1391*c54f35caSApple OSS Distributions 	o->object = data;
1392*c54f35caSApple OSS Distributions 	return o;
1393*c54f35caSApple OSS Distributions };
1394*c54f35caSApple OSS Distributions 
1395*c54f35caSApple OSS Distributions object_t *
buildNumber(parser_state_t * state,object_t * o)1396*c54f35caSApple OSS Distributions buildNumber(parser_state_t *state, object_t *o)
1397*c54f35caSApple OSS Distributions {
1398*c54f35caSApple OSS Distributions 	OSNumber *number = OSNumber::withNumber(o->number, o->size);
1399*c54f35caSApple OSS Distributions 
1400*c54f35caSApple OSS Distributions 	if (o->idref >= 0) {
1401*c54f35caSApple OSS Distributions 		rememberObject(state, o->idref, number);
1402*c54f35caSApple OSS Distributions 	}
1403*c54f35caSApple OSS Distributions 
1404*c54f35caSApple OSS Distributions 	o->object = number;
1405*c54f35caSApple OSS Distributions 	return o;
1406*c54f35caSApple OSS Distributions };
1407*c54f35caSApple OSS Distributions 
1408*c54f35caSApple OSS Distributions object_t *
buildBoolean(parser_state_t * state __unused,object_t * o)1409*c54f35caSApple OSS Distributions buildBoolean(parser_state_t *state __unused, object_t *o)
1410*c54f35caSApple OSS Distributions {
1411*c54f35caSApple OSS Distributions 	o->object = ((o->number == 0) ? kOSBooleanFalse : kOSBooleanTrue);
1412*c54f35caSApple OSS Distributions 	o->object->retain();
1413*c54f35caSApple OSS Distributions 	return o;
1414*c54f35caSApple OSS Distributions };
1415*c54f35caSApple OSS Distributions 
1416*c54f35caSApple OSS Distributions OSObject*
OSUnserializeXML(const char * buffer,OSString ** errorString)1417*c54f35caSApple OSS Distributions OSUnserializeXML(const char *buffer, OSString **errorString)
1418*c54f35caSApple OSS Distributions {
1419*c54f35caSApple OSS Distributions 	OSObject *object;
1420*c54f35caSApple OSS Distributions 
1421*c54f35caSApple OSS Distributions 	if (!buffer) {
1422*c54f35caSApple OSS Distributions 		return 0;
1423*c54f35caSApple OSS Distributions 	}
1424*c54f35caSApple OSS Distributions 	parser_state_t *state = (parser_state_t *)malloc_type(parser_state_t);
1425*c54f35caSApple OSS Distributions 	if (!state) {
1426*c54f35caSApple OSS Distributions 		return 0;
1427*c54f35caSApple OSS Distributions 	}
1428*c54f35caSApple OSS Distributions 
1429*c54f35caSApple OSS Distributions 	// just in case
1430*c54f35caSApple OSS Distributions 	if (errorString) {
1431*c54f35caSApple OSS Distributions 		*errorString = NULL;
1432*c54f35caSApple OSS Distributions 	}
1433*c54f35caSApple OSS Distributions 
1434*c54f35caSApple OSS Distributions 	state->parseBuffer = buffer;
1435*c54f35caSApple OSS Distributions 	state->parseBufferIndex = 0;
1436*c54f35caSApple OSS Distributions 	state->lineNumber = 1;
1437*c54f35caSApple OSS Distributions 	state->objects = 0;
1438*c54f35caSApple OSS Distributions 	state->freeObjects = 0;
1439*c54f35caSApple OSS Distributions 	state->tags = OSDictionary::withCapacity(128);
1440*c54f35caSApple OSS Distributions 	state->errorString = errorString;
1441*c54f35caSApple OSS Distributions 	state->parsedObject = 0;
1442*c54f35caSApple OSS Distributions 	state->parsedObjectCount = 0;
1443*c54f35caSApple OSS Distributions 	state->retrievedObjectCount = 0;
1444*c54f35caSApple OSS Distributions 
1445*c54f35caSApple OSS Distributions 	(void)yyparse((void *)state);
1446*c54f35caSApple OSS Distributions 
1447*c54f35caSApple OSS Distributions 	object = state->parsedObject;
1448*c54f35caSApple OSS Distributions 
1449*c54f35caSApple OSS Distributions 	cleanupObjects(state);
1450*c54f35caSApple OSS Distributions 	state->tags->release();
1451*c54f35caSApple OSS Distributions 	free_type(parser_state_t, state);
1452*c54f35caSApple OSS Distributions 
1453*c54f35caSApple OSS Distributions 	return object;
1454*c54f35caSApple OSS Distributions }
1455*c54f35caSApple OSS Distributions 
1456*c54f35caSApple OSS Distributions #if KERNEL
1457*c54f35caSApple OSS Distributions #include <libkern/OSSerializeBinary.h>
1458*c54f35caSApple OSS Distributions 
1459*c54f35caSApple OSS Distributions OSObject*
OSUnserializeXML(const char * buffer,size_t bufferSize,OSString ** errorString)1460*c54f35caSApple OSS Distributions OSUnserializeXML(const char *buffer, size_t bufferSize, OSString **errorString)
1461*c54f35caSApple OSS Distributions {
1462*c54f35caSApple OSS Distributions 	if (!buffer) {
1463*c54f35caSApple OSS Distributions 		return 0;
1464*c54f35caSApple OSS Distributions 	}
1465*c54f35caSApple OSS Distributions 	if (bufferSize < sizeof(kOSSerializeBinarySignature)) {
1466*c54f35caSApple OSS Distributions 		return 0;
1467*c54f35caSApple OSS Distributions 	}
1468*c54f35caSApple OSS Distributions 
1469*c54f35caSApple OSS Distributions 	if (!strcmp(kOSSerializeBinarySignature, buffer)
1470*c54f35caSApple OSS Distributions 	    || (kOSSerializeIndexedBinarySignature == (uint8_t)buffer[0])) {
1471*c54f35caSApple OSS Distributions 		return OSUnserializeBinary(buffer, bufferSize, errorString);
1472*c54f35caSApple OSS Distributions 	}
1473*c54f35caSApple OSS Distributions 
1474*c54f35caSApple OSS Distributions 	// XML must be null terminated
1475*c54f35caSApple OSS Distributions 	if (buffer[bufferSize - 1]) {
1476*c54f35caSApple OSS Distributions 		return 0;
1477*c54f35caSApple OSS Distributions 	}
1478*c54f35caSApple OSS Distributions 
1479*c54f35caSApple OSS Distributions 	return OSUnserializeXML(buffer, errorString);
1480*c54f35caSApple OSS Distributions }
1481*c54f35caSApple OSS Distributions 
1482*c54f35caSApple OSS Distributions #else /* !KERNEL */
1483*c54f35caSApple OSS Distributions 
1484*c54f35caSApple OSS Distributions OSObject*
OSUnserializeXML(const char * buffer,size_t bufferSize,OSString ** errorString)1485*c54f35caSApple OSS Distributions OSUnserializeXML(const char *buffer, size_t bufferSize, OSString **errorString)
1486*c54f35caSApple OSS Distributions {
1487*c54f35caSApple OSS Distributions 	if (!buffer) {
1488*c54f35caSApple OSS Distributions 		return 0;
1489*c54f35caSApple OSS Distributions 	}
1490*c54f35caSApple OSS Distributions 
1491*c54f35caSApple OSS Distributions 	// XML must be null terminated
1492*c54f35caSApple OSS Distributions 	if (buffer[bufferSize - 1]) {
1493*c54f35caSApple OSS Distributions 		return 0;
1494*c54f35caSApple OSS Distributions 	}
1495*c54f35caSApple OSS Distributions 
1496*c54f35caSApple OSS Distributions 	return OSUnserializeXML(buffer, errorString);
1497*c54f35caSApple OSS Distributions }
1498*c54f35caSApple OSS Distributions 
1499*c54f35caSApple OSS Distributions #endif /* KERNEL */
1500*c54f35caSApple OSS Distributions 
1501*c54f35caSApple OSS Distributions 
1502*c54f35caSApple OSS Distributions //
1503*c54f35caSApple OSS Distributions //
1504*c54f35caSApple OSS Distributions //
1505*c54f35caSApple OSS Distributions //
1506*c54f35caSApple OSS Distributions //
1507*c54f35caSApple OSS Distributions //		 DO NOT EDIT OSUnserializeXMLSharedImplementation.h!
1508*c54f35caSApple OSS Distributions //
1509*c54f35caSApple OSS Distributions //			this means you!
1510*c54f35caSApple OSS Distributions //
1511*c54f35caSApple OSS Distributions //
1512*c54f35caSApple OSS Distributions //
1513*c54f35caSApple OSS Distributions //
1514*c54f35caSApple OSS Distributions //
1515