xref: /xnu-8796.121.2/libkern/c++/OSUnserialize.y (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2000 Apple Computer, 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 /*  OSUnserialize.y created by rsulack on Nov 21 1998 */
30*c54f35caSApple OSS Distributions 
31*c54f35caSApple OSS Distributions // 		"classic" parser for unserializing OSContainer objects
32*c54f35caSApple OSS Distributions //
33*c54f35caSApple OSS Distributions //  XXX - this code should really be removed!
34*c54f35caSApple OSS Distributions //	- the XML format is now prefered
35*c54f35caSApple OSS Distributions //	- this code leaks on syntax errors, the XML doesn't
36*c54f35caSApple OSS Distributions //	- "classic" looks, reads, ... much better than XML :-(
37*c54f35caSApple OSS Distributions //	- well except the XML is more efficent on OSData
38*c54f35caSApple OSS Distributions //
39*c54f35caSApple OSS Distributions //
40*c54f35caSApple OSS Distributions // to build :
41*c54f35caSApple OSS Distributions //	bison -p OSUnserialize OSUnserialize.y
42*c54f35caSApple OSS Distributions //	head -50 OSUnserialize.y > OSUnserialize.cpp
43*c54f35caSApple OSS Distributions //	sed -e "s/stdio.h/stddef.h/" < OSUnserialize.tab.c >> OSUnserialize.cpp
44*c54f35caSApple OSS Distributions //
45*c54f35caSApple OSS Distributions //	when changing code check in both OSUnserialize.y and OSUnserialize.cpp
46*c54f35caSApple OSS Distributions //
47*c54f35caSApple OSS Distributions //
48*c54f35caSApple OSS Distributions //
49*c54f35caSApple OSS Distributions //
50*c54f35caSApple OSS Distributions //		 DO NOT EDIT OSUnserialize.tab.cpp!
51*c54f35caSApple OSS Distributions //
52*c54f35caSApple OSS Distributions //			this means you!
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 
60*c54f35caSApple OSS Distributions %{
61*c54f35caSApple OSS Distributions #include <libkern/c++/OSMetaClass.h>
62*c54f35caSApple OSS Distributions #include <libkern/c++/OSContainers.h>
63*c54f35caSApple OSS Distributions #include <libkern/c++/OSLib.h>
64*c54f35caSApple OSS Distributions 
65*c54f35caSApple OSS Distributions typedef	struct object {
66*c54f35caSApple OSS Distributions 	struct object	*next;
67*c54f35caSApple OSS Distributions 	struct object	*prev;
68*c54f35caSApple OSS Distributions 	void		*object;
69*c54f35caSApple OSS Distributions 	int		size;		// for data
70*c54f35caSApple OSS Distributions 	union {
71*c54f35caSApple OSS Distributions 		void	*key;		// for dictionary
72*c54f35caSApple OSS Distributions 		long long offset;	// for offset
73*c54f35caSApple OSS Distributions 	} u;
74*c54f35caSApple OSS Distributions 
75*c54f35caSApple OSS Distributions } object_t;
76*c54f35caSApple OSS Distributions 
77*c54f35caSApple OSS Distributions static int yyerror(const char *s);
78*c54f35caSApple OSS Distributions static int yylex();
79*c54f35caSApple OSS Distributions 
80*c54f35caSApple OSS Distributions static object_t * newObject();
81*c54f35caSApple OSS Distributions static void freeObject(object_t *o);
82*c54f35caSApple OSS Distributions 
83*c54f35caSApple OSS Distributions static OSObject *buildOSDictionary(object_t *);
84*c54f35caSApple OSS Distributions static OSObject *buildOSArray(object_t *);
85*c54f35caSApple OSS Distributions static OSObject *buildOSSet(object_t *);
86*c54f35caSApple OSS Distributions static OSObject *buildOSString(object_t *);
87*c54f35caSApple OSS Distributions static OSObject *buildOSData(object_t *);
88*c54f35caSApple OSS Distributions static OSObject *buildOSOffset(object_t *);
89*c54f35caSApple OSS Distributions static OSObject *buildOSBoolean(object_t *o);
90*c54f35caSApple OSS Distributions 
91*c54f35caSApple OSS Distributions static void rememberObject(int, object_t *);
92*c54f35caSApple OSS Distributions static OSObject *retrieveObject(int);
93*c54f35caSApple OSS Distributions 
94*c54f35caSApple OSS Distributions // temp variable to use during parsing
95*c54f35caSApple OSS Distributions static object_t *oo;
96*c54f35caSApple OSS Distributions 
97*c54f35caSApple OSS Distributions // resultant object of parsed text
98*c54f35caSApple OSS Distributions static OSObject	*parsedObject;
99*c54f35caSApple OSS Distributions 
100*c54f35caSApple OSS Distributions #define YYSTYPE object_t *
101*c54f35caSApple OSS Distributions 
102*c54f35caSApple OSS Distributions __BEGIN_DECLS
103*c54f35caSApple OSS Distributions #include <kern/kalloc.h>
104*c54f35caSApple OSS Distributions __END_DECLS
105*c54f35caSApple OSS Distributions 
106*c54f35caSApple OSS Distributions // Omit from static analysis.
107*c54f35caSApple OSS Distributions #ifndef __clang_analyzer__
108*c54f35caSApple OSS Distributions 
109*c54f35caSApple OSS Distributions #define malloc(size)         malloc_impl(size)
110*c54f35caSApple OSS Distributions #define malloc_type(type)    kalloc_type(type, Z_WAITOK)
111*c54f35caSApple OSS Distributions static inline void *
malloc_impl(size_t size)112*c54f35caSApple OSS Distributions malloc_impl(size_t size)
113*c54f35caSApple OSS Distributions {
114*c54f35caSApple OSS Distributions 	if (size == 0) {
115*c54f35caSApple OSS Distributions 		return NULL;
116*c54f35caSApple OSS Distributions 	}
117*c54f35caSApple OSS Distributions 	return kalloc_data(size,
118*c54f35caSApple OSS Distributions 		Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
119*c54f35caSApple OSS Distributions }
120*c54f35caSApple OSS Distributions 
121*c54f35caSApple OSS Distributions #define free(addr)             free_impl(addr)
122*c54f35caSApple OSS Distributions #define free_type(type, addr)  kfree_type(type, addr)
123*c54f35caSApple OSS Distributions static inline void
free_impl(void * addr)124*c54f35caSApple OSS Distributions free_impl(void *addr)
125*c54f35caSApple OSS Distributions {
126*c54f35caSApple OSS Distributions 	kfree_data_addr(addr);
127*c54f35caSApple OSS Distributions }
128*c54f35caSApple OSS Distributions static inline void
safe_free(void * addr,size_t size)129*c54f35caSApple OSS Distributions safe_free(void *addr, size_t size)
130*c54f35caSApple OSS Distributions {
131*c54f35caSApple OSS Distributions 	kfree_data(addr, size);
132*c54f35caSApple OSS Distributions }
133*c54f35caSApple OSS Distributions 
134*c54f35caSApple OSS Distributions #define realloc(addr, osize, nsize) realloc_impl(addr, osize, nsize)
135*c54f35caSApple OSS Distributions static inline void *
realloc_impl(void * addr,size_t osize,size_t nsize)136*c54f35caSApple OSS Distributions realloc_impl(void *addr, size_t osize, size_t nsize)
137*c54f35caSApple OSS Distributions {
138*c54f35caSApple OSS Distributions 	return krealloc_data(addr, osize, nsize,
139*c54f35caSApple OSS Distributions 		Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
140*c54f35caSApple OSS Distributions }
141*c54f35caSApple OSS Distributions 
142*c54f35caSApple OSS Distributions %}
143*c54f35caSApple OSS Distributions %token NUMBER
144*c54f35caSApple OSS Distributions %token STRING
145*c54f35caSApple OSS Distributions %token DATA
146*c54f35caSApple OSS Distributions %token BOOLEAN
147*c54f35caSApple OSS Distributions %token SYNTAX_ERROR
148*c54f35caSApple OSS Distributions 
149*c54f35caSApple OSS Distributions %% /* Grammar rules and actions follow */
150*c54f35caSApple OSS Distributions 
151*c54f35caSApple OSS Distributions input:	  /* empty */		{ parsedObject = (OSObject *)NULL; YYACCEPT; }
152*c54f35caSApple OSS Distributions 	| object		{ parsedObject = (OSObject *)$1;   YYACCEPT; }
153*c54f35caSApple OSS Distributions 	| SYNTAX_ERROR		{ yyerror("syntax error");	   YYERROR; }
154*c54f35caSApple OSS Distributions 	;
155*c54f35caSApple OSS Distributions 
156*c54f35caSApple OSS Distributions object:	  dict			{ $$ = (object_t *)buildOSDictionary($1); }
157*c54f35caSApple OSS Distributions 	| array			{ $$ = (object_t *)buildOSArray($1); }
158*c54f35caSApple OSS Distributions 	| set			{ $$ = (object_t *)buildOSSet($1); }
159*c54f35caSApple OSS Distributions 	| string		{ $$ = (object_t *)buildOSString($1); }
160*c54f35caSApple OSS Distributions 	| data			{ $$ = (object_t *)buildOSData($1); }
161*c54f35caSApple OSS Distributions 	| offset		{ $$ = (object_t *)buildOSOffset($1); }
162*c54f35caSApple OSS Distributions 	| boolean		{ $$ = (object_t *)buildOSBoolean($1); }
163*c54f35caSApple OSS Distributions 	| '@' NUMBER		{ $$ = (object_t *)retrieveObject($2->u.offset);
164*c54f35caSApple OSS Distributions 				  if ($$) {
165*c54f35caSApple OSS Distributions 				    ((OSObject *)$$)->retain();
166*c54f35caSApple OSS Distributions 				  } else {
167*c54f35caSApple OSS Distributions 				    yyerror("forward reference detected");
168*c54f35caSApple OSS Distributions 				    YYERROR;
169*c54f35caSApple OSS Distributions 				  }
170*c54f35caSApple OSS Distributions 				  freeObject($2);
171*c54f35caSApple OSS Distributions 				}
172*c54f35caSApple OSS Distributions 	| object '@' NUMBER	{ $$ = $1;
173*c54f35caSApple OSS Distributions 				  rememberObject($3->u.offset, $1);
174*c54f35caSApple OSS Distributions 				  freeObject($3);
175*c54f35caSApple OSS Distributions 				}
176*c54f35caSApple OSS Distributions 	;
177*c54f35caSApple OSS Distributions 
178*c54f35caSApple OSS Distributions //------------------------------------------------------------------------------
179*c54f35caSApple OSS Distributions 
180*c54f35caSApple OSS Distributions dict:	  '{' '}'		{ $$ = NULL; }
181*c54f35caSApple OSS Distributions 	| '{' pairs '}'		{ $$ = $2; }
182*c54f35caSApple OSS Distributions 	;
183*c54f35caSApple OSS Distributions 
184*c54f35caSApple OSS Distributions pairs:	  pair
185*c54f35caSApple OSS Distributions 	| pairs pair		{ $2->next = $1; $1->prev = $2; $$ = $2; }
186*c54f35caSApple OSS Distributions 	;
187*c54f35caSApple OSS Distributions 
188*c54f35caSApple OSS Distributions pair:	  object '=' object ';'	{ $$ = newObject();
189*c54f35caSApple OSS Distributions 				  $$->next = NULL;
190*c54f35caSApple OSS Distributions 				  $$->prev = NULL;
191*c54f35caSApple OSS Distributions 				  $$->u.key = $1;
192*c54f35caSApple OSS Distributions 				  $$->object = $3;
193*c54f35caSApple OSS Distributions 				}
194*c54f35caSApple OSS Distributions 	;
195*c54f35caSApple OSS Distributions 
196*c54f35caSApple OSS Distributions //------------------------------------------------------------------------------
197*c54f35caSApple OSS Distributions 
198*c54f35caSApple OSS Distributions array:	  '(' ')'		{ $$ = NULL; }
199*c54f35caSApple OSS Distributions 	| '(' elements ')'	{ $$ = $2; }
200*c54f35caSApple OSS Distributions 	;
201*c54f35caSApple OSS Distributions 
202*c54f35caSApple OSS Distributions set:	  '[' ']'		{ $$ = NULL; }
203*c54f35caSApple OSS Distributions 	| '[' elements ']'	{ $$ = $2; }
204*c54f35caSApple OSS Distributions 	;
205*c54f35caSApple OSS Distributions 
206*c54f35caSApple OSS Distributions elements: object		{ $$ = newObject();
207*c54f35caSApple OSS Distributions 				  $$->object = $1;
208*c54f35caSApple OSS Distributions 				  $$->next = NULL;
209*c54f35caSApple OSS Distributions 				  $$->prev = NULL;
210*c54f35caSApple OSS Distributions 				}
211*c54f35caSApple OSS Distributions 	| elements ',' object	{ oo = newObject();
212*c54f35caSApple OSS Distributions 				  oo->object = $3;
213*c54f35caSApple OSS Distributions 				  oo->next = $1;
214*c54f35caSApple OSS Distributions 				  oo->prev = NULL;
215*c54f35caSApple OSS Distributions 				  $1->prev = oo;
216*c54f35caSApple OSS Distributions 				  $$ = oo;
217*c54f35caSApple OSS Distributions 				}
218*c54f35caSApple OSS Distributions 	;
219*c54f35caSApple OSS Distributions 
220*c54f35caSApple OSS Distributions //------------------------------------------------------------------------------
221*c54f35caSApple OSS Distributions 
222*c54f35caSApple OSS Distributions offset:	  NUMBER ':' NUMBER	{ $$ = $1;
223*c54f35caSApple OSS Distributions 				  $$->size = $3->u.offset;
224*c54f35caSApple OSS Distributions 				  freeObject($3);
225*c54f35caSApple OSS Distributions 				}
226*c54f35caSApple OSS Distributions 	;
227*c54f35caSApple OSS Distributions 
228*c54f35caSApple OSS Distributions //------------------------------------------------------------------------------
229*c54f35caSApple OSS Distributions 
230*c54f35caSApple OSS Distributions data:	  DATA
231*c54f35caSApple OSS Distributions 	;
232*c54f35caSApple OSS Distributions 
233*c54f35caSApple OSS Distributions //------------------------------------------------------------------------------
234*c54f35caSApple OSS Distributions 
235*c54f35caSApple OSS Distributions string:	  STRING
236*c54f35caSApple OSS Distributions 	;
237*c54f35caSApple OSS Distributions 
238*c54f35caSApple OSS Distributions //------------------------------------------------------------------------------
239*c54f35caSApple OSS Distributions 
240*c54f35caSApple OSS Distributions boolean:  BOOLEAN
241*c54f35caSApple OSS Distributions 	;
242*c54f35caSApple OSS Distributions 
243*c54f35caSApple OSS Distributions %%
244*c54f35caSApple OSS Distributions 
245*c54f35caSApple OSS Distributions static int		lineNumber = 0;
246*c54f35caSApple OSS Distributions static const char	*parseBuffer;
247*c54f35caSApple OSS Distributions static int		parseBufferIndex;
248*c54f35caSApple OSS Distributions 
249*c54f35caSApple OSS Distributions #define currentChar()	(parseBuffer[parseBufferIndex])
250*c54f35caSApple OSS Distributions #define nextChar()	(parseBuffer[++parseBufferIndex])
251*c54f35caSApple OSS Distributions #define prevChar()	(parseBuffer[parseBufferIndex - 1])
252*c54f35caSApple OSS Distributions 
253*c54f35caSApple OSS Distributions #define isSpace(c)	((c) == ' ' || (c) == '\t')
254*c54f35caSApple OSS Distributions #define isAlpha(c)	(((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
255*c54f35caSApple OSS Distributions #define isDigit(c)	((c) >= '0' && (c) <= '9')
256*c54f35caSApple OSS Distributions #define isAlphaDigit(c)	((c) >= 'a' && (c) <= 'f')
257*c54f35caSApple OSS Distributions #define isHexDigit(c)	(isDigit(c) || isAlphaDigit(c))
258*c54f35caSApple OSS Distributions #define isAlphaNumeric(c) (isAlpha(c) || isDigit(c) || ((c) == '-'))
259*c54f35caSApple OSS Distributions 
260*c54f35caSApple OSS Distributions static char yyerror_message[128];
261*c54f35caSApple OSS Distributions 
262*c54f35caSApple OSS Distributions int
yyerror(const char * s)263*c54f35caSApple OSS Distributions yyerror(const char *s)  /* Called by yyparse on error */
264*c54f35caSApple OSS Distributions {
265*c54f35caSApple OSS Distributions 	snprintf(yyerror_message, sizeof(yyerror_message), "OSUnserialize: %s near line %d\n", s, lineNumber);
266*c54f35caSApple OSS Distributions 	return 0;
267*c54f35caSApple OSS Distributions }
268*c54f35caSApple OSS Distributions 
269*c54f35caSApple OSS Distributions int
yylex()270*c54f35caSApple OSS Distributions yylex()
271*c54f35caSApple OSS Distributions {
272*c54f35caSApple OSS Distributions 	int c;
273*c54f35caSApple OSS Distributions 
274*c54f35caSApple OSS Distributions 	if (parseBufferIndex == 0) lineNumber = 1;
275*c54f35caSApple OSS Distributions 
276*c54f35caSApple OSS Distributions  top:
277*c54f35caSApple OSS Distributions 	c = currentChar();
278*c54f35caSApple OSS Distributions 
279*c54f35caSApple OSS Distributions 	/* skip white space  */
280*c54f35caSApple OSS Distributions 	if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
281*c54f35caSApple OSS Distributions 
282*c54f35caSApple OSS Distributions 	/* skip over comments */
283*c54f35caSApple OSS Distributions 	if (c == '#') while ((c = nextChar()) != 0 && c != '\n') {};
284*c54f35caSApple OSS Distributions 
285*c54f35caSApple OSS Distributions 	/* keep track of line number, don't return \n's */
286*c54f35caSApple OSS Distributions 	if (c == '\n') {
287*c54f35caSApple OSS Distributions 		lineNumber++;
288*c54f35caSApple OSS Distributions 		(void)nextChar();
289*c54f35caSApple OSS Distributions 		goto top;
290*c54f35caSApple OSS Distributions 	}
291*c54f35caSApple OSS Distributions 
292*c54f35caSApple OSS Distributions 	/* parse boolean */
293*c54f35caSApple OSS Distributions 	if (c == '.') {
294*c54f35caSApple OSS Distributions 		bool boolean = false;
295*c54f35caSApple OSS Distributions 		if (nextChar() == 't') {
296*c54f35caSApple OSS Distributions 			if (nextChar() != 'r') return SYNTAX_ERROR;
297*c54f35caSApple OSS Distributions 			if (nextChar() != 'u') return SYNTAX_ERROR;
298*c54f35caSApple OSS Distributions 			if (nextChar() != 'e') return SYNTAX_ERROR;
299*c54f35caSApple OSS Distributions 			boolean = true;
300*c54f35caSApple OSS Distributions 		} else {
301*c54f35caSApple OSS Distributions 			if (currentChar() != 'f') return SYNTAX_ERROR;
302*c54f35caSApple OSS Distributions 			if (nextChar() != 'a') return SYNTAX_ERROR;
303*c54f35caSApple OSS Distributions 			if (nextChar() != 'l') return SYNTAX_ERROR;
304*c54f35caSApple OSS Distributions 			if (nextChar() != 's') return SYNTAX_ERROR;
305*c54f35caSApple OSS Distributions 			if (nextChar() != 'e') return SYNTAX_ERROR;
306*c54f35caSApple OSS Distributions 		}
307*c54f35caSApple OSS Distributions 		if (nextChar() != '.') return SYNTAX_ERROR;
308*c54f35caSApple OSS Distributions 		/* skip over dot */
309*c54f35caSApple OSS Distributions 		(void)nextChar();
310*c54f35caSApple OSS Distributions 
311*c54f35caSApple OSS Distributions 		yylval = (object_t *)boolean;
312*c54f35caSApple OSS Distributions 		return BOOLEAN;
313*c54f35caSApple OSS Distributions 	}
314*c54f35caSApple OSS Distributions 
315*c54f35caSApple OSS Distributions 	/* parse unquoted string */
316*c54f35caSApple OSS Distributions 	if (isAlpha(c)) {
317*c54f35caSApple OSS Distributions 		int start, length;
318*c54f35caSApple OSS Distributions 		char * tempString;
319*c54f35caSApple OSS Distributions 
320*c54f35caSApple OSS Distributions 		start = parseBufferIndex;
321*c54f35caSApple OSS Distributions 		/* find end of string */
322*c54f35caSApple OSS Distributions 		while (isAlphaNumeric(c)) {
323*c54f35caSApple OSS Distributions 			c = nextChar();
324*c54f35caSApple OSS Distributions 		}
325*c54f35caSApple OSS Distributions 		length = parseBufferIndex - start;
326*c54f35caSApple OSS Distributions 
327*c54f35caSApple OSS Distributions 		/* copy to null terminated buffer */
328*c54f35caSApple OSS Distributions 		tempString = (char *)malloc(length + 1);
329*c54f35caSApple OSS Distributions 		if (tempString == NULL) {
330*c54f35caSApple OSS Distributions 			printf("OSUnserialize: can't alloc temp memory\n");
331*c54f35caSApple OSS Distributions 			return 0;
332*c54f35caSApple OSS Distributions 		}
333*c54f35caSApple OSS Distributions 		bcopy(&parseBuffer[start], tempString, length);
334*c54f35caSApple OSS Distributions 		tempString[length] = 0;
335*c54f35caSApple OSS Distributions 		yylval = (object_t *)tempString;
336*c54f35caSApple OSS Distributions 		return STRING;
337*c54f35caSApple OSS Distributions 	}
338*c54f35caSApple OSS Distributions 
339*c54f35caSApple OSS Distributions 	/* parse quoted string */
340*c54f35caSApple OSS Distributions 	if (c == '"' || c == '\'') {
341*c54f35caSApple OSS Distributions 		int start, length;
342*c54f35caSApple OSS Distributions 		char * tempString;
343*c54f35caSApple OSS Distributions 		char quoteChar = c;
344*c54f35caSApple OSS Distributions 
345*c54f35caSApple OSS Distributions 		start = parseBufferIndex + 1;		// skip quote
346*c54f35caSApple OSS Distributions 		/* find end of string, line, buffer */
347*c54f35caSApple OSS Distributions 		while ((c = nextChar()) != quoteChar) {
348*c54f35caSApple OSS Distributions 			if (c == '\\') c = nextChar();
349*c54f35caSApple OSS Distributions 			if (c == '\n') lineNumber++;
350*c54f35caSApple OSS Distributions 			if (c == 0) return SYNTAX_ERROR;
351*c54f35caSApple OSS Distributions 		}
352*c54f35caSApple OSS Distributions 		length = parseBufferIndex - start;
353*c54f35caSApple OSS Distributions 		/* skip over trailing quote */
354*c54f35caSApple OSS Distributions 		(void)nextChar();
355*c54f35caSApple OSS Distributions 		/* copy to null terminated buffer */
356*c54f35caSApple OSS Distributions 		tempString = (char *)malloc(length + 1);
357*c54f35caSApple OSS Distributions 		if (tempString == NULL) {
358*c54f35caSApple OSS Distributions 			printf("OSUnserialize: can't alloc temp memory\n");
359*c54f35caSApple OSS Distributions 			return 0;
360*c54f35caSApple OSS Distributions 		}
361*c54f35caSApple OSS Distributions 
362*c54f35caSApple OSS Distributions 		int to = 0;
363*c54f35caSApple OSS Distributions 		for (int from=start; from < parseBufferIndex; from++) {
364*c54f35caSApple OSS Distributions 			// hack - skip over backslashes
365*c54f35caSApple OSS Distributions 			if (parseBuffer[from] == '\\') {
366*c54f35caSApple OSS Distributions 				length--;
367*c54f35caSApple OSS Distributions 				continue;
368*c54f35caSApple OSS Distributions 			}
369*c54f35caSApple OSS Distributions 			tempString[to] = parseBuffer[from];
370*c54f35caSApple OSS Distributions 			to++;
371*c54f35caSApple OSS Distributions 		}
372*c54f35caSApple OSS Distributions 		tempString[length] = 0;
373*c54f35caSApple OSS Distributions 		yylval = (object_t *)tempString;
374*c54f35caSApple OSS Distributions 		return STRING;
375*c54f35caSApple OSS Distributions 	}
376*c54f35caSApple OSS Distributions 
377*c54f35caSApple OSS Distributions 	/* process numbers */
378*c54f35caSApple OSS Distributions 	if (isDigit (c))
379*c54f35caSApple OSS Distributions 	{
380*c54f35caSApple OSS Distributions 		unsigned long long n = 0;
381*c54f35caSApple OSS Distributions 		int base = 10;
382*c54f35caSApple OSS Distributions 
383*c54f35caSApple OSS Distributions 		if (c == '0') {
384*c54f35caSApple OSS Distributions 			c = nextChar();
385*c54f35caSApple OSS Distributions 			if (c == 'x') {
386*c54f35caSApple OSS Distributions 				base = 16;
387*c54f35caSApple OSS Distributions 				c = nextChar();
388*c54f35caSApple OSS Distributions 			}
389*c54f35caSApple OSS Distributions 		}
390*c54f35caSApple OSS Distributions 		if (base == 10) {
391*c54f35caSApple OSS Distributions 			while(isDigit(c)) {
392*c54f35caSApple OSS Distributions 				n = (n * base + c - '0');
393*c54f35caSApple OSS Distributions 				c = nextChar();
394*c54f35caSApple OSS Distributions 			}
395*c54f35caSApple OSS Distributions 		} else {
396*c54f35caSApple OSS Distributions 			while(isHexDigit(c)) {
397*c54f35caSApple OSS Distributions 				if (isDigit(c)) {
398*c54f35caSApple OSS Distributions 					n = (n * base + c - '0');
399*c54f35caSApple OSS Distributions 				} else {
400*c54f35caSApple OSS Distributions 					n = (n * base + 0xa + c - 'a');
401*c54f35caSApple OSS Distributions 				}
402*c54f35caSApple OSS Distributions 				c = nextChar();
403*c54f35caSApple OSS Distributions 			}
404*c54f35caSApple OSS Distributions 		}
405*c54f35caSApple OSS Distributions 
406*c54f35caSApple OSS Distributions 		yylval = newObject();
407*c54f35caSApple OSS Distributions 		yylval->u.offset = n;
408*c54f35caSApple OSS Distributions 
409*c54f35caSApple OSS Distributions 		return NUMBER;
410*c54f35caSApple OSS Distributions 	}
411*c54f35caSApple OSS Distributions 
412*c54f35caSApple OSS Distributions #define OSDATA_ALLOC_SIZE 4096
413*c54f35caSApple OSS Distributions 
414*c54f35caSApple OSS Distributions 	/* process data */
415*c54f35caSApple OSS Distributions 	if (c == '<') {
416*c54f35caSApple OSS Distributions 		unsigned char *d, *start, *lastStart;
417*c54f35caSApple OSS Distributions 
418*c54f35caSApple OSS Distributions 		size_t buflen = OSDATA_ALLOC_SIZE;
419*c54f35caSApple OSS Distributions 		start = lastStart = d = (unsigned char *)malloc(buflen);
420*c54f35caSApple OSS Distributions 		c = nextChar();	// skip over '<'
421*c54f35caSApple OSS Distributions 		while (c != 0 && c != '>') {
422*c54f35caSApple OSS Distributions 
423*c54f35caSApple OSS Distributions 			if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
424*c54f35caSApple OSS Distributions 			if (c == '#') while ((c = nextChar()) != 0 && c != '\n') {};
425*c54f35caSApple OSS Distributions 			if (c == '\n') {
426*c54f35caSApple OSS Distributions 				lineNumber++;
427*c54f35caSApple OSS Distributions 				c = nextChar();
428*c54f35caSApple OSS Distributions 				continue;
429*c54f35caSApple OSS Distributions 			}
430*c54f35caSApple OSS Distributions 
431*c54f35caSApple OSS Distributions 			// get high nibble
432*c54f35caSApple OSS Distributions 			if (!isHexDigit(c)) break;
433*c54f35caSApple OSS Distributions 			if (isDigit(c)) {
434*c54f35caSApple OSS Distributions 				*d = (c - '0') << 4;
435*c54f35caSApple OSS Distributions 			} else {
436*c54f35caSApple OSS Distributions 				*d =  (0xa + (c - 'a')) << 4;
437*c54f35caSApple OSS Distributions 			}
438*c54f35caSApple OSS Distributions 
439*c54f35caSApple OSS Distributions 			// get low nibble
440*c54f35caSApple OSS Distributions 			c = nextChar();
441*c54f35caSApple OSS Distributions 			if (!isHexDigit(c)) break;
442*c54f35caSApple OSS Distributions 			if (isDigit(c)) {
443*c54f35caSApple OSS Distributions 				*d |= c - '0';
444*c54f35caSApple OSS Distributions 			} else {
445*c54f35caSApple OSS Distributions 				*d |= 0xa + (c - 'a');
446*c54f35caSApple OSS Distributions 			}
447*c54f35caSApple OSS Distributions 
448*c54f35caSApple OSS Distributions 			d++;
449*c54f35caSApple OSS Distributions 			if ((d - lastStart) >= OSDATA_ALLOC_SIZE) {
450*c54f35caSApple OSS Distributions 				int oldsize = d - start;
451*c54f35caSApple OSS Distributions 				assert(buflen == oldsize);
452*c54f35caSApple OSS Distributions 				start = (unsigned char *)realloc(start, oldsize, buflen);
453*c54f35caSApple OSS Distributions 				d = lastStart = start + oldsize;
454*c54f35caSApple OSS Distributions 			}
455*c54f35caSApple OSS Distributions 			c = nextChar();
456*c54f35caSApple OSS Distributions 		}
457*c54f35caSApple OSS Distributions 		if (c != '>' ) {
458*c54f35caSApple OSS Distributions 			safe_free(start, buflen);
459*c54f35caSApple OSS Distributions 			return SYNTAX_ERROR;
460*c54f35caSApple OSS Distributions 		}
461*c54f35caSApple OSS Distributions 
462*c54f35caSApple OSS Distributions 		// got it!
463*c54f35caSApple OSS Distributions 		yylval = newObject();
464*c54f35caSApple OSS Distributions 		yylval->object = start;
465*c54f35caSApple OSS Distributions 		yylval->size = d - start;
466*c54f35caSApple OSS Distributions 
467*c54f35caSApple OSS Distributions 		(void)nextChar();	// skip over '>'
468*c54f35caSApple OSS Distributions 		return DATA;
469*c54f35caSApple OSS Distributions 	}
470*c54f35caSApple OSS Distributions 
471*c54f35caSApple OSS Distributions 
472*c54f35caSApple OSS Distributions 	/* return single chars, move pointer to next char */
473*c54f35caSApple OSS Distributions 	(void)nextChar();
474*c54f35caSApple OSS Distributions 	return c;
475*c54f35caSApple OSS Distributions }
476*c54f35caSApple OSS Distributions 
477*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
478*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
479*c54f35caSApple OSS Distributions // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
480*c54f35caSApple OSS Distributions 
481*c54f35caSApple OSS Distributions #if DEBUG
482*c54f35caSApple OSS Distributions int debugUnserializeAllocCount = 0;
483*c54f35caSApple OSS Distributions #endif
484*c54f35caSApple OSS Distributions 
485*c54f35caSApple OSS Distributions object_t *
newObject()486*c54f35caSApple OSS Distributions newObject()
487*c54f35caSApple OSS Distributions {
488*c54f35caSApple OSS Distributions #if DEBUG
489*c54f35caSApple OSS Distributions 	debugUnserializeAllocCount++;
490*c54f35caSApple OSS Distributions #endif
491*c54f35caSApple OSS Distributions 	return malloc_type(object_t);
492*c54f35caSApple OSS Distributions }
493*c54f35caSApple OSS Distributions 
494*c54f35caSApple OSS Distributions void
freeObject(object_t * o)495*c54f35caSApple OSS Distributions freeObject(object_t *o)
496*c54f35caSApple OSS Distributions {
497*c54f35caSApple OSS Distributions #if DEBUG
498*c54f35caSApple OSS Distributions 	debugUnserializeAllocCount--;
499*c54f35caSApple OSS Distributions #endif
500*c54f35caSApple OSS Distributions 	free_type(object_t, o);
501*c54f35caSApple OSS Distributions }
502*c54f35caSApple OSS Distributions 
503*c54f35caSApple OSS Distributions static OSDictionary *tags;
504*c54f35caSApple OSS Distributions 
505*c54f35caSApple OSS Distributions static void
rememberObject(int tag,object_t * o)506*c54f35caSApple OSS Distributions rememberObject(int tag, object_t *o)
507*c54f35caSApple OSS Distributions {
508*c54f35caSApple OSS Distributions 	char key[16];
509*c54f35caSApple OSS Distributions 	snprintf(key, sizeof(key), "%u", tag);
510*c54f35caSApple OSS Distributions 
511*c54f35caSApple OSS Distributions 	tags->setObject(key, (OSObject *)o);
512*c54f35caSApple OSS Distributions }
513*c54f35caSApple OSS Distributions 
514*c54f35caSApple OSS Distributions static OSObject *
retrieveObject(int tag)515*c54f35caSApple OSS Distributions retrieveObject(int tag)
516*c54f35caSApple OSS Distributions {
517*c54f35caSApple OSS Distributions 	char key[16];
518*c54f35caSApple OSS Distributions 	snprintf(key, sizeof(key), "%u", tag);
519*c54f35caSApple OSS Distributions 
520*c54f35caSApple OSS Distributions 	return tags->getObject(key);
521*c54f35caSApple OSS Distributions }
522*c54f35caSApple OSS Distributions 
523*c54f35caSApple OSS Distributions OSObject *
buildOSDictionary(object_t * o)524*c54f35caSApple OSS Distributions buildOSDictionary(object_t *o)
525*c54f35caSApple OSS Distributions {
526*c54f35caSApple OSS Distributions 	object_t *temp, *last = o;
527*c54f35caSApple OSS Distributions 	int count = 0;
528*c54f35caSApple OSS Distributions 
529*c54f35caSApple OSS Distributions 	// get count and last object
530*c54f35caSApple OSS Distributions 	while (o) {
531*c54f35caSApple OSS Distributions 		count++;
532*c54f35caSApple OSS Distributions 		last = o;
533*c54f35caSApple OSS Distributions 		o = o->next;
534*c54f35caSApple OSS Distributions 	}
535*c54f35caSApple OSS Distributions 	o = last;
536*c54f35caSApple OSS Distributions 
537*c54f35caSApple OSS Distributions 	OSDictionary *d = OSDictionary::withCapacity(count);
538*c54f35caSApple OSS Distributions 
539*c54f35caSApple OSS Distributions 	while (o) {
540*c54f35caSApple OSS Distributions #ifdef metaclass_stuff_worksXXX
541*c54f35caSApple OSS Distributions 		if (((OSObject *)o->u.key)->metaCast("OSSymbol")) {
542*c54f35caSApple OSS Distributions 			// XXX the evil frontdoor
543*c54f35caSApple OSS Distributions 			d->setObject((OSSymbol *)o->u.key, (OSObject *)o->object);
544*c54f35caSApple OSS Distributions 		} else {
545*c54f35caSApple OSS Distributions                         // If it isn't a symbol, I hope it's a string!
546*c54f35caSApple OSS Distributions 			d->setObject((OSString *)o->u.key, (OSObject *)o->object);
547*c54f35caSApple OSS Distributions 		}
548*c54f35caSApple OSS Distributions #else
549*c54f35caSApple OSS Distributions 		d->setObject((OSString *)o->u.key, (OSObject *)o->object);
550*c54f35caSApple OSS Distributions #endif
551*c54f35caSApple OSS Distributions 		((OSObject *)o->object)->release();
552*c54f35caSApple OSS Distributions 		((OSObject *)o->u.key)->release();
553*c54f35caSApple OSS Distributions 		temp = o;
554*c54f35caSApple OSS Distributions 		o = o->prev;
555*c54f35caSApple OSS Distributions 		freeObject(temp);
556*c54f35caSApple OSS Distributions 	}
557*c54f35caSApple OSS Distributions 	return d;
558*c54f35caSApple OSS Distributions };
559*c54f35caSApple OSS Distributions 
560*c54f35caSApple OSS Distributions OSObject *
buildOSArray(object_t * o)561*c54f35caSApple OSS Distributions buildOSArray(object_t *o)
562*c54f35caSApple OSS Distributions {
563*c54f35caSApple OSS Distributions 	object_t *temp, *last = o;
564*c54f35caSApple OSS Distributions 	int count = 0;
565*c54f35caSApple OSS Distributions 
566*c54f35caSApple OSS Distributions 	// get count and last object
567*c54f35caSApple OSS Distributions 	while (o) {
568*c54f35caSApple OSS Distributions 		count++;
569*c54f35caSApple OSS Distributions 		last = o;
570*c54f35caSApple OSS Distributions 		o = o->next;
571*c54f35caSApple OSS Distributions 	}
572*c54f35caSApple OSS Distributions 	o = last;
573*c54f35caSApple OSS Distributions 
574*c54f35caSApple OSS Distributions 	OSArray *a = OSArray::withCapacity(count);
575*c54f35caSApple OSS Distributions 
576*c54f35caSApple OSS Distributions 	while (o) {
577*c54f35caSApple OSS Distributions 		a->setObject((OSObject *)o->object);
578*c54f35caSApple OSS Distributions 		((OSObject *)o->object)->release();
579*c54f35caSApple OSS Distributions 		temp = o;
580*c54f35caSApple OSS Distributions 		o = o->prev;
581*c54f35caSApple OSS Distributions 		freeObject(temp);
582*c54f35caSApple OSS Distributions 	}
583*c54f35caSApple OSS Distributions 	return a;
584*c54f35caSApple OSS Distributions };
585*c54f35caSApple OSS Distributions 
586*c54f35caSApple OSS Distributions OSObject *
buildOSSet(object_t * o)587*c54f35caSApple OSS Distributions buildOSSet(object_t *o)
588*c54f35caSApple OSS Distributions {
589*c54f35caSApple OSS Distributions 	OSArray *a = (OSArray *)buildOSArray(o);
590*c54f35caSApple OSS Distributions 	OSSet *s = OSSet::withArray(a, a->getCapacity());
591*c54f35caSApple OSS Distributions 
592*c54f35caSApple OSS Distributions 	a->release();
593*c54f35caSApple OSS Distributions 	return s;
594*c54f35caSApple OSS Distributions };
595*c54f35caSApple OSS Distributions 
596*c54f35caSApple OSS Distributions OSObject *
buildOSString(object_t * o)597*c54f35caSApple OSS Distributions buildOSString(object_t *o)
598*c54f35caSApple OSS Distributions {
599*c54f35caSApple OSS Distributions 	OSString *s = OSString::withCString((char *)o);
600*c54f35caSApple OSS Distributions 
601*c54f35caSApple OSS Distributions 	safe_free(o, strlen((char *)o) + 1);
602*c54f35caSApple OSS Distributions 
603*c54f35caSApple OSS Distributions 	return s;
604*c54f35caSApple OSS Distributions };
605*c54f35caSApple OSS Distributions 
606*c54f35caSApple OSS Distributions OSObject *
buildOSData(object_t * o)607*c54f35caSApple OSS Distributions buildOSData(object_t *o)
608*c54f35caSApple OSS Distributions {
609*c54f35caSApple OSS Distributions 	OSData *d;
610*c54f35caSApple OSS Distributions 
611*c54f35caSApple OSS Distributions 	if (o->size) {
612*c54f35caSApple OSS Distributions 		d = OSData::withBytes(o->object, o->size);
613*c54f35caSApple OSS Distributions 	} else {
614*c54f35caSApple OSS Distributions 		d = OSData::withCapacity(0);
615*c54f35caSApple OSS Distributions 	}
616*c54f35caSApple OSS Distributions 	safe_free(o->object, o->size);
617*c54f35caSApple OSS Distributions 	freeObject(o);
618*c54f35caSApple OSS Distributions 	return d;
619*c54f35caSApple OSS Distributions };
620*c54f35caSApple OSS Distributions 
621*c54f35caSApple OSS Distributions OSObject *
buildOSOffset(object_t * o)622*c54f35caSApple OSS Distributions buildOSOffset(object_t *o)
623*c54f35caSApple OSS Distributions {
624*c54f35caSApple OSS Distributions 	OSNumber *off = OSNumber::withNumber(o->u.offset, o->size);
625*c54f35caSApple OSS Distributions 	freeObject(o);
626*c54f35caSApple OSS Distributions 	return off;
627*c54f35caSApple OSS Distributions };
628*c54f35caSApple OSS Distributions 
629*c54f35caSApple OSS Distributions OSObject *
buildOSBoolean(object_t * o)630*c54f35caSApple OSS Distributions buildOSBoolean(object_t *o)
631*c54f35caSApple OSS Distributions {
632*c54f35caSApple OSS Distributions 	OSBoolean *b = OSBoolean::withBoolean((bool)o);
633*c54f35caSApple OSS Distributions 	return b;
634*c54f35caSApple OSS Distributions };
635*c54f35caSApple OSS Distributions 
636*c54f35caSApple OSS Distributions __BEGIN_DECLS
637*c54f35caSApple OSS Distributions #include <kern/locks.h>
638*c54f35caSApple OSS Distributions __END_DECLS
639*c54f35caSApple OSS Distributions 
640*c54f35caSApple OSS Distributions static lck_mtx_t *lock = 0;
641*c54f35caSApple OSS Distributions extern lck_grp_t *IOLockGroup;
642*c54f35caSApple OSS Distributions 
643*c54f35caSApple OSS Distributions OSObject*
OSUnserialize(const char * buffer,OSString ** errorString)644*c54f35caSApple OSS Distributions OSUnserialize(const char *buffer, OSString **errorString)
645*c54f35caSApple OSS Distributions {
646*c54f35caSApple OSS Distributions 	OSObject *object;
647*c54f35caSApple OSS Distributions 
648*c54f35caSApple OSS Distributions 	if (!lock) {
649*c54f35caSApple OSS Distributions 		lock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL);
650*c54f35caSApple OSS Distributions 		lck_mtx_lock(lock);
651*c54f35caSApple OSS Distributions 	} else {
652*c54f35caSApple OSS Distributions 		lck_mtx_lock(lock);
653*c54f35caSApple OSS Distributions 
654*c54f35caSApple OSS Distributions 	}
655*c54f35caSApple OSS Distributions 
656*c54f35caSApple OSS Distributions #if DEBUG
657*c54f35caSApple OSS Distributions 	debugUnserializeAllocCount = 0;
658*c54f35caSApple OSS Distributions #endif
659*c54f35caSApple OSS Distributions 	yyerror_message[0] = 0;	//just in case
660*c54f35caSApple OSS Distributions 	parseBuffer = buffer;
661*c54f35caSApple OSS Distributions 	parseBufferIndex = 0;
662*c54f35caSApple OSS Distributions 	tags = OSDictionary::withCapacity(128);
663*c54f35caSApple OSS Distributions 	if (yyparse() == 0) {
664*c54f35caSApple OSS Distributions 		object = parsedObject;
665*c54f35caSApple OSS Distributions 		if (errorString) *errorString = NULL;
666*c54f35caSApple OSS Distributions 	} else {
667*c54f35caSApple OSS Distributions 		object = NULL;
668*c54f35caSApple OSS Distributions 		if (errorString)
669*c54f35caSApple OSS Distributions 			*errorString = OSString::withCString(yyerror_message);
670*c54f35caSApple OSS Distributions 	}
671*c54f35caSApple OSS Distributions 
672*c54f35caSApple OSS Distributions 	tags->release();
673*c54f35caSApple OSS Distributions #if DEBUG
674*c54f35caSApple OSS Distributions 	if (debugUnserializeAllocCount) {
675*c54f35caSApple OSS Distributions 		printf("OSUnserialize: allocation check failed, count = %d.\n",
676*c54f35caSApple OSS Distributions 		       debugUnserializeAllocCount);
677*c54f35caSApple OSS Distributions 	}
678*c54f35caSApple OSS Distributions #endif
679*c54f35caSApple OSS Distributions 	lck_mtx_unlock(lock);
680*c54f35caSApple OSS Distributions 
681*c54f35caSApple OSS Distributions 	return object;
682*c54f35caSApple OSS Distributions }
683*c54f35caSApple OSS Distributions 
684*c54f35caSApple OSS Distributions #endif // not __clang_analyzer__
685*c54f35caSApple OSS Distributions 
686*c54f35caSApple OSS Distributions 
687*c54f35caSApple OSS Distributions //
688*c54f35caSApple OSS Distributions //
689*c54f35caSApple OSS Distributions //
690*c54f35caSApple OSS Distributions //
691*c54f35caSApple OSS Distributions //
692*c54f35caSApple OSS Distributions //		 DO NOT EDIT OSUnserialize.cpp!
693*c54f35caSApple OSS Distributions //
694*c54f35caSApple OSS Distributions //			this means you!
695*c54f35caSApple OSS Distributions //
696*c54f35caSApple OSS Distributions //
697*c54f35caSApple OSS Distributions //
698*c54f35caSApple OSS Distributions //
699*c54f35caSApple OSS Distributions //
700