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