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