1 /* 2 * Copyright (c) 2000 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 /* IOData.m created by rsulack on Thu 25-Sep-1997 */ 29 30 #include <string.h> 31 32 #include <libkern/c++/OSData.h> 33 #include <libkern/c++/OSSerialize.h> 34 #include <libkern/c++/OSLib.h> 35 #include <libkern/c++/OSString.h> 36 #include <string.h> 37 38 #define super OSObject 39 40 OSDefineMetaClassAndStructors(OSData, OSObject) 41 OSMetaClassDefineReservedUsed(OSData, 0); // setDeallocFunction 42 OSMetaClassDefineReservedUnused(OSData, 1); 43 OSMetaClassDefineReservedUnused(OSData, 2); 44 OSMetaClassDefineReservedUnused(OSData, 3); 45 OSMetaClassDefineReservedUnused(OSData, 4); 46 OSMetaClassDefineReservedUnused(OSData, 5); 47 OSMetaClassDefineReservedUnused(OSData, 6); 48 OSMetaClassDefineReservedUnused(OSData, 7); 49 50 #define EXTERNAL ((unsigned int) -1) 51 52 #if OSALLOCDEBUG 53 extern int debug_container_malloc_size; 54 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0) 55 #else 56 #define ACCUMSIZE(s) 57 #endif 58 59 bool OSData::initWithCapacity(unsigned int inCapacity) 60 { 61 if (!super::init()) 62 return false; 63 64 if (data && (!inCapacity || capacity < inCapacity) ) { 65 // clean out old data's storage if it isn't big enough 66 kfree(data, capacity); 67 data = 0; 68 ACCUMSIZE(-capacity); 69 } 70 71 if (inCapacity && !data) { 72 data = (void *) kalloc(inCapacity); 73 if (!data) 74 return false; 75 capacity = inCapacity; 76 ACCUMSIZE(inCapacity); 77 } 78 79 length = 0; 80 if (inCapacity < 16) 81 capacityIncrement = 16; 82 else 83 capacityIncrement = inCapacity; 84 85 return true; 86 } 87 88 bool OSData::initWithBytes(const void *bytes, unsigned int inLength) 89 { 90 if ((inLength && !bytes) || !initWithCapacity(inLength)) 91 return false; 92 93 if (bytes != data) 94 bcopy(bytes, data, inLength); 95 length = inLength; 96 97 return true; 98 } 99 100 bool OSData::initWithBytesNoCopy(void *bytes, unsigned int inLength) 101 { 102 if (!super::init()) 103 return false; 104 105 length = inLength; 106 capacity = EXTERNAL; 107 data = bytes; 108 109 return true; 110 } 111 112 bool OSData::initWithData(const OSData *inData) 113 { 114 return initWithBytes(inData->data, inData->length); 115 } 116 117 bool OSData::initWithData(const OSData *inData, 118 unsigned int start, unsigned int inLength) 119 { 120 const void *localData = inData->getBytesNoCopy(start, inLength); 121 122 if (localData) 123 return initWithBytes(localData, inLength); 124 else 125 return false; 126 } 127 128 OSData *OSData::withCapacity(unsigned int inCapacity) 129 { 130 OSData *me = new OSData; 131 132 if (me && !me->initWithCapacity(inCapacity)) { 133 me->release(); 134 return 0; 135 } 136 137 return me; 138 } 139 140 OSData *OSData::withBytes(const void *bytes, unsigned int inLength) 141 { 142 OSData *me = new OSData; 143 144 if (me && !me->initWithBytes(bytes, inLength)) { 145 me->release(); 146 return 0; 147 } 148 return me; 149 } 150 151 OSData *OSData::withBytesNoCopy(void *bytes, unsigned int inLength) 152 { 153 OSData *me = new OSData; 154 155 if (me && !me->initWithBytesNoCopy(bytes, inLength)) { 156 me->release(); 157 return 0; 158 } 159 160 return me; 161 } 162 163 OSData *OSData::withData(const OSData *inData) 164 { 165 OSData *me = new OSData; 166 167 if (me && !me->initWithData(inData)) { 168 me->release(); 169 return 0; 170 } 171 172 return me; 173 } 174 175 OSData *OSData::withData(const OSData *inData, 176 unsigned int start, unsigned int inLength) 177 { 178 OSData *me = new OSData; 179 180 if (me && !me->initWithData(inData, start, inLength)) { 181 me->release(); 182 return 0; 183 } 184 185 return me; 186 } 187 188 void OSData::free() 189 { 190 if (capacity != EXTERNAL && data && capacity) { 191 kfree(data, capacity); 192 ACCUMSIZE( -capacity ); 193 } else if (capacity == EXTERNAL) { 194 DeallocFunction freemem = (DeallocFunction)reserved; 195 if (freemem && data && length) { 196 freemem(data, length); 197 } 198 } 199 super::free(); 200 } 201 202 unsigned int OSData::getLength() const { return length; } 203 unsigned int OSData::getCapacity() const { return capacity; } 204 205 unsigned int OSData::getCapacityIncrement() const 206 { 207 return capacityIncrement; 208 } 209 210 unsigned int OSData::setCapacityIncrement(unsigned increment) 211 { 212 return capacityIncrement = increment; 213 } 214 215 // xx-review: does not check for capacity == EXTERNAL 216 217 unsigned int OSData::ensureCapacity(unsigned int newCapacity) 218 { 219 unsigned char * newData; 220 221 if (newCapacity <= capacity) 222 return capacity; 223 224 newCapacity = (((newCapacity - 1) / capacityIncrement) + 1) 225 * capacityIncrement; 226 227 newData = (unsigned char *) kalloc(newCapacity); 228 229 if ( newData ) { 230 bzero(newData + capacity, newCapacity - capacity); 231 if (data) { 232 bcopy(data, newData, capacity); 233 kfree(data, capacity); 234 } 235 ACCUMSIZE( newCapacity - capacity ); 236 data = (void *) newData; 237 capacity = newCapacity; 238 } 239 240 return capacity; 241 } 242 243 bool OSData::appendBytes(const void *bytes, unsigned int inLength) 244 { 245 unsigned int newSize; 246 247 if (!inLength) 248 return true; 249 250 if (capacity == EXTERNAL) 251 return false; 252 253 newSize = length + inLength; 254 if ( (newSize > capacity) && newSize > ensureCapacity(newSize) ) 255 return false; 256 257 if (bytes) 258 bcopy(bytes, &((unsigned char *)data)[length], inLength); 259 else 260 bzero(&((unsigned char *)data)[length], inLength); 261 262 length = newSize; 263 264 return true; 265 } 266 267 bool OSData::appendByte(unsigned char byte, unsigned int inLength) 268 { 269 unsigned int newSize; 270 271 if (!inLength) 272 return true; 273 274 if (capacity == EXTERNAL) 275 return false; 276 277 newSize = length + inLength; 278 if ( (newSize > capacity) && newSize > ensureCapacity(newSize) ) 279 return false; 280 281 memset(&((unsigned char *)data)[length], byte, inLength); 282 length = newSize; 283 284 return true; 285 } 286 287 bool OSData::appendBytes(const OSData *other) 288 { 289 return appendBytes(other->data, other->length); 290 } 291 292 const void *OSData::getBytesNoCopy() const 293 { 294 if (!length) 295 return 0; 296 else 297 return data; 298 } 299 300 const void *OSData::getBytesNoCopy(unsigned int start, 301 unsigned int inLength) const 302 { 303 const void *outData = 0; 304 305 if (length 306 && start < length 307 && (start + inLength) <= length) 308 outData = (const void *) ((char *) data + start); 309 310 return outData; 311 } 312 313 bool OSData::isEqualTo(const OSData *aData) const 314 { 315 unsigned int len; 316 317 len = aData->length; 318 if ( length != len ) 319 return false; 320 321 return isEqualTo(aData->data, len); 322 } 323 324 bool OSData::isEqualTo(const void *someData, unsigned int inLength) const 325 { 326 return (length >= inLength) && (bcmp(data, someData, inLength) == 0); 327 } 328 329 bool OSData::isEqualTo(const OSMetaClassBase *obj) const 330 { 331 OSData * otherData; 332 OSString * str; 333 334 if ((otherData = OSDynamicCast(OSData, obj))) 335 return isEqualTo(otherData); 336 else if ((str = OSDynamicCast (OSString, obj))) 337 return isEqualTo(str); 338 else 339 return false; 340 } 341 342 bool OSData::isEqualTo(const OSString *obj) const 343 { 344 const char * aCString; 345 char * dataPtr; 346 unsigned int checkLen = length; 347 unsigned int stringLen; 348 349 if (!obj) 350 return false; 351 352 stringLen = obj->getLength (); 353 354 dataPtr = (char *)data; 355 356 if (stringLen != checkLen) { 357 358 // check for the fact that OSData may be a buffer that 359 // that includes a termination byte and will thus have 360 // a length of the actual string length PLUS 1. In this 361 // case we verify that the additional byte is a terminator 362 // and if so count the two lengths as being the same. 363 364 if ( (checkLen - stringLen) == 1) { 365 if (dataPtr[checkLen-1] != 0) // non-zero means not a terminator and thus not likely the same 366 return false; 367 checkLen--; 368 } 369 else 370 return false; 371 } 372 373 aCString = obj->getCStringNoCopy (); 374 375 for ( unsigned int i=0; i < checkLen; i++ ) { 376 if ( *dataPtr++ != aCString[i] ) 377 return false; 378 } 379 380 return true; 381 } 382 383 //this was taken from CFPropertyList.c 384 static const char __CFPLDataEncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 385 386 bool OSData::serialize(OSSerialize *s) const 387 { 388 unsigned int i; 389 const unsigned char *p; 390 unsigned char c; 391 392 if (s->previouslySerialized(this)) return true; 393 394 if (!s->addXMLStartTag(this, "data")) return false; 395 396 for (i = 0, p = (unsigned char *)data; i < length; i++, p++) { 397 /* 3 bytes are encoded as 4 */ 398 switch (i % 3) { 399 case 0: 400 c = __CFPLDataEncodeTable [ ((p[0] >> 2) & 0x3f)]; 401 if (!s->addChar(c)) return false; 402 break; 403 case 1: 404 c = __CFPLDataEncodeTable [ ((((p[-1] << 8) | p[0]) >> 4) & 0x3f)]; 405 if (!s->addChar(c)) return false; 406 break; 407 case 2: 408 c = __CFPLDataEncodeTable [ ((((p[-1] << 8) | p[0]) >> 6) & 0x3f)]; 409 if (!s->addChar(c)) return false; 410 c = __CFPLDataEncodeTable [ (p[0] & 0x3f)]; 411 if (!s->addChar(c)) return false; 412 break; 413 } 414 } 415 switch (i % 3) { 416 case 0: 417 break; 418 case 1: 419 c = __CFPLDataEncodeTable [ ((p[-1] << 4) & 0x30)]; 420 if (!s->addChar(c)) return false; 421 if (!s->addChar('=')) return false; 422 if (!s->addChar('=')) return false; 423 break; 424 case 2: 425 c = __CFPLDataEncodeTable [ ((p[-1] << 2) & 0x3c)]; 426 if (!s->addChar(c)) return false; 427 if (!s->addChar('=')) return false; 428 break; 429 } 430 431 return s->addXMLEndTag("data"); 432 } 433 434 /* Note I am just using the reserved pointer here instead of allocating a whole buffer 435 * to hold one pointer. 436 */ 437 void OSData::setDeallocFunction(DeallocFunction func) 438 { 439 reserved = (ExpansionData *)func; 440 return; 441 } 442