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"
31 const char * name_for_subtype(uint8_t elem_type);
33 const char * name_for_subtype(uint8_t elem_type)
35 char * retval = "unknown";
38 case KC_ST_CHAR: retval = "char"; break;
39 case KC_ST_INT8: retval = "int8_t"; break;
40 case KC_ST_UINT8: retval = "uint8_t"; break;
41 case KC_ST_INT16: retval = "int16_t"; break;
42 case KC_ST_UINT16: retval = "uint16_t"; break;
43 case KC_ST_INT32: retval = "int32_t"; break;
44 case KC_ST_UINT32: retval = "uint32_t"; break;
45 case KC_ST_INT64: retval = "int64_t"; break;
46 case KC_ST_UINT64: retval = "uint64_t"; break;
48 default: retval = "Unknown"; break;
56 KCDBasicTypeDescription () {
61 struct kcdata_subtype_descriptor _subtype_desc;
66 @implementation KCDBasicTypeDescription
68 - (id)initWithKCTypeDesc:(kcdata_subtype_descriptor_t)sub_type_desc
70 _typeID = sub_type_desc->kcs_elem_type;
71 _count = kcs_get_elem_count(sub_type_desc);
72 _size = kcs_get_elem_size(sub_type_desc);
74 memcpy(&_subtype_desc, sub_type_desc, sizeof(_subtype_desc));
75 _name = [NSString stringWithFormat:@"%s", _subtype_desc.kcs_name];
80 - (id)createDefaultForType:(uint32_t)typeID
82 struct kcdata_subtype_descriptor subtype;
83 subtype.kcs_flags = KCS_SUBTYPE_FLAGS_ARRAY;
84 subtype.kcs_elem_type = KC_ST_UINT8;
85 subtype.kcs_elem_offset = 0;
86 subtype.kcs_elem_size = KCS_SUBTYPE_PACK_SIZE(UINT16_MAX, (uint16_t)sizeof(uint8_t));
87 subtype.kcs_name[0] = '\0';
88 (void)[self initWithKCTypeDesc:&subtype];
89 _name = [NSString stringWithFormat:@"Type_0x%x", typeID];
93 - (NSObject *)objectForType:(kctype_subtype_t)elem_type withData:(uint8_t *)data
98 case KC_ST_CHAR: obj = [NSString stringWithFormat:@"%c", *(char *)data]; break;
99 case KC_ST_INT8: obj = [NSNumber numberWithInt:*(int8_t *)data]; break;
100 case KC_ST_UINT8: obj = [NSNumber numberWithInt:*(uint8_t *)data]; break;
101 case KC_ST_INT16: obj = [NSNumber numberWithShort:*(int16_t *)data]; break;
102 case KC_ST_UINT16: obj = [NSNumber numberWithUnsignedShort:*(uint16_t *)data]; break;
103 case KC_ST_INT32: obj = [NSNumber numberWithInt:*(int32_t *)data]; break;
104 case KC_ST_UINT32: obj = [NSNumber numberWithUnsignedInt:*(uint32_t *)data]; break;
105 case KC_ST_INT64: obj = [NSNumber numberWithLongLong:*(int64_t *)data]; break;
106 case KC_ST_UINT64: obj = [NSNumber numberWithUnsignedLongLong:*(uint64_t *)data]; break;
108 default: obj = @"<Unknown error occurred>"; break;
114 - (NSDictionary *)parseData:(void *)dataBuffer ofLength:(uint32_t)length
116 NSMutableDictionary * retval = [[NSMutableDictionary alloc] init];
117 if (length <= _subtype_desc.kcs_elem_offset)
119 uint8_t * data = (uint8_t *)dataBuffer;
121 * Calculate the maximum number of data elements we can parse, Taking into
122 * account the maximum size specified by the type description, and also the
123 * actual length of the data buffer and the offset into the buffer where we
126 uint32_t elem_count = MIN(_count, (length - _subtype_desc.kcs_elem_offset) / (_size / _count));
127 uint32_t elem_size = _size / _count;
128 if (elem_count == 0) {
130 } else if (elem_count == 1) {
131 retval[_name] = [self objectForType:_subtype_desc.kcs_elem_type withData:&data[_subtype_desc.kcs_elem_offset]];
132 } else if (_subtype_desc.kcs_elem_type == KC_ST_CHAR) {
133 char *s = (char *)&data[_subtype_desc.kcs_elem_offset];
134 if (!(strnlen(s, length) < length)) {
137 retval[_name] = [NSString stringWithFormat:@"%s", s];
139 NSMutableArray * objArray = [NSMutableArray arrayWithCapacity:elem_count];
140 for (unsigned int i = 0; i < elem_count; i++) {
141 [objArray addObject:[self objectForType:_subtype_desc.kcs_elem_type
142 withData:&data[(_subtype_desc.kcs_elem_offset + (elem_size * i))]]];
144 retval[_name] = objArray;
149 - (NSString *)description
151 if (_subtype_desc.kcs_flags & KCS_SUBTYPE_FLAGS_ARRAY) {
152 return [NSString stringWithFormat:@"[%d,%d] %s %s[%d];", _subtype_desc.kcs_elem_offset, kcs_get_elem_size(&_subtype_desc), name_for_subtype(_subtype_desc.kcs_elem_type), _subtype_desc.kcs_name, kcs_get_elem_count(&_subtype_desc) ];
154 return [NSString stringWithFormat:@"[%d,%d] %s %s;", _subtype_desc.kcs_elem_offset, kcs_get_elem_size(&_subtype_desc), name_for_subtype(_subtype_desc.kcs_elem_type), _subtype_desc.kcs_name ];
156 //return [NSString stringWithFormat:@"type: %d => \"%@\" ", [self typeID], [self name]];
169 - (unsigned int)typeID
174 - (BOOL) shouldMergeData