xref: /xnu-11215/libkern/c++/OSData.cpp (revision a3bb9fcc)
1 /*
2  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
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
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* IOData.m created by rsulack on Thu 25-Sep-1997 */
29 
30 #include <string.h>
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>
36 #include <string.h>
37 
38 #define super OSObject
39 
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);
49 
50 #define EXTERNAL ((unsigned int) -1)
51 
52 #if OSALLOCDEBUG
53 extern int debug_container_malloc_size;
54 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
55 #else
56 #define ACCUMSIZE(s)
57 #endif
58 
59 bool OSData::initWithCapacity(unsigned int inCapacity)
60 {
61     if (!super::init())
62         return false;
63 
64     if (data && (!inCapacity || capacity < inCapacity) ) {
65         // clean out old data's storage if it isn't big enough
66         kfree(data, capacity);
67         data = 0;
68         ACCUMSIZE(-capacity);
69     }
70 
71     if (inCapacity && !data) {
72         data = (void *) kalloc(inCapacity);
73         if (!data)
74             return false;
75         capacity = inCapacity;
76         ACCUMSIZE(inCapacity);
77     }
78 
79     length = 0;
80     if (inCapacity < 16)
81         capacityIncrement = 16;
82     else
83         capacityIncrement = inCapacity;
84 
85     return true;
86 }
87 
88 bool OSData::initWithBytes(const void *bytes, unsigned int inLength)
89 {
90     if ((inLength && !bytes) || !initWithCapacity(inLength))
91         return false;
92 
93     if (bytes != data)
94 	bcopy(bytes, data, inLength);
95     length = inLength;
96 
97     return true;
98 }
99 
100 bool 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 
112 bool OSData::initWithData(const OSData *inData)
113 {
114     return initWithBytes(inData->data, inData->length);
115 }
116 
117 bool 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 
128 OSData *OSData::withCapacity(unsigned int inCapacity)
129 {
130     OSData *me = new OSData;
131 
132     if (me && !me->initWithCapacity(inCapacity)) {
133         me->release();
134         return 0;
135     }
136 
137     return me;
138 }
139 
140 OSData *OSData::withBytes(const void *bytes, unsigned int inLength)
141 {
142     OSData *me = new OSData;
143 
144     if (me && !me->initWithBytes(bytes, inLength)) {
145         me->release();
146         return 0;
147     }
148     return me;
149 }
150 
151 OSData *OSData::withBytesNoCopy(void *bytes, unsigned int inLength)
152 {
153     OSData *me = new OSData;
154 
155     if (me && !me->initWithBytesNoCopy(bytes, inLength)) {
156         me->release();
157         return 0;
158     }
159 
160     return me;
161 }
162 
163 OSData *OSData::withData(const OSData *inData)
164 {
165     OSData *me = new OSData;
166 
167     if (me && !me->initWithData(inData)) {
168         me->release();
169         return 0;
170     }
171 
172     return me;
173 }
174 
175 OSData *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)) {
181         me->release();
182         return 0;
183     }
184 
185     return me;
186 }
187 
188 void OSData::free()
189 {
190     if (capacity != EXTERNAL && data && capacity) {
191         kfree(data, capacity);
192         ACCUMSIZE( -capacity );
193     } else if (capacity == EXTERNAL) {
194 	DeallocFunction freemem = reserved ? reserved->deallocFunction : NULL;
195 	if (freemem && data && length) {
196 		freemem(data, length);
197 	}
198     }
199     if (reserved) kfree(reserved, sizeof(ExpansionData));
200     super::free();
201 }
202 
203 unsigned int OSData::getLength() const { return length; }
204 unsigned int OSData::getCapacity() const { return capacity; }
205 
206 unsigned int OSData::getCapacityIncrement() const
207 {
208     return capacityIncrement;
209 }
210 
211 unsigned int OSData::setCapacityIncrement(unsigned increment)
212 {
213     return capacityIncrement = increment;
214 }
215 
216 // xx-review: does not check for capacity == EXTERNAL
217 
218 unsigned int OSData::ensureCapacity(unsigned int newCapacity)
219 {
220     unsigned char * newData;
221     unsigned int finalCapacity;
222 
223     if (newCapacity <= capacity)
224         return capacity;
225 
226     finalCapacity = (((newCapacity - 1) / capacityIncrement) + 1)
227                 * capacityIncrement;
228 
229     // integer overflow check
230     if (finalCapacity < newCapacity)
231         return capacity;
232 
233     newData = (unsigned char *) kalloc(finalCapacity);
234 
235     if ( newData ) {
236         bzero(newData + capacity, finalCapacity - capacity);
237         if (data) {
238             bcopy(data, newData, capacity);
239             kfree(data, capacity);
240         }
241         ACCUMSIZE( finalCapacity - capacity );
242         data = (void *) newData;
243         capacity = finalCapacity;
244     }
245 
246     return capacity;
247 }
248 
249 bool OSData::appendBytes(const void *bytes, unsigned int inLength)
250 {
251     unsigned int newSize;
252 
253     if (!inLength)
254         return true;
255 
256     if (capacity == EXTERNAL)
257         return false;
258 
259     newSize = length + inLength;
260     if ( (newSize > capacity) && newSize > ensureCapacity(newSize) )
261         return false;
262 
263     if (bytes)
264         bcopy(bytes, &((unsigned char *)data)[length], inLength);
265     else
266         bzero(&((unsigned char *)data)[length], inLength);
267 
268     length = newSize;
269 
270     return true;
271 }
272 
273 bool OSData::appendByte(unsigned char byte, unsigned int inLength)
274 {
275     unsigned int newSize;
276 
277     if (!inLength)
278         return true;
279 
280     if (capacity == EXTERNAL)
281         return false;
282 
283     newSize = length + inLength;
284     if ( (newSize > capacity) && newSize > ensureCapacity(newSize) )
285         return false;
286 
287     memset(&((unsigned char *)data)[length], byte, inLength);
288     length = newSize;
289 
290     return true;
291 }
292 
293 bool OSData::appendBytes(const OSData *other)
294 {
295     return appendBytes(other->data, other->length);
296 }
297 
298 const void *OSData::getBytesNoCopy() const
299 {
300     if (!length)
301         return 0;
302     else
303         return data;
304 }
305 
306 const void *OSData::getBytesNoCopy(unsigned int start,
307                                    unsigned int inLength) const
308 {
309     const void *outData = 0;
310 
311     if (length
312     &&  start < length
313     && (start + inLength) <= length)
314         outData = (const void *) ((char *) data + start);
315 
316     return outData;
317 }
318 
319 bool OSData::isEqualTo(const OSData *aData) const
320 {
321     unsigned int len;
322 
323     len = aData->length;
324     if ( length != len )
325         return false;
326 
327     return isEqualTo(aData->data, len);
328 }
329 
330 bool OSData::isEqualTo(const void *someData, unsigned int inLength) const
331 {
332     return (length >= inLength) && (bcmp(data, someData, inLength) == 0);
333 }
334 
335 bool OSData::isEqualTo(const OSMetaClassBase *obj) const
336 {
337     OSData *	otherData;
338     OSString *  str;
339 
340     if ((otherData = OSDynamicCast(OSData, obj)))
341         return isEqualTo(otherData);
342     else if ((str = OSDynamicCast (OSString, obj)))
343         return isEqualTo(str);
344     else
345         return false;
346 }
347 
348 bool OSData::isEqualTo(const OSString *obj) const
349 {
350     const char * aCString;
351     char * dataPtr;
352     unsigned int checkLen = length;
353     unsigned int stringLen;
354 
355     if (!obj)
356       return false;
357 
358     stringLen = obj->getLength ();
359 
360     dataPtr = (char *)data;
361 
362     if (stringLen != checkLen) {
363 
364       // check for the fact that OSData may be a buffer that
365       // that includes a termination byte and will thus have
366       // a length of the actual string length PLUS 1. In this
367       // case we verify that the additional byte is a terminator
368       // and if so count the two lengths as being the same.
369 
370       if ( (checkLen - stringLen) == 1) {
371 	if (dataPtr[checkLen-1] != 0) // non-zero means not a terminator and thus not likely the same
372 	  return false;
373         checkLen--;
374       }
375       else
376 	return false;
377     }
378 
379     aCString = obj->getCStringNoCopy ();
380 
381     for ( unsigned int i=0; i < checkLen; i++ ) {
382       if ( *dataPtr++ != aCString[i] )
383         return false;
384     }
385 
386    return true;
387 }
388 
389 //this was taken from CFPropertyList.c
390 static const char __CFPLDataEncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
391 
392 bool OSData::serialize(OSSerialize *s) const
393 {
394     unsigned int i;
395     const unsigned char *p;
396     unsigned char c;
397     unsigned int serializeLength;
398 
399     if (s->previouslySerialized(this)) return true;
400 
401     if (!s->addXMLStartTag(this, "data")) return false;
402 
403     serializeLength = length;
404     if (reserved && reserved->disableSerialization) serializeLength = 0;
405 
406     for (i = 0, p = (unsigned char *)data; i < serializeLength; i++, p++) {
407         /* 3 bytes are encoded as 4 */
408         switch (i % 3) {
409 	case 0:
410 		c = __CFPLDataEncodeTable [ ((p[0] >> 2) & 0x3f)];
411 		if (!s->addChar(c)) return false;
412 		break;
413 	case 1:
414 		c = __CFPLDataEncodeTable [ ((((p[-1] << 8) | p[0]) >> 4) & 0x3f)];
415 		if (!s->addChar(c)) return false;
416 		break;
417 	case 2:
418 		c = __CFPLDataEncodeTable [ ((((p[-1] << 8) | p[0]) >> 6) & 0x3f)];
419 		if (!s->addChar(c)) return false;
420 		c = __CFPLDataEncodeTable [ (p[0] & 0x3f)];
421 		if (!s->addChar(c)) return false;
422 		break;
423 	}
424     }
425     switch (i % 3) {
426     case 0:
427 	    break;
428     case 1:
429 	    c = __CFPLDataEncodeTable [ ((p[-1] << 4) & 0x30)];
430 	    if (!s->addChar(c)) return false;
431 	    if (!s->addChar('=')) return false;
432 	    if (!s->addChar('=')) return false;
433 	    break;
434     case 2:
435 	    c = __CFPLDataEncodeTable [ ((p[-1] << 2) & 0x3c)];
436 	    if (!s->addChar(c)) return false;
437 	    if (!s->addChar('=')) return false;
438 	    break;
439     }
440 
441     return s->addXMLEndTag("data");
442 }
443 
444 void OSData::setDeallocFunction(DeallocFunction func)
445 {
446     if (!reserved)
447     {
448     	reserved = (typeof(reserved)) kalloc(sizeof(ExpansionData));
449         if (!reserved) return;
450         bzero(reserved, sizeof(ExpansionData));
451     }
452     reserved->deallocFunction = func;
453 }
454 
455 void OSData::setSerializable(bool serializable)
456 {
457     if (!reserved)
458     {
459     	reserved = (typeof(reserved)) kalloc(sizeof(ExpansionData));
460 	if (!reserved) return;
461 	bzero(reserved, sizeof(ExpansionData));
462     }
463     reserved->disableSerialization = (!serializable);
464 }
465