2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* IOData.m created by rsulack on Thu 25-Sep-1997 */
29 #include <libkern/c++/OSData.h>
30 #include <libkern/c++/OSSerialize.h>
31 #include <libkern/c++/OSLib.h>
32 #include <libkern/c++/OSString.h>
35 #define super OSObject
37 OSDefineMetaClassAndStructors(OSData
, OSObject
)
38 OSMetaClassDefineReservedUnused(OSData
, 0);
39 OSMetaClassDefineReservedUnused(OSData
, 1);
40 OSMetaClassDefineReservedUnused(OSData
, 2);
41 OSMetaClassDefineReservedUnused(OSData
, 3);
42 OSMetaClassDefineReservedUnused(OSData
, 4);
43 OSMetaClassDefineReservedUnused(OSData
, 5);
44 OSMetaClassDefineReservedUnused(OSData
, 6);
45 OSMetaClassDefineReservedUnused(OSData
, 7);
47 #define EXTERNAL ((unsigned int) -1)
50 extern int debug_container_malloc_size
;
51 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
56 bool OSData::initWithCapacity(unsigned int inCapacity
)
61 if (data
&& (!inCapacity
|| capacity
< inCapacity
) ) {
62 // clean out old data's storage if it isn't big enough
63 kfree((vm_address_t
) data
, capacity
);
68 if (inCapacity
&& !data
) {
69 data
= (void *) kalloc(inCapacity
);
72 capacity
= inCapacity
;
73 ACCUMSIZE(inCapacity
);
78 capacityIncrement
= 16;
80 capacityIncrement
= inCapacity
;
85 bool OSData::initWithBytes(const void *bytes
, unsigned int inLength
)
87 if ((inLength
&& !bytes
) || !initWithCapacity(inLength
))
91 bcopy(bytes
, data
, inLength
);
97 bool OSData::initWithBytesNoCopy(void *bytes
, unsigned int inLength
)
109 bool OSData::initWithData(const OSData
*inData
)
111 return initWithBytes(inData
->data
, inData
->length
);
114 bool OSData::initWithData(const OSData
*inData
,
115 unsigned int start
, unsigned int inLength
)
117 const void *localData
= inData
->getBytesNoCopy(start
, inLength
);
120 return initWithBytes(localData
, inLength
);
125 OSData
*OSData::withCapacity(unsigned int inCapacity
)
127 OSData
*me
= new OSData
;
129 if (me
&& !me
->initWithCapacity(inCapacity
)) {
137 OSData
*OSData::withBytes(const void *bytes
, unsigned int inLength
)
139 OSData
*me
= new OSData
;
141 if (me
&& !me
->initWithBytes(bytes
, inLength
)) {
148 OSData
*OSData::withBytesNoCopy(void *bytes
, unsigned int inLength
)
150 OSData
*me
= new OSData
;
152 if (me
&& !me
->initWithBytesNoCopy(bytes
, inLength
)) {
160 OSData
*OSData::withData(const OSData
*inData
)
162 OSData
*me
= new OSData
;
164 if (me
&& !me
->initWithData(inData
)) {
172 OSData
*OSData::withData(const OSData
*inData
,
173 unsigned int start
, unsigned int inLength
)
175 OSData
*me
= new OSData
;
177 if (me
&& !me
->initWithData(inData
, start
, inLength
)) {
187 if (capacity
!= EXTERNAL
&& data
&& capacity
) {
188 kfree((vm_offset_t
)data
, capacity
);
189 ACCUMSIZE( -capacity
);
194 unsigned int OSData::getLength() const { return length
; }
195 unsigned int OSData::getCapacity() const { return capacity
; }
197 unsigned int OSData::getCapacityIncrement() const
199 return capacityIncrement
;
202 unsigned int OSData::setCapacityIncrement(unsigned increment
)
204 return capacityIncrement
= increment
;
207 unsigned int OSData::ensureCapacity(unsigned int newCapacity
)
209 unsigned char * newData
;
211 if (newCapacity
<= capacity
)
214 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
217 newData
= (unsigned char *) kalloc(newCapacity
);
220 bzero(newData
+ capacity
, newCapacity
- capacity
);
222 bcopy(data
, newData
, capacity
);
223 kfree((vm_offset_t
)data
, capacity
);
225 ACCUMSIZE( newCapacity
- capacity
);
226 data
= (void *) newData
;
227 capacity
= newCapacity
;
233 bool OSData::appendBytes(const void *bytes
, unsigned int inLength
)
235 unsigned int newSize
;
240 if (capacity
== EXTERNAL
)
243 newSize
= length
+ inLength
;
244 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
248 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
250 bzero(&((unsigned char *)data
)[length
], inLength
);
257 bool OSData::appendByte(unsigned char byte
, unsigned int inLength
)
259 unsigned int newSize
;
264 if (capacity
== EXTERNAL
)
267 newSize
= length
+ inLength
;
268 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
271 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
277 bool OSData::appendBytes(const OSData
*other
)
279 return appendBytes(other
->data
, other
->length
);
282 const void *OSData::getBytesNoCopy() const
290 const void *OSData::getBytesNoCopy(unsigned int start
,
291 unsigned int inLength
) const
293 const void *outData
= 0;
297 && (start
+ inLength
) <= length
)
298 outData
= (const void *) ((char *) data
+ start
);
303 bool OSData::isEqualTo(const OSData
*aData
) const
311 return isEqualTo(aData
->data
, len
);
314 bool OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
316 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
319 bool OSData::isEqualTo(const OSMetaClassBase
*obj
) const
324 if ((data
= OSDynamicCast(OSData
, obj
)))
325 return isEqualTo(data
);
326 else if ((str
= OSDynamicCast (OSString
, obj
)))
327 return isEqualTo(str
);
332 bool OSData::isEqualTo(const OSString
*obj
) const
334 const char * aCString
;
336 unsigned int checkLen
= length
;
337 unsigned int stringLen
;
342 stringLen
= obj
->getLength ();
344 dataPtr
= (char *)data
;
346 if (stringLen
!= checkLen
) {
348 // check for the fact that OSData may be a buffer that
349 // that includes a termination byte and will thus have
350 // a length of the actual string length PLUS 1. In this
351 // case we verify that the additional byte is a terminator
352 // and if so count the two lengths as being the same.
354 if ( (checkLen
- stringLen
) == 1) {
355 if (dataPtr
[checkLen
-1] != 0) // non-zero means not a terminator and thus not likely the same
363 aCString
= obj
->getCStringNoCopy ();
365 for ( unsigned int i
=0; i
< checkLen
; i
++ ) {
366 if ( *dataPtr
++ != aCString
[i
] )
373 //this was taken from CFPropertyList.c
374 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
376 bool OSData::serialize(OSSerialize
*s
) const
379 const unsigned char *p
;
382 if (s
->previouslySerialized(this)) return true;
384 if (!s
->addXMLStartTag(this, "data")) return false;
386 for (i
= 0, p
= (unsigned char *)data
; i
< length
; i
++, p
++) {
387 /* 3 bytes are encoded as 4 */
390 c
= __CFPLDataEncodeTable
[ ((p
[0] >> 2) & 0x3f)];
391 if (!s
->addChar(c
)) return false;
394 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
395 if (!s
->addChar(c
)) return false;
398 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
399 if (!s
->addChar(c
)) return false;
400 c
= __CFPLDataEncodeTable
[ (p
[0] & 0x3f)];
401 if (!s
->addChar(c
)) return false;
409 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 4) & 0x30)];
410 if (!s
->addChar(c
)) return false;
411 if (!s
->addChar('=')) return false;
412 if (!s
->addChar('=')) return false;
415 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 2) & 0x3c)];
416 if (!s
->addChar(c
)) return false;
417 if (!s
->addChar('=')) return false;
421 return s
->addXMLEndTag("data");