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