2 * Copyright (c) 2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #import "KCDBasicTypeDescription.h"
32 KCDBasicTypeDescription () {
37 struct kcdata_subtype_descriptor _subtype_desc;
42 @implementation KCDBasicTypeDescription
44 - (id)initWithKCTypeDesc:(kcdata_subtype_descriptor_t)sub_type_desc
46 _typeID = sub_type_desc->kcs_elem_type;
47 _count = kcs_get_elem_count(sub_type_desc);
48 _size = kcs_get_elem_size(sub_type_desc);
50 memcpy(&_subtype_desc, sub_type_desc, sizeof(_subtype_desc));
51 _name = [NSString stringWithFormat:@"%s", _subtype_desc.kcs_name];
56 - (id)createDefaultForType:(uint32_t)typeID
58 struct kcdata_subtype_descriptor subtype;
59 subtype.kcs_flags = KCS_SUBTYPE_FLAGS_ARRAY;
60 subtype.kcs_elem_type = KC_ST_UINT8;
61 subtype.kcs_elem_offset = 0;
62 subtype.kcs_elem_size = KCS_SUBTYPE_PACK_SIZE(UINT16_MAX, (uint16_t)sizeof(uint8_t));
63 subtype.kcs_name[0] = '\0';
64 (void)[self initWithKCTypeDesc:&subtype];
65 _name = [NSString stringWithFormat:@"Type_0x%x", typeID];
69 - (NSObject *)objectForType:(kctype_subtype_t)elem_type withData:(uint8_t *)data
74 case KC_ST_CHAR: obj = [NSString stringWithFormat:@"%c", *(char *)data]; break;
75 case KC_ST_INT8: obj = [NSNumber numberWithInt:*(int8_t *)data]; break;
76 case KC_ST_UINT8: obj = [NSNumber numberWithInt:*(uint8_t *)data]; break;
77 case KC_ST_INT16: obj = [NSNumber numberWithShort:*(int16_t *)data]; break;
78 case KC_ST_UINT16: obj = [NSNumber numberWithUnsignedShort:*(uint16_t *)data]; break;
79 case KC_ST_INT32: obj = [NSNumber numberWithInt:*(int32_t *)data]; break;
80 case KC_ST_UINT32: obj = [NSNumber numberWithUnsignedInt:*(uint32_t *)data]; break;
81 case KC_ST_INT64: obj = [NSNumber numberWithLongLong:*(int64_t *)data]; break;
82 case KC_ST_UINT64: obj = [NSNumber numberWithUnsignedLongLong:*(uint64_t *)data]; break;
84 default: obj = @"<Unknown error occurred>"; break;
90 - (NSMutableDictionary *)parseData:(void *)dataBuffer ofLength:(uint32_t)length
92 NSMutableDictionary * retval = [[NSMutableDictionary alloc] init];
93 uint8_t * data = (uint8_t *)dataBuffer;
94 uint32_t elem_count = MIN(_count, length / (_size / _count));
95 uint32_t elem_size = _size / _count;
97 retval[_name] = [self objectForType:_subtype_desc.kcs_elem_type withData:&data[_subtype_desc.kcs_elem_offset]];
98 } else if (_subtype_desc.kcs_elem_type == KC_ST_CHAR) {
99 retval[_name] = [NSString stringWithFormat:@"%s", (char *)&data[_subtype_desc.kcs_elem_offset]];
101 NSMutableArray * objArray = [NSMutableArray arrayWithCapacity:elem_count];
102 for (unsigned int i = 0; i < elem_count; i++) {
103 [objArray addObject:[self objectForType:_subtype_desc.kcs_elem_type
104 withData:&data[(_subtype_desc.kcs_elem_offset + (elem_size * i))]]];
106 retval[_name] = objArray;
111 - (NSString *)description
113 return [NSString stringWithFormat:@"type: %d => \"%@\" ", [self typeID], [self name]];