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