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