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