xref: /xnu-11215/libkern/c++/OSSerialize.cpp (revision 855239e5)
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 
40 #define super OSObject
41 
42 OSDefineMetaClassAndStructors(OSSerialize, OSObject)
43 OSMetaClassDefineReservedUnused(OSSerialize, 0);
44 OSMetaClassDefineReservedUnused(OSSerialize, 1);
45 OSMetaClassDefineReservedUnused(OSSerialize, 2);
46 OSMetaClassDefineReservedUnused(OSSerialize, 3);
47 OSMetaClassDefineReservedUnused(OSSerialize, 4);
48 OSMetaClassDefineReservedUnused(OSSerialize, 5);
49 OSMetaClassDefineReservedUnused(OSSerialize, 6);
50 OSMetaClassDefineReservedUnused(OSSerialize, 7);
51 
52 #if OSALLOCDEBUG
53 extern "C" {
54     extern int debug_container_malloc_size;
55 };
56 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
57 #else
58 #define ACCUMSIZE(s)
59 #endif
60 
61 char * OSSerialize::text() const
62 {
63 	return data;
64 }
65 
66 void OSSerialize::clearText()
67 {
68 	bzero((void *)data, capacity);
69 	length = 1;
70 	tag = 0;
71 	tags->flushCollection();
72 }
73 
74 bool OSSerialize::previouslySerialized(const OSMetaClassBase *o)
75 {
76 	char temp[16];
77 	OSString *tagString;
78 
79 	// look it up
80 	tagString = (OSString *)tags->getObject((const OSSymbol *) o);
81 
82 // xx-review: no error checking here for addString calls!
83 	// does it exist?
84 	if (tagString) {
85 		addString("<reference IDREF=\"");
86 		addString(tagString->getCStringNoCopy());
87 		addString("\"/>");
88 		return true;
89 	}
90 
91 	// build a tag
92 	snprintf(temp, sizeof(temp), "%u", tag++);
93 	tagString = OSString::withCString(temp);
94 
95 	// add to tag dictionary
96         tags->setObject((const OSSymbol *) o, tagString);// XXX check return
97 	tagString->release();
98 
99 	return false;
100 }
101 
102 bool OSSerialize::addXMLStartTag(const OSMetaClassBase *o, const char *tagString)
103 {
104 
105 	if (!addChar('<')) return false;
106 	if (!addString(tagString)) return false;
107 	if (!addString(" ID=\"")) return false;
108 	if (!addString(((OSString *)tags->getObject((const OSSymbol *)o))->getCStringNoCopy()))
109 		return false;
110 	if (!addChar('\"')) return false;
111 	if (!addChar('>')) return false;
112 	return true;
113 }
114 
115 bool OSSerialize::addXMLEndTag(const char *tagString)
116 {
117 
118 	if (!addChar('<')) return false;
119 	if (!addChar('/')) return false;
120 	if (!addString(tagString)) return false;
121 	if (!addChar('>')) return false;
122 	return true;
123 }
124 
125 bool OSSerialize::addChar(const char c)
126 {
127 	// add char, possibly extending our capacity
128 	if (length >= capacity && length >=ensureCapacity(capacity+capacityIncrement))
129 		return false;
130 
131 	data[length - 1] = c;
132 	length++;
133 
134 	return true;
135 }
136 
137 bool OSSerialize::addString(const char *s)
138 {
139 	bool rc = false;
140 
141 	while (*s && (rc = addChar(*s++))) ;
142 
143 	return rc;
144 }
145 
146 bool OSSerialize::initWithCapacity(unsigned int inCapacity)
147 {
148     if (!super::init())
149             return false;
150 
151     tags = OSDictionary::withCapacity(32);
152     if (!tags) {
153         return false;
154     }
155 
156     tag = 0;
157     length = 1;
158     capacity = (inCapacity) ? round_page_32(inCapacity) : round_page_32(1);
159     capacityIncrement = capacity;
160 
161     // allocate from the kernel map so that we can safely map this data
162     // into user space (the primary use of the OSSerialize object)
163 
164     kern_return_t rc = kmem_alloc(kernel_map, (vm_offset_t *)&data, capacity);
165     if (rc) {
166         tags->release();
167         tags = 0;
168         return false;
169     }
170     bzero((void *)data, capacity);
171 
172 
173     ACCUMSIZE(capacity);
174 
175     return true;
176 }
177 
178 OSSerialize *OSSerialize::withCapacity(unsigned int inCapacity)
179 {
180 	OSSerialize *me = new OSSerialize;
181 
182 	if (me && !me->initWithCapacity(inCapacity)) {
183 		me->release();
184 		return 0;
185 	}
186 
187 	return me;
188 }
189 
190 unsigned int OSSerialize::getLength() const { return length; }
191 unsigned int OSSerialize::getCapacity() const { return capacity; }
192 unsigned int OSSerialize::getCapacityIncrement() const { return capacityIncrement; }
193 unsigned int OSSerialize::setCapacityIncrement(unsigned int increment)
194 {
195     capacityIncrement = (increment)? increment : 256;
196     return capacityIncrement;
197 }
198 
199 unsigned int OSSerialize::ensureCapacity(unsigned int newCapacity)
200 {
201 	char *newData;
202 
203 	if (newCapacity <= capacity)
204 		return capacity;
205 
206 	// round up
207 	newCapacity = round_page_32(newCapacity);
208 
209 	kern_return_t rc = kmem_realloc(kernel_map,
210 					(vm_offset_t)data,
211 					capacity,
212 					(vm_offset_t *)&newData,
213 					newCapacity);
214 	if (!rc) {
215 	    ACCUMSIZE(newCapacity);
216 
217 	    // kmem realloc does not free the old address range
218 	    kmem_free(kernel_map, (vm_offset_t)data, capacity);
219 	    ACCUMSIZE(-capacity);
220 
221 	    // kmem realloc does not zero out the new memory
222 	    // and this could end up going to user land
223 	    bzero(&newData[capacity], newCapacity - capacity);
224 
225 	    data = newData;
226 	    capacity = newCapacity;
227 	}
228 
229 	return capacity;
230 }
231 
232 void OSSerialize::free()
233 {
234     if (tags)
235         tags->release();
236 
237     if (data) {
238 	kmem_free(kernel_map, (vm_offset_t)data, capacity);
239         ACCUMSIZE( -capacity );
240     }
241     super::free();
242 }
243 
244 
245 OSDefineMetaClassAndStructors(OSSerializer, OSObject)
246 
247 OSSerializer * OSSerializer::forTarget( void * target,
248                                OSSerializerCallback callback, void * ref )
249 {
250     OSSerializer * thing;
251 
252     thing = new OSSerializer;
253     if( thing && !thing->init()) {
254 	thing->release();
255 	thing = 0;
256     }
257 
258     if( thing) {
259 	thing->target	= target;
260         thing->ref	= ref;
261         thing->callback = callback;
262     }
263     return( thing );
264 }
265 
266 bool OSSerializer::serialize( OSSerialize * s ) const
267 {
268     return( (*callback)(target, ref, s) );
269 }
270