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