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 OSMetaClassDefineReservedUsed(OSData
, 0); // setDeallocFunction
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 struct OSData::ExpansionData
61 DeallocFunction deallocFunction
;
62 bool disableSerialization
;
65 bool OSData::initWithCapacity(unsigned int inCapacity
)
70 if (data
&& (!inCapacity
|| capacity
< inCapacity
) ) {
71 // clean out old data's storage if it isn't big enough
72 kfree(data
, capacity
);
77 if (inCapacity
&& !data
) {
78 data
= (void *) kalloc(inCapacity
);
81 capacity
= inCapacity
;
82 ACCUMSIZE(inCapacity
);
87 capacityIncrement
= 16;
89 capacityIncrement
= inCapacity
;
94 bool OSData::initWithBytes(const void *bytes
, unsigned int inLength
)
96 if ((inLength
&& !bytes
) || !initWithCapacity(inLength
))
100 bcopy(bytes
, data
, inLength
);
106 bool OSData::initWithBytesNoCopy(void *bytes
, unsigned int inLength
)
118 bool OSData::initWithData(const OSData
*inData
)
120 return initWithBytes(inData
->data
, inData
->length
);
123 bool OSData::initWithData(const OSData
*inData
,
124 unsigned int start
, unsigned int inLength
)
126 const void *localData
= inData
->getBytesNoCopy(start
, inLength
);
129 return initWithBytes(localData
, inLength
);
134 OSData
*OSData::withCapacity(unsigned int inCapacity
)
136 OSData
*me
= new OSData
;
138 if (me
&& !me
->initWithCapacity(inCapacity
)) {
146 OSData
*OSData::withBytes(const void *bytes
, unsigned int inLength
)
148 OSData
*me
= new OSData
;
150 if (me
&& !me
->initWithBytes(bytes
, inLength
)) {
157 OSData
*OSData::withBytesNoCopy(void *bytes
, unsigned int inLength
)
159 OSData
*me
= new OSData
;
161 if (me
&& !me
->initWithBytesNoCopy(bytes
, inLength
)) {
169 OSData
*OSData::withData(const OSData
*inData
)
171 OSData
*me
= new OSData
;
173 if (me
&& !me
->initWithData(inData
)) {
181 OSData
*OSData::withData(const OSData
*inData
,
182 unsigned int start
, unsigned int inLength
)
184 OSData
*me
= new OSData
;
186 if (me
&& !me
->initWithData(inData
, start
, inLength
)) {
196 if (capacity
!= EXTERNAL
&& data
&& capacity
) {
197 kfree(data
, capacity
);
198 ACCUMSIZE( -capacity
);
199 } else if (capacity
== EXTERNAL
) {
200 DeallocFunction freemem
= reserved
? reserved
->deallocFunction
: NULL
;
201 if (freemem
&& data
&& length
) {
202 freemem(data
, length
);
205 if (reserved
) kfree(reserved
, sizeof(ExpansionData
));
209 unsigned int OSData::getLength() const { return length
; }
210 unsigned int OSData::getCapacity() const { return capacity
; }
212 unsigned int OSData::getCapacityIncrement() const
214 return capacityIncrement
;
217 unsigned int OSData::setCapacityIncrement(unsigned increment
)
219 return capacityIncrement
= increment
;
222 // xx-review: does not check for capacity == EXTERNAL
224 unsigned int OSData::ensureCapacity(unsigned int newCapacity
)
226 unsigned char * newData
;
228 if (newCapacity
<= capacity
)
231 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
234 newData
= (unsigned char *) kalloc(newCapacity
);
237 bzero(newData
+ capacity
, newCapacity
- capacity
);
239 bcopy(data
, newData
, capacity
);
240 kfree(data
, capacity
);
242 ACCUMSIZE( newCapacity
- capacity
);
243 data
= (void *) newData
;
244 capacity
= newCapacity
;
250 bool OSData::appendBytes(const void *bytes
, unsigned int inLength
)
252 unsigned int newSize
;
257 if (capacity
== EXTERNAL
)
260 newSize
= length
+ inLength
;
261 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
265 bcopy(bytes
, &((unsigned char *)data
)[length
], inLength
);
267 bzero(&((unsigned char *)data
)[length
], inLength
);
274 bool OSData::appendByte(unsigned char byte
, unsigned int inLength
)
276 unsigned int newSize
;
281 if (capacity
== EXTERNAL
)
284 newSize
= length
+ inLength
;
285 if ( (newSize
> capacity
) && newSize
> ensureCapacity(newSize
) )
288 memset(&((unsigned char *)data
)[length
], byte
, inLength
);
294 bool OSData::appendBytes(const OSData
*other
)
296 return appendBytes(other
->data
, other
->length
);
299 const void *OSData::getBytesNoCopy() const
307 const void *OSData::getBytesNoCopy(unsigned int start
,
308 unsigned int inLength
) const
310 const void *outData
= 0;
314 && (start
+ inLength
) <= length
)
315 outData
= (const void *) ((char *) data
+ start
);
320 bool OSData::isEqualTo(const OSData
*aData
) const
328 return isEqualTo(aData
->data
, len
);
331 bool OSData::isEqualTo(const void *someData
, unsigned int inLength
) const
333 return (length
>= inLength
) && (bcmp(data
, someData
, inLength
) == 0);
336 bool OSData::isEqualTo(const OSMetaClassBase
*obj
) const
341 if ((otherData
= OSDynamicCast(OSData
, obj
)))
342 return isEqualTo(otherData
);
343 else if ((str
= OSDynamicCast (OSString
, obj
)))
344 return isEqualTo(str
);
349 bool OSData::isEqualTo(const OSString
*obj
) const
351 const char * aCString
;
353 unsigned int checkLen
= length
;
354 unsigned int stringLen
;
359 stringLen
= obj
->getLength ();
361 dataPtr
= (char *)data
;
363 if (stringLen
!= checkLen
) {
365 // check for the fact that OSData may be a buffer that
366 // that includes a termination byte and will thus have
367 // a length of the actual string length PLUS 1. In this
368 // case we verify that the additional byte is a terminator
369 // and if so count the two lengths as being the same.
371 if ( (checkLen
- stringLen
) == 1) {
372 if (dataPtr
[checkLen
-1] != 0) // non-zero means not a terminator and thus not likely the same
380 aCString
= obj
->getCStringNoCopy ();
382 for ( unsigned int i
=0; i
< checkLen
; i
++ ) {
383 if ( *dataPtr
++ != aCString
[i
] )
390 //this was taken from CFPropertyList.c
391 static const char __CFPLDataEncodeTable
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
393 bool OSData::serialize(OSSerialize
*s
) const
396 const unsigned char *p
;
398 unsigned int serializeLength
;
400 if (s
->previouslySerialized(this)) return true;
402 if (!s
->addXMLStartTag(this, "data")) return false;
404 serializeLength
= length
;
405 if (reserved
&& reserved
->disableSerialization
) serializeLength
= 0;
407 for (i
= 0, p
= (unsigned char *)data
; i
< serializeLength
; i
++, p
++) {
408 /* 3 bytes are encoded as 4 */
411 c
= __CFPLDataEncodeTable
[ ((p
[0] >> 2) & 0x3f)];
412 if (!s
->addChar(c
)) return false;
415 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 4) & 0x3f)];
416 if (!s
->addChar(c
)) return false;
419 c
= __CFPLDataEncodeTable
[ ((((p
[-1] << 8) | p
[0]) >> 6) & 0x3f)];
420 if (!s
->addChar(c
)) return false;
421 c
= __CFPLDataEncodeTable
[ (p
[0] & 0x3f)];
422 if (!s
->addChar(c
)) return false;
430 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 4) & 0x30)];
431 if (!s
->addChar(c
)) return false;
432 if (!s
->addChar('=')) return false;
433 if (!s
->addChar('=')) return false;
436 c
= __CFPLDataEncodeTable
[ ((p
[-1] << 2) & 0x3c)];
437 if (!s
->addChar(c
)) return false;
438 if (!s
->addChar('=')) return false;
442 return s
->addXMLEndTag("data");
445 void OSData::setDeallocFunction(DeallocFunction func
)
449 reserved
= (typeof(reserved
)) kalloc(sizeof(ExpansionData
));
450 if (!reserved
) return;
451 bzero(reserved
, sizeof(ExpansionData
));
453 reserved
->deallocFunction
= func
;
456 void OSData::setSerializable(bool serializable
)
460 reserved
= (typeof(reserved
)) kalloc(sizeof(ExpansionData
));
461 if (!reserved
) return;
462 bzero(reserved
, sizeof(ExpansionData
));
464 reserved
->disableSerialization
= (!serializable
);