xref: /xnu-10063.121.3/libkern/c++/OSSerializeBinary.cpp (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2014 Apple Computer, 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 #include <libkern/c++/OSSharedPtr.h>
31*2c2f96dcSApple OSS Distributions #include <libkern/OSSerializeBinary.h>
32*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSContainers.h>
33*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSLib.h>
34*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSDictionary.h>
35*2c2f96dcSApple OSS Distributions #include <libkern/OSSerializeBinary.h>
36*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
37*2c2f96dcSApple OSS Distributions 
38*2c2f96dcSApple OSS Distributions #include <IOKit/IOLib.h>
39*2c2f96dcSApple OSS Distributions 
40*2c2f96dcSApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
41*2c2f96dcSApple OSS Distributions 
42*2c2f96dcSApple OSS Distributions #if 0
43*2c2f96dcSApple OSS Distributions #define DEBG(fmt, args ...)  { kprintf(fmt, args); }
44*2c2f96dcSApple OSS Distributions #else
45*2c2f96dcSApple OSS Distributions #define DEBG(fmt, args ...)      {}
46*2c2f96dcSApple OSS Distributions #endif
47*2c2f96dcSApple OSS Distributions 
48*2c2f96dcSApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
49*2c2f96dcSApple OSS Distributions 
50*2c2f96dcSApple OSS Distributions OSSerialize *
binaryWithCapacity(unsigned int inCapacity,Editor editor,void * reference)51*2c2f96dcSApple OSS Distributions OSSerialize::binaryWithCapacity(unsigned int inCapacity,
52*2c2f96dcSApple OSS Distributions     Editor editor, void * reference)
53*2c2f96dcSApple OSS Distributions {
54*2c2f96dcSApple OSS Distributions 	OSSerialize *me;
55*2c2f96dcSApple OSS Distributions 
56*2c2f96dcSApple OSS Distributions 	if (inCapacity < sizeof(uint32_t)) {
57*2c2f96dcSApple OSS Distributions 		return NULL;
58*2c2f96dcSApple OSS Distributions 	}
59*2c2f96dcSApple OSS Distributions 	me = OSSerialize::withCapacity(inCapacity);
60*2c2f96dcSApple OSS Distributions 	if (!me) {
61*2c2f96dcSApple OSS Distributions 		return NULL;
62*2c2f96dcSApple OSS Distributions 	}
63*2c2f96dcSApple OSS Distributions 
64*2c2f96dcSApple OSS Distributions 	me->binary        = true;
65*2c2f96dcSApple OSS Distributions 	me->endCollection = true;
66*2c2f96dcSApple OSS Distributions 	me->editor        = editor;
67*2c2f96dcSApple OSS Distributions 	me->editRef       = reference;
68*2c2f96dcSApple OSS Distributions 
69*2c2f96dcSApple OSS Distributions 	bcopy(kOSSerializeBinarySignature, &me->data[0], sizeof(kOSSerializeBinarySignature));
70*2c2f96dcSApple OSS Distributions 	me->length = sizeof(kOSSerializeBinarySignature);
71*2c2f96dcSApple OSS Distributions 
72*2c2f96dcSApple OSS Distributions 	return me;
73*2c2f96dcSApple OSS Distributions }
74*2c2f96dcSApple OSS Distributions 
75*2c2f96dcSApple OSS Distributions bool
addBinary(const void * bits,size_t size)76*2c2f96dcSApple OSS Distributions OSSerialize::addBinary(const void * bits, size_t size)
77*2c2f96dcSApple OSS Distributions {
78*2c2f96dcSApple OSS Distributions 	unsigned int newCapacity;
79*2c2f96dcSApple OSS Distributions 	size_t       alignSize;
80*2c2f96dcSApple OSS Distributions 
81*2c2f96dcSApple OSS Distributions 	if (os_add_overflow(size, 3, &alignSize)) {
82*2c2f96dcSApple OSS Distributions 		return false;
83*2c2f96dcSApple OSS Distributions 	}
84*2c2f96dcSApple OSS Distributions 	alignSize &= ~3L;
85*2c2f96dcSApple OSS Distributions 	if (os_add_overflow(length, alignSize, &newCapacity)) {
86*2c2f96dcSApple OSS Distributions 		return false;
87*2c2f96dcSApple OSS Distributions 	}
88*2c2f96dcSApple OSS Distributions 	if (newCapacity >= capacity) {
89*2c2f96dcSApple OSS Distributions 		newCapacity = (((newCapacity - 1) / capacityIncrement) + 1) * capacityIncrement;
90*2c2f96dcSApple OSS Distributions 		if (newCapacity < capacity) {
91*2c2f96dcSApple OSS Distributions 			return false;
92*2c2f96dcSApple OSS Distributions 		}
93*2c2f96dcSApple OSS Distributions 		if (newCapacity > ensureCapacity(newCapacity)) {
94*2c2f96dcSApple OSS Distributions 			return false;
95*2c2f96dcSApple OSS Distributions 		}
96*2c2f96dcSApple OSS Distributions 	}
97*2c2f96dcSApple OSS Distributions 
98*2c2f96dcSApple OSS Distributions 	bcopy(bits, &data[length], size);
99*2c2f96dcSApple OSS Distributions 	length += alignSize;
100*2c2f96dcSApple OSS Distributions 
101*2c2f96dcSApple OSS Distributions 	return true;
102*2c2f96dcSApple OSS Distributions }
103*2c2f96dcSApple OSS Distributions 
104*2c2f96dcSApple OSS Distributions void
setIndexed(bool index __unused)105*2c2f96dcSApple OSS Distributions OSSerialize::setIndexed(bool index __unused)
106*2c2f96dcSApple OSS Distributions {
107*2c2f96dcSApple OSS Distributions 	assert(index && !indexData);
108*2c2f96dcSApple OSS Distributions 	indexData = OSData::withCapacity(256);
109*2c2f96dcSApple OSS Distributions 	assert(indexData);
110*2c2f96dcSApple OSS Distributions }
111*2c2f96dcSApple OSS Distributions 
112*2c2f96dcSApple OSS Distributions bool
addBinaryObject(const OSMetaClassBase * o,uint32_t key,const void * bits,uint32_t size,uint32_t * startCollection)113*2c2f96dcSApple OSS Distributions OSSerialize::addBinaryObject(const OSMetaClassBase * o, uint32_t key,
114*2c2f96dcSApple OSS Distributions     const void * bits, uint32_t size,
115*2c2f96dcSApple OSS Distributions     uint32_t * startCollection)
116*2c2f96dcSApple OSS Distributions {
117*2c2f96dcSApple OSS Distributions 	unsigned int newCapacity;
118*2c2f96dcSApple OSS Distributions 	size_t       alignSize;
119*2c2f96dcSApple OSS Distributions 	size_t       headerSize;
120*2c2f96dcSApple OSS Distributions 
121*2c2f96dcSApple OSS Distributions 	// add to tag array
122*2c2f96dcSApple OSS Distributions 	tags->setObject(o);
123*2c2f96dcSApple OSS Distributions 
124*2c2f96dcSApple OSS Distributions 	headerSize = sizeof(key);
125*2c2f96dcSApple OSS Distributions 	if (indexData) {
126*2c2f96dcSApple OSS Distributions 		uint32_t offset = length;
127*2c2f96dcSApple OSS Distributions 		if (startCollection) {
128*2c2f96dcSApple OSS Distributions 			*startCollection = offset;
129*2c2f96dcSApple OSS Distributions 			headerSize += sizeof(uint32_t);
130*2c2f96dcSApple OSS Distributions 		}
131*2c2f96dcSApple OSS Distributions 		offset /= sizeof(uint32_t);
132*2c2f96dcSApple OSS Distributions 		indexData->appendValue(offset);
133*2c2f96dcSApple OSS Distributions 	}
134*2c2f96dcSApple OSS Distributions 
135*2c2f96dcSApple OSS Distributions 	if (os_add3_overflow(size, headerSize, 3, &alignSize)) {
136*2c2f96dcSApple OSS Distributions 		return false;
137*2c2f96dcSApple OSS Distributions 	}
138*2c2f96dcSApple OSS Distributions 	alignSize &= ~3L;
139*2c2f96dcSApple OSS Distributions 	if (os_add_overflow(length, alignSize, &newCapacity)) {
140*2c2f96dcSApple OSS Distributions 		return false;
141*2c2f96dcSApple OSS Distributions 	}
142*2c2f96dcSApple OSS Distributions 	if (newCapacity >= capacity) {
143*2c2f96dcSApple OSS Distributions 		newCapacity = (((newCapacity - 1) / capacityIncrement) + 1) * capacityIncrement;
144*2c2f96dcSApple OSS Distributions 		if (newCapacity < capacity) {
145*2c2f96dcSApple OSS Distributions 			return false;
146*2c2f96dcSApple OSS Distributions 		}
147*2c2f96dcSApple OSS Distributions 		if (newCapacity > ensureCapacity(newCapacity)) {
148*2c2f96dcSApple OSS Distributions 			return false;
149*2c2f96dcSApple OSS Distributions 		}
150*2c2f96dcSApple OSS Distributions 	}
151*2c2f96dcSApple OSS Distributions 
152*2c2f96dcSApple OSS Distributions 	if (endCollection) {
153*2c2f96dcSApple OSS Distributions 		endCollection = false;
154*2c2f96dcSApple OSS Distributions 		key |= kOSSerializeEndCollecton;
155*2c2f96dcSApple OSS Distributions 	}
156*2c2f96dcSApple OSS Distributions 
157*2c2f96dcSApple OSS Distributions 	bcopy(&key, &data[length], sizeof(key));
158*2c2f96dcSApple OSS Distributions 	bcopy(bits, &data[length + headerSize], size);
159*2c2f96dcSApple OSS Distributions 	length += alignSize;
160*2c2f96dcSApple OSS Distributions 
161*2c2f96dcSApple OSS Distributions 	return true;
162*2c2f96dcSApple OSS Distributions }
163*2c2f96dcSApple OSS Distributions 
164*2c2f96dcSApple OSS Distributions void
endBinaryCollection(uint32_t startCollection)165*2c2f96dcSApple OSS Distributions OSSerialize::endBinaryCollection(uint32_t startCollection)
166*2c2f96dcSApple OSS Distributions {
167*2c2f96dcSApple OSS Distributions 	uint32_t clength;
168*2c2f96dcSApple OSS Distributions 
169*2c2f96dcSApple OSS Distributions 	if (!indexData) {
170*2c2f96dcSApple OSS Distributions 		return;
171*2c2f96dcSApple OSS Distributions 	}
172*2c2f96dcSApple OSS Distributions 
173*2c2f96dcSApple OSS Distributions 	assert(length > startCollection);
174*2c2f96dcSApple OSS Distributions 	if (length <= startCollection) {
175*2c2f96dcSApple OSS Distributions 		return;
176*2c2f96dcSApple OSS Distributions 	}
177*2c2f96dcSApple OSS Distributions 
178*2c2f96dcSApple OSS Distributions 	clength = length - startCollection;
179*2c2f96dcSApple OSS Distributions 	assert(!(clength & 3));
180*2c2f96dcSApple OSS Distributions 	clength /= sizeof(uint32_t);
181*2c2f96dcSApple OSS Distributions 
182*2c2f96dcSApple OSS Distributions 	memcpy(&data[startCollection + sizeof(uint32_t)], &clength, sizeof(clength));
183*2c2f96dcSApple OSS Distributions }
184*2c2f96dcSApple OSS Distributions 
185*2c2f96dcSApple OSS Distributions bool
binarySerialize(const OSMetaClassBase * o)186*2c2f96dcSApple OSS Distributions OSSerialize::binarySerialize(const OSMetaClassBase *o)
187*2c2f96dcSApple OSS Distributions {
188*2c2f96dcSApple OSS Distributions 	bool ok;
189*2c2f96dcSApple OSS Distributions 	uint32_t header;
190*2c2f96dcSApple OSS Distributions 
191*2c2f96dcSApple OSS Distributions 	ok = binarySerializeInternal(o);
192*2c2f96dcSApple OSS Distributions 	if (!ok) {
193*2c2f96dcSApple OSS Distributions 		return ok;
194*2c2f96dcSApple OSS Distributions 	}
195*2c2f96dcSApple OSS Distributions 
196*2c2f96dcSApple OSS Distributions 	if (indexData) {
197*2c2f96dcSApple OSS Distributions 		header = indexData->getLength() / sizeof(uint32_t);
198*2c2f96dcSApple OSS Distributions 		assert(header <= kOSSerializeDataMask);
199*2c2f96dcSApple OSS Distributions 		header <<= 8;
200*2c2f96dcSApple OSS Distributions 		header |= kOSSerializeIndexedBinarySignature;
201*2c2f96dcSApple OSS Distributions 
202*2c2f96dcSApple OSS Distributions 		memcpy(&data[0], &header, sizeof(header));
203*2c2f96dcSApple OSS Distributions 	}
204*2c2f96dcSApple OSS Distributions 
205*2c2f96dcSApple OSS Distributions 	return ok;
206*2c2f96dcSApple OSS Distributions }
207*2c2f96dcSApple OSS Distributions 
208*2c2f96dcSApple OSS Distributions bool
binarySerializeInternal(const OSMetaClassBase * o)209*2c2f96dcSApple OSS Distributions OSSerialize::binarySerializeInternal(const OSMetaClassBase *o)
210*2c2f96dcSApple OSS Distributions {
211*2c2f96dcSApple OSS Distributions 	OSDictionary * dict;
212*2c2f96dcSApple OSS Distributions 	OSArray      * array;
213*2c2f96dcSApple OSS Distributions 	OSSet        * set;
214*2c2f96dcSApple OSS Distributions 	OSNumber     * num;
215*2c2f96dcSApple OSS Distributions 	OSSymbol     * sym;
216*2c2f96dcSApple OSS Distributions 	OSString     * str;
217*2c2f96dcSApple OSS Distributions 	OSData       * ldata;
218*2c2f96dcSApple OSS Distributions 	OSBoolean    * boo;
219*2c2f96dcSApple OSS Distributions 
220*2c2f96dcSApple OSS Distributions 	unsigned int  tagIdx;
221*2c2f96dcSApple OSS Distributions 	uint32_t   i, key, startCollection = 0;
222*2c2f96dcSApple OSS Distributions 	uint32_t   len;
223*2c2f96dcSApple OSS Distributions 	bool       ok;
224*2c2f96dcSApple OSS Distributions 
225*2c2f96dcSApple OSS Distributions 	tagIdx = tags->getNextIndexOfObject(o, 0);
226*2c2f96dcSApple OSS Distributions 	// does it exist?
227*2c2f96dcSApple OSS Distributions 	if (-1U != tagIdx) {
228*2c2f96dcSApple OSS Distributions 		if (indexData) {
229*2c2f96dcSApple OSS Distributions 			assert(indexData->getLength() > (tagIdx * sizeof(uint32_t)));
230*2c2f96dcSApple OSS Distributions 			tagIdx = ((const uint32_t *)indexData->getBytesNoCopy())[tagIdx];
231*2c2f96dcSApple OSS Distributions 			assert(tagIdx <= kOSSerializeDataMask);
232*2c2f96dcSApple OSS Distributions 		}
233*2c2f96dcSApple OSS Distributions 		key = (kOSSerializeObject | tagIdx);
234*2c2f96dcSApple OSS Distributions 		if (endCollection) {
235*2c2f96dcSApple OSS Distributions 			endCollection = false;
236*2c2f96dcSApple OSS Distributions 			key |= kOSSerializeEndCollecton;
237*2c2f96dcSApple OSS Distributions 		}
238*2c2f96dcSApple OSS Distributions 		ok = addBinary(&key, sizeof(key));
239*2c2f96dcSApple OSS Distributions 		return ok;
240*2c2f96dcSApple OSS Distributions 	}
241*2c2f96dcSApple OSS Distributions 
242*2c2f96dcSApple OSS Distributions 	if ((dict = OSDynamicCast(OSDictionary, o))) {
243*2c2f96dcSApple OSS Distributions 		key = (kOSSerializeDictionary | dict->count);
244*2c2f96dcSApple OSS Distributions 		ok = addBinaryObject(o, key, NULL, 0, &startCollection);
245*2c2f96dcSApple OSS Distributions 		for (i = 0; ok && (i < dict->count);) {
246*2c2f96dcSApple OSS Distributions 			const OSSymbol        * dictKey;
247*2c2f96dcSApple OSS Distributions 			const OSMetaClassBase * dictValue;
248*2c2f96dcSApple OSS Distributions 			const OSMetaClassBase * nvalue = NULL;
249*2c2f96dcSApple OSS Distributions 
250*2c2f96dcSApple OSS Distributions 			dictKey = dict->dictionary[i].key;
251*2c2f96dcSApple OSS Distributions 			dictValue = dict->dictionary[i].value;
252*2c2f96dcSApple OSS Distributions 			i++;
253*2c2f96dcSApple OSS Distributions 			if (editor) {
254*2c2f96dcSApple OSS Distributions 				dictValue = nvalue = (*editor)(editRef, this, dict, dictKey, dictValue);
255*2c2f96dcSApple OSS Distributions 				if (!dictValue) {
256*2c2f96dcSApple OSS Distributions 					dictValue = dict;
257*2c2f96dcSApple OSS Distributions 				}
258*2c2f96dcSApple OSS Distributions 			}
259*2c2f96dcSApple OSS Distributions 			ok = binarySerialize(dictKey);
260*2c2f96dcSApple OSS Distributions 			if (!ok) {
261*2c2f96dcSApple OSS Distributions 				break;
262*2c2f96dcSApple OSS Distributions 			}
263*2c2f96dcSApple OSS Distributions 			endCollection = (i == dict->count);
264*2c2f96dcSApple OSS Distributions 			ok = binarySerialize(dictValue);
265*2c2f96dcSApple OSS Distributions 			if (!ok) {
266*2c2f96dcSApple OSS Distributions 				ok = dictValue->serialize(this);
267*2c2f96dcSApple OSS Distributions 			}
268*2c2f96dcSApple OSS Distributions 			if (nvalue) {
269*2c2f96dcSApple OSS Distributions 				nvalue->release();
270*2c2f96dcSApple OSS Distributions 			}
271*2c2f96dcSApple OSS Distributions //			if (!ok) ok = binarySerialize(kOSBooleanFalse);
272*2c2f96dcSApple OSS Distributions 		}
273*2c2f96dcSApple OSS Distributions 		endBinaryCollection(startCollection);
274*2c2f96dcSApple OSS Distributions 	} else if ((array = OSDynamicCast(OSArray, o))) {
275*2c2f96dcSApple OSS Distributions 		key = (kOSSerializeArray | array->count);
276*2c2f96dcSApple OSS Distributions 		ok = addBinaryObject(o, key, NULL, 0, &startCollection);
277*2c2f96dcSApple OSS Distributions 		for (i = 0; ok && (i < array->count);) {
278*2c2f96dcSApple OSS Distributions 			i++;
279*2c2f96dcSApple OSS Distributions 			endCollection = (i == array->count);
280*2c2f96dcSApple OSS Distributions 			ok = binarySerialize(array->array[i - 1]);
281*2c2f96dcSApple OSS Distributions 			if (!ok) {
282*2c2f96dcSApple OSS Distributions 				ok = array->array[i - 1]->serialize(this);
283*2c2f96dcSApple OSS Distributions 			}
284*2c2f96dcSApple OSS Distributions //			if (!ok) ok = binarySerialize(kOSBooleanFalse);
285*2c2f96dcSApple OSS Distributions 		}
286*2c2f96dcSApple OSS Distributions 		endBinaryCollection(startCollection);
287*2c2f96dcSApple OSS Distributions 	} else if ((set = OSDynamicCast(OSSet, o))) {
288*2c2f96dcSApple OSS Distributions 		key = (kOSSerializeSet | set->members->count);
289*2c2f96dcSApple OSS Distributions 		ok = addBinaryObject(o, key, NULL, 0, &startCollection);
290*2c2f96dcSApple OSS Distributions 		for (i = 0; ok && (i < set->members->count);) {
291*2c2f96dcSApple OSS Distributions 			i++;
292*2c2f96dcSApple OSS Distributions 			endCollection = (i == set->members->count);
293*2c2f96dcSApple OSS Distributions 			ok = binarySerialize(set->members->array[i - 1]);
294*2c2f96dcSApple OSS Distributions 			if (!ok) {
295*2c2f96dcSApple OSS Distributions 				ok = set->members->array[i - 1]->serialize(this);
296*2c2f96dcSApple OSS Distributions 			}
297*2c2f96dcSApple OSS Distributions //			if (!ok) ok = binarySerialize(kOSBooleanFalse);
298*2c2f96dcSApple OSS Distributions 		}
299*2c2f96dcSApple OSS Distributions 		endBinaryCollection(startCollection);
300*2c2f96dcSApple OSS Distributions 	} else if ((num = OSDynamicCast(OSNumber, o))) {
301*2c2f96dcSApple OSS Distributions 		key = (kOSSerializeNumber | num->size);
302*2c2f96dcSApple OSS Distributions 		ok = addBinaryObject(o, key, &num->value, sizeof(num->value), NULL);
303*2c2f96dcSApple OSS Distributions 	} else if ((boo = OSDynamicCast(OSBoolean, o))) {
304*2c2f96dcSApple OSS Distributions 		key = (kOSSerializeBoolean | (kOSBooleanTrue == boo));
305*2c2f96dcSApple OSS Distributions 		ok = addBinaryObject(o, key, NULL, 0, NULL);
306*2c2f96dcSApple OSS Distributions 	} else if ((sym = OSDynamicCast(OSSymbol, o))) {
307*2c2f96dcSApple OSS Distributions 		len = (sym->getLength() + 1);
308*2c2f96dcSApple OSS Distributions 		key = (kOSSerializeSymbol | len);
309*2c2f96dcSApple OSS Distributions 		ok = addBinaryObject(o, key, sym->getCStringNoCopy(), len, NULL);
310*2c2f96dcSApple OSS Distributions 	} else if ((str = OSDynamicCast(OSString, o))) {
311*2c2f96dcSApple OSS Distributions 		len = str->getLength();
312*2c2f96dcSApple OSS Distributions 		key = (kOSSerializeString | len);
313*2c2f96dcSApple OSS Distributions 		ok = addBinaryObject(o, key, str->getCStringNoCopy(), len, NULL);
314*2c2f96dcSApple OSS Distributions 	} else if ((ldata = OSDynamicCast(OSData, o))) {
315*2c2f96dcSApple OSS Distributions 		len = ldata->getLength();
316*2c2f96dcSApple OSS Distributions 		if (ldata->reserved && ldata->reserved->disableSerialization) {
317*2c2f96dcSApple OSS Distributions 			len = 0;
318*2c2f96dcSApple OSS Distributions 		}
319*2c2f96dcSApple OSS Distributions 		key = (kOSSerializeData | len);
320*2c2f96dcSApple OSS Distributions 		ok = addBinaryObject(o, key, ldata->getBytesNoCopy(), len, NULL);
321*2c2f96dcSApple OSS Distributions 	} else {
322*2c2f96dcSApple OSS Distributions 		return false;
323*2c2f96dcSApple OSS Distributions 	}
324*2c2f96dcSApple OSS Distributions 
325*2c2f96dcSApple OSS Distributions 	return ok;
326*2c2f96dcSApple OSS Distributions }
327*2c2f96dcSApple OSS Distributions 
328*2c2f96dcSApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
329*2c2f96dcSApple OSS Distributions 
330*2c2f96dcSApple OSS Distributions #define setAtIndex(v, idx, o)                                                  \
331*2c2f96dcSApple OSS Distributions 	ok = idx < v##Capacity;                                                \
332*2c2f96dcSApple OSS Distributions 	if (!ok && v##Capacity < v##CapacityMax) {                             \
333*2c2f96dcSApple OSS Distributions 	    uint32_t ncap = v##Capacity + 64;                                  \
334*2c2f96dcSApple OSS Distributions 	    typeof(v##Array) nbuf = kreallocp_type_container(OSObject *,       \
335*2c2f96dcSApple OSS Distributions 	        v##Array, v##Capacity, &ncap, Z_WAITOK_ZERO);                  \
336*2c2f96dcSApple OSS Distributions 	    if (nbuf) {                                                        \
337*2c2f96dcSApple OSS Distributions 	        ok = true;                                                     \
338*2c2f96dcSApple OSS Distributions 	        v##Array    = nbuf;                                            \
339*2c2f96dcSApple OSS Distributions 	        v##Capacity = ncap;                                            \
340*2c2f96dcSApple OSS Distributions 	    }                                                                  \
341*2c2f96dcSApple OSS Distributions 	}                                                                      \
342*2c2f96dcSApple OSS Distributions 	if (ok) v##Array[idx] = o
343*2c2f96dcSApple OSS Distributions 
344*2c2f96dcSApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
345*2c2f96dcSApple OSS Distributions 
346*2c2f96dcSApple OSS Distributions OSObject *
OSUnserializeBinary(const char * buffer,size_t bufferSize,OSString ** errorString)347*2c2f96dcSApple OSS Distributions OSUnserializeBinary(const char *buffer, size_t bufferSize, OSString **errorString)
348*2c2f96dcSApple OSS Distributions {
349*2c2f96dcSApple OSS Distributions 	OSObject ** objsArray;
350*2c2f96dcSApple OSS Distributions 	uint32_t    objsCapacity;
351*2c2f96dcSApple OSS Distributions 	enum      { objsCapacityMax = 16 * 1024 * 1024 };
352*2c2f96dcSApple OSS Distributions 	uint32_t    objsIdx;
353*2c2f96dcSApple OSS Distributions 
354*2c2f96dcSApple OSS Distributions 	OSObject ** stackArray;
355*2c2f96dcSApple OSS Distributions 	uint32_t    stackCapacity;
356*2c2f96dcSApple OSS Distributions 	enum      { stackCapacityMax = 64 };
357*2c2f96dcSApple OSS Distributions 	uint32_t    stackIdx;
358*2c2f96dcSApple OSS Distributions 
359*2c2f96dcSApple OSS Distributions 	OSObject     * result;
360*2c2f96dcSApple OSS Distributions 	OSObject     * parent;
361*2c2f96dcSApple OSS Distributions 	OSDictionary * dict;
362*2c2f96dcSApple OSS Distributions 	OSArray      * array;
363*2c2f96dcSApple OSS Distributions 	OSSet        * set;
364*2c2f96dcSApple OSS Distributions 	OSDictionary * newDict;
365*2c2f96dcSApple OSS Distributions 	OSArray      * newArray;
366*2c2f96dcSApple OSS Distributions 	OSSet        * newSet;
367*2c2f96dcSApple OSS Distributions 	OSObject     * o;
368*2c2f96dcSApple OSS Distributions 	OSSymbol     * sym;
369*2c2f96dcSApple OSS Distributions 	OSString     * str;
370*2c2f96dcSApple OSS Distributions 
371*2c2f96dcSApple OSS Distributions 	size_t           bufferPos;
372*2c2f96dcSApple OSS Distributions 	const uint32_t * next;
373*2c2f96dcSApple OSS Distributions 	uint32_t         key, len, wordLen, length;
374*2c2f96dcSApple OSS Distributions 	bool             end, newCollect, isRef;
375*2c2f96dcSApple OSS Distributions 	union {
376*2c2f96dcSApple OSS Distributions 		unsigned long long value;
377*2c2f96dcSApple OSS Distributions 		double fpValue;
378*2c2f96dcSApple OSS Distributions 	} value;
379*2c2f96dcSApple OSS Distributions 	bool ok, indexed, hasLength;
380*2c2f96dcSApple OSS Distributions 
381*2c2f96dcSApple OSS Distributions 	indexed = false;
382*2c2f96dcSApple OSS Distributions 	if (errorString) {
383*2c2f96dcSApple OSS Distributions 		*errorString = NULL;
384*2c2f96dcSApple OSS Distributions 	}
385*2c2f96dcSApple OSS Distributions 
386*2c2f96dcSApple OSS Distributions 	if (bufferSize < sizeof(kOSSerializeBinarySignature)) {
387*2c2f96dcSApple OSS Distributions 		return NULL;
388*2c2f96dcSApple OSS Distributions 	}
389*2c2f96dcSApple OSS Distributions 	if (kOSSerializeIndexedBinarySignature == (((const uint8_t *) buffer)[0])) {
390*2c2f96dcSApple OSS Distributions 		indexed = true;
391*2c2f96dcSApple OSS Distributions 	} else if (0 != strcmp(kOSSerializeBinarySignature, buffer)) {
392*2c2f96dcSApple OSS Distributions 		return NULL;
393*2c2f96dcSApple OSS Distributions 	}
394*2c2f96dcSApple OSS Distributions 	if (3 & ((uintptr_t) buffer)) {
395*2c2f96dcSApple OSS Distributions 		return NULL;
396*2c2f96dcSApple OSS Distributions 	}
397*2c2f96dcSApple OSS Distributions 
398*2c2f96dcSApple OSS Distributions 	bufferPos = sizeof(kOSSerializeBinarySignature);
399*2c2f96dcSApple OSS Distributions 	next = (typeof(next))(((uintptr_t) buffer) + bufferPos);
400*2c2f96dcSApple OSS Distributions 
401*2c2f96dcSApple OSS Distributions 	DEBG("---------OSUnserializeBinary(%p)\n", buffer);
402*2c2f96dcSApple OSS Distributions 
403*2c2f96dcSApple OSS Distributions 	objsArray = stackArray    = NULL;
404*2c2f96dcSApple OSS Distributions 	objsIdx   = objsCapacity  = 0;
405*2c2f96dcSApple OSS Distributions 	stackIdx  = stackCapacity = 0;
406*2c2f96dcSApple OSS Distributions 
407*2c2f96dcSApple OSS Distributions 	result   = NULL;
408*2c2f96dcSApple OSS Distributions 	parent   = NULL;
409*2c2f96dcSApple OSS Distributions 	dict     = NULL;
410*2c2f96dcSApple OSS Distributions 	array    = NULL;
411*2c2f96dcSApple OSS Distributions 	set      = NULL;
412*2c2f96dcSApple OSS Distributions 	sym      = NULL;
413*2c2f96dcSApple OSS Distributions 
414*2c2f96dcSApple OSS Distributions 	ok = true;
415*2c2f96dcSApple OSS Distributions 	while (ok) {
416*2c2f96dcSApple OSS Distributions 		bufferPos += sizeof(*next);
417*2c2f96dcSApple OSS Distributions 		if (!(ok = (bufferPos <= bufferSize))) {
418*2c2f96dcSApple OSS Distributions 			break;
419*2c2f96dcSApple OSS Distributions 		}
420*2c2f96dcSApple OSS Distributions 		key = *next++;
421*2c2f96dcSApple OSS Distributions 		length = 0;
422*2c2f96dcSApple OSS Distributions 
423*2c2f96dcSApple OSS Distributions 		len = (key & kOSSerializeDataMask);
424*2c2f96dcSApple OSS Distributions 		wordLen = (len + 3) >> 2;
425*2c2f96dcSApple OSS Distributions 		end = (0 != (kOSSerializeEndCollecton & key));
426*2c2f96dcSApple OSS Distributions 		DEBG("key 0x%08x: 0x%04x, %d\n", key, len, end);
427*2c2f96dcSApple OSS Distributions 
428*2c2f96dcSApple OSS Distributions 		newCollect = isRef = hasLength = false;
429*2c2f96dcSApple OSS Distributions 		o = NULL; newDict = NULL; newArray = NULL; newSet = NULL;
430*2c2f96dcSApple OSS Distributions 
431*2c2f96dcSApple OSS Distributions 		switch (kOSSerializeTypeMask & key) {
432*2c2f96dcSApple OSS Distributions 		case kOSSerializeDictionary:
433*2c2f96dcSApple OSS Distributions 			o = newDict = OSDictionary::withCapacity(len);
434*2c2f96dcSApple OSS Distributions 			newCollect = (len != 0);
435*2c2f96dcSApple OSS Distributions 			hasLength  = indexed;
436*2c2f96dcSApple OSS Distributions 			break;
437*2c2f96dcSApple OSS Distributions 		case kOSSerializeArray:
438*2c2f96dcSApple OSS Distributions 			o = newArray = OSArray::withCapacity(len);
439*2c2f96dcSApple OSS Distributions 			newCollect = (len != 0);
440*2c2f96dcSApple OSS Distributions 			hasLength  = indexed;
441*2c2f96dcSApple OSS Distributions 			break;
442*2c2f96dcSApple OSS Distributions 		case kOSSerializeSet:
443*2c2f96dcSApple OSS Distributions 			o = newSet = OSSet::withCapacity(len);
444*2c2f96dcSApple OSS Distributions 			newCollect = (len != 0);
445*2c2f96dcSApple OSS Distributions 			hasLength  = indexed;
446*2c2f96dcSApple OSS Distributions 			break;
447*2c2f96dcSApple OSS Distributions 
448*2c2f96dcSApple OSS Distributions 		case kOSSerializeObject:
449*2c2f96dcSApple OSS Distributions 			if (len >= objsIdx) {
450*2c2f96dcSApple OSS Distributions 				break;
451*2c2f96dcSApple OSS Distributions 			}
452*2c2f96dcSApple OSS Distributions 			o = objsArray[len];
453*2c2f96dcSApple OSS Distributions 			isRef = true;
454*2c2f96dcSApple OSS Distributions 			break;
455*2c2f96dcSApple OSS Distributions 
456*2c2f96dcSApple OSS Distributions 		case kOSSerializeNumber:
457*2c2f96dcSApple OSS Distributions 			bufferPos += sizeof(long long);
458*2c2f96dcSApple OSS Distributions 			if (bufferPos > bufferSize) {
459*2c2f96dcSApple OSS Distributions 				break;
460*2c2f96dcSApple OSS Distributions 			}
461*2c2f96dcSApple OSS Distributions 			value.value = next[1];
462*2c2f96dcSApple OSS Distributions 			value.value <<= 32;
463*2c2f96dcSApple OSS Distributions 			value.value |= next[0];
464*2c2f96dcSApple OSS Distributions 			switch (len) {
465*2c2f96dcSApple OSS Distributions 			case 63:
466*2c2f96dcSApple OSS Distributions 				o = OSNumber::withDouble(value.fpValue);
467*2c2f96dcSApple OSS Distributions 				break;
468*2c2f96dcSApple OSS Distributions 			case 31:
469*2c2f96dcSApple OSS Distributions 				o = OSNumber::withFloat((float) value.fpValue);
470*2c2f96dcSApple OSS Distributions 				break;
471*2c2f96dcSApple OSS Distributions 			case 64:
472*2c2f96dcSApple OSS Distributions 			case 32:
473*2c2f96dcSApple OSS Distributions 			case 16:
474*2c2f96dcSApple OSS Distributions 			case 8:
475*2c2f96dcSApple OSS Distributions 				o = OSNumber::withNumber(value.value, len);
476*2c2f96dcSApple OSS Distributions 				break;
477*2c2f96dcSApple OSS Distributions 			}
478*2c2f96dcSApple OSS Distributions 			next += 2;
479*2c2f96dcSApple OSS Distributions 			break;
480*2c2f96dcSApple OSS Distributions 
481*2c2f96dcSApple OSS Distributions 		case kOSSerializeSymbol:
482*2c2f96dcSApple OSS Distributions 			bufferPos += (wordLen * sizeof(uint32_t));
483*2c2f96dcSApple OSS Distributions 			if (bufferPos > bufferSize) {
484*2c2f96dcSApple OSS Distributions 				break;
485*2c2f96dcSApple OSS Distributions 			}
486*2c2f96dcSApple OSS Distributions 			if (len < 1) {
487*2c2f96dcSApple OSS Distributions 				break;
488*2c2f96dcSApple OSS Distributions 			}
489*2c2f96dcSApple OSS Distributions 			if (0 != ((const char *)next)[len - 1]) {
490*2c2f96dcSApple OSS Distributions 				break;
491*2c2f96dcSApple OSS Distributions 			}
492*2c2f96dcSApple OSS Distributions 			o = (OSObject *) OSSymbol::withCString((const char *) next);
493*2c2f96dcSApple OSS Distributions 			next += wordLen;
494*2c2f96dcSApple OSS Distributions 			break;
495*2c2f96dcSApple OSS Distributions 
496*2c2f96dcSApple OSS Distributions 		case kOSSerializeString:
497*2c2f96dcSApple OSS Distributions 			bufferPos += (wordLen * sizeof(uint32_t));
498*2c2f96dcSApple OSS Distributions 			if (bufferPos > bufferSize) {
499*2c2f96dcSApple OSS Distributions 				break;
500*2c2f96dcSApple OSS Distributions 			}
501*2c2f96dcSApple OSS Distributions 			o = OSString::withCString((const char *) next, len);
502*2c2f96dcSApple OSS Distributions 			next += wordLen;
503*2c2f96dcSApple OSS Distributions 			break;
504*2c2f96dcSApple OSS Distributions 
505*2c2f96dcSApple OSS Distributions 		case kOSSerializeData:
506*2c2f96dcSApple OSS Distributions 			bufferPos += (wordLen * sizeof(uint32_t));
507*2c2f96dcSApple OSS Distributions 			if (bufferPos > bufferSize) {
508*2c2f96dcSApple OSS Distributions 				break;
509*2c2f96dcSApple OSS Distributions 			}
510*2c2f96dcSApple OSS Distributions 			o = OSData::withBytes(next, len);
511*2c2f96dcSApple OSS Distributions 			next += wordLen;
512*2c2f96dcSApple OSS Distributions 			break;
513*2c2f96dcSApple OSS Distributions 
514*2c2f96dcSApple OSS Distributions 		case kOSSerializeBoolean:
515*2c2f96dcSApple OSS Distributions 			o = (len ? kOSBooleanTrue : kOSBooleanFalse);
516*2c2f96dcSApple OSS Distributions 			break;
517*2c2f96dcSApple OSS Distributions 
518*2c2f96dcSApple OSS Distributions 		default:
519*2c2f96dcSApple OSS Distributions 			break;
520*2c2f96dcSApple OSS Distributions 		}
521*2c2f96dcSApple OSS Distributions 
522*2c2f96dcSApple OSS Distributions 		if (!(ok = (o != NULL))) {
523*2c2f96dcSApple OSS Distributions 			break;
524*2c2f96dcSApple OSS Distributions 		}
525*2c2f96dcSApple OSS Distributions 
526*2c2f96dcSApple OSS Distributions 		if (hasLength) {
527*2c2f96dcSApple OSS Distributions 			bufferPos += sizeof(*next);
528*2c2f96dcSApple OSS Distributions 			if (!(ok = (bufferPos <= bufferSize))) {
529*2c2f96dcSApple OSS Distributions 				o->release();
530*2c2f96dcSApple OSS Distributions 				break;
531*2c2f96dcSApple OSS Distributions 			}
532*2c2f96dcSApple OSS Distributions 			length = *next++;
533*2c2f96dcSApple OSS Distributions 		}
534*2c2f96dcSApple OSS Distributions 
535*2c2f96dcSApple OSS Distributions 		if (!isRef) {
536*2c2f96dcSApple OSS Distributions 			setAtIndex(objs, objsIdx, o);
537*2c2f96dcSApple OSS Distributions 			if (!ok) {
538*2c2f96dcSApple OSS Distributions 				o->release();
539*2c2f96dcSApple OSS Distributions 				break;
540*2c2f96dcSApple OSS Distributions 			}
541*2c2f96dcSApple OSS Distributions 			objsIdx++;
542*2c2f96dcSApple OSS Distributions 		}
543*2c2f96dcSApple OSS Distributions 
544*2c2f96dcSApple OSS Distributions 		if (dict) {
545*2c2f96dcSApple OSS Distributions 			if (!sym) {
546*2c2f96dcSApple OSS Distributions 				sym = (OSSymbol *) o;
547*2c2f96dcSApple OSS Distributions 			} else {
548*2c2f96dcSApple OSS Distributions 				str = sym;
549*2c2f96dcSApple OSS Distributions 				sym = OSDynamicCast(OSSymbol, sym);
550*2c2f96dcSApple OSS Distributions 				if (!sym && (str = OSDynamicCast(OSString, str))) {
551*2c2f96dcSApple OSS Distributions 					sym = const_cast<OSSymbol *>(OSSymbol::withString(str));
552*2c2f96dcSApple OSS Distributions 					ok = (sym != NULL);
553*2c2f96dcSApple OSS Distributions 					if (!ok) {
554*2c2f96dcSApple OSS Distributions 						break;
555*2c2f96dcSApple OSS Distributions 					}
556*2c2f96dcSApple OSS Distributions 				}
557*2c2f96dcSApple OSS Distributions 				DEBG("%s = %s\n", sym->getCStringNoCopy(), o->getMetaClass()->getClassName());
558*2c2f96dcSApple OSS Distributions 				if (o != dict) {
559*2c2f96dcSApple OSS Distributions 					ok = dict->setObject(sym, o);
560*2c2f96dcSApple OSS Distributions 				}
561*2c2f96dcSApple OSS Distributions 				if (sym && (sym != str)) {
562*2c2f96dcSApple OSS Distributions 					sym->release();
563*2c2f96dcSApple OSS Distributions 				}
564*2c2f96dcSApple OSS Distributions 				sym = NULL;
565*2c2f96dcSApple OSS Distributions 			}
566*2c2f96dcSApple OSS Distributions 		} else if (array) {
567*2c2f96dcSApple OSS Distributions 			ok = array->setObject(o);
568*2c2f96dcSApple OSS Distributions 		} else if (set) {
569*2c2f96dcSApple OSS Distributions 			ok = set->setObject(o);
570*2c2f96dcSApple OSS Distributions 		} else if (result) {
571*2c2f96dcSApple OSS Distributions 			ok = false;
572*2c2f96dcSApple OSS Distributions 		} else {
573*2c2f96dcSApple OSS Distributions 			assert(!parent);
574*2c2f96dcSApple OSS Distributions 			result = o;
575*2c2f96dcSApple OSS Distributions 		}
576*2c2f96dcSApple OSS Distributions 
577*2c2f96dcSApple OSS Distributions 		if (!ok) {
578*2c2f96dcSApple OSS Distributions 			break;
579*2c2f96dcSApple OSS Distributions 		}
580*2c2f96dcSApple OSS Distributions 
581*2c2f96dcSApple OSS Distributions 		if (end) {
582*2c2f96dcSApple OSS Distributions 			parent = NULL;
583*2c2f96dcSApple OSS Distributions 		}
584*2c2f96dcSApple OSS Distributions 		if (newCollect) {
585*2c2f96dcSApple OSS Distributions 			stackIdx++;
586*2c2f96dcSApple OSS Distributions 			setAtIndex(stack, stackIdx, parent);
587*2c2f96dcSApple OSS Distributions 			if (!ok) {
588*2c2f96dcSApple OSS Distributions 				break;
589*2c2f96dcSApple OSS Distributions 			}
590*2c2f96dcSApple OSS Distributions 			DEBG("++stack[%d] %p\n", stackIdx, parent);
591*2c2f96dcSApple OSS Distributions 			parent = o;
592*2c2f96dcSApple OSS Distributions 			dict   = newDict;
593*2c2f96dcSApple OSS Distributions 			array  = newArray;
594*2c2f96dcSApple OSS Distributions 			set    = newSet;
595*2c2f96dcSApple OSS Distributions 			end    = false;
596*2c2f96dcSApple OSS Distributions 		}
597*2c2f96dcSApple OSS Distributions 
598*2c2f96dcSApple OSS Distributions 		if (end) {
599*2c2f96dcSApple OSS Distributions 			while (stackIdx) {
600*2c2f96dcSApple OSS Distributions 				parent = stackArray[stackIdx];
601*2c2f96dcSApple OSS Distributions 				DEBG("--stack[%d] %p\n", stackIdx, parent);
602*2c2f96dcSApple OSS Distributions 				stackIdx--;
603*2c2f96dcSApple OSS Distributions 				if (parent) {
604*2c2f96dcSApple OSS Distributions 					break;
605*2c2f96dcSApple OSS Distributions 				}
606*2c2f96dcSApple OSS Distributions 			}
607*2c2f96dcSApple OSS Distributions 			if (!parent) {
608*2c2f96dcSApple OSS Distributions 				break;
609*2c2f96dcSApple OSS Distributions 			}
610*2c2f96dcSApple OSS Distributions 			set   = NULL;
611*2c2f96dcSApple OSS Distributions 			dict  = NULL;
612*2c2f96dcSApple OSS Distributions 			array = NULL;
613*2c2f96dcSApple OSS Distributions 			if (!(dict = OSDynamicCast(OSDictionary, parent))) {
614*2c2f96dcSApple OSS Distributions 				if (!(array = OSDynamicCast(OSArray, parent))) {
615*2c2f96dcSApple OSS Distributions 					ok = (NULL != (set = OSDynamicCast(OSSet, parent)));
616*2c2f96dcSApple OSS Distributions 				}
617*2c2f96dcSApple OSS Distributions 			}
618*2c2f96dcSApple OSS Distributions 		}
619*2c2f96dcSApple OSS Distributions 	}
620*2c2f96dcSApple OSS Distributions 	DEBG("ret %p\n", result);
621*2c2f96dcSApple OSS Distributions 
622*2c2f96dcSApple OSS Distributions 	if (!ok) {
623*2c2f96dcSApple OSS Distributions 		result = NULL;
624*2c2f96dcSApple OSS Distributions 	}
625*2c2f96dcSApple OSS Distributions 
626*2c2f96dcSApple OSS Distributions 	if (objsCapacity) {
627*2c2f96dcSApple OSS Distributions 		for (len = (result != NULL); len < objsIdx; len++) {
628*2c2f96dcSApple OSS Distributions 			objsArray[len]->release();
629*2c2f96dcSApple OSS Distributions 		}
630*2c2f96dcSApple OSS Distributions 		kfree_type(OSObject *, objsCapacity, objsArray);
631*2c2f96dcSApple OSS Distributions 	}
632*2c2f96dcSApple OSS Distributions 	if (stackCapacity) {
633*2c2f96dcSApple OSS Distributions 		kfree_type(OSObject *, stackCapacity, stackArray);
634*2c2f96dcSApple OSS Distributions 	}
635*2c2f96dcSApple OSS Distributions 
636*2c2f96dcSApple OSS Distributions 	return result;
637*2c2f96dcSApple OSS Distributions }
638*2c2f96dcSApple OSS Distributions 
639*2c2f96dcSApple OSS Distributions OSObject*
OSUnserializeXML(const char * buffer,OSSharedPtr<OSString> & errorString)640*2c2f96dcSApple OSS Distributions OSUnserializeXML(
641*2c2f96dcSApple OSS Distributions 	const char  * buffer,
642*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSString>& errorString)
643*2c2f96dcSApple OSS Distributions {
644*2c2f96dcSApple OSS Distributions 	OSString* errorStringRaw = NULL;
645*2c2f96dcSApple OSS Distributions 	OSObject* result = OSUnserializeXML(buffer, &errorStringRaw);
646*2c2f96dcSApple OSS Distributions 	errorString.reset(errorStringRaw, OSNoRetain);
647*2c2f96dcSApple OSS Distributions 	return result;
648*2c2f96dcSApple OSS Distributions }
649*2c2f96dcSApple OSS Distributions 
650*2c2f96dcSApple OSS Distributions OSObject*
OSUnserializeXML(const char * buffer,size_t bufferSize,OSSharedPtr<OSString> & errorString)651*2c2f96dcSApple OSS Distributions OSUnserializeXML(
652*2c2f96dcSApple OSS Distributions 	const char  * buffer,
653*2c2f96dcSApple OSS Distributions 	size_t        bufferSize,
654*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSString> &errorString)
655*2c2f96dcSApple OSS Distributions {
656*2c2f96dcSApple OSS Distributions 	OSString* errorStringRaw = NULL;
657*2c2f96dcSApple OSS Distributions 	OSObject* result = OSUnserializeXML(buffer, bufferSize, &errorStringRaw);
658*2c2f96dcSApple OSS Distributions 	errorString.reset(errorStringRaw, OSNoRetain);
659*2c2f96dcSApple OSS Distributions 	return result;
660*2c2f96dcSApple OSS Distributions }
661*2c2f96dcSApple OSS Distributions 
662*2c2f96dcSApple OSS Distributions OSObject*
OSUnserializeBinary(const char * buffer,size_t bufferSize,OSSharedPtr<OSString> & errorString)663*2c2f96dcSApple OSS Distributions OSUnserializeBinary(const char *buffer, size_t bufferSize, OSSharedPtr<OSString>& errorString)
664*2c2f96dcSApple OSS Distributions {
665*2c2f96dcSApple OSS Distributions 	OSString* errorStringRaw = NULL;
666*2c2f96dcSApple OSS Distributions 	OSObject* result = OSUnserializeBinary(buffer, bufferSize, &errorStringRaw);
667*2c2f96dcSApple OSS Distributions 	errorString.reset(errorStringRaw, OSNoRetain);
668*2c2f96dcSApple OSS Distributions 	return result;
669*2c2f96dcSApple OSS Distributions }
670*2c2f96dcSApple OSS Distributions 
671*2c2f96dcSApple OSS Distributions OSObject*
OSUnserialize(const char * buffer,OSSharedPtr<OSString> & errorString)672*2c2f96dcSApple OSS Distributions OSUnserialize(const char *buffer, OSSharedPtr<OSString>& errorString)
673*2c2f96dcSApple OSS Distributions {
674*2c2f96dcSApple OSS Distributions 	OSString* errorStringRaw = NULL;
675*2c2f96dcSApple OSS Distributions 	OSObject* result = OSUnserialize(buffer, &errorStringRaw);
676*2c2f96dcSApple OSS Distributions 	errorString.reset(errorStringRaw, OSNoRetain);
677*2c2f96dcSApple OSS Distributions 	return result;
678*2c2f96dcSApple OSS Distributions }
679