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
= (DeallocFunction
)reserved
;
195 if (freemem
&& data
&& length
) {
196 freemem(data
, length
);
202 unsigned int OSData::getLength() const { return length
; }
203 unsigned int OSData::getCapacity() const { return capacity
; }
205 unsigned int OSData::getCapacityIncrement() const
207 return capacityIncrement
;
210 unsigned int OSData::setCapacityIncrement(unsigned increment
)
212 return capacityIncrement
= increment
;
215 // xx-review: does not check for capacity == EXTERNAL
217 unsigned int OSData::ensureCapacity(unsigned int newCapacity
)
219 unsigned char * newData
;
221 if (newCapacity
<= capacity
)
224 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
227 newData
= (unsigned char *) kalloc(newCapacity
);
230 bzero(newData
+ capacity
, newCapacity
- capacity
);
232 bcopy(data
, newData
, capacity
);
233 kfree(data
, capacity
);
235 ACCUMSIZE( newCapacity
- capacity
);
236 data
= (void *) newData
;
237 capacity
= newCapacity
;
243 bool OSData::appendBytes(const void *bytes
, unsigned int inLength
)
245 unsigned int newSize
;
250 if (capacity
== EXTERNAL
)
253 newSize
= length
+ inLength
;
254 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
258 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
260 bzero(&((unsigned char *)data
)[length
], inLength
);
267 bool OSData::appendByte(unsigned char byte
, unsigned int inLength
)
269 unsigned int newSize
;
274 if (capacity
== EXTERNAL
)
277 newSize
= length
+ inLength
;
278 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
281 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
287 bool OSData::appendBytes(const OSData
*other
)
289 return appendBytes(other
->data
, other
->length
);
292 const void *OSData::getBytesNoCopy() const
300 const void *OSData::getBytesNoCopy(unsigned int start
,
301 unsigned int inLength
) const
303 const void *outData
= 0;
307 && (start
+ inLength
) <= length
)
308 outData
= (const void *) ((char *) data
+ start
);
313 bool OSData::isEqualTo(const OSData
*aData
) const
321 return isEqualTo(aData
->data
, len
);
324 bool OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
326 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
329 bool OSData::isEqualTo(const OSMetaClassBase
*obj
) const
334 if ((otherData
= OSDynamicCast(OSData
, obj
)))
335 return isEqualTo(otherData
);
336 else if ((str
= OSDynamicCast (OSString
, obj
)))
337 return isEqualTo(str
);
342 bool OSData::isEqualTo(const OSString
*obj
) const
344 const char * aCString
;
346 unsigned int checkLen
= length
;
347 unsigned int stringLen
;
352 stringLen
= obj
->getLength ();
354 dataPtr
= (char *)data
;
356 if (stringLen
!= checkLen
) {
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.
364 if ( (checkLen
- stringLen
) == 1) {
365 if (dataPtr
[checkLen
-1] != 0) // non-zero means not a terminator and thus not likely the same
373 aCString
= obj
->getCStringNoCopy ();
375 for ( unsigned int i
=0; i
< checkLen
; i
++ ) {
376 if ( *dataPtr
++ != aCString
[i
] )
383 //this was taken from CFPropertyList.c
384 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
386 bool OSData::serialize(OSSerialize
*s
) const
389 const unsigned char *p
;
392 if (s
->previouslySerialized(this)) return true;
394 if (!s
->addXMLStartTag(this, "data")) return false;
396 for (i
= 0, p
= (unsigned char *)data
; i
< length
; i
++, p
++) {
397 /* 3 bytes are encoded as 4 */
400 c
= __CFPLDataEncodeTable
[ ((p
[0] >> 2) & 0x3f)];
401 if (!s
->addChar(c
)) return false;
404 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
405 if (!s
->addChar(c
)) return false;
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;
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;
425 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 2) & 0x3c)];
426 if (!s
->addChar(c
)) return false;
427 if (!s
->addChar('=')) return false;
431 return s
->addXMLEndTag("data");
434 /* Note I am just using the reserved pointer here instead of allocating a whole buffer
435 * to hold one pointer.
437 void OSData::setDeallocFunction(DeallocFunction func
)
439 reserved
= (ExpansionData
*)func
;