2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* OSUnserialize.y created by rsulack on Nov 21 1998 */
31 // "classic" parser for unserializing OSContainer objects
33 // XXX - this code should really be removed!
34 // - the XML format is now prefered
35 // - this code leaks on syntax errors, the XML doesn't
36 // - "classic" looks, reads, ... much better than XML :-(
37 // - well except the XML is more efficent on OSData
41 // bison -p OSUnserialize OSUnserialize.y
42 // head -50 OSUnserialize.y > OSUnserialize.cpp
43 // sed -e "s/stdio.h/stddef.h/" < OSUnserialize.tab.c >> OSUnserialize.cpp
45 // when changing code check in both OSUnserialize.y and OSUnserialize.cpp
50 // DO NOT EDIT OSUnserialize.tab.cpp!
61 #include <libkern/c++/OSMetaClass.h>
62 #include <libkern/c++/OSContainers.h>
63 #include <libkern/c++/OSLib.h>
65 typedef struct object {
71 void *key; // for dictionary
72 long long offset; // for offset
78 static int yyerror(char *s);
81 static object_t * newObject();
82 static void freeObject(object_t *o);
84 static OSObject *buildOSDictionary(object_t *);
85 static OSObject *buildOSArray(object_t *);
86 static OSObject *buildOSSet(object_t *);
87 static OSObject *buildOSString(object_t *);
88 static OSObject *buildOSData(object_t *);
89 static OSObject *buildOSOffset(object_t *);
90 static OSObject *buildOSBoolean(object_t *o);
92 static void rememberObject(int, object_t *);
93 static OSObject *retrieveObject(int);
95 // temp variable to use during parsing
98 // resultant object of parsed text
99 static OSObject *parsedObject;
101 #define YYSTYPE object_t *
104 extern void *kern_os_malloc(size_t size);
105 extern void *kern_os_realloc(void * addr, size_t size);
106 extern void kern_os_free(void * addr);
109 #define malloc(s) kern_os_malloc(s)
110 #define realloc(a, s) kern_os_realloc(a, s)
111 #define free(a) kern_os_free(a)
120 %% /* Grammar rules and actions follow */
122 input: /* empty */ { parsedObject = (OSObject *)NULL; YYACCEPT; }
123 | object { parsedObject = (OSObject *)$1; YYACCEPT; }
124 | SYNTAX_ERROR { yyerror("syntax error"); YYERROR; }
127 object: dict { $$ = (object_t *)buildOSDictionary($1); }
128 | array { $$ = (object_t *)buildOSArray($1); }
129 | set { $$ = (object_t *)buildOSSet($1); }
130 | string { $$ = (object_t *)buildOSString($1); }
131 | data { $$ = (object_t *)buildOSData($1); }
132 | offset { $$ = (object_t *)buildOSOffset($1); }
133 | boolean { $$ = (object_t *)buildOSBoolean($1); }
134 | '@' NUMBER { $$ = (object_t *)retrieveObject($2->u.offset);
136 ((OSObject *)$$)->retain();
138 yyerror("forward reference detected");
143 | object '@' NUMBER { $$ = $1;
144 rememberObject($3->u.offset, $1);
149 //------------------------------------------------------------------------------
151 dict: '{' '}' { $$ = NULL; }
152 | '{' pairs '}' { $$ = $2; }
156 | pairs pair { $2->next = $1; $1->prev = $2; $$ = $2; }
159 pair: object '=' object ';' { $$ = newObject();
167 //------------------------------------------------------------------------------
169 array: '(' ')' { $$ = NULL; }
170 | '(' elements ')' { $$ = $2; }
173 set: '[' ']' { $$ = NULL; }
174 | '[' elements ']' { $$ = $2; }
177 elements: object { $$ = newObject();
182 | elements ',' object { o = newObject();
191 //------------------------------------------------------------------------------
193 offset: NUMBER ':' NUMBER { $$ = $1;
194 $$->size = $3->u.offset;
199 //------------------------------------------------------------------------------
204 //------------------------------------------------------------------------------
209 //------------------------------------------------------------------------------
216 static int lineNumber = 0;
217 static const char *parseBuffer;
218 static int parseBufferIndex;
220 #define currentChar() (parseBuffer[parseBufferIndex])
221 #define nextChar() (parseBuffer[++parseBufferIndex])
222 #define prevChar() (parseBuffer[parseBufferIndex - 1])
224 #define isSpace(c) ((c) == ' ' || (c) == '\t')
225 #define isAlpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
226 #define isDigit(c) ((c) >= '0' && (c) <= '9')
227 #define isAlphaDigit(c) ((c) >= 'a' && (c) <= 'f')
228 #define isHexDigit(c) (isDigit(c) || isAlphaDigit(c))
229 #define isAlphaNumeric(c) (isAlpha(c) || isDigit(c) || ((c) == '-'))
231 static char yyerror_message[128];
234 yyerror(char *s) /* Called by yyparse on error */
236 sprintf(yyerror_message, "OSUnserialize: %s near line %d\n", s, lineNumber);
245 if (parseBufferIndex == 0) lineNumber = 1;
250 /* skip white space */
251 if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
253 /* skip over comments */
254 if (c == '#') while ((c = nextChar()) != 0 && c != '\n') {};
256 /* keep track of line number, don't return \n's */
265 bool boolean = false;
266 if (nextChar() == 't') {
267 if (nextChar() != 'r') return SYNTAX_ERROR;
268 if (nextChar() != 'u') return SYNTAX_ERROR;
269 if (nextChar() != 'e') return SYNTAX_ERROR;
272 if (currentChar() != 'f') return SYNTAX_ERROR;
273 if (nextChar() != 'a') return SYNTAX_ERROR;
274 if (nextChar() != 'l') return SYNTAX_ERROR;
275 if (nextChar() != 's') return SYNTAX_ERROR;
276 if (nextChar() != 'e') return SYNTAX_ERROR;
278 if (nextChar() != '.') return SYNTAX_ERROR;
282 yylval = (object_t *)boolean;
286 /* parse unquoted string */
291 start = parseBufferIndex;
292 /* find end of string */
293 while (isAlphaNumeric(c)) {
296 length = parseBufferIndex - start;
298 /* copy to null terminated buffer */
299 tempString = (char *)malloc(length + 1);
300 if (tempString == 0) {
301 printf("OSUnserialize: can't alloc temp memory\n");
304 bcopy(&parseBuffer[start], tempString, length);
305 tempString[length] = 0;
306 yylval = (object_t *)tempString;
310 /* parse quoted string */
311 if (c == '"' || c == '\'') {
316 start = parseBufferIndex + 1; // skip quote
317 /* find end of string, line, buffer */
318 while ((c = nextChar()) != quoteChar) {
319 if (c == '\\') c = nextChar();
320 if (c == '\n') lineNumber++;
321 if (c == 0) return SYNTAX_ERROR;
323 length = parseBufferIndex - start;
324 /* skip over trailing quote */
326 /* copy to null terminated buffer */
327 tempString = (char *)malloc(length + 1);
328 if (tempString == 0) {
329 printf("OSUnserialize: can't alloc temp memory\n");
334 for (int from=start; from < parseBufferIndex; from++) {
335 // hack - skip over backslashes
336 if (parseBuffer[from] == '\\') {
340 tempString[to] = parseBuffer[from];
343 tempString[length] = 0;
344 yylval = (object_t *)tempString;
348 /* process numbers */
351 unsigned long long n = 0;
363 n = (n * base + c - '0');
367 while(isHexDigit(c)) {
369 n = (n * base + c - '0');
371 n = (n * base + 0xa + c - 'a');
377 yylval = newObject();
378 yylval->u.offset = n;
383 #define OSDATA_ALLOC_SIZE 4096
387 unsigned char *d, *start, *lastStart;
389 start = lastStart = d = (unsigned char *)malloc(OSDATA_ALLOC_SIZE);
390 c = nextChar(); // skip over '<'
391 while (c != 0 && c != '>') {
393 if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
394 if (c == '#') while ((c = nextChar()) != 0 && c != '\n') {};
402 if (!isHexDigit(c)) break;
406 *d = (0xa + (c - 'a')) << 4;
411 if (!isHexDigit(c)) break;
415 *d |= 0xa + (c - 'a');
419 if ((d - lastStart) >= OSDATA_ALLOC_SIZE) {
420 int oldsize = d - start;
421 start = (unsigned char *)realloc(start, oldsize + OSDATA_ALLOC_SIZE);
422 d = lastStart = start + oldsize;
432 yylval = newObject();
433 yylval->object = start;
434 yylval->size = d - start;
436 (void)nextChar(); // skip over '>'
441 /* return single chars, move pointer to next char */
446 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
447 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
448 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
451 int debugUnserializeAllocCount = 0;
458 debugUnserializeAllocCount++;
460 return (object_t *)malloc(sizeof(object_t));
464 freeObject(object_t *o)
467 debugUnserializeAllocCount--;
472 static OSDictionary *tags;
475 rememberObject(int tag, object_t *o)
478 sprintf(key, "%u", tag);
480 tags->setObject(key, (OSObject *)o);
484 retrieveObject(int tag)
487 sprintf(key, "%u", tag);
489 return tags->getObject(key);
493 buildOSDictionary(object_t *o)
495 object_t *temp, *last = o;
498 // get count and last object
506 OSDictionary *d = OSDictionary::withCapacity(count);
509 #ifdef metaclass_stuff_worksXXX
510 if (((OSObject *)o->u.key)->metaCast("OSSymbol")) {
511 // XXX the evil frontdoor
512 d->setObject((OSSymbol *)o->u.key, (OSObject *)o->object);
514 // If it isn't a symbol, I hope it's a string!
515 d->setObject((OSString *)o->u.key, (OSObject *)o->object);
518 d->setObject((OSString *)o->u.key, (OSObject *)o->object);
520 ((OSObject *)o->object)->release();
521 ((OSObject *)o->u.key)->release();
530 buildOSArray(object_t *o)
532 object_t *temp, *last = o;
535 // get count and last object
543 OSArray *a = OSArray::withCapacity(count);
546 a->setObject((OSObject *)o->object);
547 ((OSObject *)o->object)->release();
556 buildOSSet(object_t *o)
558 OSArray *a = (OSArray *)buildOSArray(o);
559 OSSet *s = OSSet::withArray(a, a->getCapacity());
566 buildOSString(object_t *o)
568 OSString *s = OSString::withCString((char *)o);
576 buildOSData(object_t *o)
581 d = OSData::withBytes(o->object, o->size);
583 d = OSData::withCapacity(0);
591 buildOSOffset(object_t *o)
593 OSNumber *off = OSNumber::withNumber(o->u.offset, o->size);
599 buildOSBoolean(object_t *o)
601 OSBoolean *b = OSBoolean::withBoolean((bool)o);
606 #include <kern/lock.h>
609 static mutex_t *lock = 0;
612 OSUnserialize(const char *buffer, OSString **errorString)
617 lock = mutex_alloc(ETAP_IO_AHA);
625 debugUnserializeAllocCount = 0;
627 yyerror_message[0] = 0; //just in case
628 parseBuffer = buffer;
629 parseBufferIndex = 0;
630 tags = OSDictionary::withCapacity(128);
631 if (yyparse() == 0) {
632 object = parsedObject;
633 if (errorString) *errorString = 0;
637 *errorString = OSString::withCString(yyerror_message);
642 if (debugUnserializeAllocCount) {
643 printf("OSUnserialize: allocation check failed, count = %d.\n",
644 debugUnserializeAllocCount);
658 // DO NOT EDIT OSUnserialize.cpp!