]>
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 IOKIT_ENABLE_SHARED_PTR
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 OSDefineMetaClassAndStructorsWithZone(OSData
, OSObject
, ZC_ZFREE_CLEARMEM
)
47 OSMetaClassDefineReservedUsedX86(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
)
64 OSCONTAINER_ACCUMSIZE(-((size_t)capacity
));
65 if (!inCapacity
|| (capacity
< inCapacity
)) {
66 // clean out old data's storage if it isn't big enough
67 if (capacity
< page_size
) {
68 kfree_data_container(data
, capacity
);
70 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
81 if (inCapacity
&& !data
) {
82 if (inCapacity
< page_size
) {
83 data
= (void *)kalloc_data_container(inCapacity
, Z_WAITOK
);
86 if (round_page_overflow(inCapacity
, &inCapacity
)) {
87 kr
= KERN_RESOURCE_SHORTAGE
;
89 kr
= kmem_alloc(kernel_map
, (vm_offset_t
*)&_data
, inCapacity
, IOMemoryTag(kernel_map
));
92 if (KERN_SUCCESS
!= kr
) {
99 capacity
= inCapacity
;
101 OSCONTAINER_ACCUMSIZE(capacity
);
104 if (inCapacity
< 16) {
105 capacityIncrement
= 16;
107 capacityIncrement
= inCapacity
;
114 OSData::initWithBytes(const void *bytes
, unsigned int inLength
)
116 if ((inLength
&& !bytes
) || !initWithCapacity(inLength
)) {
121 bcopy(bytes
, data
, inLength
);
129 OSData::initWithBytesNoCopy(void *bytes
, unsigned int inLength
)
131 if (!super::init()) {
143 OSData::initWithData(const OSData
*inData
)
145 return initWithBytes(inData
->data
, inData
->length
);
149 OSData::initWithData(const OSData
*inData
,
150 unsigned int start
, unsigned int inLength
)
152 const void *localData
= inData
->getBytesNoCopy(start
, inLength
);
155 return initWithBytes(localData
, inLength
);
162 OSData::withCapacity(unsigned int inCapacity
)
164 OSSharedPtr
<OSData
> me
= OSMakeShared
<OSData
>();
166 if (me
&& !me
->initWithCapacity(inCapacity
)) {
174 OSData::withBytes(const void *bytes
, unsigned int inLength
)
176 OSSharedPtr
<OSData
> me
= OSMakeShared
<OSData
>();
178 if (me
&& !me
->initWithBytes(bytes
, inLength
)) {
185 OSData::withBytesNoCopy(void *bytes
, unsigned int inLength
)
187 OSSharedPtr
<OSData
> me
= OSMakeShared
<OSData
>();
189 if (me
&& !me
->initWithBytesNoCopy(bytes
, inLength
)) {
197 OSData::withData(const OSData
*inData
)
199 OSSharedPtr
<OSData
> me
= OSMakeShared
<OSData
>();
201 if (me
&& !me
->initWithData(inData
)) {
209 OSData::withData(const OSData
*inData
,
210 unsigned int start
, unsigned int inLength
)
212 OSSharedPtr
<OSData
> me
= OSMakeShared
<OSData
>();
214 if (me
&& !me
->initWithData(inData
, start
, inLength
)) {
224 if ((capacity
!= EXTERNAL
) && data
&& capacity
) {
225 if (capacity
< page_size
) {
226 kfree_data_container(data
, capacity
);
228 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
230 OSCONTAINER_ACCUMSIZE( -((size_t)capacity
));
231 } else if (capacity
== EXTERNAL
) {
232 DeallocFunction freemem
= reserved
? reserved
->deallocFunction
: NULL
;
233 if (freemem
&& data
&& length
) {
234 freemem(data
, length
);
238 kfree(reserved
, sizeof(ExpansionData
));
244 OSData::getLength() const
249 OSData::getCapacity() const
255 OSData::getCapacityIncrement() const
257 return capacityIncrement
;
261 OSData::setCapacityIncrement(unsigned increment
)
263 return capacityIncrement
= increment
;
266 // xx-review: does not check for capacity == EXTERNAL
269 OSData::ensureCapacity(unsigned int newCapacity
)
271 unsigned char * newData
;
272 unsigned int finalCapacity
;
276 if (newCapacity
<= capacity
) {
280 finalCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
283 // integer overflow check
284 if (finalCapacity
< newCapacity
) {
290 if (finalCapacity
>= page_size
) {
292 finalCapacity
= round_page_32(finalCapacity
);
293 // integer overflow check
294 if (finalCapacity
< newCapacity
) {
297 if (capacity
>= page_size
) {
299 kr
= kmem_realloc(kernel_map
,
302 (vm_offset_t
*)&newData
,
304 IOMemoryTag(kernel_map
));
306 kr
= kmem_alloc(kernel_map
, (vm_offset_t
*)&newData
, finalCapacity
, IOMemoryTag(kernel_map
));
308 if (KERN_SUCCESS
!= kr
) {
312 newData
= (unsigned char *)kalloc_data_container(finalCapacity
, Z_WAITOK
);
316 bzero(newData
+ capacity
, finalCapacity
- capacity
);
318 bcopy(copydata
, newData
, capacity
);
321 if (capacity
< page_size
) {
322 kfree_data_container(data
, capacity
);
324 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
327 OSCONTAINER_ACCUMSIZE(((size_t)finalCapacity
) - ((size_t)capacity
));
328 data
= (void *) newData
;
329 capacity
= finalCapacity
;
336 OSData::appendBytes(const void *bytes
, unsigned int inLength
)
338 unsigned int newSize
;
344 if (capacity
== EXTERNAL
) {
348 if (os_add_overflow(length
, inLength
, &newSize
)) {
352 if ((newSize
> capacity
) && newSize
> ensureCapacity(newSize
)) {
357 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
359 bzero(&((unsigned char *)data
)[length
], inLength
);
368 OSData::appendByte(unsigned char byte
, unsigned int inLength
)
370 unsigned int newSize
;
376 if (capacity
== EXTERNAL
) {
380 if (os_add_overflow(length
, inLength
, &newSize
)) {
384 if ((newSize
> capacity
) && newSize
> ensureCapacity(newSize
)) {
388 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
395 OSData::appendBytes(const OSData
*other
)
397 return appendBytes(other
->data
, other
->length
);
401 OSData::getBytesNoCopy() const
411 OSData::getBytesNoCopy(unsigned int start
,
412 unsigned int inLength
) const
414 const void *outData
= NULL
;
418 && (start
+ inLength
) >= inLength
// overflow check
419 && (start
+ inLength
) <= length
) {
420 outData
= (const void *) ((char *) data
+ start
);
427 OSData::isEqualTo(const OSData
*aData
) const
436 return isEqualTo(aData
->data
, len
);
440 OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
442 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
446 OSData::isEqualTo(const OSMetaClassBase
*obj
) const
451 if ((otherData
= OSDynamicCast(OSData
, obj
))) {
452 return isEqualTo(otherData
);
453 } else if ((str
= OSDynamicCast(OSString
, obj
))) {
454 return isEqualTo(str
);
461 OSData::isEqualTo(const OSString
*obj
) const
463 const char * aCString
;
465 unsigned int checkLen
= length
;
466 unsigned int stringLen
;
472 stringLen
= obj
->getLength();
474 dataPtr
= (char *)data
;
476 if (stringLen
!= checkLen
) {
477 // check for the fact that OSData may be a buffer that
478 // that includes a termination byte and will thus have
479 // a length of the actual string length PLUS 1. In this
480 // case we verify that the additional byte is a terminator
481 // and if so count the two lengths as being the same.
483 if ((checkLen
- stringLen
) == 1) {
484 if (dataPtr
[checkLen
- 1] != 0) { // non-zero means not a terminator and thus not likely the same
493 aCString
= obj
->getCStringNoCopy();
495 for (unsigned int i
= 0; i
< checkLen
; i
++) {
496 if (*dataPtr
++ != aCString
[i
]) {
504 //this was taken from CFPropertyList.c
505 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
508 OSData::serialize(OSSerialize
*s
) const
511 const unsigned char *p
;
513 unsigned int serializeLength
;
515 if (s
->previouslySerialized(this)) {
519 if (!s
->addXMLStartTag(this, "data")) {
523 serializeLength
= length
;
524 if (reserved
&& reserved
->disableSerialization
) {
528 for (i
= 0, p
= (unsigned char *)data
; i
< serializeLength
; i
++, p
++) {
529 /* 3 bytes are encoded as 4 */
532 c
= __CFPLDataEncodeTable
[((p
[0] >> 2) & 0x3f)];
533 if (!s
->addChar(c
)) {
538 c
= __CFPLDataEncodeTable
[((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
539 if (!s
->addChar(c
)) {
544 c
= __CFPLDataEncodeTable
[((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
545 if (!s
->addChar(c
)) {
548 c
= __CFPLDataEncodeTable
[(p
[0] & 0x3f)];
549 if (!s
->addChar(c
)) {
559 c
= __CFPLDataEncodeTable
[((p
[-1] << 4) & 0x30)];
560 if (!s
->addChar(c
)) {
563 if (!s
->addChar('=')) {
566 if (!s
->addChar('=')) {
571 c
= __CFPLDataEncodeTable
[((p
[-1] << 2) & 0x3c)];
572 if (!s
->addChar(c
)) {
575 if (!s
->addChar('=')) {
581 return s
->addXMLEndTag("data");
585 OSData::setDeallocFunction(DeallocFunction func
)
588 reserved
= (typeof(reserved
))kalloc_container(sizeof(ExpansionData
));
592 bzero(reserved
, sizeof(ExpansionData
));
594 reserved
->deallocFunction
= func
;
598 OSData::setSerializable(bool serializable
)
601 reserved
= (typeof(reserved
))kalloc_container(sizeof(ExpansionData
));
605 bzero(reserved
, sizeof(ExpansionData
));
607 reserved
->disableSerialization
= (!serializable
);
611 OSData::isSerializable(void)
613 return !reserved
|| !reserved
->disableSerialization
;