]> git.saurik.com Git - apple/xnu.git/blob - libkdd/kcdata/KCDBasicTypeDescription.m
xnu-3248.60.10.tar.gz
[apple/xnu.git] / libkdd / kcdata / KCDBasicTypeDescription.m
1 /*
2 * Copyright (c) 2015 Apple 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
29 #import "KCDBasicTypeDescription.h"
30
31 @interface
32 KCDBasicTypeDescription () {
33 int _typeID;
34 uint32_t _size;
35 uint32_t _count;
36 NSString * _name;
37 struct kcdata_subtype_descriptor _subtype_desc;
38 }
39
40 @end
41
42 @implementation KCDBasicTypeDescription
43
44 - (id)initWithKCTypeDesc:(kcdata_subtype_descriptor_t)sub_type_desc
45 {
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);
49
50 memcpy(&_subtype_desc, sub_type_desc, sizeof(_subtype_desc));
51 _name = [NSString stringWithFormat:@"%s", _subtype_desc.kcs_name];
52
53 return self;
54 }
55
56 - (id)createDefaultForType:(uint32_t)typeID
57 {
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];
66 return self;
67 }
68
69 - (NSObject *)objectForType:(kctype_subtype_t)elem_type withData:(uint8_t *)data
70 {
71 NSObject * obj;
72
73 switch (elem_type) {
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;
83
84 default: obj = @"<Unknown error occurred>"; break;
85 }
86
87 return obj;
88 }
89
90 - (NSMutableDictionary *)parseData:(void *)dataBuffer ofLength:(uint32_t)length
91 {
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;
96 if (_count == 1) {
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]];
100 } else {
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))]]];
105 }
106 retval[_name] = objArray;
107 }
108 return retval;
109 }
110
111 - (NSString *)description
112 {
113 return [NSString stringWithFormat:@"type: %d => \"%@\" ", [self typeID], [self name]];
114 }
115
116 - (NSString *)name
117 {
118 return _name;
119 }
120
121 - (uint32_t)count
122 {
123 return _count;
124 }
125
126 - (int)typeID
127 {
128 return _typeID;
129 }
130
131 @end