2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* IOData.m created by rsulack on Thu 25-Sep-1997 */
34 #include <libkern/c++/OSData.h>
35 #include <libkern/c++/OSSerialize.h>
36 #include <libkern/c++/OSLib.h>
37 #include <libkern/c++/OSString.h>
40 #define super OSObject
42 OSDefineMetaClassAndStructors(OSData
, OSObject
)
43 OSMetaClassDefineReservedUnused(OSData
, 0);
44 OSMetaClassDefineReservedUnused(OSData
, 1);
45 OSMetaClassDefineReservedUnused(OSData
, 2);
46 OSMetaClassDefineReservedUnused(OSData
, 3);
47 OSMetaClassDefineReservedUnused(OSData
, 4);
48 OSMetaClassDefineReservedUnused(OSData
, 5);
49 OSMetaClassDefineReservedUnused(OSData
, 6);
50 OSMetaClassDefineReservedUnused(OSData
, 7);
52 #define EXTERNAL ((unsigned int) -1)
55 extern int debug_container_malloc_size
;
56 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
61 bool OSData::initWithCapacity(unsigned int inCapacity
)
66 if (data
&& (!inCapacity
|| capacity
< inCapacity
) ) {
67 // clean out old data's storage if it isn't big enough
68 kfree(data
, capacity
);
73 if (inCapacity
&& !data
) {
74 data
= (void *) kalloc(inCapacity
);
77 capacity
= inCapacity
;
78 ACCUMSIZE(inCapacity
);
83 capacityIncrement
= 16;
85 capacityIncrement
= inCapacity
;
90 bool OSData::initWithBytes(const void *bytes
, unsigned int inLength
)
92 if ((inLength
&& !bytes
) || !initWithCapacity(inLength
))
96 bcopy(bytes
, data
, inLength
);
102 bool OSData::initWithBytesNoCopy(void *bytes
, unsigned int inLength
)
114 bool OSData::initWithData(const OSData
*inData
)
116 return initWithBytes(inData
->data
, inData
->length
);
119 bool OSData::initWithData(const OSData
*inData
,
120 unsigned int start
, unsigned int inLength
)
122 const void *localData
= inData
->getBytesNoCopy(start
, inLength
);
125 return initWithBytes(localData
, inLength
);
130 OSData
*OSData::withCapacity(unsigned int inCapacity
)
132 OSData
*me
= new OSData
;
134 if (me
&& !me
->initWithCapacity(inCapacity
)) {
142 OSData
*OSData::withBytes(const void *bytes
, unsigned int inLength
)
144 OSData
*me
= new OSData
;
146 if (me
&& !me
->initWithBytes(bytes
, inLength
)) {
153 OSData
*OSData::withBytesNoCopy(void *bytes
, unsigned int inLength
)
155 OSData
*me
= new OSData
;
157 if (me
&& !me
->initWithBytesNoCopy(bytes
, inLength
)) {
165 OSData
*OSData::withData(const OSData
*inData
)
167 OSData
*me
= new OSData
;
169 if (me
&& !me
->initWithData(inData
)) {
177 OSData
*OSData::withData(const OSData
*inData
,
178 unsigned int start
, unsigned int inLength
)
180 OSData
*me
= new OSData
;
182 if (me
&& !me
->initWithData(inData
, start
, inLength
)) {
192 if (capacity
!= EXTERNAL
&& data
&& capacity
) {
193 kfree(data
, capacity
);
194 ACCUMSIZE( -capacity
);
199 unsigned int OSData::getLength() const { return length
; }
200 unsigned int OSData::getCapacity() const { return capacity
; }
202 unsigned int OSData::getCapacityIncrement() const
204 return capacityIncrement
;
207 unsigned int OSData::setCapacityIncrement(unsigned increment
)
209 return capacityIncrement
= increment
;
212 unsigned int OSData::ensureCapacity(unsigned int newCapacity
)
214 unsigned char * newData
;
216 if (newCapacity
<= capacity
)
219 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
222 newData
= (unsigned char *) kalloc(newCapacity
);
225 bzero(newData
+ capacity
, newCapacity
- capacity
);
227 bcopy(data
, newData
, capacity
);
228 kfree(data
, capacity
);
230 ACCUMSIZE( newCapacity
- capacity
);
231 data
= (void *) newData
;
232 capacity
= newCapacity
;
238 bool OSData::appendBytes(const void *bytes
, unsigned int inLength
)
240 unsigned int newSize
;
245 if (capacity
== EXTERNAL
)
248 newSize
= length
+ inLength
;
249 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
253 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
255 bzero(&((unsigned char *)data
)[length
], inLength
);
262 bool OSData::appendByte(unsigned char byte
, unsigned int inLength
)
264 unsigned int newSize
;
269 if (capacity
== EXTERNAL
)
272 newSize
= length
+ inLength
;
273 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
276 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
282 bool OSData::appendBytes(const OSData
*other
)
284 return appendBytes(other
->data
, other
->length
);
287 const void *OSData::getBytesNoCopy() const
295 const void *OSData::getBytesNoCopy(unsigned int start
,
296 unsigned int inLength
) const
298 const void *outData
= 0;
302 && (start
+ inLength
) <= length
)
303 outData
= (const void *) ((char *) data
+ start
);
308 bool OSData::isEqualTo(const OSData
*aData
) const
316 return isEqualTo(aData
->data
, len
);
319 bool OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
321 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
324 bool OSData::isEqualTo(const OSMetaClassBase
*obj
) const
329 if ((data
= OSDynamicCast(OSData
, obj
)))
330 return isEqualTo(data
);
331 else if ((str
= OSDynamicCast (OSString
, obj
)))
332 return isEqualTo(str
);
337 bool OSData::isEqualTo(const OSString
*obj
) const
339 const char * aCString
;
341 unsigned int checkLen
= length
;
342 unsigned int stringLen
;
347 stringLen
= obj
->getLength ();
349 dataPtr
= (char *)data
;
351 if (stringLen
!= checkLen
) {
353 // check for the fact that OSData may be a buffer that
354 // that includes a termination byte and will thus have
355 // a length of the actual string length PLUS 1. In this
356 // case we verify that the additional byte is a terminator
357 // and if so count the two lengths as being the same.
359 if ( (checkLen
- stringLen
) == 1) {
360 if (dataPtr
[checkLen
-1] != 0) // non-zero means not a terminator and thus not likely the same
368 aCString
= obj
->getCStringNoCopy ();
370 for ( unsigned int i
=0; i
< checkLen
; i
++ ) {
371 if ( *dataPtr
++ != aCString
[i
] )
378 //this was taken from CFPropertyList.c
379 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
381 bool OSData::serialize(OSSerialize
*s
) const
384 const unsigned char *p
;
387 if (s
->previouslySerialized(this)) return true;
389 if (!s
->addXMLStartTag(this, "data")) return false;
391 for (i
= 0, p
= (unsigned char *)data
; i
< length
; i
++, p
++) {
392 /* 3 bytes are encoded as 4 */
395 c
= __CFPLDataEncodeTable
[ ((p
[0] >> 2) & 0x3f)];
396 if (!s
->addChar(c
)) return false;
399 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
400 if (!s
->addChar(c
)) return false;
403 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
404 if (!s
->addChar(c
)) return false;
405 c
= __CFPLDataEncodeTable
[ (p
[0] & 0x3f)];
406 if (!s
->addChar(c
)) return false;
414 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 4) & 0x30)];
415 if (!s
->addChar(c
)) return false;
416 if (!s
->addChar('=')) return false;
417 if (!s
->addChar('=')) return false;
420 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 2) & 0x3c)];
421 if (!s
->addChar(c
)) return false;
422 if (!s
->addChar('=')) return false;
426 return s
->addXMLEndTag("data");