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
77 static int yyerror(const char *s);
80 static object_t * newObject();
81 static void freeObject(object_t *o);
83 static OSObject *buildOSDictionary(object_t *);
84 static OSObject *buildOSArray(object_t *);
85 static OSObject *buildOSSet(object_t *);
86 static OSObject *buildOSString(object_t *);
87 static OSObject *buildOSData(object_t *);
88 static OSObject *buildOSOffset(object_t *);
89 static OSObject *buildOSBoolean(object_t *o);
91 static void rememberObject(int, object_t *);
92 static OSObject *retrieveObject(int);
94 // temp variable to use during parsing
97 // resultant object of parsed text
98 static OSObject *parsedObject;
100 #define YYSTYPE object_t *
103 extern void *kern_os_malloc(size_t size);
104 extern void *kern_os_realloc(void * addr, size_t size);
105 extern void kern_os_free(void * addr);
108 #define malloc(s) kern_os_malloc(s)
109 #define realloc(a, s) kern_os_realloc(a, s)
110 #define free(a) kern_os_free(a)
119 %% /* Grammar rules and actions follow */
121 input: /* empty */ { parsedObject = (OSObject *)NULL; YYACCEPT; }
122 | object { parsedObject = (OSObject *)$1; YYACCEPT; }
123 | SYNTAX_ERROR { yyerror("syntax error"); YYERROR; }
126 object: dict { $$ = (object_t *)buildOSDictionary($1); }
127 | array { $$ = (object_t *)buildOSArray($1); }
128 | set { $$ = (object_t *)buildOSSet($1); }
129 | string { $$ = (object_t *)buildOSString($1); }
130 | data { $$ = (object_t *)buildOSData($1); }
131 | offset { $$ = (object_t *)buildOSOffset($1); }
132 | boolean { $$ = (object_t *)buildOSBoolean($1); }
133 | '@' NUMBER { $$ = (object_t *)retrieveObject($2->u.offset);
135 ((OSObject *)$$)->retain();
137 yyerror("forward reference detected");
142 | object '@' NUMBER { $$ = $1;
143 rememberObject($3->u.offset, $1);
148 //------------------------------------------------------------------------------
150 dict: '{' '}' { $$ = NULL; }
151 | '{' pairs '}' { $$ = $2; }
155 | pairs pair { $2->next = $1; $1->prev = $2; $$ = $2; }
158 pair: object '=' object ';' { $$ = newObject();
166 //------------------------------------------------------------------------------
168 array: '(' ')' { $$ = NULL; }
169 | '(' elements ')' { $$ = $2; }
172 set: '[' ']' { $$ = NULL; }
173 | '[' elements ']' { $$ = $2; }
176 elements: object { $$ = newObject();
181 | elements ',' object { oo = newObject();
190 //------------------------------------------------------------------------------
192 offset: NUMBER ':' NUMBER { $$ = $1;
193 $$->size = $3->u.offset;
198 //------------------------------------------------------------------------------
203 //------------------------------------------------------------------------------
208 //------------------------------------------------------------------------------
215 static int lineNumber = 0;
216 static const char *parseBuffer;
217 static int parseBufferIndex;
219 #define currentChar() (parseBuffer[parseBufferIndex])
220 #define nextChar() (parseBuffer[++parseBufferIndex])
221 #define prevChar() (parseBuffer[parseBufferIndex - 1])
223 #define isSpace(c) ((c) == ' ' || (c) == '\t')
224 #define isAlpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
225 #define isDigit(c) ((c) >= '0' && (c) <= '9')
226 #define isAlphaDigit(c) ((c) >= 'a' && (c) <= 'f')
227 #define isHexDigit(c) (isDigit(c) || isAlphaDigit(c))
228 #define isAlphaNumeric(c) (isAlpha(c) || isDigit(c) || ((c) == '-'))
230 static char yyerror_message[128];
233 yyerror(const char *s) /* Called by yyparse on error */
235 snprintf(yyerror_message, sizeof(yyerror_message), "OSUnserialize: %s near line %d\n", s, lineNumber);
244 if (parseBufferIndex == 0) lineNumber = 1;
249 /* skip white space */
250 if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
252 /* skip over comments */
253 if (c == '#') while ((c = nextChar()) != 0 && c != '\n') {};
255 /* keep track of line number, don't return \n's */
264 bool boolean = false;
265 if (nextChar() == 't') {
266 if (nextChar() != 'r') return SYNTAX_ERROR;
267 if (nextChar() != 'u') return SYNTAX_ERROR;
268 if (nextChar() != 'e') return SYNTAX_ERROR;
271 if (currentChar() != 'f') return SYNTAX_ERROR;
272 if (nextChar() != 'a') return SYNTAX_ERROR;
273 if (nextChar() != 'l') return SYNTAX_ERROR;
274 if (nextChar() != 's') return SYNTAX_ERROR;
275 if (nextChar() != 'e') return SYNTAX_ERROR;
277 if (nextChar() != '.') return SYNTAX_ERROR;
281 yylval = (object_t *)boolean;
285 /* parse unquoted string */
290 start = parseBufferIndex;
291 /* find end of string */
292 while (isAlphaNumeric(c)) {
295 length = parseBufferIndex - start;
297 /* copy to null terminated buffer */
298 tempString = (char *)malloc(length + 1);
299 if (tempString == 0) {
300 printf("OSUnserialize: can't alloc temp memory\n");
303 bcopy(&parseBuffer[start], tempString, length);
304 tempString[length] = 0;
305 yylval = (object_t *)tempString;
309 /* parse quoted string */
310 if (c == '"' || c == '\'') {
315 start = parseBufferIndex + 1; // skip quote
316 /* find end of string, line, buffer */
317 while ((c = nextChar()) != quoteChar) {
318 if (c == '\\') c = nextChar();
319 if (c == '\n') lineNumber++;
320 if (c == 0) return SYNTAX_ERROR;
322 length = parseBufferIndex - start;
323 /* skip over trailing quote */
325 /* copy to null terminated buffer */
326 tempString = (char *)malloc(length + 1);
327 if (tempString == 0) {
328 printf("OSUnserialize: can't alloc temp memory\n");
333 for (int from=start; from < parseBufferIndex; from++) {
334 // hack - skip over backslashes
335 if (parseBuffer[from] == '\\') {
339 tempString[to] = parseBuffer[from];
342 tempString[length] = 0;
343 yylval = (object_t *)tempString;
347 /* process numbers */
350 unsigned long long n = 0;
362 n = (n * base + c - '0');
366 while(isHexDigit(c)) {
368 n = (n * base + c - '0');
370 n = (n * base + 0xa + c - 'a');
376 yylval = newObject();
377 yylval->u.offset = n;
382 #define OSDATA_ALLOC_SIZE 4096
386 unsigned char *d, *start, *lastStart;
388 start = lastStart = d = (unsigned char *)malloc(OSDATA_ALLOC_SIZE);
389 c = nextChar(); // skip over '<'
390 while (c != 0 && c != '>') {
392 if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
393 if (c == '#') while ((c = nextChar()) != 0 && c != '\n') {};
401 if (!isHexDigit(c)) break;
405 *d = (0xa + (c - 'a')) << 4;
410 if (!isHexDigit(c)) break;
414 *d |= 0xa + (c - 'a');
418 if ((d - lastStart) >= OSDATA_ALLOC_SIZE) {
419 int oldsize = d - start;
420 start = (unsigned char *)realloc(start, oldsize + OSDATA_ALLOC_SIZE);
421 d = lastStart = start + oldsize;
431 yylval = newObject();
432 yylval->object = start;
433 yylval->size = d - start;
435 (void)nextChar(); // skip over '>'
440 /* return single chars, move pointer to next char */
445 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
446 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
447 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
450 int debugUnserializeAllocCount = 0;
457 debugUnserializeAllocCount++;
459 return (object_t *)malloc(sizeof(object_t));
463 freeObject(object_t *o)
466 debugUnserializeAllocCount--;
471 static OSDictionary *tags;
474 rememberObject(int tag, object_t *o)
477 snprintf(key, sizeof(key), "%u", tag);
479 tags->setObject(key, (OSObject *)o);
483 retrieveObject(int tag)
486 snprintf(key, sizeof(key), "%u", tag);
488 return tags->getObject(key);
492 buildOSDictionary(object_t *o)
494 object_t *temp, *last = o;
497 // get count and last object
505 OSDictionary *d = OSDictionary::withCapacity(count);
508 #ifdef metaclass_stuff_worksXXX
509 if (((OSObject *)o->u.key)->metaCast("OSSymbol")) {
510 // XXX the evil frontdoor
511 d->setObject((OSSymbol *)o->u.key, (OSObject *)o->object);
513 // If it isn't a symbol, I hope it's a string!
514 d->setObject((OSString *)o->u.key, (OSObject *)o->object);
517 d->setObject((OSString *)o->u.key, (OSObject *)o->object);
519 ((OSObject *)o->object)->release();
520 ((OSObject *)o->u.key)->release();
529 buildOSArray(object_t *o)
531 object_t *temp, *last = o;
534 // get count and last object
542 OSArray *a = OSArray::withCapacity(count);
545 a->setObject((OSObject *)o->object);
546 ((OSObject *)o->object)->release();
555 buildOSSet(object_t *o)
557 OSArray *a = (OSArray *)buildOSArray(o);
558 OSSet *s = OSSet::withArray(a, a->getCapacity());
565 buildOSString(object_t *o)
567 OSString *s = OSString::withCString((char *)o);
575 buildOSData(object_t *o)
580 d = OSData::withBytes(o->object, o->size);
582 d = OSData::withCapacity(0);
590 buildOSOffset(object_t *o)
592 OSNumber *off = OSNumber::withNumber(o->u.offset, o->size);
598 buildOSBoolean(object_t *o)
600 OSBoolean *b = OSBoolean::withBoolean((bool)o);
605 #include <kern/locks.h>
608 static lck_mtx_t *lock = 0;
609 extern lck_grp_t *IOLockGroup;
612 OSUnserialize(const char *buffer, OSString **errorString)
617 lock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL);
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);
647 lck_mtx_unlock(lock);
658 // DO NOT EDIT OSUnserialize.cpp!