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 OSMetaClassDefineReservedUnused(OSData
, 0);
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
);
197 unsigned int OSData::getLength() const { return length
; }
198 unsigned int OSData::getCapacity() const { return capacity
; }
200 unsigned int OSData::getCapacityIncrement() const
202 return capacityIncrement
;
205 unsigned int OSData::setCapacityIncrement(unsigned increment
)
207 return capacityIncrement
= increment
;
210 unsigned int OSData::ensureCapacity(unsigned int newCapacity
)
212 unsigned char * newData
;
214 if (newCapacity
<= capacity
)
217 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
220 newData
= (unsigned char *) kalloc(newCapacity
);
223 bzero(newData
+ capacity
, newCapacity
- capacity
);
225 bcopy(data
, newData
, capacity
);
226 kfree(data
, capacity
);
228 ACCUMSIZE( newCapacity
- capacity
);
229 data
= (void *) newData
;
230 capacity
= newCapacity
;
236 bool OSData::appendBytes(const void *bytes
, unsigned int inLength
)
238 unsigned int newSize
;
243 if (capacity
== EXTERNAL
)
246 newSize
= length
+ inLength
;
247 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
251 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
253 bzero(&((unsigned char *)data
)[length
], inLength
);
260 bool OSData::appendByte(unsigned char byte
, unsigned int inLength
)
262 unsigned int newSize
;
267 if (capacity
== EXTERNAL
)
270 newSize
= length
+ inLength
;
271 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
274 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
280 bool OSData::appendBytes(const OSData
*other
)
282 return appendBytes(other
->data
, other
->length
);
285 const void *OSData::getBytesNoCopy() const
293 const void *OSData::getBytesNoCopy(unsigned int start
,
294 unsigned int inLength
) const
296 const void *outData
= 0;
300 && (start
+ inLength
) <= length
)
301 outData
= (const void *) ((char *) data
+ start
);
306 bool OSData::isEqualTo(const OSData
*aData
) const
314 return isEqualTo(aData
->data
, len
);
317 bool OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
319 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
322 bool OSData::isEqualTo(const OSMetaClassBase
*obj
) const
327 if ((data
= OSDynamicCast(OSData
, obj
)))
328 return isEqualTo(data
);
329 else if ((str
= OSDynamicCast (OSString
, obj
)))
330 return isEqualTo(str
);
335 bool OSData::isEqualTo(const OSString
*obj
) const
337 const char * aCString
;
339 unsigned int checkLen
= length
;
340 unsigned int stringLen
;
345 stringLen
= obj
->getLength ();
347 dataPtr
= (char *)data
;
349 if (stringLen
!= checkLen
) {
351 // check for the fact that OSData may be a buffer that
352 // that includes a termination byte and will thus have
353 // a length of the actual string length PLUS 1. In this
354 // case we verify that the additional byte is a terminator
355 // and if so count the two lengths as being the same.
357 if ( (checkLen
- stringLen
) == 1) {
358 if (dataPtr
[checkLen
-1] != 0) // non-zero means not a terminator and thus not likely the same
366 aCString
= obj
->getCStringNoCopy ();
368 for ( unsigned int i
=0; i
< checkLen
; i
++ ) {
369 if ( *dataPtr
++ != aCString
[i
] )
376 //this was taken from CFPropertyList.c
377 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
379 bool OSData::serialize(OSSerialize
*s
) const
382 const unsigned char *p
;
385 if (s
->previouslySerialized(this)) return true;
387 if (!s
->addXMLStartTag(this, "data")) return false;
389 for (i
= 0, p
= (unsigned char *)data
; i
< length
; i
++, p
++) {
390 /* 3 bytes are encoded as 4 */
393 c
= __CFPLDataEncodeTable
[ ((p
[0] >> 2) & 0x3f)];
394 if (!s
->addChar(c
)) return false;
397 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
398 if (!s
->addChar(c
)) return false;
401 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
402 if (!s
->addChar(c
)) return false;
403 c
= __CFPLDataEncodeTable
[ (p
[0] & 0x3f)];
404 if (!s
->addChar(c
)) return false;
412 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 4) & 0x30)];
413 if (!s
->addChar(c
)) return false;
414 if (!s
->addChar('=')) return false;
415 if (!s
->addChar('=')) return false;
418 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 2) & 0x3c)];
419 if (!s
->addChar(c
)) return false;
420 if (!s
->addChar('=')) return false;
424 return s
->addXMLEndTag("data");