]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* IOData.m created by rsulack on Thu 25-Sep-1997 */ | |
29 | ||
9bccf70c | 30 | #include <string.h> |
1c79356b | 31 | |
39037602 A |
32 | __BEGIN_DECLS |
33 | #include <vm/vm_kern.h> | |
34 | __END_DECLS | |
35 | ||
1c79356b A |
36 | #include <libkern/c++/OSData.h> |
37 | #include <libkern/c++/OSSerialize.h> | |
38 | #include <libkern/c++/OSLib.h> | |
39 | #include <libkern/c++/OSString.h> | |
39037602 | 40 | #include <IOKit/IOLib.h> |
1c79356b A |
41 | |
42 | #define super OSObject | |
43 | ||
44 | OSDefineMetaClassAndStructors(OSData, OSObject) | |
b0d623f7 | 45 | OSMetaClassDefineReservedUsed(OSData, 0); // setDeallocFunction |
1c79356b A |
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 | ||
1c79356b A |
56 | bool OSData::initWithCapacity(unsigned int inCapacity) |
57 | { | |
3e170ce0 A |
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 | |
39037602 A |
64 | if (capacity < page_size) kfree(data, capacity); |
65 | else kmem_free(kernel_map, (vm_offset_t)data, capacity); | |
3e170ce0 A |
66 | data = 0; |
67 | capacity = 0; | |
68 | } | |
69 | } | |
70 | ||
1c79356b A |
71 | if (!super::init()) |
72 | return false; | |
73 | ||
55e303ae | 74 | if (inCapacity && !data) { |
39037602 A |
75 | |
76 | if (inCapacity < page_size) data = (void *) kalloc_container(inCapacity); | |
77 | else { | |
78 | kern_return_t kr; | |
79 | inCapacity = round_page_32(inCapacity); | |
80 | kr = kmem_alloc(kernel_map, (vm_offset_t *)&data, inCapacity, IOMemoryTag(kernel_map)); | |
81 | if (KERN_SUCCESS != kr) data = NULL; | |
82 | } | |
1c79356b A |
83 | if (!data) |
84 | return false; | |
55e303ae | 85 | capacity = inCapacity; |
1c79356b | 86 | } |
3e170ce0 | 87 | OSCONTAINER_ACCUMSIZE(capacity); |
1c79356b A |
88 | |
89 | length = 0; | |
55e303ae | 90 | if (inCapacity < 16) |
1c79356b | 91 | capacityIncrement = 16; |
55e303ae A |
92 | else |
93 | capacityIncrement = inCapacity; | |
1c79356b A |
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 | ||
55e303ae A |
103 | if (bytes != data) |
104 | bcopy(bytes, data, inLength); | |
1c79356b A |
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)) { | |
55e303ae | 143 | me->release(); |
1c79356b A |
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)) { | |
55e303ae | 155 | me->release(); |
1c79356b A |
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)) { | |
55e303ae | 166 | me->release(); |
1c79356b A |
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)) { | |
55e303ae | 178 | me->release(); |
1c79356b A |
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)) { | |
55e303ae | 191 | me->release(); |
1c79356b A |
192 | return 0; |
193 | } | |
194 | ||
195 | return me; | |
196 | } | |
197 | ||
198 | void OSData::free() | |
199 | { | |
39037602 A |
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); | |
3e170ce0 | 203 | OSCONTAINER_ACCUMSIZE( -((size_t)capacity) ); |
b0d623f7 | 204 | } else if (capacity == EXTERNAL) { |
316670eb A |
205 | DeallocFunction freemem = reserved ? reserved->deallocFunction : NULL; |
206 | if (freemem && data && length) { | |
207 | freemem(data, length); | |
b0d623f7 | 208 | } |
316670eb A |
209 | } |
210 | if (reserved) kfree(reserved, sizeof(ExpansionData)); | |
1c79356b A |
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 | ||
b0d623f7 A |
227 | // xx-review: does not check for capacity == EXTERNAL |
228 | ||
1c79356b A |
229 | unsigned int OSData::ensureCapacity(unsigned int newCapacity) |
230 | { | |
231 | unsigned char * newData; | |
fe8ab488 | 232 | unsigned int finalCapacity; |
39037602 A |
233 | void * copydata; |
234 | kern_return_t kr; | |
1c79356b A |
235 | |
236 | if (newCapacity <= capacity) | |
237 | return capacity; | |
238 | ||
fe8ab488 | 239 | finalCapacity = (((newCapacity - 1) / capacityIncrement) + 1) |
1c79356b A |
240 | * capacityIncrement; |
241 | ||
fe8ab488 | 242 | // integer overflow check |
39037602 A |
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); | |
fe8ab488 | 266 | |
1c79356b | 267 | if ( newData ) { |
fe8ab488 | 268 | bzero(newData + capacity, finalCapacity - capacity); |
39037602 | 269 | if (copydata) bcopy(copydata, newData, capacity); |
1c79356b | 270 | if (data) { |
39037602 A |
271 | if (capacity < page_size) kfree(data, capacity); |
272 | else kmem_free(kernel_map, (vm_offset_t)data, capacity); | |
1c79356b | 273 | } |
3e170ce0 | 274 | OSCONTAINER_ACCUMSIZE( ((size_t)finalCapacity) - ((size_t)capacity) ); |
1c79356b | 275 | data = (void *) newData; |
fe8ab488 | 276 | capacity = finalCapacity; |
1c79356b A |
277 | } |
278 | ||
279 | return capacity; | |
280 | } | |
281 | ||
282 | bool OSData::appendBytes(const void *bytes, unsigned int inLength) | |
283 | { | |
284 | unsigned int newSize; | |
285 | ||
55e303ae | 286 | if (!inLength) |
1c79356b A |
287 | return true; |
288 | ||
289 | if (capacity == EXTERNAL) | |
290 | return false; | |
291 | ||
292 | newSize = length + inLength; | |
293 | if ( (newSize > capacity) && newSize > ensureCapacity(newSize) ) | |
294 | return false; | |
295 | ||
55e303ae A |
296 | if (bytes) |
297 | bcopy(bytes, &((unsigned char *)data)[length], inLength); | |
298 | else | |
299 | bzero(&((unsigned char *)data)[length], inLength); | |
300 | ||
1c79356b A |
301 | length = newSize; |
302 | ||
303 | return true; | |
304 | } | |
305 | ||
306 | bool OSData::appendByte(unsigned char byte, unsigned int inLength) | |
307 | { | |
308 | unsigned int newSize; | |
309 | ||
55e303ae | 310 | if (!inLength) |
1c79356b A |
311 | return true; |
312 | ||
313 | if (capacity == EXTERNAL) | |
314 | return false; | |
315 | ||
316 | newSize = length + inLength; | |
317 | if ( (newSize > capacity) && newSize > ensureCapacity(newSize) ) | |
318 | return false; | |
319 | ||
320 | memset(&((unsigned char *)data)[length], byte, inLength); | |
321 | length = newSize; | |
322 | ||
323 | return true; | |
324 | } | |
325 | ||
326 | bool OSData::appendBytes(const OSData *other) | |
327 | { | |
328 | return appendBytes(other->data, other->length); | |
329 | } | |
330 | ||
331 | const void *OSData::getBytesNoCopy() const | |
332 | { | |
55e303ae | 333 | if (!length) |
1c79356b A |
334 | return 0; |
335 | else | |
336 | return data; | |
337 | } | |
338 | ||
339 | const void *OSData::getBytesNoCopy(unsigned int start, | |
340 | unsigned int inLength) const | |
341 | { | |
342 | const void *outData = 0; | |
343 | ||
344 | if (length | |
345 | && start < length | |
39037602 | 346 | && (start + inLength) >= inLength // overflow check |
1c79356b A |
347 | && (start + inLength) <= length) |
348 | outData = (const void *) ((char *) data + start); | |
349 | ||
350 | return outData; | |
351 | } | |
352 | ||
353 | bool OSData::isEqualTo(const OSData *aData) const | |
354 | { | |
355 | unsigned int len; | |
356 | ||
357 | len = aData->length; | |
358 | if ( length != len ) | |
359 | return false; | |
360 | ||
361 | return isEqualTo(aData->data, len); | |
362 | } | |
363 | ||
364 | bool OSData::isEqualTo(const void *someData, unsigned int inLength) const | |
365 | { | |
366 | return (length >= inLength) && (bcmp(data, someData, inLength) == 0); | |
367 | } | |
368 | ||
369 | bool OSData::isEqualTo(const OSMetaClassBase *obj) const | |
370 | { | |
b0d623f7 | 371 | OSData * otherData; |
1c79356b A |
372 | OSString * str; |
373 | ||
b0d623f7 A |
374 | if ((otherData = OSDynamicCast(OSData, obj))) |
375 | return isEqualTo(otherData); | |
1c79356b A |
376 | else if ((str = OSDynamicCast (OSString, obj))) |
377 | return isEqualTo(str); | |
378 | else | |
379 | return false; | |
380 | } | |
381 | ||
382 | bool OSData::isEqualTo(const OSString *obj) const | |
383 | { | |
384 | const char * aCString; | |
385 | char * dataPtr; | |
386 | unsigned int checkLen = length; | |
387 | unsigned int stringLen; | |
388 | ||
55e303ae | 389 | if (!obj) |
1c79356b A |
390 | return false; |
391 | ||
392 | stringLen = obj->getLength (); | |
393 | ||
394 | dataPtr = (char *)data; | |
395 | ||
396 | if (stringLen != checkLen) { | |
397 | ||
398 | // check for the fact that OSData may be a buffer that | |
399 | // that includes a termination byte and will thus have | |
400 | // a length of the actual string length PLUS 1. In this | |
401 | // case we verify that the additional byte is a terminator | |
402 | // and if so count the two lengths as being the same. | |
403 | ||
404 | if ( (checkLen - stringLen) == 1) { | |
405 | if (dataPtr[checkLen-1] != 0) // non-zero means not a terminator and thus not likely the same | |
406 | return false; | |
407 | checkLen--; | |
408 | } | |
409 | else | |
410 | return false; | |
411 | } | |
412 | ||
413 | aCString = obj->getCStringNoCopy (); | |
414 | ||
415 | for ( unsigned int i=0; i < checkLen; i++ ) { | |
416 | if ( *dataPtr++ != aCString[i] ) | |
417 | return false; | |
418 | } | |
419 | ||
420 | return true; | |
421 | } | |
422 | ||
423 | //this was taken from CFPropertyList.c | |
424 | static const char __CFPLDataEncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
425 | ||
426 | bool OSData::serialize(OSSerialize *s) const | |
427 | { | |
428 | unsigned int i; | |
429 | const unsigned char *p; | |
430 | unsigned char c; | |
316670eb | 431 | unsigned int serializeLength; |
1c79356b A |
432 | |
433 | if (s->previouslySerialized(this)) return true; | |
434 | ||
435 | if (!s->addXMLStartTag(this, "data")) return false; | |
436 | ||
316670eb A |
437 | serializeLength = length; |
438 | if (reserved && reserved->disableSerialization) serializeLength = 0; | |
439 | ||
440 | for (i = 0, p = (unsigned char *)data; i < serializeLength; i++, p++) { | |
1c79356b A |
441 | /* 3 bytes are encoded as 4 */ |
442 | switch (i % 3) { | |
443 | case 0: | |
444 | c = __CFPLDataEncodeTable [ ((p[0] >> 2) & 0x3f)]; | |
445 | if (!s->addChar(c)) return false; | |
446 | break; | |
447 | case 1: | |
448 | c = __CFPLDataEncodeTable [ ((((p[-1] << 8) | p[0]) >> 4) & 0x3f)]; | |
449 | if (!s->addChar(c)) return false; | |
450 | break; | |
451 | case 2: | |
452 | c = __CFPLDataEncodeTable [ ((((p[-1] << 8) | p[0]) >> 6) & 0x3f)]; | |
453 | if (!s->addChar(c)) return false; | |
454 | c = __CFPLDataEncodeTable [ (p[0] & 0x3f)]; | |
455 | if (!s->addChar(c)) return false; | |
456 | break; | |
457 | } | |
458 | } | |
459 | switch (i % 3) { | |
460 | case 0: | |
461 | break; | |
462 | case 1: | |
463 | c = __CFPLDataEncodeTable [ ((p[-1] << 4) & 0x30)]; | |
464 | if (!s->addChar(c)) return false; | |
465 | if (!s->addChar('=')) return false; | |
466 | if (!s->addChar('=')) return false; | |
467 | break; | |
468 | case 2: | |
469 | c = __CFPLDataEncodeTable [ ((p[-1] << 2) & 0x3c)]; | |
470 | if (!s->addChar(c)) return false; | |
471 | if (!s->addChar('=')) return false; | |
472 | break; | |
473 | } | |
474 | ||
475 | return s->addXMLEndTag("data"); | |
476 | } | |
b0d623f7 | 477 | |
b0d623f7 A |
478 | void OSData::setDeallocFunction(DeallocFunction func) |
479 | { | |
316670eb A |
480 | if (!reserved) |
481 | { | |
3e170ce0 | 482 | reserved = (typeof(reserved)) kalloc_container(sizeof(ExpansionData)); |
39236c6e A |
483 | if (!reserved) return; |
484 | bzero(reserved, sizeof(ExpansionData)); | |
316670eb A |
485 | } |
486 | reserved->deallocFunction = func; | |
487 | } | |
488 | ||
489 | void OSData::setSerializable(bool serializable) | |
490 | { | |
491 | if (!reserved) | |
492 | { | |
3e170ce0 | 493 | reserved = (typeof(reserved)) kalloc_container(sizeof(ExpansionData)); |
316670eb A |
494 | if (!reserved) return; |
495 | bzero(reserved, sizeof(ExpansionData)); | |
496 | } | |
497 | reserved->disableSerialization = (!serializable); | |
b0d623f7 | 498 | } |