]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSData.cpp
a542ee60364f7add87a7a8d16be835dce9be8780
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* IOData.m created by rsulack on Thu 25-Sep-1997 */
33 #include <vm/vm_kern.h>
36 #include <libkern/c++/OSData.h>
37 #include <libkern/c++/OSSerialize.h>
38 #include <libkern/c++/OSLib.h>
39 #include <libkern/c++/OSString.h>
40 #include <IOKit/IOLib.h>
42 #define super OSObject
44 OSDefineMetaClassAndStructors(OSData
, OSObject
)
45 OSMetaClassDefineReservedUsed(OSData
, 0); // setDeallocFunction
46 OSMetaClassDefineReservedUnused(OSData
, 1);
47 OSMetaClassDefineReservedUnused(OSData
, 2);
48 OSMetaClassDefineReservedUnused(OSData
, 3);
49 OSMetaClassDefineReservedUnused(OSData
, 4);
50 OSMetaClassDefineReservedUnused(OSData
, 5);
51 OSMetaClassDefineReservedUnused(OSData
, 6);
52 OSMetaClassDefineReservedUnused(OSData
, 7);
54 #define EXTERNAL ((unsigned int) -1)
56 bool OSData::initWithCapacity(unsigned int inCapacity
)
60 OSCONTAINER_ACCUMSIZE(-((size_t)capacity
));
61 if (!inCapacity
|| (capacity
< inCapacity
))
63 // clean out old data's storage if it isn't big enough
64 if (capacity
< page_size
) kfree(data
, capacity
);
65 else kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
74 if (inCapacity
&& !data
) {
76 if (inCapacity
< page_size
) data
= (void *) kalloc_container(inCapacity
);
79 inCapacity
= round_page_32(inCapacity
);
80 kr
= kmem_alloc(kernel_map
, (vm_offset_t
*)&data
, inCapacity
, IOMemoryTag(kernel_map
));
81 if (KERN_SUCCESS
!= kr
) data
= NULL
;
85 capacity
= inCapacity
;
87 OSCONTAINER_ACCUMSIZE(capacity
);
91 capacityIncrement
= 16;
93 capacityIncrement
= inCapacity
;
98 bool OSData::initWithBytes(const void *bytes
, unsigned int inLength
)
100 if ((inLength
&& !bytes
) || !initWithCapacity(inLength
))
104 bcopy(bytes
, data
, inLength
);
110 bool OSData::initWithBytesNoCopy(void *bytes
, unsigned int inLength
)
122 bool OSData::initWithData(const OSData
*inData
)
124 return initWithBytes(inData
->data
, inData
->length
);
127 bool OSData::initWithData(const OSData
*inData
,
128 unsigned int start
, unsigned int inLength
)
130 const void *localData
= inData
->getBytesNoCopy(start
, inLength
);
133 return initWithBytes(localData
, inLength
);
138 OSData
*OSData::withCapacity(unsigned int inCapacity
)
140 OSData
*me
= new OSData
;
142 if (me
&& !me
->initWithCapacity(inCapacity
)) {
150 OSData
*OSData::withBytes(const void *bytes
, unsigned int inLength
)
152 OSData
*me
= new OSData
;
154 if (me
&& !me
->initWithBytes(bytes
, inLength
)) {
161 OSData
*OSData::withBytesNoCopy(void *bytes
, unsigned int inLength
)
163 OSData
*me
= new OSData
;
165 if (me
&& !me
->initWithBytesNoCopy(bytes
, inLength
)) {
173 OSData
*OSData::withData(const OSData
*inData
)
175 OSData
*me
= new OSData
;
177 if (me
&& !me
->initWithData(inData
)) {
185 OSData
*OSData::withData(const OSData
*inData
,
186 unsigned int start
, unsigned int inLength
)
188 OSData
*me
= new OSData
;
190 if (me
&& !me
->initWithData(inData
, start
, inLength
)) {
200 if ((capacity
!= EXTERNAL
) && data
&& capacity
) {
201 if (capacity
< page_size
) kfree(data
, capacity
);
202 else kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
203 OSCONTAINER_ACCUMSIZE( -((size_t)capacity
) );
204 } else if (capacity
== EXTERNAL
) {
205 DeallocFunction freemem
= reserved
? reserved
->deallocFunction
: NULL
;
206 if (freemem
&& data
&& length
) {
207 freemem(data
, length
);
210 if (reserved
) kfree(reserved
, sizeof(ExpansionData
));
214 unsigned int OSData::getLength() const { return length
; }
215 unsigned int OSData::getCapacity() const { return capacity
; }
217 unsigned int OSData::getCapacityIncrement() const
219 return capacityIncrement
;
222 unsigned int OSData::setCapacityIncrement(unsigned increment
)
224 return capacityIncrement
= increment
;
227 // xx-review: does not check for capacity == EXTERNAL
229 unsigned int OSData::ensureCapacity(unsigned int newCapacity
)
231 unsigned char * newData
;
232 unsigned int finalCapacity
;
236 if (newCapacity
<= capacity
)
239 finalCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
242 // integer overflow check
243 if (finalCapacity
< newCapacity
) return capacity
;
247 if (finalCapacity
>= page_size
) {
249 finalCapacity
= round_page_32(finalCapacity
);
250 // integer overflow check
251 if (finalCapacity
< newCapacity
) return capacity
;
252 if (capacity
>= page_size
) {
254 kr
= kmem_realloc(kernel_map
,
257 (vm_offset_t
*)&newData
,
259 IOMemoryTag(kernel_map
));
261 kr
= kmem_alloc(kernel_map
, (vm_offset_t
*)&newData
, finalCapacity
, IOMemoryTag(kernel_map
));
263 if (KERN_SUCCESS
!= kr
) newData
= NULL
;
265 else newData
= (unsigned char *) kalloc_container(finalCapacity
);
268 bzero(newData
+ capacity
, finalCapacity
- capacity
);
269 if (copydata
) bcopy(copydata
, newData
, capacity
);
271 if (capacity
< page_size
) kfree(data
, capacity
);
272 else kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
274 OSCONTAINER_ACCUMSIZE( ((size_t)finalCapacity
) - ((size_t)capacity
) );
275 data
= (void *) newData
;
276 capacity
= finalCapacity
;
282 bool OSData::appendBytes(const void *bytes
, unsigned int inLength
)
284 unsigned int newSize
;
289 if (capacity
== EXTERNAL
)
292 newSize
= length
+ inLength
;
293 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
297 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
299 bzero(&((unsigned char *)data
)[length
], inLength
);
306 bool OSData::appendByte(unsigned char byte
, unsigned int inLength
)
308 unsigned int newSize
;
313 if (capacity
== EXTERNAL
)
316 newSize
= length
+ inLength
;
317 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
320 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
326 bool OSData::appendBytes(const OSData
*other
)
328 return appendBytes(other
->data
, other
->length
);
331 const void *OSData::getBytesNoCopy() const
339 const void *OSData::getBytesNoCopy(unsigned int start
,
340 unsigned int inLength
) const
342 const void *outData
= 0;
346 && (start
+ inLength
) >= inLength
// overflow check
347 && (start
+ inLength
) <= length
)
348 outData
= (const void *) ((char *) data
+ start
);
353 bool OSData::isEqualTo(const OSData
*aData
) const
361 return isEqualTo(aData
->data
, len
);
364 bool OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
366 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
369 bool OSData::isEqualTo(const OSMetaClassBase
*obj
) const
374 if ((otherData
= OSDynamicCast(OSData
, obj
)))
375 return isEqualTo(otherData
);
376 else if ((str
= OSDynamicCast (OSString
, obj
)))
377 return isEqualTo(str
);
382 bool OSData::isEqualTo(const OSString
*obj
) const
384 const char * aCString
;
386 unsigned int checkLen
= length
;
387 unsigned int stringLen
;
392 stringLen
= obj
->getLength ();
394 dataPtr
= (char *)data
;
396 if (stringLen
!= checkLen
) {
398 // check for the fact that OSData may be a buffer that
399 // that includes a termination byte and will thus have
400 // a length of the actual string length PLUS 1. In this
401 // case we verify that the additional byte is a terminator
402 // and if so count the two lengths as being the same.
404 if ( (checkLen
- stringLen
) == 1) {
405 if (dataPtr
[checkLen
-1] != 0) // non-zero means not a terminator and thus not likely the same
413 aCString
= obj
->getCStringNoCopy ();
415 for ( unsigned int i
=0; i
< checkLen
; i
++ ) {
416 if ( *dataPtr
++ != aCString
[i
] )
423 //this was taken from CFPropertyList.c
424 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
426 bool OSData::serialize(OSSerialize
*s
) const
429 const unsigned char *p
;
431 unsigned int serializeLength
;
433 if (s
->previouslySerialized(this)) return true;
435 if (!s
->addXMLStartTag(this, "data")) return false;
437 serializeLength
= length
;
438 if (reserved
&& reserved
->disableSerialization
) serializeLength
= 0;
440 for (i
= 0, p
= (unsigned char *)data
; i
< serializeLength
; i
++, p
++) {
441 /* 3 bytes are encoded as 4 */
444 c
= __CFPLDataEncodeTable
[ ((p
[0] >> 2) & 0x3f)];
445 if (!s
->addChar(c
)) return false;
448 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
449 if (!s
->addChar(c
)) return false;
452 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
453 if (!s
->addChar(c
)) return false;
454 c
= __CFPLDataEncodeTable
[ (p
[0] & 0x3f)];
455 if (!s
->addChar(c
)) return false;
463 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 4) & 0x30)];
464 if (!s
->addChar(c
)) return false;
465 if (!s
->addChar('=')) return false;
466 if (!s
->addChar('=')) return false;
469 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 2) & 0x3c)];
470 if (!s
->addChar(c
)) return false;
471 if (!s
->addChar('=')) return false;
475 return s
->addXMLEndTag("data");
478 void OSData::setDeallocFunction(DeallocFunction func
)
482 reserved
= (typeof(reserved
)) kalloc_container(sizeof(ExpansionData
));
483 if (!reserved
) return;
484 bzero(reserved
, sizeof(ExpansionData
));
486 reserved
->deallocFunction
= func
;
489 void OSData::setSerializable(bool serializable
)
493 reserved
= (typeof(reserved
)) kalloc_container(sizeof(ExpansionData
));
494 if (!reserved
) return;
495 bzero(reserved
, sizeof(ExpansionData
));
497 reserved
->disableSerialization
= (!serializable
);
500 bool OSData::isSerializable(void)
502 return (!reserved
|| !reserved
->disableSerialization
);