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)
51 extern int debug_container_malloc_size
;
53 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
58 bool OSData::initWithCapacity(unsigned int inCapacity
)
64 data
= (void *) kalloc(inCapacity
);
70 capacity
= inCapacity
;
71 capacityIncrement
= capacity
;
72 if(!capacityIncrement
)
73 capacityIncrement
= 16;
80 bool OSData::initWithBytes(const void *bytes
, unsigned int inLength
)
82 if ((inLength
&& !bytes
) || !initWithCapacity(inLength
))
85 bcopy(bytes
, data
, inLength
);
91 bool OSData::initWithBytesNoCopy(void *bytes
, unsigned int inLength
)
103 bool OSData::initWithData(const OSData
*inData
)
105 return initWithBytes(inData
->data
, inData
->length
);
108 bool OSData::initWithData(const OSData
*inData
,
109 unsigned int start
, unsigned int inLength
)
111 const void *localData
= inData
->getBytesNoCopy(start
, inLength
);
114 return initWithBytes(localData
, inLength
);
119 OSData
*OSData::withCapacity(unsigned int inCapacity
)
121 OSData
*me
= new OSData
;
123 if (me
&& !me
->initWithCapacity(inCapacity
)) {
131 OSData
*OSData::withBytes(const void *bytes
, unsigned int inLength
)
133 OSData
*me
= new OSData
;
135 if (me
&& !me
->initWithBytes(bytes
, inLength
)) {
142 OSData
*OSData::withBytesNoCopy(void *bytes
, unsigned int inLength
)
144 OSData
*me
= new OSData
;
146 if (me
&& !me
->initWithBytesNoCopy(bytes
, inLength
)) {
154 OSData
*OSData::withData(const OSData
*inData
)
156 OSData
*me
= new OSData
;
158 if (me
&& !me
->initWithData(inData
)) {
166 OSData
*OSData::withData(const OSData
*inData
,
167 unsigned int start
, unsigned int inLength
)
169 OSData
*me
= new OSData
;
171 if (me
&& !me
->initWithData(inData
, start
, inLength
)) {
181 if (capacity
!= EXTERNAL
&& data
&& capacity
) {
182 kfree((vm_offset_t
)data
, capacity
);
183 ACCUMSIZE( -capacity
);
188 unsigned int OSData::getLength() const { return length
; }
189 unsigned int OSData::getCapacity() const { return capacity
; }
191 unsigned int OSData::getCapacityIncrement() const
193 return capacityIncrement
;
196 unsigned int OSData::setCapacityIncrement(unsigned increment
)
198 return capacityIncrement
= increment
;
201 unsigned int OSData::ensureCapacity(unsigned int newCapacity
)
203 unsigned char * newData
;
205 if (newCapacity
<= capacity
)
208 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
211 newData
= (unsigned char *) kalloc(newCapacity
);
214 bzero(newData
+ capacity
, newCapacity
- capacity
);
216 bcopy(data
, newData
, capacity
);
217 kfree((vm_offset_t
)data
, capacity
);
219 ACCUMSIZE( newCapacity
- capacity
);
220 data
= (void *) newData
;
221 capacity
= newCapacity
;
227 bool OSData::appendBytes(const void *bytes
, unsigned int inLength
)
229 unsigned int newSize
;
234 if (capacity
== EXTERNAL
)
237 newSize
= length
+ inLength
;
238 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
241 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
247 bool OSData::appendByte(unsigned char byte
, unsigned int inLength
)
249 unsigned int newSize
;
254 if (capacity
== EXTERNAL
)
257 newSize
= length
+ inLength
;
258 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
261 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
267 bool OSData::appendBytes(const OSData
*other
)
269 return appendBytes(other
->data
, other
->length
);
272 const void *OSData::getBytesNoCopy() const
280 const void *OSData::getBytesNoCopy(unsigned int start
,
281 unsigned int inLength
) const
283 const void *outData
= 0;
287 && (start
+ inLength
) <= length
)
288 outData
= (const void *) ((char *) data
+ start
);
293 bool OSData::isEqualTo(const OSData
*aData
) const
301 return isEqualTo(aData
->data
, len
);
304 bool OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
306 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
309 bool OSData::isEqualTo(const OSMetaClassBase
*obj
) const
314 if ((data
= OSDynamicCast(OSData
, obj
)))
315 return isEqualTo(data
);
316 else if ((str
= OSDynamicCast (OSString
, obj
)))
317 return isEqualTo(str
);
322 bool OSData::isEqualTo(const OSString
*obj
) const
324 const char * aCString
;
326 unsigned int checkLen
= length
;
327 unsigned int stringLen
;
332 stringLen
= obj
->getLength ();
334 dataPtr
= (char *)data
;
336 if (stringLen
!= checkLen
) {
338 // check for the fact that OSData may be a buffer that
339 // that includes a termination byte and will thus have
340 // a length of the actual string length PLUS 1. In this
341 // case we verify that the additional byte is a terminator
342 // and if so count the two lengths as being the same.
344 if ( (checkLen
- stringLen
) == 1) {
345 if (dataPtr
[checkLen
-1] != 0) // non-zero means not a terminator and thus not likely the same
353 aCString
= obj
->getCStringNoCopy ();
355 for ( unsigned int i
=0; i
< checkLen
; i
++ ) {
356 if ( *dataPtr
++ != aCString
[i
] )
363 //this was taken from CFPropertyList.c
364 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
366 bool OSData::serialize(OSSerialize
*s
) const
369 const unsigned char *p
;
372 if (s
->previouslySerialized(this)) return true;
374 if (!s
->addXMLStartTag(this, "data")) return false;
376 for (i
= 0, p
= (unsigned char *)data
; i
< length
; i
++, p
++) {
377 /* 3 bytes are encoded as 4 */
380 c
= __CFPLDataEncodeTable
[ ((p
[0] >> 2) & 0x3f)];
381 if (!s
->addChar(c
)) return false;
384 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
385 if (!s
->addChar(c
)) return false;
388 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
389 if (!s
->addChar(c
)) return false;
390 c
= __CFPLDataEncodeTable
[ (p
[0] & 0x3f)];
391 if (!s
->addChar(c
)) return false;
399 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 4) & 0x30)];
400 if (!s
->addChar(c
)) return false;
401 if (!s
->addChar('=')) return false;
402 if (!s
->addChar('=')) return false;
405 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 2) & 0x3c)];
406 if (!s
->addChar(c
)) return false;
407 if (!s
->addChar('=')) return false;
411 return s
->addXMLEndTag("data");