xref: /xnu-8796.121.2/libkern/c++/OSData.cpp (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions /* IOData.m created by rsulack on Thu 25-Sep-1997 */
29*c54f35caSApple OSS Distributions 
30*c54f35caSApple OSS Distributions #include <string.h>
31*c54f35caSApple OSS Distributions 
32*c54f35caSApple OSS Distributions #include <vm/vm_kern.h>
33*c54f35caSApple OSS Distributions 
34*c54f35caSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
35*c54f35caSApple OSS Distributions 
36*c54f35caSApple OSS Distributions #include <libkern/c++/OSData.h>
37*c54f35caSApple OSS Distributions #include <libkern/c++/OSSerialize.h>
38*c54f35caSApple OSS Distributions #include <libkern/c++/OSLib.h>
39*c54f35caSApple OSS Distributions #include <libkern/c++/OSString.h>
40*c54f35caSApple OSS Distributions #include <IOKit/IOLib.h>
41*c54f35caSApple OSS Distributions 
42*c54f35caSApple OSS Distributions #define super OSObject
43*c54f35caSApple OSS Distributions 
44*c54f35caSApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(OSData, OSObject, ZC_ZFREE_CLEARMEM)
45*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUsedX86(OSData, 0);    // setDeallocFunction
46*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSData, 1);
47*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSData, 2);
48*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSData, 3);
49*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSData, 4);
50*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSData, 5);
51*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSData, 6);
52*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSData, 7);
53*c54f35caSApple OSS Distributions 
54*c54f35caSApple OSS Distributions #define EXTERNAL ((unsigned int) -1)
55*c54f35caSApple OSS Distributions 
56*c54f35caSApple OSS Distributions bool
initWithCapacity(unsigned int inCapacity)57*c54f35caSApple OSS Distributions OSData::initWithCapacity(unsigned int inCapacity)
58*c54f35caSApple OSS Distributions {
59*c54f35caSApple OSS Distributions 	struct kalloc_result kr;
60*c54f35caSApple OSS Distributions 	bool success = true;
61*c54f35caSApple OSS Distributions 
62*c54f35caSApple OSS Distributions 	if (!super::init()) {
63*c54f35caSApple OSS Distributions 		return false;
64*c54f35caSApple OSS Distributions 	}
65*c54f35caSApple OSS Distributions 
66*c54f35caSApple OSS Distributions 	/*
67*c54f35caSApple OSS Distributions 	 * OSData use of Z_MAY_COPYINMAP serves 2 purpposes:
68*c54f35caSApple OSS Distributions 	 *
69*c54f35caSApple OSS Distributions 	 * - It makes sure than when it goes to the VM, it uses its own object
70*c54f35caSApple OSS Distributions 	 *   rather than the kernel object so that vm_map_copyin() can be used.
71*c54f35caSApple OSS Distributions 	 *
72*c54f35caSApple OSS Distributions 	 * - On Intel, it goes to the VM for any size >= PAGE_SIZE to maintain
73*c54f35caSApple OSS Distributions 	 *   old (inefficient) ABI. On arm64 it will use kalloc_data() instead
74*c54f35caSApple OSS Distributions 	 *   until the vm_map_copy_t msg_ool_size_small threshold for copies.
75*c54f35caSApple OSS Distributions 	 */
76*c54f35caSApple OSS Distributions 
77*c54f35caSApple OSS Distributions 	if (inCapacity == 0) {
78*c54f35caSApple OSS Distributions 		if (capacity) {
79*c54f35caSApple OSS Distributions 			OSCONTAINER_ACCUMSIZE(-(size_t)capacity);
80*c54f35caSApple OSS Distributions 			/* can't use kfree() as we need to pass Z_MAY_COPYINMAP */
81*c54f35caSApple OSS Distributions 			__kheap_realloc(KHEAP_DATA_BUFFERS, data, capacity, 0,
82*c54f35caSApple OSS Distributions 			    Z_VM_TAG_BT(Z_WAITOK_ZERO | Z_FULLSIZE | Z_MAY_COPYINMAP,
83*c54f35caSApple OSS Distributions 			    VM_KERN_MEMORY_LIBKERN), (void *)&this->data);
84*c54f35caSApple OSS Distributions 			data     = nullptr;
85*c54f35caSApple OSS Distributions 			capacity = 0;
86*c54f35caSApple OSS Distributions 		}
87*c54f35caSApple OSS Distributions 	} else if (inCapacity <= capacity) {
88*c54f35caSApple OSS Distributions 		/*
89*c54f35caSApple OSS Distributions 		 * Nothing to change
90*c54f35caSApple OSS Distributions 		 */
91*c54f35caSApple OSS Distributions 	} else {
92*c54f35caSApple OSS Distributions 		kr = kalloc_ext(KHEAP_DATA_BUFFERS, inCapacity,
93*c54f35caSApple OSS Distributions 		    Z_VM_TAG_BT(Z_WAITOK_ZERO | Z_FULLSIZE | Z_MAY_COPYINMAP,
94*c54f35caSApple OSS Distributions 		    VM_KERN_MEMORY_LIBKERN), (void *)&this->data);
95*c54f35caSApple OSS Distributions 
96*c54f35caSApple OSS Distributions 		if (kr.addr) {
97*c54f35caSApple OSS Distributions 			size_t delta = 0;
98*c54f35caSApple OSS Distributions 
99*c54f35caSApple OSS Distributions 			data     = kr.addr;
100*c54f35caSApple OSS Distributions 			delta   -= capacity;
101*c54f35caSApple OSS Distributions 			capacity = (uint32_t)MIN(kr.size, UINT32_MAX);
102*c54f35caSApple OSS Distributions 			delta   += capacity;
103*c54f35caSApple OSS Distributions 			OSCONTAINER_ACCUMSIZE(delta);
104*c54f35caSApple OSS Distributions 		} else {
105*c54f35caSApple OSS Distributions 			success = false;
106*c54f35caSApple OSS Distributions 		}
107*c54f35caSApple OSS Distributions 	}
108*c54f35caSApple OSS Distributions 
109*c54f35caSApple OSS Distributions 	length = 0;
110*c54f35caSApple OSS Distributions 	capacityIncrement = MAX(16, inCapacity);
111*c54f35caSApple OSS Distributions 
112*c54f35caSApple OSS Distributions 	return success;
113*c54f35caSApple OSS Distributions }
114*c54f35caSApple OSS Distributions 
115*c54f35caSApple OSS Distributions bool
initWithBytes(const void * bytes,unsigned int inLength)116*c54f35caSApple OSS Distributions OSData::initWithBytes(const void *bytes, unsigned int inLength)
117*c54f35caSApple OSS Distributions {
118*c54f35caSApple OSS Distributions 	if ((inLength && !bytes) || !initWithCapacity(inLength)) {
119*c54f35caSApple OSS Distributions 		return false;
120*c54f35caSApple OSS Distributions 	}
121*c54f35caSApple OSS Distributions 
122*c54f35caSApple OSS Distributions 	if (bytes != data) {
123*c54f35caSApple OSS Distributions 		bcopy(bytes, data, inLength);
124*c54f35caSApple OSS Distributions 	}
125*c54f35caSApple OSS Distributions 	length = inLength;
126*c54f35caSApple OSS Distributions 
127*c54f35caSApple OSS Distributions 	return true;
128*c54f35caSApple OSS Distributions }
129*c54f35caSApple OSS Distributions 
130*c54f35caSApple OSS Distributions bool
initWithBytesNoCopy(void * bytes,unsigned int inLength)131*c54f35caSApple OSS Distributions OSData::initWithBytesNoCopy(void *bytes, unsigned int inLength)
132*c54f35caSApple OSS Distributions {
133*c54f35caSApple OSS Distributions 	if (!super::init()) {
134*c54f35caSApple OSS Distributions 		return false;
135*c54f35caSApple OSS Distributions 	}
136*c54f35caSApple OSS Distributions 
137*c54f35caSApple OSS Distributions 	length = inLength;
138*c54f35caSApple OSS Distributions 	capacity = EXTERNAL;
139*c54f35caSApple OSS Distributions 	data = bytes;
140*c54f35caSApple OSS Distributions 
141*c54f35caSApple OSS Distributions 	return true;
142*c54f35caSApple OSS Distributions }
143*c54f35caSApple OSS Distributions 
144*c54f35caSApple OSS Distributions bool
initWithData(const OSData * inData)145*c54f35caSApple OSS Distributions OSData::initWithData(const OSData *inData)
146*c54f35caSApple OSS Distributions {
147*c54f35caSApple OSS Distributions 	return initWithBytes(inData->data, inData->length);
148*c54f35caSApple OSS Distributions }
149*c54f35caSApple OSS Distributions 
150*c54f35caSApple OSS Distributions bool
initWithData(const OSData * inData,unsigned int start,unsigned int inLength)151*c54f35caSApple OSS Distributions OSData::initWithData(const OSData *inData,
152*c54f35caSApple OSS Distributions     unsigned int start, unsigned int inLength)
153*c54f35caSApple OSS Distributions {
154*c54f35caSApple OSS Distributions 	const void *localData = inData->getBytesNoCopy(start, inLength);
155*c54f35caSApple OSS Distributions 
156*c54f35caSApple OSS Distributions 	if (localData) {
157*c54f35caSApple OSS Distributions 		return initWithBytes(localData, inLength);
158*c54f35caSApple OSS Distributions 	} else {
159*c54f35caSApple OSS Distributions 		return false;
160*c54f35caSApple OSS Distributions 	}
161*c54f35caSApple OSS Distributions }
162*c54f35caSApple OSS Distributions 
163*c54f35caSApple OSS Distributions OSSharedPtr<OSData>
withCapacity(unsigned int inCapacity)164*c54f35caSApple OSS Distributions OSData::withCapacity(unsigned int inCapacity)
165*c54f35caSApple OSS Distributions {
166*c54f35caSApple OSS Distributions 	OSSharedPtr<OSData> me = OSMakeShared<OSData>();
167*c54f35caSApple OSS Distributions 
168*c54f35caSApple OSS Distributions 	if (me && !me->initWithCapacity(inCapacity)) {
169*c54f35caSApple OSS Distributions 		return nullptr;
170*c54f35caSApple OSS Distributions 	}
171*c54f35caSApple OSS Distributions 
172*c54f35caSApple OSS Distributions 	return me;
173*c54f35caSApple OSS Distributions }
174*c54f35caSApple OSS Distributions 
175*c54f35caSApple OSS Distributions OSSharedPtr<OSData>
withBytes(const void * bytes,unsigned int inLength)176*c54f35caSApple OSS Distributions OSData::withBytes(const void *bytes, unsigned int inLength)
177*c54f35caSApple OSS Distributions {
178*c54f35caSApple OSS Distributions 	OSSharedPtr<OSData> me = OSMakeShared<OSData>();
179*c54f35caSApple OSS Distributions 
180*c54f35caSApple OSS Distributions 	if (me && !me->initWithBytes(bytes, inLength)) {
181*c54f35caSApple OSS Distributions 		return nullptr;
182*c54f35caSApple OSS Distributions 	}
183*c54f35caSApple OSS Distributions 	return me;
184*c54f35caSApple OSS Distributions }
185*c54f35caSApple OSS Distributions 
186*c54f35caSApple OSS Distributions OSSharedPtr<OSData>
withBytesNoCopy(void * bytes,unsigned int inLength)187*c54f35caSApple OSS Distributions OSData::withBytesNoCopy(void *bytes, unsigned int inLength)
188*c54f35caSApple OSS Distributions {
189*c54f35caSApple OSS Distributions 	OSSharedPtr<OSData> me = OSMakeShared<OSData>();
190*c54f35caSApple OSS Distributions 
191*c54f35caSApple OSS Distributions 	if (me && !me->initWithBytesNoCopy(bytes, inLength)) {
192*c54f35caSApple OSS Distributions 		return nullptr;
193*c54f35caSApple OSS Distributions 	}
194*c54f35caSApple OSS Distributions 
195*c54f35caSApple OSS Distributions 	return me;
196*c54f35caSApple OSS Distributions }
197*c54f35caSApple OSS Distributions 
198*c54f35caSApple OSS Distributions OSSharedPtr<OSData>
withData(const OSData * inData)199*c54f35caSApple OSS Distributions OSData::withData(const OSData *inData)
200*c54f35caSApple OSS Distributions {
201*c54f35caSApple OSS Distributions 	OSSharedPtr<OSData> me = OSMakeShared<OSData>();
202*c54f35caSApple OSS Distributions 
203*c54f35caSApple OSS Distributions 	if (me && !me->initWithData(inData)) {
204*c54f35caSApple OSS Distributions 		return nullptr;
205*c54f35caSApple OSS Distributions 	}
206*c54f35caSApple OSS Distributions 
207*c54f35caSApple OSS Distributions 	return me;
208*c54f35caSApple OSS Distributions }
209*c54f35caSApple OSS Distributions 
210*c54f35caSApple OSS Distributions OSSharedPtr<OSData>
withData(const OSData * inData,unsigned int start,unsigned int inLength)211*c54f35caSApple OSS Distributions OSData::withData(const OSData *inData,
212*c54f35caSApple OSS Distributions     unsigned int start, unsigned int inLength)
213*c54f35caSApple OSS Distributions {
214*c54f35caSApple OSS Distributions 	OSSharedPtr<OSData> me = OSMakeShared<OSData>();
215*c54f35caSApple OSS Distributions 
216*c54f35caSApple OSS Distributions 	if (me && !me->initWithData(inData, start, inLength)) {
217*c54f35caSApple OSS Distributions 		return nullptr;
218*c54f35caSApple OSS Distributions 	}
219*c54f35caSApple OSS Distributions 
220*c54f35caSApple OSS Distributions 	return me;
221*c54f35caSApple OSS Distributions }
222*c54f35caSApple OSS Distributions 
223*c54f35caSApple OSS Distributions void
free()224*c54f35caSApple OSS Distributions OSData::free()
225*c54f35caSApple OSS Distributions {
226*c54f35caSApple OSS Distributions 	if ((capacity != EXTERNAL) && data && capacity) {
227*c54f35caSApple OSS Distributions 		/* can't use kfree() as we need to pass Z_MAY_COPYINMAP */
228*c54f35caSApple OSS Distributions 		__kheap_realloc(KHEAP_DATA_BUFFERS, data, capacity, 0,
229*c54f35caSApple OSS Distributions 		    Z_VM_TAG_BT(Z_WAITOK_ZERO | Z_FULLSIZE | Z_MAY_COPYINMAP,
230*c54f35caSApple OSS Distributions 		    VM_KERN_MEMORY_LIBKERN), (void *)&this->data);
231*c54f35caSApple OSS Distributions 		OSCONTAINER_ACCUMSIZE( -((size_t)capacity));
232*c54f35caSApple OSS Distributions 	} else if (capacity == EXTERNAL) {
233*c54f35caSApple OSS Distributions 		DeallocFunction freemem = reserved ? reserved->deallocFunction : NULL;
234*c54f35caSApple OSS Distributions 		if (freemem && data && length) {
235*c54f35caSApple OSS Distributions 			freemem(data, length);
236*c54f35caSApple OSS Distributions 		}
237*c54f35caSApple OSS Distributions 	}
238*c54f35caSApple OSS Distributions 	if (reserved) {
239*c54f35caSApple OSS Distributions 		kfree_type(ExpansionData, reserved);
240*c54f35caSApple OSS Distributions 	}
241*c54f35caSApple OSS Distributions 	super::free();
242*c54f35caSApple OSS Distributions }
243*c54f35caSApple OSS Distributions 
244*c54f35caSApple OSS Distributions unsigned int
getLength() const245*c54f35caSApple OSS Distributions OSData::getLength() const
246*c54f35caSApple OSS Distributions {
247*c54f35caSApple OSS Distributions 	return length;
248*c54f35caSApple OSS Distributions }
249*c54f35caSApple OSS Distributions unsigned int
getCapacity() const250*c54f35caSApple OSS Distributions OSData::getCapacity() const
251*c54f35caSApple OSS Distributions {
252*c54f35caSApple OSS Distributions 	return capacity;
253*c54f35caSApple OSS Distributions }
254*c54f35caSApple OSS Distributions 
255*c54f35caSApple OSS Distributions unsigned int
getCapacityIncrement() const256*c54f35caSApple OSS Distributions OSData::getCapacityIncrement() const
257*c54f35caSApple OSS Distributions {
258*c54f35caSApple OSS Distributions 	return capacityIncrement;
259*c54f35caSApple OSS Distributions }
260*c54f35caSApple OSS Distributions 
261*c54f35caSApple OSS Distributions unsigned int
setCapacityIncrement(unsigned increment)262*c54f35caSApple OSS Distributions OSData::setCapacityIncrement(unsigned increment)
263*c54f35caSApple OSS Distributions {
264*c54f35caSApple OSS Distributions 	return capacityIncrement = increment;
265*c54f35caSApple OSS Distributions }
266*c54f35caSApple OSS Distributions 
267*c54f35caSApple OSS Distributions // xx-review: does not check for capacity == EXTERNAL
268*c54f35caSApple OSS Distributions 
269*c54f35caSApple OSS Distributions unsigned int
ensureCapacity(unsigned int newCapacity)270*c54f35caSApple OSS Distributions OSData::ensureCapacity(unsigned int newCapacity)
271*c54f35caSApple OSS Distributions {
272*c54f35caSApple OSS Distributions 	struct kalloc_result kr;
273*c54f35caSApple OSS Distributions 	unsigned int finalCapacity;
274*c54f35caSApple OSS Distributions 
275*c54f35caSApple OSS Distributions 	if (newCapacity <= capacity) {
276*c54f35caSApple OSS Distributions 		return capacity;
277*c54f35caSApple OSS Distributions 	}
278*c54f35caSApple OSS Distributions 
279*c54f35caSApple OSS Distributions 	finalCapacity = (((newCapacity - 1) / capacityIncrement) + 1)
280*c54f35caSApple OSS Distributions 	    * capacityIncrement;
281*c54f35caSApple OSS Distributions 
282*c54f35caSApple OSS Distributions 	// integer overflow check
283*c54f35caSApple OSS Distributions 	if (finalCapacity < newCapacity) {
284*c54f35caSApple OSS Distributions 		return capacity;
285*c54f35caSApple OSS Distributions 	}
286*c54f35caSApple OSS Distributions 
287*c54f35caSApple OSS Distributions 	kr = krealloc_ext((void *)KHEAP_DATA_BUFFERS, data, capacity, finalCapacity,
288*c54f35caSApple OSS Distributions 	    Z_VM_TAG_BT(Z_WAITOK_ZERO | Z_FULLSIZE | Z_MAY_COPYINMAP,
289*c54f35caSApple OSS Distributions 	    VM_KERN_MEMORY_LIBKERN), (void *)&this->data);
290*c54f35caSApple OSS Distributions 
291*c54f35caSApple OSS Distributions 	if (kr.addr) {
292*c54f35caSApple OSS Distributions 		size_t delta = 0;
293*c54f35caSApple OSS Distributions 
294*c54f35caSApple OSS Distributions 		data     = kr.addr;
295*c54f35caSApple OSS Distributions 		delta   -= capacity;
296*c54f35caSApple OSS Distributions 		capacity = (uint32_t)MIN(kr.size, UINT32_MAX);
297*c54f35caSApple OSS Distributions 		delta   += capacity;
298*c54f35caSApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(delta);
299*c54f35caSApple OSS Distributions 	}
300*c54f35caSApple OSS Distributions 
301*c54f35caSApple OSS Distributions 	return capacity;
302*c54f35caSApple OSS Distributions }
303*c54f35caSApple OSS Distributions 
304*c54f35caSApple OSS Distributions bool
clipForCopyout()305*c54f35caSApple OSS Distributions OSData::clipForCopyout()
306*c54f35caSApple OSS Distributions {
307*c54f35caSApple OSS Distributions 	unsigned int newCapacity = (uint32_t)round_page(length);
308*c54f35caSApple OSS Distributions 	__assert_only struct kalloc_result kr;
309*c54f35caSApple OSS Distributions 
310*c54f35caSApple OSS Distributions 	/*
311*c54f35caSApple OSS Distributions 	 * OSData allocations are atomic, which means that if copyoutkdata()
312*c54f35caSApple OSS Distributions 	 * is used on them, and that there are fully unused pages at the end
313*c54f35caSApple OSS Distributions 	 * of the OSData buffer, then vm_map_copyin() will try to clip the VM
314*c54f35caSApple OSS Distributions 	 * entry which will panic.
315*c54f35caSApple OSS Distributions 	 *
316*c54f35caSApple OSS Distributions 	 * In order to avoid this, trim down the unused pages.
317*c54f35caSApple OSS Distributions 	 *
318*c54f35caSApple OSS Distributions 	 * We know this operation never fails and keeps the allocation
319*c54f35caSApple OSS Distributions 	 * address stable.
320*c54f35caSApple OSS Distributions 	 */
321*c54f35caSApple OSS Distributions 	if (length >= msg_ool_size_small && newCapacity < capacity) {
322*c54f35caSApple OSS Distributions 		kr = krealloc_ext((void *)KHEAP_DATA_BUFFERS,
323*c54f35caSApple OSS Distributions 		    data, capacity, newCapacity,
324*c54f35caSApple OSS Distributions 		    Z_VM_TAG_BT(Z_WAITOK_ZERO | Z_FULLSIZE | Z_MAY_COPYINMAP,
325*c54f35caSApple OSS Distributions 		    VM_KERN_MEMORY_LIBKERN), (void *)&this->data);
326*c54f35caSApple OSS Distributions 		assert(kr.addr == data);
327*c54f35caSApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(((size_t)newCapacity) - ((size_t)capacity));
328*c54f35caSApple OSS Distributions 		capacity = newCapacity;
329*c54f35caSApple OSS Distributions 	}
330*c54f35caSApple OSS Distributions 	return true;
331*c54f35caSApple OSS Distributions }
332*c54f35caSApple OSS Distributions 
333*c54f35caSApple OSS Distributions bool
appendBytes(const void * bytes,unsigned int inLength)334*c54f35caSApple OSS Distributions OSData::appendBytes(const void *bytes, unsigned int inLength)
335*c54f35caSApple OSS Distributions {
336*c54f35caSApple OSS Distributions 	unsigned int newSize;
337*c54f35caSApple OSS Distributions 
338*c54f35caSApple OSS Distributions 	if (!inLength) {
339*c54f35caSApple OSS Distributions 		return true;
340*c54f35caSApple OSS Distributions 	}
341*c54f35caSApple OSS Distributions 
342*c54f35caSApple OSS Distributions 	if (capacity == EXTERNAL) {
343*c54f35caSApple OSS Distributions 		return false;
344*c54f35caSApple OSS Distributions 	}
345*c54f35caSApple OSS Distributions 
346*c54f35caSApple OSS Distributions 	if (os_add_overflow(length, inLength, &newSize)) {
347*c54f35caSApple OSS Distributions 		return false;
348*c54f35caSApple OSS Distributions 	}
349*c54f35caSApple OSS Distributions 
350*c54f35caSApple OSS Distributions 	if ((newSize > capacity) && newSize > ensureCapacity(newSize)) {
351*c54f35caSApple OSS Distributions 		return false;
352*c54f35caSApple OSS Distributions 	}
353*c54f35caSApple OSS Distributions 
354*c54f35caSApple OSS Distributions 	if (bytes) {
355*c54f35caSApple OSS Distributions 		bcopy(bytes, &((unsigned char *)data)[length], inLength);
356*c54f35caSApple OSS Distributions 	} else {
357*c54f35caSApple OSS Distributions 		bzero(&((unsigned char *)data)[length], inLength);
358*c54f35caSApple OSS Distributions 	}
359*c54f35caSApple OSS Distributions 
360*c54f35caSApple OSS Distributions 	length = newSize;
361*c54f35caSApple OSS Distributions 
362*c54f35caSApple OSS Distributions 	return true;
363*c54f35caSApple OSS Distributions }
364*c54f35caSApple OSS Distributions 
365*c54f35caSApple OSS Distributions bool
appendByte(unsigned char byte,unsigned int inLength)366*c54f35caSApple OSS Distributions OSData::appendByte(unsigned char byte, unsigned int inLength)
367*c54f35caSApple OSS Distributions {
368*c54f35caSApple OSS Distributions 	unsigned int newSize;
369*c54f35caSApple OSS Distributions 
370*c54f35caSApple OSS Distributions 	if (!inLength) {
371*c54f35caSApple OSS Distributions 		return true;
372*c54f35caSApple OSS Distributions 	}
373*c54f35caSApple OSS Distributions 
374*c54f35caSApple OSS Distributions 	if (capacity == EXTERNAL) {
375*c54f35caSApple OSS Distributions 		return false;
376*c54f35caSApple OSS Distributions 	}
377*c54f35caSApple OSS Distributions 
378*c54f35caSApple OSS Distributions 	if (os_add_overflow(length, inLength, &newSize)) {
379*c54f35caSApple OSS Distributions 		return false;
380*c54f35caSApple OSS Distributions 	}
381*c54f35caSApple OSS Distributions 
382*c54f35caSApple OSS Distributions 	if ((newSize > capacity) && newSize > ensureCapacity(newSize)) {
383*c54f35caSApple OSS Distributions 		return false;
384*c54f35caSApple OSS Distributions 	}
385*c54f35caSApple OSS Distributions 
386*c54f35caSApple OSS Distributions 	memset(&((unsigned char *)data)[length], byte, inLength);
387*c54f35caSApple OSS Distributions 	length = newSize;
388*c54f35caSApple OSS Distributions 
389*c54f35caSApple OSS Distributions 	return true;
390*c54f35caSApple OSS Distributions }
391*c54f35caSApple OSS Distributions 
392*c54f35caSApple OSS Distributions bool
appendBytes(const OSData * other)393*c54f35caSApple OSS Distributions OSData::appendBytes(const OSData *other)
394*c54f35caSApple OSS Distributions {
395*c54f35caSApple OSS Distributions 	return appendBytes(other->data, other->length);
396*c54f35caSApple OSS Distributions }
397*c54f35caSApple OSS Distributions 
398*c54f35caSApple OSS Distributions const void *
getBytesNoCopy() const399*c54f35caSApple OSS Distributions OSData::getBytesNoCopy() const
400*c54f35caSApple OSS Distributions {
401*c54f35caSApple OSS Distributions 	if (!length) {
402*c54f35caSApple OSS Distributions 		return NULL;
403*c54f35caSApple OSS Distributions 	} else {
404*c54f35caSApple OSS Distributions 		return data;
405*c54f35caSApple OSS Distributions 	}
406*c54f35caSApple OSS Distributions }
407*c54f35caSApple OSS Distributions 
408*c54f35caSApple OSS Distributions const void *
getBytesNoCopy(unsigned int start,unsigned int inLength) const409*c54f35caSApple OSS Distributions OSData::getBytesNoCopy(unsigned int start,
410*c54f35caSApple OSS Distributions     unsigned int inLength) const
411*c54f35caSApple OSS Distributions {
412*c54f35caSApple OSS Distributions 	const void *outData = NULL;
413*c54f35caSApple OSS Distributions 
414*c54f35caSApple OSS Distributions 	if (length
415*c54f35caSApple OSS Distributions 	    && start < length
416*c54f35caSApple OSS Distributions 	    && (start + inLength) >= inLength // overflow check
417*c54f35caSApple OSS Distributions 	    && (start + inLength) <= length) {
418*c54f35caSApple OSS Distributions 		outData = (const void *) ((char *) data + start);
419*c54f35caSApple OSS Distributions 	}
420*c54f35caSApple OSS Distributions 
421*c54f35caSApple OSS Distributions 	return outData;
422*c54f35caSApple OSS Distributions }
423*c54f35caSApple OSS Distributions 
424*c54f35caSApple OSS Distributions bool
isEqualTo(const OSData * aData) const425*c54f35caSApple OSS Distributions OSData::isEqualTo(const OSData *aData) const
426*c54f35caSApple OSS Distributions {
427*c54f35caSApple OSS Distributions 	unsigned int len;
428*c54f35caSApple OSS Distributions 
429*c54f35caSApple OSS Distributions 	len = aData->length;
430*c54f35caSApple OSS Distributions 	if (length != len) {
431*c54f35caSApple OSS Distributions 		return false;
432*c54f35caSApple OSS Distributions 	}
433*c54f35caSApple OSS Distributions 
434*c54f35caSApple OSS Distributions 	return isEqualTo(aData->data, len);
435*c54f35caSApple OSS Distributions }
436*c54f35caSApple OSS Distributions 
437*c54f35caSApple OSS Distributions bool
isEqualTo(const void * someData,unsigned int inLength) const438*c54f35caSApple OSS Distributions OSData::isEqualTo(const void *someData, unsigned int inLength) const
439*c54f35caSApple OSS Distributions {
440*c54f35caSApple OSS Distributions 	return (length >= inLength) && (bcmp(data, someData, inLength) == 0);
441*c54f35caSApple OSS Distributions }
442*c54f35caSApple OSS Distributions 
443*c54f35caSApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * obj) const444*c54f35caSApple OSS Distributions OSData::isEqualTo(const OSMetaClassBase *obj) const
445*c54f35caSApple OSS Distributions {
446*c54f35caSApple OSS Distributions 	OSData *    otherData;
447*c54f35caSApple OSS Distributions 	OSString *  str;
448*c54f35caSApple OSS Distributions 
449*c54f35caSApple OSS Distributions 	if ((otherData = OSDynamicCast(OSData, obj))) {
450*c54f35caSApple OSS Distributions 		return isEqualTo(otherData);
451*c54f35caSApple OSS Distributions 	} else if ((str = OSDynamicCast(OSString, obj))) {
452*c54f35caSApple OSS Distributions 		return isEqualTo(str);
453*c54f35caSApple OSS Distributions 	} else {
454*c54f35caSApple OSS Distributions 		return false;
455*c54f35caSApple OSS Distributions 	}
456*c54f35caSApple OSS Distributions }
457*c54f35caSApple OSS Distributions 
458*c54f35caSApple OSS Distributions bool
isEqualTo(const OSString * obj) const459*c54f35caSApple OSS Distributions OSData::isEqualTo(const OSString *obj) const
460*c54f35caSApple OSS Distributions {
461*c54f35caSApple OSS Distributions 	const char * aCString;
462*c54f35caSApple OSS Distributions 	char * dataPtr;
463*c54f35caSApple OSS Distributions 	unsigned int checkLen = length;
464*c54f35caSApple OSS Distributions 	unsigned int stringLen;
465*c54f35caSApple OSS Distributions 
466*c54f35caSApple OSS Distributions 	if (!obj) {
467*c54f35caSApple OSS Distributions 		return false;
468*c54f35caSApple OSS Distributions 	}
469*c54f35caSApple OSS Distributions 
470*c54f35caSApple OSS Distributions 	stringLen = obj->getLength();
471*c54f35caSApple OSS Distributions 
472*c54f35caSApple OSS Distributions 	dataPtr = (char *)data;
473*c54f35caSApple OSS Distributions 
474*c54f35caSApple OSS Distributions 	if (stringLen != checkLen) {
475*c54f35caSApple OSS Distributions 		// check for the fact that OSData may be a buffer that
476*c54f35caSApple OSS Distributions 		// that includes a termination byte and will thus have
477*c54f35caSApple OSS Distributions 		// a length of the actual string length PLUS 1. In this
478*c54f35caSApple OSS Distributions 		// case we verify that the additional byte is a terminator
479*c54f35caSApple OSS Distributions 		// and if so count the two lengths as being the same.
480*c54f35caSApple OSS Distributions 
481*c54f35caSApple OSS Distributions 		if ((checkLen - stringLen) == 1) {
482*c54f35caSApple OSS Distributions 			if (dataPtr[checkLen - 1] != 0) { // non-zero means not a terminator and thus not likely the same
483*c54f35caSApple OSS Distributions 				return false;
484*c54f35caSApple OSS Distributions 			}
485*c54f35caSApple OSS Distributions 			checkLen--;
486*c54f35caSApple OSS Distributions 		} else {
487*c54f35caSApple OSS Distributions 			return false;
488*c54f35caSApple OSS Distributions 		}
489*c54f35caSApple OSS Distributions 	}
490*c54f35caSApple OSS Distributions 
491*c54f35caSApple OSS Distributions 	aCString = obj->getCStringNoCopy();
492*c54f35caSApple OSS Distributions 
493*c54f35caSApple OSS Distributions 	for (unsigned int i = 0; i < checkLen; i++) {
494*c54f35caSApple OSS Distributions 		if (*dataPtr++ != aCString[i]) {
495*c54f35caSApple OSS Distributions 			return false;
496*c54f35caSApple OSS Distributions 		}
497*c54f35caSApple OSS Distributions 	}
498*c54f35caSApple OSS Distributions 
499*c54f35caSApple OSS Distributions 	return true;
500*c54f35caSApple OSS Distributions }
501*c54f35caSApple OSS Distributions 
502*c54f35caSApple OSS Distributions //this was taken from CFPropertyList.c
503*c54f35caSApple OSS Distributions static const char __CFPLDataEncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
504*c54f35caSApple OSS Distributions 
505*c54f35caSApple OSS Distributions bool
serialize(OSSerialize * s) const506*c54f35caSApple OSS Distributions OSData::serialize(OSSerialize *s) const
507*c54f35caSApple OSS Distributions {
508*c54f35caSApple OSS Distributions 	unsigned int i;
509*c54f35caSApple OSS Distributions 	const unsigned char *p;
510*c54f35caSApple OSS Distributions 	unsigned char c;
511*c54f35caSApple OSS Distributions 	unsigned int serializeLength;
512*c54f35caSApple OSS Distributions 
513*c54f35caSApple OSS Distributions 	if (s->previouslySerialized(this)) {
514*c54f35caSApple OSS Distributions 		return true;
515*c54f35caSApple OSS Distributions 	}
516*c54f35caSApple OSS Distributions 
517*c54f35caSApple OSS Distributions 	if (!s->addXMLStartTag(this, "data")) {
518*c54f35caSApple OSS Distributions 		return false;
519*c54f35caSApple OSS Distributions 	}
520*c54f35caSApple OSS Distributions 
521*c54f35caSApple OSS Distributions 	serializeLength = length;
522*c54f35caSApple OSS Distributions 	if (reserved && reserved->disableSerialization) {
523*c54f35caSApple OSS Distributions 		serializeLength = 0;
524*c54f35caSApple OSS Distributions 	}
525*c54f35caSApple OSS Distributions 
526*c54f35caSApple OSS Distributions 	for (i = 0, p = (unsigned char *)data; i < serializeLength; i++, p++) {
527*c54f35caSApple OSS Distributions 		/* 3 bytes are encoded as 4 */
528*c54f35caSApple OSS Distributions 		switch (i % 3) {
529*c54f35caSApple OSS Distributions 		case 0:
530*c54f35caSApple OSS Distributions 			c = __CFPLDataEncodeTable[((p[0] >> 2) & 0x3f)];
531*c54f35caSApple OSS Distributions 			if (!s->addChar(c)) {
532*c54f35caSApple OSS Distributions 				return false;
533*c54f35caSApple OSS Distributions 			}
534*c54f35caSApple OSS Distributions 			break;
535*c54f35caSApple OSS Distributions 		case 1:
536*c54f35caSApple OSS Distributions 			c = __CFPLDataEncodeTable[((((p[-1] << 8) | p[0]) >> 4) & 0x3f)];
537*c54f35caSApple OSS Distributions 			if (!s->addChar(c)) {
538*c54f35caSApple OSS Distributions 				return false;
539*c54f35caSApple OSS Distributions 			}
540*c54f35caSApple OSS Distributions 			break;
541*c54f35caSApple OSS Distributions 		case 2:
542*c54f35caSApple OSS Distributions 			c = __CFPLDataEncodeTable[((((p[-1] << 8) | p[0]) >> 6) & 0x3f)];
543*c54f35caSApple OSS Distributions 			if (!s->addChar(c)) {
544*c54f35caSApple OSS Distributions 				return false;
545*c54f35caSApple OSS Distributions 			}
546*c54f35caSApple OSS Distributions 			c = __CFPLDataEncodeTable[(p[0] & 0x3f)];
547*c54f35caSApple OSS Distributions 			if (!s->addChar(c)) {
548*c54f35caSApple OSS Distributions 				return false;
549*c54f35caSApple OSS Distributions 			}
550*c54f35caSApple OSS Distributions 			break;
551*c54f35caSApple OSS Distributions 		}
552*c54f35caSApple OSS Distributions 	}
553*c54f35caSApple OSS Distributions 	switch (i % 3) {
554*c54f35caSApple OSS Distributions 	case 0:
555*c54f35caSApple OSS Distributions 		break;
556*c54f35caSApple OSS Distributions 	case 1:
557*c54f35caSApple OSS Distributions 		c = __CFPLDataEncodeTable[((p[-1] << 4) & 0x30)];
558*c54f35caSApple OSS Distributions 		if (!s->addChar(c)) {
559*c54f35caSApple OSS Distributions 			return false;
560*c54f35caSApple OSS Distributions 		}
561*c54f35caSApple OSS Distributions 		if (!s->addChar('=')) {
562*c54f35caSApple OSS Distributions 			return false;
563*c54f35caSApple OSS Distributions 		}
564*c54f35caSApple OSS Distributions 		if (!s->addChar('=')) {
565*c54f35caSApple OSS Distributions 			return false;
566*c54f35caSApple OSS Distributions 		}
567*c54f35caSApple OSS Distributions 		break;
568*c54f35caSApple OSS Distributions 	case 2:
569*c54f35caSApple OSS Distributions 		c = __CFPLDataEncodeTable[((p[-1] << 2) & 0x3c)];
570*c54f35caSApple OSS Distributions 		if (!s->addChar(c)) {
571*c54f35caSApple OSS Distributions 			return false;
572*c54f35caSApple OSS Distributions 		}
573*c54f35caSApple OSS Distributions 		if (!s->addChar('=')) {
574*c54f35caSApple OSS Distributions 			return false;
575*c54f35caSApple OSS Distributions 		}
576*c54f35caSApple OSS Distributions 		break;
577*c54f35caSApple OSS Distributions 	}
578*c54f35caSApple OSS Distributions 
579*c54f35caSApple OSS Distributions 	return s->addXMLEndTag("data");
580*c54f35caSApple OSS Distributions }
581*c54f35caSApple OSS Distributions 
582*c54f35caSApple OSS Distributions void
setDeallocFunction(DeallocFunction func)583*c54f35caSApple OSS Distributions OSData::setDeallocFunction(DeallocFunction func)
584*c54f35caSApple OSS Distributions {
585*c54f35caSApple OSS Distributions 	if (!reserved) {
586*c54f35caSApple OSS Distributions 		reserved = (typeof(reserved))kalloc_type(ExpansionData, (zalloc_flags_t)(Z_WAITOK | Z_ZERO));
587*c54f35caSApple OSS Distributions 		if (!reserved) {
588*c54f35caSApple OSS Distributions 			return;
589*c54f35caSApple OSS Distributions 		}
590*c54f35caSApple OSS Distributions 	}
591*c54f35caSApple OSS Distributions 	reserved->deallocFunction = func;
592*c54f35caSApple OSS Distributions }
593*c54f35caSApple OSS Distributions 
594*c54f35caSApple OSS Distributions void
setSerializable(bool serializable)595*c54f35caSApple OSS Distributions OSData::setSerializable(bool serializable)
596*c54f35caSApple OSS Distributions {
597*c54f35caSApple OSS Distributions 	if (!reserved) {
598*c54f35caSApple OSS Distributions 		reserved = (typeof(reserved))kalloc_type(ExpansionData, (zalloc_flags_t)(Z_WAITOK | Z_ZERO));
599*c54f35caSApple OSS Distributions 		if (!reserved) {
600*c54f35caSApple OSS Distributions 			return;
601*c54f35caSApple OSS Distributions 		}
602*c54f35caSApple OSS Distributions 	}
603*c54f35caSApple OSS Distributions 	reserved->disableSerialization = (!serializable);
604*c54f35caSApple OSS Distributions }
605*c54f35caSApple OSS Distributions 
606*c54f35caSApple OSS Distributions bool
isSerializable(void)607*c54f35caSApple OSS Distributions OSData::isSerializable(void)
608*c54f35caSApple OSS Distributions {
609*c54f35caSApple OSS Distributions 	return !reserved || !reserved->disableSerialization;
610*c54f35caSApple OSS Distributions }
611