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 #include <kern/kalloc.h>
106 #define malloc(size) malloc_impl(size)
108 malloc_impl(size_t size)
113 return kheap_alloc_tag_bt(KHEAP_DEFAULT, size,
114 (zalloc_flags_t) (Z_WAITOK | Z_ZERO),
115 VM_KERN_MEMORY_LIBKERN);
118 #define free(addr) free_impl(addr)
120 free_impl(void *addr)
122 kheap_free_addr(KHEAP_DEFAULT, addr);
125 safe_free(void *addr, size_t size)
129 kheap_free(KHEAP_DEFAULT, addr, size);
133 #define realloc(addr, osize, nsize) realloc_impl(addr, osize, nsize)
135 realloc_impl(void *addr, size_t osize, size_t nsize)
138 return malloc(nsize);
140 if (nsize == osize) {
143 void *nmem = malloc(nsize);
145 safe_free(addr, osize);
148 (void)memcpy(nmem, addr, (nsize > osize) ? osize : nsize);
149 safe_free(addr, osize);
161 %% /* Grammar rules and actions follow */
163 input: /* empty */ { parsedObject = (OSObject *)NULL; YYACCEPT; }
164 | object { parsedObject = (OSObject *)$1; YYACCEPT; }
165 | SYNTAX_ERROR { yyerror("syntax error"); YYERROR; }
168 object: dict { $$ = (object_t *)buildOSDictionary($1); }
169 | array { $$ = (object_t *)buildOSArray($1); }
170 | set { $$ = (object_t *)buildOSSet($1); }
171 | string { $$ = (object_t *)buildOSString($1); }
172 | data { $$ = (object_t *)buildOSData($1); }
173 | offset { $$ = (object_t *)buildOSOffset($1); }
174 | boolean { $$ = (object_t *)buildOSBoolean($1); }
175 | '@' NUMBER { $$ = (object_t *)retrieveObject($2->u.offset);
177 ((OSObject *)$$)->retain();
179 yyerror("forward reference detected");
184 | object '@' NUMBER { $$ = $1;
185 rememberObject($3->u.offset, $1);
190 //------------------------------------------------------------------------------
192 dict: '{' '}' { $$ = NULL; }
193 | '{' pairs '}' { $$ = $2; }
197 | pairs pair { $2->next = $1; $1->prev = $2; $$ = $2; }
200 pair: object '=' object ';' { $$ = newObject();
208 //------------------------------------------------------------------------------
210 array: '(' ')' { $$ = NULL; }
211 | '(' elements ')' { $$ = $2; }
214 set: '[' ']' { $$ = NULL; }
215 | '[' elements ']' { $$ = $2; }
218 elements: object { $$ = newObject();
223 | elements ',' object { oo = newObject();
232 //------------------------------------------------------------------------------
234 offset: NUMBER ':' NUMBER { $$ = $1;
235 $$->size = $3->u.offset;
240 //------------------------------------------------------------------------------
245 //------------------------------------------------------------------------------
250 //------------------------------------------------------------------------------
257 static int lineNumber = 0;
258 static const char *parseBuffer;
259 static int parseBufferIndex;
261 #define currentChar() (parseBuffer[parseBufferIndex])
262 #define nextChar() (parseBuffer[++parseBufferIndex])
263 #define prevChar() (parseBuffer[parseBufferIndex - 1])
265 #define isSpace(c) ((c) == ' ' || (c) == '\t')
266 #define isAlpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
267 #define isDigit(c) ((c) >= '0' && (c) <= '9')
268 #define isAlphaDigit(c) ((c) >= 'a' && (c) <= 'f')
269 #define isHexDigit(c) (isDigit(c) || isAlphaDigit(c))
270 #define isAlphaNumeric(c) (isAlpha(c) || isDigit(c) || ((c) == '-'))
272 static char yyerror_message[128];
275 yyerror(const char *s) /* Called by yyparse on error */
277 snprintf(yyerror_message, sizeof(yyerror_message), "OSUnserialize: %s near line %d\n", s, lineNumber);
286 if (parseBufferIndex == 0) lineNumber = 1;
291 /* skip white space */
292 if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
294 /* skip over comments */
295 if (c == '#') while ((c = nextChar()) != 0 && c != '\n') {};
297 /* keep track of line number, don't return \n's */
306 bool boolean = false;
307 if (nextChar() == 't') {
308 if (nextChar() != 'r') return SYNTAX_ERROR;
309 if (nextChar() != 'u') return SYNTAX_ERROR;
310 if (nextChar() != 'e') return SYNTAX_ERROR;
313 if (currentChar() != 'f') return SYNTAX_ERROR;
314 if (nextChar() != 'a') return SYNTAX_ERROR;
315 if (nextChar() != 'l') return SYNTAX_ERROR;
316 if (nextChar() != 's') return SYNTAX_ERROR;
317 if (nextChar() != 'e') return SYNTAX_ERROR;
319 if (nextChar() != '.') return SYNTAX_ERROR;
323 yylval = (object_t *)boolean;
327 /* parse unquoted string */
332 start = parseBufferIndex;
333 /* find end of string */
334 while (isAlphaNumeric(c)) {
337 length = parseBufferIndex - start;
339 /* copy to null terminated buffer */
340 tempString = (char *)malloc(length + 1);
341 if (tempString == NULL) {
342 printf("OSUnserialize: can't alloc temp memory\n");
345 bcopy(&parseBuffer[start], tempString, length);
346 tempString[length] = 0;
347 yylval = (object_t *)tempString;
351 /* parse quoted string */
352 if (c == '"' || c == '\'') {
357 start = parseBufferIndex + 1; // skip quote
358 /* find end of string, line, buffer */
359 while ((c = nextChar()) != quoteChar) {
360 if (c == '\\') c = nextChar();
361 if (c == '\n') lineNumber++;
362 if (c == 0) return SYNTAX_ERROR;
364 length = parseBufferIndex - start;
365 /* skip over trailing quote */
367 /* copy to null terminated buffer */
368 tempString = (char *)malloc(length + 1);
369 if (tempString == NULL) {
370 printf("OSUnserialize: can't alloc temp memory\n");
375 for (int from=start; from < parseBufferIndex; from++) {
376 // hack - skip over backslashes
377 if (parseBuffer[from] == '\\') {
381 tempString[to] = parseBuffer[from];
384 tempString[length] = 0;
385 yylval = (object_t *)tempString;
389 /* process numbers */
392 unsigned long long n = 0;
404 n = (n * base + c - '0');
408 while(isHexDigit(c)) {
410 n = (n * base + c - '0');
412 n = (n * base + 0xa + c - 'a');
418 yylval = newObject();
419 yylval->u.offset = n;
424 #define OSDATA_ALLOC_SIZE 4096
428 unsigned char *d, *start, *lastStart;
430 size_t buflen = OSDATA_ALLOC_SIZE;
431 start = lastStart = d = (unsigned char *)malloc(buflen);
432 c = nextChar(); // skip over '<'
433 while (c != 0 && c != '>') {
435 if (isSpace(c)) while ((c = nextChar()) != 0 && isSpace(c)) {};
436 if (c == '#') while ((c = nextChar()) != 0 && c != '\n') {};
444 if (!isHexDigit(c)) break;
448 *d = (0xa + (c - 'a')) << 4;
453 if (!isHexDigit(c)) break;
457 *d |= 0xa + (c - 'a');
461 if ((d - lastStart) >= OSDATA_ALLOC_SIZE) {
462 int oldsize = d - start;
463 assert(buflen == oldsize);
464 start = (unsigned char *)realloc(start, oldsize, buflen);
465 d = lastStart = start + oldsize;
470 safe_free(start, buflen);
475 yylval = newObject();
476 yylval->object = start;
477 yylval->size = d - start;
479 (void)nextChar(); // skip over '>'
484 /* return single chars, move pointer to next char */
489 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
490 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
491 // !@$&)(^Q$&*^!$(*!@$_(^%_(*Q#$(_*&!$_(*&!$_(*&!#$(*!@&^!@#%!_!#
494 int debugUnserializeAllocCount = 0;
501 debugUnserializeAllocCount++;
503 return (object_t *)malloc(sizeof(object_t));
507 freeObject(object_t *o)
510 debugUnserializeAllocCount--;
512 safe_free(o, sizeof(object_t));
515 static OSDictionary *tags;
518 rememberObject(int tag, object_t *o)
521 snprintf(key, sizeof(key), "%u", tag);
523 tags->setObject(key, (OSObject *)o);
527 retrieveObject(int tag)
530 snprintf(key, sizeof(key), "%u", tag);
532 return tags->getObject(key);
536 buildOSDictionary(object_t *o)
538 object_t *temp, *last = o;
541 // get count and last object
549 OSDictionary *d = OSDictionary::withCapacity(count);
552 #ifdef metaclass_stuff_worksXXX
553 if (((OSObject *)o->u.key)->metaCast("OSSymbol")) {
554 // XXX the evil frontdoor
555 d->setObject((OSSymbol *)o->u.key, (OSObject *)o->object);
557 // If it isn't a symbol, I hope it's a string!
558 d->setObject((OSString *)o->u.key, (OSObject *)o->object);
561 d->setObject((OSString *)o->u.key, (OSObject *)o->object);
563 ((OSObject *)o->object)->release();
564 ((OSObject *)o->u.key)->release();
573 buildOSArray(object_t *o)
575 object_t *temp, *last = o;
578 // get count and last object
586 OSArray *a = OSArray::withCapacity(count);
589 a->setObject((OSObject *)o->object);
590 ((OSObject *)o->object)->release();
599 buildOSSet(object_t *o)
601 OSArray *a = (OSArray *)buildOSArray(o);
602 OSSet *s = OSSet::withArray(a, a->getCapacity());
609 buildOSString(object_t *o)
611 OSString *s = OSString::withCString((char *)o);
613 safe_free(o, strlen((char *)o) + 1);
619 buildOSData(object_t *o)
624 d = OSData::withBytes(o->object, o->size);
626 d = OSData::withCapacity(0);
628 safe_free(o->object, o->size);
634 buildOSOffset(object_t *o)
636 OSNumber *off = OSNumber::withNumber(o->u.offset, o->size);
642 buildOSBoolean(object_t *o)
644 OSBoolean *b = OSBoolean::withBoolean((bool)o);
649 #include <kern/locks.h>
652 static lck_mtx_t *lock = 0;
653 extern lck_grp_t *IOLockGroup;
656 OSUnserialize(const char *buffer, OSString **errorString)
661 lock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL);
669 debugUnserializeAllocCount = 0;
671 yyerror_message[0] = 0; //just in case
672 parseBuffer = buffer;
673 parseBufferIndex = 0;
674 tags = OSDictionary::withCapacity(128);
675 if (yyparse() == 0) {
676 object = parsedObject;
677 if (errorString) *errorString = NULL;
681 *errorString = OSString::withCString(yyerror_message);
686 if (debugUnserializeAllocCount) {
687 printf("OSUnserialize: allocation check failed, count = %d.\n",
688 debugUnserializeAllocCount);
691 lck_mtx_unlock(lock);
702 // DO NOT EDIT OSUnserialize.cpp!