]> git.saurik.com Git - apple/xnu.git/blame - libkern/c++/OSData.cpp
xnu-1228.tar.gz
[apple/xnu.git] / libkern / c++ / OSData.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* IOData.m created by rsulack on Thu 25-Sep-1997 */
29
9bccf70c 30#include <string.h>
1c79356b
A
31
32#include <libkern/c++/OSData.h>
33#include <libkern/c++/OSSerialize.h>
34#include <libkern/c++/OSLib.h>
35#include <libkern/c++/OSString.h>
9bccf70c 36#include <string.h>
1c79356b
A
37
38#define super OSObject
39
40OSDefineMetaClassAndStructors(OSData, OSObject)
41OSMetaClassDefineReservedUnused(OSData, 0);
42OSMetaClassDefineReservedUnused(OSData, 1);
43OSMetaClassDefineReservedUnused(OSData, 2);
44OSMetaClassDefineReservedUnused(OSData, 3);
45OSMetaClassDefineReservedUnused(OSData, 4);
46OSMetaClassDefineReservedUnused(OSData, 5);
47OSMetaClassDefineReservedUnused(OSData, 6);
48OSMetaClassDefineReservedUnused(OSData, 7);
49
50#define EXTERNAL ((unsigned int) -1)
51
52#if OSALLOCDEBUG
55e303ae 53extern int debug_container_malloc_size;
1c79356b
A
54#define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
55#else
56#define ACCUMSIZE(s)
57#endif
58
59bool OSData::initWithCapacity(unsigned int inCapacity)
60{
61 if (!super::init())
62 return false;
63
55e303ae
A
64 if (data && (!inCapacity || capacity < inCapacity) ) {
65 // clean out old data's storage if it isn't big enough
0c530ab8 66 kfree(data, capacity);
55e303ae
A
67 data = 0;
68 ACCUMSIZE(-capacity);
69 }
70
71 if (inCapacity && !data) {
1c79356b
A
72 data = (void *) kalloc(inCapacity);
73 if (!data)
74 return false;
55e303ae
A
75 capacity = inCapacity;
76 ACCUMSIZE(inCapacity);
1c79356b
A
77 }
78
79 length = 0;
55e303ae 80 if (inCapacity < 16)
1c79356b 81 capacityIncrement = 16;
55e303ae
A
82 else
83 capacityIncrement = inCapacity;
1c79356b
A
84
85 return true;
86}
87
88bool OSData::initWithBytes(const void *bytes, unsigned int inLength)
89{
90 if ((inLength && !bytes) || !initWithCapacity(inLength))
91 return false;
92
55e303ae
A
93 if (bytes != data)
94 bcopy(bytes, data, inLength);
1c79356b
A
95 length = inLength;
96
97 return true;
98}
99
100bool OSData::initWithBytesNoCopy(void *bytes, unsigned int inLength)
101{
102 if (!super::init())
103 return false;
104
105 length = inLength;
106 capacity = EXTERNAL;
107 data = bytes;
108
109 return true;
110}
111
112bool OSData::initWithData(const OSData *inData)
113{
114 return initWithBytes(inData->data, inData->length);
115}
116
117bool OSData::initWithData(const OSData *inData,
118 unsigned int start, unsigned int inLength)
119{
120 const void *localData = inData->getBytesNoCopy(start, inLength);
121
122 if (localData)
123 return initWithBytes(localData, inLength);
124 else
125 return false;
126}
127
128OSData *OSData::withCapacity(unsigned int inCapacity)
129{
130 OSData *me = new OSData;
131
132 if (me && !me->initWithCapacity(inCapacity)) {
55e303ae 133 me->release();
1c79356b
A
134 return 0;
135 }
136
137 return me;
138}
139
140OSData *OSData::withBytes(const void *bytes, unsigned int inLength)
141{
142 OSData *me = new OSData;
143
144 if (me && !me->initWithBytes(bytes, inLength)) {
55e303ae 145 me->release();
1c79356b
A
146 return 0;
147 }
148 return me;
149}
150
151OSData *OSData::withBytesNoCopy(void *bytes, unsigned int inLength)
152{
153 OSData *me = new OSData;
154
155 if (me && !me->initWithBytesNoCopy(bytes, inLength)) {
55e303ae 156 me->release();
1c79356b
A
157 return 0;
158 }
159
160 return me;
161}
162
163OSData *OSData::withData(const OSData *inData)
164{
165 OSData *me = new OSData;
166
167 if (me && !me->initWithData(inData)) {
55e303ae 168 me->release();
1c79356b
A
169 return 0;
170 }
171
172 return me;
173}
174
175OSData *OSData::withData(const OSData *inData,
176 unsigned int start, unsigned int inLength)
177{
178 OSData *me = new OSData;
179
180 if (me && !me->initWithData(inData, start, inLength)) {
55e303ae 181 me->release();
1c79356b
A
182 return 0;
183 }
184
185 return me;
186}
187
188void OSData::free()
189{
190 if (capacity != EXTERNAL && data && capacity) {
0c530ab8 191 kfree(data, capacity);
1c79356b
A
192 ACCUMSIZE( -capacity );
193 }
194 super::free();
195}
196
197unsigned int OSData::getLength() const { return length; }
198unsigned int OSData::getCapacity() const { return capacity; }
199
200unsigned int OSData::getCapacityIncrement() const
201{
202 return capacityIncrement;
203}
204
205unsigned int OSData::setCapacityIncrement(unsigned increment)
206{
207 return capacityIncrement = increment;
208}
209
210unsigned int OSData::ensureCapacity(unsigned int newCapacity)
211{
212 unsigned char * newData;
213
214 if (newCapacity <= capacity)
215 return capacity;
216
217 newCapacity = (((newCapacity - 1) / capacityIncrement) + 1)
218 * capacityIncrement;
219
220 newData = (unsigned char *) kalloc(newCapacity);
221
222 if ( newData ) {
223 bzero(newData + capacity, newCapacity - capacity);
224 if (data) {
225 bcopy(data, newData, capacity);
0c530ab8 226 kfree(data, capacity);
1c79356b
A
227 }
228 ACCUMSIZE( newCapacity - capacity );
229 data = (void *) newData;
230 capacity = newCapacity;
231 }
232
233 return capacity;
234}
235
236bool OSData::appendBytes(const void *bytes, unsigned int inLength)
237{
238 unsigned int newSize;
239
55e303ae 240 if (!inLength)
1c79356b
A
241 return true;
242
243 if (capacity == EXTERNAL)
244 return false;
245
246 newSize = length + inLength;
247 if ( (newSize > capacity) && newSize > ensureCapacity(newSize) )
248 return false;
249
55e303ae
A
250 if (bytes)
251 bcopy(bytes, &((unsigned char *)data)[length], inLength);
252 else
253 bzero(&((unsigned char *)data)[length], inLength);
254
1c79356b
A
255 length = newSize;
256
257 return true;
258}
259
260bool OSData::appendByte(unsigned char byte, unsigned int inLength)
261{
262 unsigned int newSize;
263
55e303ae 264 if (!inLength)
1c79356b
A
265 return true;
266
267 if (capacity == EXTERNAL)
268 return false;
269
270 newSize = length + inLength;
271 if ( (newSize > capacity) && newSize > ensureCapacity(newSize) )
272 return false;
273
274 memset(&((unsigned char *)data)[length], byte, inLength);
275 length = newSize;
276
277 return true;
278}
279
280bool OSData::appendBytes(const OSData *other)
281{
282 return appendBytes(other->data, other->length);
283}
284
285const void *OSData::getBytesNoCopy() const
286{
55e303ae 287 if (!length)
1c79356b
A
288 return 0;
289 else
290 return data;
291}
292
293const void *OSData::getBytesNoCopy(unsigned int start,
294 unsigned int inLength) const
295{
296 const void *outData = 0;
297
298 if (length
299 && start < length
300 && (start + inLength) <= length)
301 outData = (const void *) ((char *) data + start);
302
303 return outData;
304}
305
306bool OSData::isEqualTo(const OSData *aData) const
307{
308 unsigned int len;
309
310 len = aData->length;
311 if ( length != len )
312 return false;
313
314 return isEqualTo(aData->data, len);
315}
316
317bool OSData::isEqualTo(const void *someData, unsigned int inLength) const
318{
319 return (length >= inLength) && (bcmp(data, someData, inLength) == 0);
320}
321
322bool OSData::isEqualTo(const OSMetaClassBase *obj) const
323{
324 OSData * data;
325 OSString * str;
326
327 if ((data = OSDynamicCast(OSData, obj)))
328 return isEqualTo(data);
329 else if ((str = OSDynamicCast (OSString, obj)))
330 return isEqualTo(str);
331 else
332 return false;
333}
334
335bool OSData::isEqualTo(const OSString *obj) const
336{
337 const char * aCString;
338 char * dataPtr;
339 unsigned int checkLen = length;
340 unsigned int stringLen;
341
55e303ae 342 if (!obj)
1c79356b
A
343 return false;
344
345 stringLen = obj->getLength ();
346
347 dataPtr = (char *)data;
348
349 if (stringLen != checkLen) {
350
351 // check for the fact that OSData may be a buffer that
352 // that includes a termination byte and will thus have
353 // a length of the actual string length PLUS 1. In this
354 // case we verify that the additional byte is a terminator
355 // and if so count the two lengths as being the same.
356
357 if ( (checkLen - stringLen) == 1) {
358 if (dataPtr[checkLen-1] != 0) // non-zero means not a terminator and thus not likely the same
359 return false;
360 checkLen--;
361 }
362 else
363 return false;
364 }
365
366 aCString = obj->getCStringNoCopy ();
367
368 for ( unsigned int i=0; i < checkLen; i++ ) {
369 if ( *dataPtr++ != aCString[i] )
370 return false;
371 }
372
373 return true;
374}
375
376//this was taken from CFPropertyList.c
377static const char __CFPLDataEncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
378
379bool OSData::serialize(OSSerialize *s) const
380{
381 unsigned int i;
382 const unsigned char *p;
383 unsigned char c;
384
385 if (s->previouslySerialized(this)) return true;
386
387 if (!s->addXMLStartTag(this, "data")) return false;
388
389 for (i = 0, p = (unsigned char *)data; i < length; i++, p++) {
390 /* 3 bytes are encoded as 4 */
391 switch (i % 3) {
392 case 0:
393 c = __CFPLDataEncodeTable [ ((p[0] >> 2) & 0x3f)];
394 if (!s->addChar(c)) return false;
395 break;
396 case 1:
397 c = __CFPLDataEncodeTable [ ((((p[-1] << 8) | p[0]) >> 4) & 0x3f)];
398 if (!s->addChar(c)) return false;
399 break;
400 case 2:
401 c = __CFPLDataEncodeTable [ ((((p[-1] << 8) | p[0]) >> 6) & 0x3f)];
402 if (!s->addChar(c)) return false;
403 c = __CFPLDataEncodeTable [ (p[0] & 0x3f)];
404 if (!s->addChar(c)) return false;
405 break;
406 }
407 }
408 switch (i % 3) {
409 case 0:
410 break;
411 case 1:
412 c = __CFPLDataEncodeTable [ ((p[-1] << 4) & 0x30)];
413 if (!s->addChar(c)) return false;
414 if (!s->addChar('=')) return false;
415 if (!s->addChar('=')) return false;
416 break;
417 case 2:
418 c = __CFPLDataEncodeTable [ ((p[-1] << 2) & 0x3c)];
419 if (!s->addChar(c)) return false;
420 if (!s->addChar('=')) return false;
421 break;
422 }
423
424 return s->addXMLEndTag("data");
425}