]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSData.cpp
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 #define LIBKERN_SMART_POINTERS
38 #include <libkern/c++/OSData.h>
39 #include <libkern/c++/OSSerialize.h>
40 #include <libkern/c++/OSLib.h>
41 #include <libkern/c++/OSString.h>
42 #include <IOKit/IOLib.h>
44 #define super OSObject
46 OSDefineMetaClassAndStructors(OSData
, OSObject
)
47 OSMetaClassDefineReservedUsed(OSData
, 0); // setDeallocFunction
48 OSMetaClassDefineReservedUnused(OSData
, 1);
49 OSMetaClassDefineReservedUnused(OSData
, 2);
50 OSMetaClassDefineReservedUnused(OSData
, 3);
51 OSMetaClassDefineReservedUnused(OSData
, 4);
52 OSMetaClassDefineReservedUnused(OSData
, 5);
53 OSMetaClassDefineReservedUnused(OSData
, 6);
54 OSMetaClassDefineReservedUnused(OSData
, 7);
56 #define EXTERNAL ((unsigned int) -1)
59 OSData::initWithCapacity(unsigned int inCapacity
)
62 OSCONTAINER_ACCUMSIZE(-((size_t)capacity
));
63 if (!inCapacity
|| (capacity
< inCapacity
)) {
64 // clean out old data's storage if it isn't big enough
65 if (capacity
< page_size
) {
66 kfree(data
, capacity
);
68 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
79 if (inCapacity
&& !data
) {
80 if (inCapacity
< page_size
) {
81 data
= (void *) kalloc_container(inCapacity
);
84 if (round_page_overflow(inCapacity
, &inCapacity
)) {
85 kr
= KERN_RESOURCE_SHORTAGE
;
87 kr
= kmem_alloc(kernel_map
, (vm_offset_t
*)&data
, inCapacity
, IOMemoryTag(kernel_map
));
89 if (KERN_SUCCESS
!= kr
) {
96 capacity
= inCapacity
;
98 OSCONTAINER_ACCUMSIZE(capacity
);
101 if (inCapacity
< 16) {
102 capacityIncrement
= 16;
104 capacityIncrement
= inCapacity
;
111 OSData::initWithBytes(const void *bytes
, unsigned int inLength
)
113 if ((inLength
&& !bytes
) || !initWithCapacity(inLength
)) {
118 bcopy(bytes
, data
, inLength
);
126 OSData::initWithBytesNoCopy(void *bytes
, unsigned int inLength
)
128 if (!super::init()) {
140 OSData::initWithData(const OSData
*inData
)
142 return initWithBytes(inData
->data
, inData
->length
);
146 OSData::initWithData(const OSData
*inData
,
147 unsigned int start
, unsigned int inLength
)
149 const void *localData
= inData
->getBytesNoCopy(start
, inLength
);
152 return initWithBytes(localData
, inLength
);
159 OSData::withCapacity(unsigned int inCapacity
)
161 OSDataPtr me
= OSDataPtr::alloc();
163 if (me
&& !me
->initWithCapacity(inCapacity
)) {
171 OSData::withBytes(const void *bytes
, unsigned int inLength
)
173 OSDataPtr me
= OSDataPtr::alloc();
175 if (me
&& !me
->initWithBytes(bytes
, inLength
)) {
182 OSData::withBytesNoCopy(void *bytes
, unsigned int inLength
)
184 OSDataPtr me
= OSDataPtr::alloc();
186 if (me
&& !me
->initWithBytesNoCopy(bytes
, inLength
)) {
194 OSData::withData(const OSData
*inData
)
196 OSDataPtr me
= OSDataPtr::alloc();
198 if (me
&& !me
->initWithData(inData
)) {
206 OSData::withData(const OSData
*inData
,
207 unsigned int start
, unsigned int inLength
)
209 OSDataPtr me
= OSDataPtr::alloc();
211 if (me
&& !me
->initWithData(inData
, start
, inLength
)) {
221 if ((capacity
!= EXTERNAL
) && data
&& capacity
) {
222 if (capacity
< page_size
) {
223 kfree(data
, capacity
);
225 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
227 OSCONTAINER_ACCUMSIZE( -((size_t)capacity
));
228 } else if (capacity
== EXTERNAL
) {
229 DeallocFunction freemem
= reserved
? reserved
->deallocFunction
: NULL
;
230 if (freemem
&& data
&& length
) {
231 freemem(data
, length
);
235 kfree(reserved
, sizeof(ExpansionData
));
241 OSData::getLength() const
246 OSData::getCapacity() const
252 OSData::getCapacityIncrement() const
254 return capacityIncrement
;
258 OSData::setCapacityIncrement(unsigned increment
)
260 return capacityIncrement
= increment
;
263 // xx-review: does not check for capacity == EXTERNAL
266 OSData::ensureCapacity(unsigned int newCapacity
)
268 unsigned char * newData
;
269 unsigned int finalCapacity
;
273 if (newCapacity
<= capacity
) {
277 finalCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
280 // integer overflow check
281 if (finalCapacity
< newCapacity
) {
287 if (finalCapacity
>= page_size
) {
289 finalCapacity
= round_page_32(finalCapacity
);
290 // integer overflow check
291 if (finalCapacity
< newCapacity
) {
294 if (capacity
>= page_size
) {
296 kr
= kmem_realloc(kernel_map
,
299 (vm_offset_t
*)&newData
,
301 IOMemoryTag(kernel_map
));
303 kr
= kmem_alloc(kernel_map
, (vm_offset_t
*)&newData
, finalCapacity
, IOMemoryTag(kernel_map
));
305 if (KERN_SUCCESS
!= kr
) {
309 newData
= (unsigned char *) kalloc_container(finalCapacity
);
313 bzero(newData
+ capacity
, finalCapacity
- capacity
);
315 bcopy(copydata
, newData
, capacity
);
318 if (capacity
< page_size
) {
319 kfree(data
, capacity
);
321 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
324 OSCONTAINER_ACCUMSIZE(((size_t)finalCapacity
) - ((size_t)capacity
));
325 data
= (void *) newData
;
326 capacity
= finalCapacity
;
333 OSData::appendBytes(const void *bytes
, unsigned int inLength
)
335 unsigned int newSize
;
341 if (capacity
== EXTERNAL
) {
345 if (os_add_overflow(length
, inLength
, &newSize
)) {
349 if ((newSize
> capacity
) && newSize
> ensureCapacity(newSize
)) {
354 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
356 bzero(&((unsigned char *)data
)[length
], inLength
);
365 OSData::appendByte(unsigned char byte
, unsigned int inLength
)
367 unsigned int newSize
;
373 if (capacity
== EXTERNAL
) {
377 if (os_add_overflow(length
, inLength
, &newSize
)) {
381 if ((newSize
> capacity
) && newSize
> ensureCapacity(newSize
)) {
385 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
392 OSData::appendBytes(const OSData
*other
)
394 return appendBytes(other
->data
, other
->length
);
398 OSData::getBytesNoCopy() const
408 OSData::getBytesNoCopy(unsigned int start
,
409 unsigned int inLength
) const
411 const void *outData
= NULL
;
415 && (start
+ inLength
) >= inLength
// overflow check
416 && (start
+ inLength
) <= length
) {
417 outData
= (const void *) ((char *) data
+ start
);
424 OSData::isEqualTo(const OSData
*aData
) const
433 return isEqualTo(aData
->data
, len
);
437 OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
439 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
443 OSData::isEqualTo(const OSMetaClassBase
*obj
) const
448 if ((otherData
= OSDynamicCast(OSData
, obj
))) {
449 return isEqualTo(otherData
);
450 } else if ((str
= OSDynamicCast(OSString
, obj
))) {
451 return isEqualTo(str
);
458 OSData::isEqualTo(const OSString
*obj
) const
460 const char * aCString
;
462 unsigned int checkLen
= length
;
463 unsigned int stringLen
;
469 stringLen
= obj
->getLength();
471 dataPtr
= (char *)data
;
473 if (stringLen
!= checkLen
) {
474 // check for the fact that OSData may be a buffer that
475 // that includes a termination byte and will thus have
476 // a length of the actual string length PLUS 1. In this
477 // case we verify that the additional byte is a terminator
478 // and if so count the two lengths as being the same.
480 if ((checkLen
- stringLen
) == 1) {
481 if (dataPtr
[checkLen
- 1] != 0) { // non-zero means not a terminator and thus not likely the same
490 aCString
= obj
->getCStringNoCopy();
492 for (unsigned int i
= 0; i
< checkLen
; i
++) {
493 if (*dataPtr
++ != aCString
[i
]) {
501 //this was taken from CFPropertyList.c
502 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
505 OSData::serialize(OSSerialize
*s
) const
508 const unsigned char *p
;
510 unsigned int serializeLength
;
512 if (s
->previouslySerialized(this)) {
516 if (!s
->addXMLStartTag(this, "data")) {
520 serializeLength
= length
;
521 if (reserved
&& reserved
->disableSerialization
) {
525 for (i
= 0, p
= (unsigned char *)data
; i
< serializeLength
; i
++, p
++) {
526 /* 3 bytes are encoded as 4 */
529 c
= __CFPLDataEncodeTable
[((p
[0] >> 2) & 0x3f)];
530 if (!s
->addChar(c
)) {
535 c
= __CFPLDataEncodeTable
[((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
536 if (!s
->addChar(c
)) {
541 c
= __CFPLDataEncodeTable
[((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
542 if (!s
->addChar(c
)) {
545 c
= __CFPLDataEncodeTable
[(p
[0] & 0x3f)];
546 if (!s
->addChar(c
)) {
556 c
= __CFPLDataEncodeTable
[((p
[-1] << 4) & 0x30)];
557 if (!s
->addChar(c
)) {
560 if (!s
->addChar('=')) {
563 if (!s
->addChar('=')) {
568 c
= __CFPLDataEncodeTable
[((p
[-1] << 2) & 0x3c)];
569 if (!s
->addChar(c
)) {
572 if (!s
->addChar('=')) {
578 return s
->addXMLEndTag("data");
582 OSData::setDeallocFunction(DeallocFunction func
)
585 reserved
= (typeof(reserved
))kalloc_container(sizeof(ExpansionData
));
589 bzero(reserved
, sizeof(ExpansionData
));
591 reserved
->deallocFunction
= func
;
595 OSData::setSerializable(bool serializable
)
598 reserved
= (typeof(reserved
))kalloc_container(sizeof(ExpansionData
));
602 bzero(reserved
, sizeof(ExpansionData
));
604 reserved
->disableSerialization
= (!serializable
);
608 OSData::isSerializable(void)
610 return !reserved
|| !reserved
->disableSerialization
;