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