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