1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5 | * |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License |
8 | * Version 2.0 (the 'License'). You may not use this file except in |
9 | * compliance with the License. The rights granted to you under the License |
10 | * may not be used to create, or enable the creation or redistribution of, |
11 | * unlawful or unlicensed copies of an Apple operating system, or to |
12 | * circumvent, violate, or enable the circumvention or violation of, any |
13 | * terms of an Apple operating system software license agreement. |
14 | * |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
17 | * |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and |
24 | * limitations under the License. |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ |
28 | /* IOData.m created by rsulack on Thu 25-Sep-1997 */ |
29 | |
30 | #include <string.h> |
31 | |
32 | __BEGIN_DECLS |
33 | #include <vm/vm_kern.h> |
34 | __END_DECLS |
35 | |
36 | #include <libkern/c++/OSData.h> |
37 | #include <libkern/c++/OSSerialize.h> |
38 | #include <libkern/c++/OSLib.h> |
39 | #include <libkern/c++/OSString.h> |
40 | #include <IOKit/IOLib.h> |
41 | |
42 | #define super OSObject |
43 | |
44 | OSDefineMetaClassAndStructors(OSData, OSObject) |
45 | OSMetaClassDefineReservedUsed(OSData, 0); // setDeallocFunction |
46 | OSMetaClassDefineReservedUnused(OSData, 1); |
47 | OSMetaClassDefineReservedUnused(OSData, 2); |
48 | OSMetaClassDefineReservedUnused(OSData, 3); |
49 | OSMetaClassDefineReservedUnused(OSData, 4); |
50 | OSMetaClassDefineReservedUnused(OSData, 5); |
51 | OSMetaClassDefineReservedUnused(OSData, 6); |
52 | OSMetaClassDefineReservedUnused(OSData, 7); |
53 | |
54 | #define EXTERNAL ((unsigned int) -1) |
55 | |
56 | bool OSData::initWithCapacity(unsigned int inCapacity) |
57 | { |
58 | if (data) |
59 | { |
60 | OSCONTAINER_ACCUMSIZE(-((size_t)capacity)); |
61 | if (!inCapacity || (capacity < inCapacity)) |
62 | { |
63 | // clean out old data's storage if it isn't big enough |
64 | if (capacity < page_size) kfree(data, capacity); |
65 | else kmem_free(kernel_map, (vm_offset_t)data, capacity); |
66 | data = 0; |
67 | capacity = 0; |
68 | } |
69 | } |
70 | |
71 | if (!super::init()) |
72 | return false; |
73 | |
74 | if (inCapacity && !data) { |
75 | |
76 | if (inCapacity < page_size) data = (void *) kalloc_container(inCapacity); |
77 | else { |
78 | kern_return_t kr; |
79 | if (round_page_overflow(inCapacity, &inCapacity)) kr = KERN_RESOURCE_SHORTAGE; |
80 | else kr = kmem_alloc(kernel_map, (vm_offset_t *)&data, inCapacity, IOMemoryTag(kernel_map)); |
81 | if (KERN_SUCCESS != kr) data = NULL; |
82 | } |
83 | if (!data) |
84 | return false; |
85 | capacity = inCapacity; |
86 | } |
87 | OSCONTAINER_ACCUMSIZE(capacity); |
88 | |
89 | length = 0; |
90 | if (inCapacity < 16) |
91 | capacityIncrement = 16; |
92 | else |
93 | capacityIncrement = inCapacity; |
94 | |
95 | return true; |
96 | } |
97 | |
98 | bool OSData::initWithBytes(const void *bytes, unsigned int inLength) |
99 | { |
100 | if ((inLength && !bytes) || !initWithCapacity(inLength)) |
101 | return false; |
102 | |
103 | if (bytes != data) |
104 | bcopy(bytes, data, inLength); |
105 | length = inLength; |
106 | |
107 | return true; |
108 | } |
109 | |
110 | bool OSData::initWithBytesNoCopy(void *bytes, unsigned int inLength) |
111 | { |
112 | if (!super::init()) |
113 | return false; |
114 | |
115 | length = inLength; |
116 | capacity = EXTERNAL; |
117 | data = bytes; |
118 | |
119 | return true; |
120 | } |
121 | |
122 | bool OSData::initWithData(const OSData *inData) |
123 | { |
124 | return initWithBytes(inData->data, inData->length); |
125 | } |
126 | |
127 | bool OSData::initWithData(const OSData *inData, |
128 | unsigned int start, unsigned int inLength) |
129 | { |
130 | const void *localData = inData->getBytesNoCopy(start, inLength); |
131 | |
132 | if (localData) |
133 | return initWithBytes(localData, inLength); |
134 | else |
135 | return false; |
136 | } |
137 | |
138 | OSData *OSData::withCapacity(unsigned int inCapacity) |
139 | { |
140 | OSData *me = new OSData; |
141 | |
142 | if (me && !me->initWithCapacity(inCapacity)) { |
143 | me->release(); |
144 | return 0; |
145 | } |
146 | |
147 | return me; |
148 | } |
149 | |
150 | OSData *OSData::withBytes(const void *bytes, unsigned int inLength) |
151 | { |
152 | OSData *me = new OSData; |
153 | |
154 | if (me && !me->initWithBytes(bytes, inLength)) { |
155 | me->release(); |
156 | return 0; |
157 | } |
158 | return me; |
159 | } |
160 | |
161 | OSData *OSData::withBytesNoCopy(void *bytes, unsigned int inLength) |
162 | { |
163 | OSData *me = new OSData; |
164 | |
165 | if (me && !me->initWithBytesNoCopy(bytes, inLength)) { |
166 | me->release(); |
167 | return 0; |
168 | } |
169 | |
170 | return me; |
171 | } |
172 | |
173 | OSData *OSData::withData(const OSData *inData) |
174 | { |
175 | OSData *me = new OSData; |
176 | |
177 | if (me && !me->initWithData(inData)) { |
178 | me->release(); |
179 | return 0; |
180 | } |
181 | |
182 | return me; |
183 | } |
184 | |
185 | OSData *OSData::withData(const OSData *inData, |
186 | unsigned int start, unsigned int inLength) |
187 | { |
188 | OSData *me = new OSData; |
189 | |
190 | if (me && !me->initWithData(inData, start, inLength)) { |
191 | me->release(); |
192 | return 0; |
193 | } |
194 | |
195 | return me; |
196 | } |
197 | |
198 | void OSData::free() |
199 | { |
200 | if ((capacity != EXTERNAL) && data && capacity) { |
201 | if (capacity < page_size) kfree(data, capacity); |
202 | else kmem_free(kernel_map, (vm_offset_t)data, capacity); |
203 | OSCONTAINER_ACCUMSIZE( -((size_t)capacity) ); |
204 | } else if (capacity == EXTERNAL) { |
205 | DeallocFunction freemem = reserved ? reserved->deallocFunction : NULL; |
206 | if (freemem && data && length) { |
207 | freemem(data, length); |
208 | } |
209 | } |
210 | if (reserved) kfree(reserved, sizeof(ExpansionData)); |
211 | super::free(); |
212 | } |
213 | |
214 | unsigned int OSData::getLength() const { return length; } |
215 | unsigned int OSData::getCapacity() const { return capacity; } |
216 | |
217 | unsigned int OSData::getCapacityIncrement() const |
218 | { |
219 | return capacityIncrement; |
220 | } |
221 | |
222 | unsigned int OSData::setCapacityIncrement(unsigned increment) |
223 | { |
224 | return capacityIncrement = increment; |
225 | } |
226 | |
227 | // xx-review: does not check for capacity == EXTERNAL |
228 | |
229 | unsigned int OSData::ensureCapacity(unsigned int newCapacity) |
230 | { |
231 | unsigned char * newData; |
232 | unsigned int finalCapacity; |
233 | void * copydata; |
234 | kern_return_t kr; |
235 | |
236 | if (newCapacity <= capacity) |
237 | return capacity; |
238 | |
239 | finalCapacity = (((newCapacity - 1) / capacityIncrement) + 1) |
240 | * capacityIncrement; |
241 | |
242 | // integer overflow check |
243 | if (finalCapacity < newCapacity) return capacity; |
244 | |
245 | copydata = data; |
246 | |
247 | if (finalCapacity >= page_size) { |
248 | // round up |
249 | finalCapacity = round_page_32(finalCapacity); |
250 | // integer overflow check |
251 | if (finalCapacity < newCapacity) return capacity; |
252 | if (capacity >= page_size) { |
253 | copydata = NULL; |
254 | kr = kmem_realloc(kernel_map, |
255 | (vm_offset_t)data, |
256 | capacity, |
257 | (vm_offset_t *)&newData, |
258 | finalCapacity, |
259 | IOMemoryTag(kernel_map)); |
260 | } else { |
261 | kr = kmem_alloc(kernel_map, (vm_offset_t *)&newData, finalCapacity, IOMemoryTag(kernel_map)); |
262 | } |
263 | if (KERN_SUCCESS != kr) newData = NULL; |
264 | } |
265 | else newData = (unsigned char *) kalloc_container(finalCapacity); |
266 | |
267 | if ( newData ) { |
268 | bzero(newData + capacity, finalCapacity - capacity); |
269 | if (copydata) bcopy(copydata, newData, capacity); |
270 | if (data) { |
271 | if (capacity < page_size) kfree(data, capacity); |
272 | else kmem_free(kernel_map, (vm_offset_t)data, capacity); |
273 | } |
274 | OSCONTAINER_ACCUMSIZE( ((size_t)finalCapacity) - ((size_t)capacity) ); |
275 | data = (void *) newData; |
276 | capacity = finalCapacity; |
277 | } |
278 | |
279 | return capacity; |
280 | } |
281 | |
282 | bool OSData::appendBytes(const void *bytes, unsigned int inLength) |
283 | { |
284 | unsigned int newSize; |
285 | |
286 | if (!inLength) |
287 | return true; |
288 | |
289 | if (capacity == EXTERNAL) |
290 | return false; |
291 | |
292 | if (os_add_overflow(length, inLength, &newSize)) |
293 | return false; |
294 | |
295 | if ( (newSize > capacity) && newSize > ensureCapacity(newSize) ) |
296 | return false; |
297 | |
298 | if (bytes) |
299 | bcopy(bytes, &((unsigned char *)data)[length], inLength); |
300 | else |
301 | bzero(&((unsigned char *)data)[length], inLength); |
302 | |
303 | length = newSize; |
304 | |
305 | return true; |
306 | } |
307 | |
308 | bool OSData::appendByte(unsigned char byte, unsigned int inLength) |
309 | { |
310 | unsigned int newSize; |
311 | |
312 | if (!inLength) |
313 | return true; |
314 | |
315 | if (capacity == EXTERNAL) |
316 | return false; |
317 | |
318 | if (os_add_overflow(length, inLength, &newSize)) |
319 | return false; |
320 | |
321 | if ( (newSize > capacity) && newSize > ensureCapacity(newSize) ) |
322 | return false; |
323 | |
324 | memset(&((unsigned char *)data)[length], byte, inLength); |
325 | length = newSize; |
326 | |
327 | return true; |
328 | } |
329 | |
330 | bool OSData::appendBytes(const OSData *other) |
331 | { |
332 | return appendBytes(other->data, other->length); |
333 | } |
334 | |
335 | const void *OSData::getBytesNoCopy() const |
336 | { |
337 | if (!length) |
338 | return 0; |
339 | else |
340 | return data; |
341 | } |
342 | |
343 | const void *OSData::getBytesNoCopy(unsigned int start, |
344 | unsigned int inLength) const |
345 | { |
346 | const void *outData = 0; |
347 | |
348 | if (length |
349 | && start < length |
350 | && (start + inLength) >= inLength // overflow check |
351 | && (start + inLength) <= length) |
352 | outData = (const void *) ((char *) data + start); |
353 | |
354 | return outData; |
355 | } |
356 | |
357 | bool OSData::isEqualTo(const OSData *aData) const |
358 | { |
359 | unsigned int len; |
360 | |
361 | len = aData->length; |
362 | if ( length != len ) |
363 | return false; |
364 | |
365 | return isEqualTo(aData->data, len); |
366 | } |
367 | |
368 | bool OSData::isEqualTo(const void *someData, unsigned int inLength) const |
369 | { |
370 | return (length >= inLength) && (bcmp(data, someData, inLength) == 0); |
371 | } |
372 | |
373 | bool OSData::isEqualTo(const OSMetaClassBase *obj) const |
374 | { |
375 | OSData * otherData; |
376 | OSString * str; |
377 | |
378 | if ((otherData = OSDynamicCast(OSData, obj))) |
379 | return isEqualTo(otherData); |
380 | else if ((str = OSDynamicCast (OSString, obj))) |
381 | return isEqualTo(str); |
382 | else |
383 | return false; |
384 | } |
385 | |
386 | bool OSData::isEqualTo(const OSString *obj) const |
387 | { |
388 | const char * aCString; |
389 | char * dataPtr; |
390 | unsigned int checkLen = length; |
391 | unsigned int stringLen; |
392 | |
393 | if (!obj) |
394 | return false; |
395 | |
396 | stringLen = obj->getLength (); |
397 | |
398 | dataPtr = (char *)data; |
399 | |
400 | if (stringLen != checkLen) { |
401 | |
402 | // check for the fact that OSData may be a buffer that |
403 | // that includes a termination byte and will thus have |
404 | // a length of the actual string length PLUS 1. In this |
405 | // case we verify that the additional byte is a terminator |
406 | // and if so count the two lengths as being the same. |
407 | |
408 | if ( (checkLen - stringLen) == 1) { |
409 | if (dataPtr[checkLen-1] != 0) // non-zero means not a terminator and thus not likely the same |
410 | return false; |
411 | checkLen--; |
412 | } |
413 | else |
414 | return false; |
415 | } |
416 | |
417 | aCString = obj->getCStringNoCopy (); |
418 | |
419 | for ( unsigned int i=0; i < checkLen; i++ ) { |
420 | if ( *dataPtr++ != aCString[i] ) |
421 | return false; |
422 | } |
423 | |
424 | return true; |
425 | } |
426 | |
427 | //this was taken from CFPropertyList.c |
428 | static const char __CFPLDataEncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ; |
429 | |
430 | bool OSData::serialize(OSSerialize *s) const |
431 | { |
432 | unsigned int i; |
433 | const unsigned char *p; |
434 | unsigned char c; |
435 | unsigned int serializeLength; |
436 | |
437 | if (s->previouslySerialized(this)) return true; |
438 | |
439 | if (!s->addXMLStartTag(this, "data" )) return false; |
440 | |
441 | serializeLength = length; |
442 | if (reserved && reserved->disableSerialization) serializeLength = 0; |
443 | |
444 | for (i = 0, p = (unsigned char *)data; i < serializeLength; i++, p++) { |
445 | /* 3 bytes are encoded as 4 */ |
446 | switch (i % 3) { |
447 | case 0: |
448 | c = __CFPLDataEncodeTable [ ((p[0] >> 2) & 0x3f)]; |
449 | if (!s->addChar(c)) return false; |
450 | break; |
451 | case 1: |
452 | c = __CFPLDataEncodeTable [ ((((p[-1] << 8) | p[0]) >> 4) & 0x3f)]; |
453 | if (!s->addChar(c)) return false; |
454 | break; |
455 | case 2: |
456 | c = __CFPLDataEncodeTable [ ((((p[-1] << 8) | p[0]) >> 6) & 0x3f)]; |
457 | if (!s->addChar(c)) return false; |
458 | c = __CFPLDataEncodeTable [ (p[0] & 0x3f)]; |
459 | if (!s->addChar(c)) return false; |
460 | break; |
461 | } |
462 | } |
463 | switch (i % 3) { |
464 | case 0: |
465 | break; |
466 | case 1: |
467 | c = __CFPLDataEncodeTable [ ((p[-1] << 4) & 0x30)]; |
468 | if (!s->addChar(c)) return false; |
469 | if (!s->addChar('=')) return false; |
470 | if (!s->addChar('=')) return false; |
471 | break; |
472 | case 2: |
473 | c = __CFPLDataEncodeTable [ ((p[-1] << 2) & 0x3c)]; |
474 | if (!s->addChar(c)) return false; |
475 | if (!s->addChar('=')) return false; |
476 | break; |
477 | } |
478 | |
479 | return s->addXMLEndTag("data" ); |
480 | } |
481 | |
482 | void OSData::setDeallocFunction(DeallocFunction func) |
483 | { |
484 | if (!reserved) |
485 | { |
486 | reserved = (typeof(reserved)) kalloc_container(sizeof(ExpansionData)); |
487 | if (!reserved) return; |
488 | bzero(reserved, sizeof(ExpansionData)); |
489 | } |
490 | reserved->deallocFunction = func; |
491 | } |
492 | |
493 | void OSData::setSerializable(bool serializable) |
494 | { |
495 | if (!reserved) |
496 | { |
497 | reserved = (typeof(reserved)) kalloc_container(sizeof(ExpansionData)); |
498 | if (!reserved) return; |
499 | bzero(reserved, sizeof(ExpansionData)); |
500 | } |
501 | reserved->disableSerialization = (!serializable); |
502 | } |
503 | |
504 | bool OSData::isSerializable(void) |
505 | { |
506 | return (!reserved || !reserved->disableSerialization); |
507 | } |
508 | |