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