xref: /xnu-11215/libkern/c++/OSSerialize.cpp (revision 76e12aa3)
1 /*
2  * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* OSSerialize.cpp created by rsulack on Wen 25-Nov-1998 */
29 
30 #include <sys/cdefs.h>
31 
32 __BEGIN_DECLS
33 #include <vm/vm_kern.h>
34 __END_DECLS
35 
36 #include <libkern/c++/OSContainers.h>
37 #include <libkern/c++/OSLib.h>
38 #include <libkern/c++/OSDictionary.h>
39 #include <libkern/OSSerializeBinary.h>
40 #include <IOKit/IOLib.h>
41 
42 #define super OSObject
43 
44 OSDefineMetaClassAndStructors(OSSerialize, OSObject)
45 OSMetaClassDefineReservedUnused(OSSerialize, 0);
46 OSMetaClassDefineReservedUnused(OSSerialize, 1);
47 OSMetaClassDefineReservedUnused(OSSerialize, 2);
48 OSMetaClassDefineReservedUnused(OSSerialize, 3);
49 OSMetaClassDefineReservedUnused(OSSerialize, 4);
50 OSMetaClassDefineReservedUnused(OSSerialize, 5);
51 OSMetaClassDefineReservedUnused(OSSerialize, 6);
52 OSMetaClassDefineReservedUnused(OSSerialize, 7);
53 
54 
55 char * OSSerialize::text() const
56 {
57 	return data;
58 }
59 
60 void OSSerialize::clearText()
61 {
62 	if (binary)
63 	{
64 		length = sizeof(kOSSerializeBinarySignature);
65 		bzero(&data[length], capacity - length);
66 		endCollection = true;
67 	}
68     else
69     {
70 		bzero((void *)data, capacity);
71 		length = 1;
72     }
73 	tags->flushCollection();
74 }
75 
76 bool OSSerialize::previouslySerialized(const OSMetaClassBase *o)
77 {
78 	char temp[16];
79 	unsigned int tagIdx;
80 
81 	if (binary) return (binarySerialize(o));
82 
83 	// look it up
84 	tagIdx = tags->getNextIndexOfObject(o, 0);
85 
86 // xx-review: no error checking here for addString calls!
87 	// does it exist?
88 	if (tagIdx != -1U) {
89 		addString("<reference IDREF=\"");
90 		snprintf(temp, sizeof(temp), "%u", tagIdx);
91 		addString(temp);
92 		addString("\"/>");
93 		return true;
94 	}
95 
96 	// add to tag array
97     tags->setObject(o);// XXX check return
98 
99 	return false;
100 }
101 
102 bool OSSerialize::addXMLStartTag(const OSMetaClassBase *o, const char *tagString)
103 {
104 	char temp[16];
105 	unsigned int tagIdx;
106 
107 	if (binary)
108     {
109 		printf("class %s: xml serialize\n", o->getMetaClass()->getClassName());
110 		return (false);
111 	}
112 
113 	if (!addChar('<')) return false;
114 	if (!addString(tagString)) return false;
115 	if (!addString(" ID=\"")) return false;
116 	tagIdx = tags->getNextIndexOfObject(o, 0);
117 	assert(tagIdx != -1U);
118 	snprintf(temp, sizeof(temp), "%u", tagIdx);
119 	if (!addString(temp))
120 		return false;
121 	if (!addChar('\"')) return false;
122 	if (!addChar('>')) return false;
123 	return true;
124 }
125 
126 bool OSSerialize::addXMLEndTag(const char *tagString)
127 {
128 
129 	if (!addChar('<')) return false;
130 	if (!addChar('/')) return false;
131 	if (!addString(tagString)) return false;
132 	if (!addChar('>')) return false;
133 	return true;
134 }
135 
136 bool OSSerialize::addChar(const char c)
137 {
138 	if (binary)
139     {
140 		printf("xml serialize\n");
141 		return (false);
142 	}
143 
144 	// add char, possibly extending our capacity
145 	if (length >= capacity && length >=ensureCapacity(capacity+capacityIncrement))
146 		return false;
147 
148 	data[length - 1] = c;
149 	length++;
150 
151 	return true;
152 }
153 
154 bool OSSerialize::addString(const char *s)
155 {
156 	bool rc = false;
157 
158 	while (*s && (rc = addChar(*s++))) ;
159 
160 	return rc;
161 }
162 
163 bool OSSerialize::initWithCapacity(unsigned int inCapacity)
164 {
165     if (!super::init())
166             return false;
167 
168     tags = OSArray::withCapacity(256);
169     if (!tags) {
170         return false;
171     }
172 
173     length = 1;
174 
175     if (!inCapacity) {
176         inCapacity = 1;
177     }
178     if (round_page_overflow(inCapacity, &capacity)) {
179         tags->release();
180         tags = 0;
181         return false;
182     }
183 
184     capacityIncrement = capacity;
185 
186     // allocate from the kernel map so that we can safely map this data
187     // into user space (the primary use of the OSSerialize object)
188 
189     kern_return_t rc = kmem_alloc(kernel_map, (vm_offset_t *)&data, capacity, IOMemoryTag(kernel_map));
190     if (rc) {
191         tags->release();
192         tags = 0;
193         return false;
194     }
195     bzero((void *)data, capacity);
196 
197 
198     OSCONTAINER_ACCUMSIZE(capacity);
199 
200     return true;
201 }
202 
203 OSSerialize *OSSerialize::withCapacity(unsigned int inCapacity)
204 {
205 	OSSerialize *me = new OSSerialize;
206 
207 	if (me && !me->initWithCapacity(inCapacity)) {
208 		me->release();
209 		return 0;
210 	}
211 
212 	return me;
213 }
214 
215 unsigned int OSSerialize::getLength() const { return length; }
216 unsigned int OSSerialize::getCapacity() const { return capacity; }
217 unsigned int OSSerialize::getCapacityIncrement() const { return capacityIncrement; }
218 unsigned int OSSerialize::setCapacityIncrement(unsigned int increment)
219 {
220     capacityIncrement = (increment)? increment : 256;
221     return capacityIncrement;
222 }
223 
224 unsigned int OSSerialize::ensureCapacity(unsigned int newCapacity)
225 {
226 	char *newData;
227 
228 	if (newCapacity <= capacity)
229 		return capacity;
230 
231 	if (round_page_overflow(newCapacity, &newCapacity)) {
232 		return capacity;
233 	}
234 
235 	kern_return_t rc = kmem_realloc(kernel_map,
236 					(vm_offset_t)data,
237 					capacity,
238 					(vm_offset_t *)&newData,
239 					newCapacity,
240 					VM_KERN_MEMORY_IOKIT);
241 	if (!rc) {
242 	    OSCONTAINER_ACCUMSIZE(newCapacity);
243 
244 	    // kmem realloc does not free the old address range
245 	    kmem_free(kernel_map, (vm_offset_t)data, capacity);
246 	    OSCONTAINER_ACCUMSIZE(-((size_t)capacity));
247 
248 	    // kmem realloc does not zero out the new memory
249 	    // and this could end up going to user land
250 	    bzero(&newData[capacity], newCapacity - capacity);
251 
252 	    data = newData;
253 	    capacity = newCapacity;
254 	}
255 
256 	return capacity;
257 }
258 
259 void OSSerialize::free()
260 {
261     if (tags)
262         tags->release();
263 
264     if (data) {
265 	kmem_free(kernel_map, (vm_offset_t)data, capacity);
266         OSCONTAINER_ACCUMSIZE( -((size_t)capacity) );
267     }
268     super::free();
269 }
270 
271 
272 OSDefineMetaClassAndStructors(OSSerializer, OSObject)
273 
274 OSSerializer * OSSerializer::forTarget( void * target,
275                                OSSerializerCallback callback, void * ref )
276 {
277     OSSerializer * thing;
278 
279     thing = new OSSerializer;
280     if( thing && !thing->init()) {
281 	thing->release();
282 	thing = 0;
283     }
284 
285     if( thing) {
286 	thing->target	= target;
287         thing->ref	= ref;
288         thing->callback = callback;
289     }
290     return( thing );
291 }
292 
293 bool OSSerializer::serialize( OSSerialize * s ) const
294 {
295     return( (*callback)(target, ref, s) );
296 }
297