2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* IOData.m created by rsulack on Thu 25-Sep-1997 */
25 #include <libkern/c++/OSData.h>
26 #include <libkern/c++/OSSerialize.h>
27 #include <libkern/c++/OSLib.h>
28 #include <libkern/c++/OSString.h>
30 #define super OSObject
32 OSDefineMetaClassAndStructors(OSData
, OSObject
)
33 OSMetaClassDefineReservedUnused(OSData
, 0);
34 OSMetaClassDefineReservedUnused(OSData
, 1);
35 OSMetaClassDefineReservedUnused(OSData
, 2);
36 OSMetaClassDefineReservedUnused(OSData
, 3);
37 OSMetaClassDefineReservedUnused(OSData
, 4);
38 OSMetaClassDefineReservedUnused(OSData
, 5);
39 OSMetaClassDefineReservedUnused(OSData
, 6);
40 OSMetaClassDefineReservedUnused(OSData
, 7);
42 #define EXTERNAL ((unsigned int) -1)
46 extern int debug_container_malloc_size
;
48 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
53 bool OSData::initWithCapacity(unsigned int inCapacity
)
59 data
= (void *) kalloc(inCapacity
);
65 capacity
= inCapacity
;
66 capacityIncrement
= capacity
;
67 if(!capacityIncrement
)
68 capacityIncrement
= 16;
75 bool OSData::initWithBytes(const void *bytes
, unsigned int inLength
)
77 if ((inLength
&& !bytes
) || !initWithCapacity(inLength
))
80 bcopy(bytes
, data
, inLength
);
86 bool OSData::initWithBytesNoCopy(void *bytes
, unsigned int inLength
)
98 bool OSData::initWithData(const OSData
*inData
)
100 return initWithBytes(inData
->data
, inData
->length
);
103 bool OSData::initWithData(const OSData
*inData
,
104 unsigned int start
, unsigned int inLength
)
106 const void *localData
= inData
->getBytesNoCopy(start
, inLength
);
109 return initWithBytes(localData
, inLength
);
114 OSData
*OSData::withCapacity(unsigned int inCapacity
)
116 OSData
*me
= new OSData
;
118 if (me
&& !me
->initWithCapacity(inCapacity
)) {
126 OSData
*OSData::withBytes(const void *bytes
, unsigned int inLength
)
128 OSData
*me
= new OSData
;
130 if (me
&& !me
->initWithBytes(bytes
, inLength
)) {
137 OSData
*OSData::withBytesNoCopy(void *bytes
, unsigned int inLength
)
139 OSData
*me
= new OSData
;
141 if (me
&& !me
->initWithBytesNoCopy(bytes
, inLength
)) {
149 OSData
*OSData::withData(const OSData
*inData
)
151 OSData
*me
= new OSData
;
153 if (me
&& !me
->initWithData(inData
)) {
161 OSData
*OSData::withData(const OSData
*inData
,
162 unsigned int start
, unsigned int inLength
)
164 OSData
*me
= new OSData
;
166 if (me
&& !me
->initWithData(inData
, start
, inLength
)) {
176 if (capacity
!= EXTERNAL
&& data
&& capacity
) {
177 kfree((vm_offset_t
)data
, capacity
);
178 ACCUMSIZE( -capacity
);
183 unsigned int OSData::getLength() const { return length
; }
184 unsigned int OSData::getCapacity() const { return capacity
; }
186 unsigned int OSData::getCapacityIncrement() const
188 return capacityIncrement
;
191 unsigned int OSData::setCapacityIncrement(unsigned increment
)
193 return capacityIncrement
= increment
;
196 unsigned int OSData::ensureCapacity(unsigned int newCapacity
)
198 unsigned char * newData
;
200 if (newCapacity
<= capacity
)
203 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
206 newData
= (unsigned char *) kalloc(newCapacity
);
209 bzero(newData
+ capacity
, newCapacity
- capacity
);
211 bcopy(data
, newData
, capacity
);
212 kfree((vm_offset_t
)data
, capacity
);
214 ACCUMSIZE( newCapacity
- capacity
);
215 data
= (void *) newData
;
216 capacity
= newCapacity
;
222 bool OSData::appendBytes(const void *bytes
, unsigned int inLength
)
224 unsigned int newSize
;
229 if (capacity
== EXTERNAL
)
232 newSize
= length
+ inLength
;
233 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
236 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
242 bool OSData::appendByte(unsigned char byte
, unsigned int inLength
)
244 unsigned int newSize
;
249 if (capacity
== EXTERNAL
)
252 newSize
= length
+ inLength
;
253 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
256 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
262 bool OSData::appendBytes(const OSData
*other
)
264 return appendBytes(other
->data
, other
->length
);
267 const void *OSData::getBytesNoCopy() const
275 const void *OSData::getBytesNoCopy(unsigned int start
,
276 unsigned int inLength
) const
278 const void *outData
= 0;
282 && (start
+ inLength
) <= length
)
283 outData
= (const void *) ((char *) data
+ start
);
288 bool OSData::isEqualTo(const OSData
*aData
) const
296 return isEqualTo(aData
->data
, len
);
299 bool OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
301 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
304 bool OSData::isEqualTo(const OSMetaClassBase
*obj
) const
309 if ((data
= OSDynamicCast(OSData
, obj
)))
310 return isEqualTo(data
);
311 else if ((str
= OSDynamicCast (OSString
, obj
)))
312 return isEqualTo(str
);
317 bool OSData::isEqualTo(const OSString
*obj
) const
319 const char * aCString
;
321 unsigned int checkLen
= length
;
322 unsigned int stringLen
;
327 stringLen
= obj
->getLength ();
329 dataPtr
= (char *)data
;
331 if (stringLen
!= checkLen
) {
333 // check for the fact that OSData may be a buffer that
334 // that includes a termination byte and will thus have
335 // a length of the actual string length PLUS 1. In this
336 // case we verify that the additional byte is a terminator
337 // and if so count the two lengths as being the same.
339 if ( (checkLen
- stringLen
) == 1) {
340 if (dataPtr
[checkLen
-1] != 0) // non-zero means not a terminator and thus not likely the same
348 aCString
= obj
->getCStringNoCopy ();
350 for ( unsigned int i
=0; i
< checkLen
; i
++ ) {
351 if ( *dataPtr
++ != aCString
[i
] )
358 //this was taken from CFPropertyList.c
359 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
361 bool OSData::serialize(OSSerialize
*s
) const
364 const unsigned char *p
;
367 if (s
->previouslySerialized(this)) return true;
369 if (!s
->addXMLStartTag(this, "data")) return false;
371 for (i
= 0, p
= (unsigned char *)data
; i
< length
; i
++, p
++) {
372 /* 3 bytes are encoded as 4 */
375 c
= __CFPLDataEncodeTable
[ ((p
[0] >> 2) & 0x3f)];
376 if (!s
->addChar(c
)) return false;
379 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
380 if (!s
->addChar(c
)) return false;
383 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
384 if (!s
->addChar(c
)) return false;
385 c
= __CFPLDataEncodeTable
[ (p
[0] & 0x3f)];
386 if (!s
->addChar(c
)) return false;
394 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 4) & 0x30)];
395 if (!s
->addChar(c
)) return false;
396 if (!s
->addChar('=')) return false;
397 if (!s
->addChar('=')) return false;
400 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 2) & 0x3c)];
401 if (!s
->addChar(c
)) return false;
402 if (!s
->addChar('=')) return false;
406 return s
->addXMLEndTag("data");