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 */
32 #include <libkern/c++/OSData.h>
33 #include <libkern/c++/OSSerialize.h>
34 #include <libkern/c++/OSLib.h>
35 #include <libkern/c++/OSString.h>
38 #define super OSObject
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);
50 #define EXTERNAL ((unsigned int) -1)
53 extern int debug_container_malloc_size
;
54 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
59 bool OSData::initWithCapacity(unsigned int inCapacity
)
64 if (data
&& (!inCapacity
|| capacity
< inCapacity
) ) {
65 // clean out old data's storage if it isn't big enough
66 kfree(data
, capacity
);
71 if (inCapacity
&& !data
) {
72 data
= (void *) kalloc(inCapacity
);
75 capacity
= inCapacity
;
76 ACCUMSIZE(inCapacity
);
81 capacityIncrement
= 16;
83 capacityIncrement
= inCapacity
;
88 bool OSData::initWithBytes(const void *bytes
, unsigned int inLength
)
90 if ((inLength
&& !bytes
) || !initWithCapacity(inLength
))
94 bcopy(bytes
, data
, inLength
);
100 bool OSData::initWithBytesNoCopy(void *bytes
, unsigned int inLength
)
112 bool OSData::initWithData(const OSData
*inData
)
114 return initWithBytes(inData
->data
, inData
->length
);
117 bool OSData::initWithData(const OSData
*inData
,
118 unsigned int start
, unsigned int inLength
)
120 const void *localData
= inData
->getBytesNoCopy(start
, inLength
);
123 return initWithBytes(localData
, inLength
);
128 OSData
*OSData::withCapacity(unsigned int inCapacity
)
130 OSData
*me
= new OSData
;
132 if (me
&& !me
->initWithCapacity(inCapacity
)) {
140 OSData
*OSData::withBytes(const void *bytes
, unsigned int inLength
)
142 OSData
*me
= new OSData
;
144 if (me
&& !me
->initWithBytes(bytes
, inLength
)) {
151 OSData
*OSData::withBytesNoCopy(void *bytes
, unsigned int inLength
)
153 OSData
*me
= new OSData
;
155 if (me
&& !me
->initWithBytesNoCopy(bytes
, inLength
)) {
163 OSData
*OSData::withData(const OSData
*inData
)
165 OSData
*me
= new OSData
;
167 if (me
&& !me
->initWithData(inData
)) {
175 OSData
*OSData::withData(const OSData
*inData
,
176 unsigned int start
, unsigned int inLength
)
178 OSData
*me
= new OSData
;
180 if (me
&& !me
->initWithData(inData
, start
, inLength
)) {
190 if (capacity
!= EXTERNAL
&& data
&& capacity
) {
191 kfree(data
, capacity
);
192 ACCUMSIZE( -capacity
);
193 } else if (capacity
== EXTERNAL
) {
194 DeallocFunction freemem
= reserved
? reserved
->deallocFunction
: NULL
;
195 if (freemem
&& data
&& length
) {
196 freemem(data
, length
);
199 if (reserved
) kfree(reserved
, sizeof(ExpansionData
));
203 unsigned int OSData::getLength() const { return length
; }
204 unsigned int OSData::getCapacity() const { return capacity
; }
206 unsigned int OSData::getCapacityIncrement() const
208 return capacityIncrement
;
211 unsigned int OSData::setCapacityIncrement(unsigned increment
)
213 return capacityIncrement
= increment
;
216 // xx-review: does not check for capacity == EXTERNAL
218 unsigned int OSData::ensureCapacity(unsigned int newCapacity
)
220 unsigned char * newData
;
221 unsigned int finalCapacity
;
223 if (newCapacity
<= capacity
)
226 finalCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
229 // integer overflow check
230 if (finalCapacity
< newCapacity
)
233 newData
= (unsigned char *) kalloc(finalCapacity
);
236 bzero(newData
+ capacity
, finalCapacity
- capacity
);
238 bcopy(data
, newData
, capacity
);
239 kfree(data
, capacity
);
241 ACCUMSIZE( finalCapacity
- capacity
);
242 data
= (void *) newData
;
243 capacity
= finalCapacity
;
249 bool OSData::appendBytes(const void *bytes
, unsigned int inLength
)
251 unsigned int newSize
;
256 if (capacity
== EXTERNAL
)
259 newSize
= length
+ inLength
;
260 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
264 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
266 bzero(&((unsigned char *)data
)[length
], inLength
);
273 bool OSData::appendByte(unsigned char byte
, unsigned int inLength
)
275 unsigned int newSize
;
280 if (capacity
== EXTERNAL
)
283 newSize
= length
+ inLength
;
284 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
287 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
293 bool OSData::appendBytes(const OSData
*other
)
295 return appendBytes(other
->data
, other
->length
);
298 const void *OSData::getBytesNoCopy() const
306 const void *OSData::getBytesNoCopy(unsigned int start
,
307 unsigned int inLength
) const
309 const void *outData
= 0;
313 && (start
+ inLength
) <= length
)
314 outData
= (const void *) ((char *) data
+ start
);
319 bool OSData::isEqualTo(const OSData
*aData
) const
327 return isEqualTo(aData
->data
, len
);
330 bool OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
332 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
335 bool OSData::isEqualTo(const OSMetaClassBase
*obj
) const
340 if ((otherData
= OSDynamicCast(OSData
, obj
)))
341 return isEqualTo(otherData
);
342 else if ((str
= OSDynamicCast (OSString
, obj
)))
343 return isEqualTo(str
);
348 bool OSData::isEqualTo(const OSString
*obj
) const
350 const char * aCString
;
352 unsigned int checkLen
= length
;
353 unsigned int stringLen
;
358 stringLen
= obj
->getLength ();
360 dataPtr
= (char *)data
;
362 if (stringLen
!= checkLen
) {
364 // check for the fact that OSData may be a buffer that
365 // that includes a termination byte and will thus have
366 // a length of the actual string length PLUS 1. In this
367 // case we verify that the additional byte is a terminator
368 // and if so count the two lengths as being the same.
370 if ( (checkLen
- stringLen
) == 1) {
371 if (dataPtr
[checkLen
-1] != 0) // non-zero means not a terminator and thus not likely the same
379 aCString
= obj
->getCStringNoCopy ();
381 for ( unsigned int i
=0; i
< checkLen
; i
++ ) {
382 if ( *dataPtr
++ != aCString
[i
] )
389 //this was taken from CFPropertyList.c
390 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
392 bool OSData::serialize(OSSerialize
*s
) const
395 const unsigned char *p
;
397 unsigned int serializeLength
;
399 if (s
->previouslySerialized(this)) return true;
401 if (!s
->addXMLStartTag(this, "data")) return false;
403 serializeLength
= length
;
404 if (reserved
&& reserved
->disableSerialization
) serializeLength
= 0;
406 for (i
= 0, p
= (unsigned char *)data
; i
< serializeLength
; i
++, p
++) {
407 /* 3 bytes are encoded as 4 */
410 c
= __CFPLDataEncodeTable
[ ((p
[0] >> 2) & 0x3f)];
411 if (!s
->addChar(c
)) return false;
414 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
415 if (!s
->addChar(c
)) return false;
418 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
419 if (!s
->addChar(c
)) return false;
420 c
= __CFPLDataEncodeTable
[ (p
[0] & 0x3f)];
421 if (!s
->addChar(c
)) return false;
429 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 4) & 0x30)];
430 if (!s
->addChar(c
)) return false;
431 if (!s
->addChar('=')) return false;
432 if (!s
->addChar('=')) return false;
435 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 2) & 0x3c)];
436 if (!s
->addChar(c
)) return false;
437 if (!s
->addChar('=')) return false;
441 return s
->addXMLEndTag("data");
444 void OSData::setDeallocFunction(DeallocFunction func
)
448 reserved
= (typeof(reserved
)) kalloc(sizeof(ExpansionData
));
449 if (!reserved
) return;
450 bzero(reserved
, sizeof(ExpansionData
));
452 reserved
->deallocFunction
= func
;
455 void OSData::setSerializable(bool serializable
)
459 reserved
= (typeof(reserved
)) kalloc(sizeof(ExpansionData
));
460 if (!reserved
) return;
461 bzero(reserved
, sizeof(ExpansionData
));
463 reserved
->disableSerialization
= (!serializable
);