2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* IOData.m created by rsulack on Thu 25-Sep-1997 */
27 #include <libkern/c++/OSData.h>
28 #include <libkern/c++/OSSerialize.h>
29 #include <libkern/c++/OSLib.h>
30 #include <libkern/c++/OSString.h>
33 #define super OSObject
35 OSDefineMetaClassAndStructors(OSData
, OSObject
)
36 OSMetaClassDefineReservedUnused(OSData
, 0);
37 OSMetaClassDefineReservedUnused(OSData
, 1);
38 OSMetaClassDefineReservedUnused(OSData
, 2);
39 OSMetaClassDefineReservedUnused(OSData
, 3);
40 OSMetaClassDefineReservedUnused(OSData
, 4);
41 OSMetaClassDefineReservedUnused(OSData
, 5);
42 OSMetaClassDefineReservedUnused(OSData
, 6);
43 OSMetaClassDefineReservedUnused(OSData
, 7);
45 #define EXTERNAL ((unsigned int) -1)
48 extern int debug_container_malloc_size
;
49 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
54 bool OSData::initWithCapacity(unsigned int inCapacity
)
59 if (data
&& (!inCapacity
|| capacity
< inCapacity
) ) {
60 // clean out old data's storage if it isn't big enough
61 kfree((vm_address_t
) data
, capacity
);
66 if (inCapacity
&& !data
) {
67 data
= (void *) kalloc(inCapacity
);
70 capacity
= inCapacity
;
71 ACCUMSIZE(inCapacity
);
76 capacityIncrement
= 16;
78 capacityIncrement
= inCapacity
;
83 bool OSData::initWithBytes(const void *bytes
, unsigned int inLength
)
85 if ((inLength
&& !bytes
) || !initWithCapacity(inLength
))
89 bcopy(bytes
, data
, inLength
);
95 bool OSData::initWithBytesNoCopy(void *bytes
, unsigned int inLength
)
107 bool OSData::initWithData(const OSData
*inData
)
109 return initWithBytes(inData
->data
, inData
->length
);
112 bool OSData::initWithData(const OSData
*inData
,
113 unsigned int start
, unsigned int inLength
)
115 const void *localData
= inData
->getBytesNoCopy(start
, inLength
);
118 return initWithBytes(localData
, inLength
);
123 OSData
*OSData::withCapacity(unsigned int inCapacity
)
125 OSData
*me
= new OSData
;
127 if (me
&& !me
->initWithCapacity(inCapacity
)) {
135 OSData
*OSData::withBytes(const void *bytes
, unsigned int inLength
)
137 OSData
*me
= new OSData
;
139 if (me
&& !me
->initWithBytes(bytes
, inLength
)) {
146 OSData
*OSData::withBytesNoCopy(void *bytes
, unsigned int inLength
)
148 OSData
*me
= new OSData
;
150 if (me
&& !me
->initWithBytesNoCopy(bytes
, inLength
)) {
158 OSData
*OSData::withData(const OSData
*inData
)
160 OSData
*me
= new OSData
;
162 if (me
&& !me
->initWithData(inData
)) {
170 OSData
*OSData::withData(const OSData
*inData
,
171 unsigned int start
, unsigned int inLength
)
173 OSData
*me
= new OSData
;
175 if (me
&& !me
->initWithData(inData
, start
, inLength
)) {
185 if (capacity
!= EXTERNAL
&& data
&& capacity
) {
186 kfree((vm_offset_t
)data
, capacity
);
187 ACCUMSIZE( -capacity
);
192 unsigned int OSData::getLength() const { return length
; }
193 unsigned int OSData::getCapacity() const { return capacity
; }
195 unsigned int OSData::getCapacityIncrement() const
197 return capacityIncrement
;
200 unsigned int OSData::setCapacityIncrement(unsigned increment
)
202 return capacityIncrement
= increment
;
205 unsigned int OSData::ensureCapacity(unsigned int newCapacity
)
207 unsigned char * newData
;
209 if (newCapacity
<= capacity
)
212 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
215 newData
= (unsigned char *) kalloc(newCapacity
);
218 bzero(newData
+ capacity
, newCapacity
- capacity
);
220 bcopy(data
, newData
, capacity
);
221 kfree((vm_offset_t
)data
, capacity
);
223 ACCUMSIZE( newCapacity
- capacity
);
224 data
= (void *) newData
;
225 capacity
= newCapacity
;
231 bool OSData::appendBytes(const void *bytes
, unsigned int inLength
)
233 unsigned int newSize
;
238 if (capacity
== EXTERNAL
)
241 newSize
= length
+ inLength
;
242 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
246 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
248 bzero(&((unsigned char *)data
)[length
], inLength
);
255 bool OSData::appendByte(unsigned char byte
, unsigned int inLength
)
257 unsigned int newSize
;
262 if (capacity
== EXTERNAL
)
265 newSize
= length
+ inLength
;
266 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
269 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
275 bool OSData::appendBytes(const OSData
*other
)
277 return appendBytes(other
->data
, other
->length
);
280 const void *OSData::getBytesNoCopy() const
288 const void *OSData::getBytesNoCopy(unsigned int start
,
289 unsigned int inLength
) const
291 const void *outData
= 0;
295 && (start
+ inLength
) <= length
)
296 outData
= (const void *) ((char *) data
+ start
);
301 bool OSData::isEqualTo(const OSData
*aData
) const
309 return isEqualTo(aData
->data
, len
);
312 bool OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
314 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
317 bool OSData::isEqualTo(const OSMetaClassBase
*obj
) const
322 if ((data
= OSDynamicCast(OSData
, obj
)))
323 return isEqualTo(data
);
324 else if ((str
= OSDynamicCast (OSString
, obj
)))
325 return isEqualTo(str
);
330 bool OSData::isEqualTo(const OSString
*obj
) const
332 const char * aCString
;
334 unsigned int checkLen
= length
;
335 unsigned int stringLen
;
340 stringLen
= obj
->getLength ();
342 dataPtr
= (char *)data
;
344 if (stringLen
!= checkLen
) {
346 // check for the fact that OSData may be a buffer that
347 // that includes a termination byte and will thus have
348 // a length of the actual string length PLUS 1. In this
349 // case we verify that the additional byte is a terminator
350 // and if so count the two lengths as being the same.
352 if ( (checkLen
- stringLen
) == 1) {
353 if (dataPtr
[checkLen
-1] != 0) // non-zero means not a terminator and thus not likely the same
361 aCString
= obj
->getCStringNoCopy ();
363 for ( unsigned int i
=0; i
< checkLen
; i
++ ) {
364 if ( *dataPtr
++ != aCString
[i
] )
371 //this was taken from CFPropertyList.c
372 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
374 bool OSData::serialize(OSSerialize
*s
) const
377 const unsigned char *p
;
380 if (s
->previouslySerialized(this)) return true;
382 if (!s
->addXMLStartTag(this, "data")) return false;
384 for (i
= 0, p
= (unsigned char *)data
; i
< length
; i
++, p
++) {
385 /* 3 bytes are encoded as 4 */
388 c
= __CFPLDataEncodeTable
[ ((p
[0] >> 2) & 0x3f)];
389 if (!s
->addChar(c
)) return false;
392 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
393 if (!s
->addChar(c
)) return false;
396 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
397 if (!s
->addChar(c
)) return false;
398 c
= __CFPLDataEncodeTable
[ (p
[0] & 0x3f)];
399 if (!s
->addChar(c
)) return false;
407 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 4) & 0x30)];
408 if (!s
->addChar(c
)) return false;
409 if (!s
->addChar('=')) return false;
410 if (!s
->addChar('=')) return false;
413 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 2) & 0x3c)];
414 if (!s
->addChar(c
)) return false;
415 if (!s
->addChar('=')) return false;
419 return s
->addXMLEndTag("data");