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