1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3*1031c584SApple OSS Distributions *
4*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions *
6*1031c584SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions *
15*1031c584SApple OSS Distributions * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions *
18*1031c584SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions * limitations under the License.
25*1031c584SApple OSS Distributions *
26*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions */
28*1031c584SApple OSS Distributions /* OSSerialize.cpp created by rsulack on Wen 25-Nov-1998 */
29*1031c584SApple OSS Distributions
30*1031c584SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31*1031c584SApple OSS Distributions
32*1031c584SApple OSS Distributions #include <sys/cdefs.h>
33*1031c584SApple OSS Distributions #include <vm/vm_kern.h>
34*1031c584SApple OSS Distributions #include <os/hash.h>
35*1031c584SApple OSS Distributions
36*1031c584SApple OSS Distributions #include <libkern/c++/OSContainers.h>
37*1031c584SApple OSS Distributions #include <libkern/c++/OSLib.h>
38*1031c584SApple OSS Distributions #include <libkern/c++/OSDictionary.h>
39*1031c584SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
40*1031c584SApple OSS Distributions #include <libkern/OSSerializeBinary.h>
41*1031c584SApple OSS Distributions #include <libkern/Block.h>
42*1031c584SApple OSS Distributions #include <IOKit/IOLib.h>
43*1031c584SApple OSS Distributions
44*1031c584SApple OSS Distributions #define super OSObject
45*1031c584SApple OSS Distributions
46*1031c584SApple OSS Distributions OSDefineMetaClassAndStructors(OSSerialize, OSObject)
47*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 0);
48*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 1);
49*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 2);
50*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 3);
51*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 4);
52*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 5);
53*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 6);
54*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 7);
55*1031c584SApple OSS Distributions
56*1031c584SApple OSS Distributions static inline kmem_guard_t
OSSerialize_guard()57*1031c584SApple OSS Distributions OSSerialize_guard()
58*1031c584SApple OSS Distributions {
59*1031c584SApple OSS Distributions kmem_guard_t guard = {
60*1031c584SApple OSS Distributions .kmg_tag = IOMemoryTag(kernel_map),
61*1031c584SApple OSS Distributions };
62*1031c584SApple OSS Distributions
63*1031c584SApple OSS Distributions return guard;
64*1031c584SApple OSS Distributions }
65*1031c584SApple OSS Distributions
66*1031c584SApple OSS Distributions
67*1031c584SApple OSS Distributions char *
text() const68*1031c584SApple OSS Distributions OSSerialize::text() const
69*1031c584SApple OSS Distributions {
70*1031c584SApple OSS Distributions return data;
71*1031c584SApple OSS Distributions }
72*1031c584SApple OSS Distributions
73*1031c584SApple OSS Distributions void
clearText()74*1031c584SApple OSS Distributions OSSerialize::clearText()
75*1031c584SApple OSS Distributions {
76*1031c584SApple OSS Distributions if (binary) {
77*1031c584SApple OSS Distributions length = sizeof(kOSSerializeBinarySignature);
78*1031c584SApple OSS Distributions bzero(&data[length], capacity - length);
79*1031c584SApple OSS Distributions endCollection = true;
80*1031c584SApple OSS Distributions } else {
81*1031c584SApple OSS Distributions bzero((void *)data, capacity);
82*1031c584SApple OSS Distributions length = 1;
83*1031c584SApple OSS Distributions }
84*1031c584SApple OSS Distributions tags->flushCollection();
85*1031c584SApple OSS Distributions }
86*1031c584SApple OSS Distributions
87*1031c584SApple OSS Distributions bool
previouslySerialized(const OSMetaClassBase * o)88*1031c584SApple OSS Distributions OSSerialize::previouslySerialized(const OSMetaClassBase *o)
89*1031c584SApple OSS Distributions {
90*1031c584SApple OSS Distributions char temp[16];
91*1031c584SApple OSS Distributions unsigned int tagIdx;
92*1031c584SApple OSS Distributions
93*1031c584SApple OSS Distributions if (binary) {
94*1031c584SApple OSS Distributions return binarySerialize(o);
95*1031c584SApple OSS Distributions }
96*1031c584SApple OSS Distributions
97*1031c584SApple OSS Distributions // look it up
98*1031c584SApple OSS Distributions tagIdx = tags->getNextIndexOfObject(o, 0);
99*1031c584SApple OSS Distributions
100*1031c584SApple OSS Distributions // xx-review: no error checking here for addString calls!
101*1031c584SApple OSS Distributions // does it exist?
102*1031c584SApple OSS Distributions if (tagIdx != -1U) {
103*1031c584SApple OSS Distributions addString("<reference IDREF=\"");
104*1031c584SApple OSS Distributions snprintf(temp, sizeof(temp), "%u", tagIdx);
105*1031c584SApple OSS Distributions addString(temp);
106*1031c584SApple OSS Distributions addString("\"/>");
107*1031c584SApple OSS Distributions return true;
108*1031c584SApple OSS Distributions }
109*1031c584SApple OSS Distributions
110*1031c584SApple OSS Distributions // add to tag array
111*1031c584SApple OSS Distributions tags->setObject(o);// XXX check return
112*1031c584SApple OSS Distributions
113*1031c584SApple OSS Distributions return false;
114*1031c584SApple OSS Distributions }
115*1031c584SApple OSS Distributions
116*1031c584SApple OSS Distributions bool
addXMLStartTag(const OSMetaClassBase * o,const char * tagString)117*1031c584SApple OSS Distributions OSSerialize::addXMLStartTag(const OSMetaClassBase *o, const char *tagString)
118*1031c584SApple OSS Distributions {
119*1031c584SApple OSS Distributions char temp[16];
120*1031c584SApple OSS Distributions unsigned int tagIdx;
121*1031c584SApple OSS Distributions
122*1031c584SApple OSS Distributions if (binary) {
123*1031c584SApple OSS Distributions printf("class %s: xml serialize\n", o->getMetaClass()->getClassName());
124*1031c584SApple OSS Distributions return false;
125*1031c584SApple OSS Distributions }
126*1031c584SApple OSS Distributions
127*1031c584SApple OSS Distributions if (!addChar('<')) {
128*1031c584SApple OSS Distributions return false;
129*1031c584SApple OSS Distributions }
130*1031c584SApple OSS Distributions if (!addString(tagString)) {
131*1031c584SApple OSS Distributions return false;
132*1031c584SApple OSS Distributions }
133*1031c584SApple OSS Distributions if (!addString(" ID=\"")) {
134*1031c584SApple OSS Distributions return false;
135*1031c584SApple OSS Distributions }
136*1031c584SApple OSS Distributions tagIdx = tags->getNextIndexOfObject(o, 0);
137*1031c584SApple OSS Distributions assert(tagIdx != -1U);
138*1031c584SApple OSS Distributions snprintf(temp, sizeof(temp), "%u", tagIdx);
139*1031c584SApple OSS Distributions if (!addString(temp)) {
140*1031c584SApple OSS Distributions return false;
141*1031c584SApple OSS Distributions }
142*1031c584SApple OSS Distributions if (!addChar('\"')) {
143*1031c584SApple OSS Distributions return false;
144*1031c584SApple OSS Distributions }
145*1031c584SApple OSS Distributions if (!addChar('>')) {
146*1031c584SApple OSS Distributions return false;
147*1031c584SApple OSS Distributions }
148*1031c584SApple OSS Distributions return true;
149*1031c584SApple OSS Distributions }
150*1031c584SApple OSS Distributions
151*1031c584SApple OSS Distributions bool
addXMLEndTag(const char * tagString)152*1031c584SApple OSS Distributions OSSerialize::addXMLEndTag(const char *tagString)
153*1031c584SApple OSS Distributions {
154*1031c584SApple OSS Distributions if (!addChar('<')) {
155*1031c584SApple OSS Distributions return false;
156*1031c584SApple OSS Distributions }
157*1031c584SApple OSS Distributions if (!addChar('/')) {
158*1031c584SApple OSS Distributions return false;
159*1031c584SApple OSS Distributions }
160*1031c584SApple OSS Distributions if (!addString(tagString)) {
161*1031c584SApple OSS Distributions return false;
162*1031c584SApple OSS Distributions }
163*1031c584SApple OSS Distributions if (!addChar('>')) {
164*1031c584SApple OSS Distributions return false;
165*1031c584SApple OSS Distributions }
166*1031c584SApple OSS Distributions return true;
167*1031c584SApple OSS Distributions }
168*1031c584SApple OSS Distributions
169*1031c584SApple OSS Distributions bool
addChar(const char c)170*1031c584SApple OSS Distributions OSSerialize::addChar(const char c)
171*1031c584SApple OSS Distributions {
172*1031c584SApple OSS Distributions if (binary) {
173*1031c584SApple OSS Distributions printf("xml serialize\n");
174*1031c584SApple OSS Distributions return false;
175*1031c584SApple OSS Distributions }
176*1031c584SApple OSS Distributions
177*1031c584SApple OSS Distributions // add char, possibly extending our capacity
178*1031c584SApple OSS Distributions if (length >= capacity && length >= ensureCapacity(capacity + capacityIncrement)) {
179*1031c584SApple OSS Distributions return false;
180*1031c584SApple OSS Distributions }
181*1031c584SApple OSS Distributions
182*1031c584SApple OSS Distributions data[length - 1] = c;
183*1031c584SApple OSS Distributions length++;
184*1031c584SApple OSS Distributions
185*1031c584SApple OSS Distributions return true;
186*1031c584SApple OSS Distributions }
187*1031c584SApple OSS Distributions
188*1031c584SApple OSS Distributions bool
addString(const char * s)189*1031c584SApple OSS Distributions OSSerialize::addString(const char *s)
190*1031c584SApple OSS Distributions {
191*1031c584SApple OSS Distributions bool rc = false;
192*1031c584SApple OSS Distributions
193*1031c584SApple OSS Distributions while (*s && (rc = addChar(*s++))) {
194*1031c584SApple OSS Distributions ;
195*1031c584SApple OSS Distributions }
196*1031c584SApple OSS Distributions
197*1031c584SApple OSS Distributions return rc;
198*1031c584SApple OSS Distributions }
199*1031c584SApple OSS Distributions
200*1031c584SApple OSS Distributions bool
initWithCapacity(unsigned int inCapacity)201*1031c584SApple OSS Distributions OSSerialize::initWithCapacity(unsigned int inCapacity)
202*1031c584SApple OSS Distributions {
203*1031c584SApple OSS Distributions kmem_return_t kmr;
204*1031c584SApple OSS Distributions
205*1031c584SApple OSS Distributions if (!super::init()) {
206*1031c584SApple OSS Distributions return false;
207*1031c584SApple OSS Distributions }
208*1031c584SApple OSS Distributions
209*1031c584SApple OSS Distributions tags = OSArray::withCapacity(256);
210*1031c584SApple OSS Distributions if (!tags) {
211*1031c584SApple OSS Distributions return false;
212*1031c584SApple OSS Distributions }
213*1031c584SApple OSS Distributions
214*1031c584SApple OSS Distributions length = 1;
215*1031c584SApple OSS Distributions
216*1031c584SApple OSS Distributions if (!inCapacity) {
217*1031c584SApple OSS Distributions inCapacity = 1;
218*1031c584SApple OSS Distributions }
219*1031c584SApple OSS Distributions if (round_page_overflow(inCapacity, &inCapacity)) {
220*1031c584SApple OSS Distributions tags.reset();
221*1031c584SApple OSS Distributions return false;
222*1031c584SApple OSS Distributions }
223*1031c584SApple OSS Distributions
224*1031c584SApple OSS Distributions capacityIncrement = inCapacity;
225*1031c584SApple OSS Distributions
226*1031c584SApple OSS Distributions // allocate from the kernel map so that we can safely map this data
227*1031c584SApple OSS Distributions // into user space (the primary use of the OSSerialize object)
228*1031c584SApple OSS Distributions
229*1031c584SApple OSS Distributions kmr = kmem_alloc_guard(kernel_map, inCapacity, /* mask */ 0,
230*1031c584SApple OSS Distributions (kma_flags_t)(KMA_ZERO | KMA_DATA), OSSerialize_guard());
231*1031c584SApple OSS Distributions
232*1031c584SApple OSS Distributions if (kmr.kmr_return == KERN_SUCCESS) {
233*1031c584SApple OSS Distributions data = (char *)kmr.kmr_ptr;
234*1031c584SApple OSS Distributions capacity = inCapacity;
235*1031c584SApple OSS Distributions OSCONTAINER_ACCUMSIZE(capacity);
236*1031c584SApple OSS Distributions return true;
237*1031c584SApple OSS Distributions }
238*1031c584SApple OSS Distributions
239*1031c584SApple OSS Distributions capacity = 0;
240*1031c584SApple OSS Distributions return false;
241*1031c584SApple OSS Distributions }
242*1031c584SApple OSS Distributions
243*1031c584SApple OSS Distributions OSSharedPtr<OSSerialize>
withCapacity(unsigned int inCapacity)244*1031c584SApple OSS Distributions OSSerialize::withCapacity(unsigned int inCapacity)
245*1031c584SApple OSS Distributions {
246*1031c584SApple OSS Distributions OSSharedPtr<OSSerialize> me = OSMakeShared<OSSerialize>();
247*1031c584SApple OSS Distributions
248*1031c584SApple OSS Distributions if (me && !me->initWithCapacity(inCapacity)) {
249*1031c584SApple OSS Distributions return nullptr;
250*1031c584SApple OSS Distributions }
251*1031c584SApple OSS Distributions
252*1031c584SApple OSS Distributions return me;
253*1031c584SApple OSS Distributions }
254*1031c584SApple OSS Distributions
255*1031c584SApple OSS Distributions unsigned int
getLength() const256*1031c584SApple OSS Distributions OSSerialize::getLength() const
257*1031c584SApple OSS Distributions {
258*1031c584SApple OSS Distributions return length;
259*1031c584SApple OSS Distributions }
260*1031c584SApple OSS Distributions unsigned int
getCapacity() const261*1031c584SApple OSS Distributions OSSerialize::getCapacity() const
262*1031c584SApple OSS Distributions {
263*1031c584SApple OSS Distributions return capacity;
264*1031c584SApple OSS Distributions }
265*1031c584SApple OSS Distributions unsigned int
getCapacityIncrement() const266*1031c584SApple OSS Distributions OSSerialize::getCapacityIncrement() const
267*1031c584SApple OSS Distributions {
268*1031c584SApple OSS Distributions return capacityIncrement;
269*1031c584SApple OSS Distributions }
270*1031c584SApple OSS Distributions unsigned int
setCapacityIncrement(unsigned int increment)271*1031c584SApple OSS Distributions OSSerialize::setCapacityIncrement(unsigned int increment)
272*1031c584SApple OSS Distributions {
273*1031c584SApple OSS Distributions capacityIncrement = (increment)? increment : 256;
274*1031c584SApple OSS Distributions return capacityIncrement;
275*1031c584SApple OSS Distributions }
276*1031c584SApple OSS Distributions
277*1031c584SApple OSS Distributions unsigned int
ensureCapacity(unsigned int newCapacity)278*1031c584SApple OSS Distributions OSSerialize::ensureCapacity(unsigned int newCapacity)
279*1031c584SApple OSS Distributions {
280*1031c584SApple OSS Distributions kmem_return_t kmr;
281*1031c584SApple OSS Distributions
282*1031c584SApple OSS Distributions if (newCapacity <= capacity) {
283*1031c584SApple OSS Distributions return capacity;
284*1031c584SApple OSS Distributions }
285*1031c584SApple OSS Distributions
286*1031c584SApple OSS Distributions if (round_page_overflow(newCapacity, &newCapacity)) {
287*1031c584SApple OSS Distributions return capacity;
288*1031c584SApple OSS Distributions }
289*1031c584SApple OSS Distributions
290*1031c584SApple OSS Distributions kmr = kmem_realloc_guard(kernel_map, (vm_offset_t)data, capacity,
291*1031c584SApple OSS Distributions newCapacity, (kmr_flags_t)(KMR_ZERO | KMR_DATA | KMR_FREEOLD),
292*1031c584SApple OSS Distributions OSSerialize_guard());
293*1031c584SApple OSS Distributions
294*1031c584SApple OSS Distributions if (kmr.kmr_return == KERN_SUCCESS) {
295*1031c584SApple OSS Distributions size_t delta = 0;
296*1031c584SApple OSS Distributions
297*1031c584SApple OSS Distributions data = (char *)kmr.kmr_ptr;
298*1031c584SApple OSS Distributions delta -= capacity;
299*1031c584SApple OSS Distributions capacity = newCapacity;
300*1031c584SApple OSS Distributions delta += capacity;
301*1031c584SApple OSS Distributions OSCONTAINER_ACCUMSIZE(delta);
302*1031c584SApple OSS Distributions }
303*1031c584SApple OSS Distributions
304*1031c584SApple OSS Distributions return capacity;
305*1031c584SApple OSS Distributions }
306*1031c584SApple OSS Distributions
307*1031c584SApple OSS Distributions void
free()308*1031c584SApple OSS Distributions OSSerialize::free()
309*1031c584SApple OSS Distributions {
310*1031c584SApple OSS Distributions if (capacity) {
311*1031c584SApple OSS Distributions kmem_free_guard(kernel_map, (vm_offset_t)data, capacity,
312*1031c584SApple OSS Distributions KMF_NONE, OSSerialize_guard());
313*1031c584SApple OSS Distributions OSCONTAINER_ACCUMSIZE( -((size_t)capacity));
314*1031c584SApple OSS Distributions data = nullptr;
315*1031c584SApple OSS Distributions capacity = 0;
316*1031c584SApple OSS Distributions }
317*1031c584SApple OSS Distributions super::free();
318*1031c584SApple OSS Distributions }
319*1031c584SApple OSS Distributions
320*1031c584SApple OSS Distributions
OSDefineMetaClassAndStructors(OSSerializer,OSObject)321*1031c584SApple OSS Distributions OSDefineMetaClassAndStructors(OSSerializer, OSObject)
322*1031c584SApple OSS Distributions
323*1031c584SApple OSS Distributions OSSharedPtr<OSSerializer>
324*1031c584SApple OSS Distributions OSSerializer::forTarget( void * target,
325*1031c584SApple OSS Distributions OSSerializerCallback callback, void * ref )
326*1031c584SApple OSS Distributions {
327*1031c584SApple OSS Distributions OSSharedPtr<OSSerializer> thing = OSMakeShared<OSSerializer>();
328*1031c584SApple OSS Distributions
329*1031c584SApple OSS Distributions if (thing && !thing->init()) {
330*1031c584SApple OSS Distributions thing.reset();
331*1031c584SApple OSS Distributions }
332*1031c584SApple OSS Distributions
333*1031c584SApple OSS Distributions if (thing) {
334*1031c584SApple OSS Distributions thing->target = target;
335*1031c584SApple OSS Distributions thing->ref = ref;
336*1031c584SApple OSS Distributions thing->callback = callback;
337*1031c584SApple OSS Distributions }
338*1031c584SApple OSS Distributions return thing;
339*1031c584SApple OSS Distributions }
340*1031c584SApple OSS Distributions
341*1031c584SApple OSS Distributions bool
callbackToBlock(void * target __unused,void * ref,OSSerialize * serializer)342*1031c584SApple OSS Distributions OSSerializer::callbackToBlock(void * target __unused, void * ref,
343*1031c584SApple OSS Distributions OSSerialize * serializer)
344*1031c584SApple OSS Distributions {
345*1031c584SApple OSS Distributions return ((OSSerializerBlock)ref)(serializer);
346*1031c584SApple OSS Distributions }
347*1031c584SApple OSS Distributions
348*1031c584SApple OSS Distributions OSSharedPtr<OSSerializer>
withBlock(OSSerializerBlock callback)349*1031c584SApple OSS Distributions OSSerializer::withBlock(
350*1031c584SApple OSS Distributions OSSerializerBlock callback)
351*1031c584SApple OSS Distributions {
352*1031c584SApple OSS Distributions OSSharedPtr<OSSerializer> serializer;
353*1031c584SApple OSS Distributions OSSerializerBlock block;
354*1031c584SApple OSS Distributions
355*1031c584SApple OSS Distributions block = Block_copy(callback);
356*1031c584SApple OSS Distributions if (!block) {
357*1031c584SApple OSS Distributions return NULL;
358*1031c584SApple OSS Distributions }
359*1031c584SApple OSS Distributions
360*1031c584SApple OSS Distributions serializer = (OSSerializer::forTarget(NULL, &OSSerializer::callbackToBlock, block));
361*1031c584SApple OSS Distributions
362*1031c584SApple OSS Distributions if (!serializer) {
363*1031c584SApple OSS Distributions Block_release(block);
364*1031c584SApple OSS Distributions }
365*1031c584SApple OSS Distributions
366*1031c584SApple OSS Distributions return serializer;
367*1031c584SApple OSS Distributions }
368*1031c584SApple OSS Distributions
369*1031c584SApple OSS Distributions void
free(void)370*1031c584SApple OSS Distributions OSSerializer::free(void)
371*1031c584SApple OSS Distributions {
372*1031c584SApple OSS Distributions if (callback == &callbackToBlock) {
373*1031c584SApple OSS Distributions Block_release(ref);
374*1031c584SApple OSS Distributions }
375*1031c584SApple OSS Distributions
376*1031c584SApple OSS Distributions super::free();
377*1031c584SApple OSS Distributions }
378*1031c584SApple OSS Distributions
379*1031c584SApple OSS Distributions bool
serialize(OSSerialize * s) const380*1031c584SApple OSS Distributions OSSerializer::serialize( OSSerialize * s ) const
381*1031c584SApple OSS Distributions {
382*1031c584SApple OSS Distributions return (*callback)(target, ref, s);
383*1031c584SApple OSS Distributions }
384