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