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