xref: /xnu-8796.121.2/libkdd/KCDBasicTypeDescription.m (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions/*
2*c54f35caSApple OSS Distributions * Copyright (c) 2015 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributions#import "KCDBasicTypeDescription.h"
30*c54f35caSApple OSS Distributions
31*c54f35caSApple OSS Distributionsconst char * name_for_subtype(uint8_t elem_type);
32*c54f35caSApple OSS Distributions
33*c54f35caSApple OSS Distributionsconst char * name_for_subtype(uint8_t elem_type)
34*c54f35caSApple OSS Distributions{
35*c54f35caSApple OSS Distributions    char * retval = "unknown";
36*c54f35caSApple OSS Distributions
37*c54f35caSApple OSS Distributions    switch (elem_type) {
38*c54f35caSApple OSS Distributions        case KC_ST_CHAR: retval = "char"; break;
39*c54f35caSApple OSS Distributions        case KC_ST_INT8: retval = "int8_t"; break;
40*c54f35caSApple OSS Distributions        case KC_ST_UINT8: retval = "uint8_t"; break;
41*c54f35caSApple OSS Distributions        case KC_ST_INT16: retval = "int16_t"; break;
42*c54f35caSApple OSS Distributions        case KC_ST_UINT16: retval = "uint16_t"; break;
43*c54f35caSApple OSS Distributions        case KC_ST_INT32: retval = "int32_t"; break;
44*c54f35caSApple OSS Distributions        case KC_ST_UINT32: retval = "uint32_t"; break;
45*c54f35caSApple OSS Distributions        case KC_ST_INT64: retval = "int64_t"; break;
46*c54f35caSApple OSS Distributions        case KC_ST_UINT64: retval = "uint64_t"; break;
47*c54f35caSApple OSS Distributions
48*c54f35caSApple OSS Distributions        default: retval = "Unknown"; break;
49*c54f35caSApple OSS Distributions    }
50*c54f35caSApple OSS Distributions
51*c54f35caSApple OSS Distributions    return retval;
52*c54f35caSApple OSS Distributions}
53*c54f35caSApple OSS Distributions
54*c54f35caSApple OSS Distributions
55*c54f35caSApple OSS Distributions@interface
56*c54f35caSApple OSS DistributionsKCDBasicTypeDescription () {
57*c54f35caSApple OSS Distributions	unsigned int _typeID;
58*c54f35caSApple OSS Distributions	uint32_t _size;
59*c54f35caSApple OSS Distributions	uint32_t _count;
60*c54f35caSApple OSS Distributions	NSString * _name;
61*c54f35caSApple OSS Distributions	struct kcdata_subtype_descriptor _subtype_desc;
62*c54f35caSApple OSS Distributions}
63*c54f35caSApple OSS Distributions
64*c54f35caSApple OSS Distributions@end
65*c54f35caSApple OSS Distributions
66*c54f35caSApple OSS Distributions@implementation KCDBasicTypeDescription
67*c54f35caSApple OSS Distributions
68*c54f35caSApple OSS Distributions- (id)initWithKCTypeDesc:(kcdata_subtype_descriptor_t)sub_type_desc
69*c54f35caSApple OSS Distributions{
70*c54f35caSApple OSS Distributions	_typeID = sub_type_desc->kcs_elem_type;
71*c54f35caSApple OSS Distributions	_count = kcs_get_elem_count(sub_type_desc);
72*c54f35caSApple OSS Distributions	_size = kcs_get_elem_size(sub_type_desc);
73*c54f35caSApple OSS Distributions
74*c54f35caSApple OSS Distributions	memcpy(&_subtype_desc, sub_type_desc, sizeof(_subtype_desc));
75*c54f35caSApple OSS Distributions	_name = [NSString stringWithFormat:@"%s", _subtype_desc.kcs_name];
76*c54f35caSApple OSS Distributions
77*c54f35caSApple OSS Distributions	return self;
78*c54f35caSApple OSS Distributions}
79*c54f35caSApple OSS Distributions
80*c54f35caSApple OSS Distributions- (id)createDefaultForType:(uint32_t)typeID
81*c54f35caSApple OSS Distributions{
82*c54f35caSApple OSS Distributions	struct kcdata_subtype_descriptor subtype;
83*c54f35caSApple OSS Distributions	subtype.kcs_flags = KCS_SUBTYPE_FLAGS_ARRAY;
84*c54f35caSApple OSS Distributions	subtype.kcs_elem_type = KC_ST_UINT8;
85*c54f35caSApple OSS Distributions	subtype.kcs_elem_offset = 0;
86*c54f35caSApple OSS Distributions	subtype.kcs_elem_size = KCS_SUBTYPE_PACK_SIZE(UINT16_MAX, (uint16_t)sizeof(uint8_t));
87*c54f35caSApple OSS Distributions	subtype.kcs_name[0] = '\0';
88*c54f35caSApple OSS Distributions	(void)[self initWithKCTypeDesc:&subtype];
89*c54f35caSApple OSS Distributions	_name = [NSString stringWithFormat:@"Type_0x%x", typeID];
90*c54f35caSApple OSS Distributions	return self;
91*c54f35caSApple OSS Distributions}
92*c54f35caSApple OSS Distributions
93*c54f35caSApple OSS Distributions#define read_unaligned(type, data) ({ \
94*c54f35caSApple OSS Distributions    type x; \
95*c54f35caSApple OSS Distributions    memcpy((void*)&x, (void*)(data), sizeof(type)); \
96*c54f35caSApple OSS Distributions    x; })
97*c54f35caSApple OSS Distributions
98*c54f35caSApple OSS Distributions- (NSObject *)objectForType:(kctype_subtype_t)elem_type withData:(uint8_t *)data
99*c54f35caSApple OSS Distributions{
100*c54f35caSApple OSS Distributions	NSObject * obj;
101*c54f35caSApple OSS Distributions
102*c54f35caSApple OSS Distributions	switch (elem_type) {
103*c54f35caSApple OSS Distributions	case KC_ST_CHAR: obj = [NSString stringWithFormat:@"%c", *(char *)data]; break;
104*c54f35caSApple OSS Distributions	case KC_ST_INT8: obj =   [NSNumber numberWithInt:read_unaligned(int8_t, data)]; break;
105*c54f35caSApple OSS Distributions	case KC_ST_UINT8: obj =  [NSNumber numberWithInt:read_unaligned(uint8_t, data)]; break;
106*c54f35caSApple OSS Distributions	case KC_ST_INT16: obj =  [NSNumber numberWithShort:read_unaligned(int16_t, data)]; break;
107*c54f35caSApple OSS Distributions	case KC_ST_UINT16: obj = [NSNumber numberWithUnsignedShort:read_unaligned(uint16_t, data)]; break;
108*c54f35caSApple OSS Distributions	case KC_ST_INT32: obj =  [NSNumber numberWithInt:read_unaligned(int32_t, data)]; break;
109*c54f35caSApple OSS Distributions	case KC_ST_UINT32: obj = [NSNumber numberWithUnsignedInt:read_unaligned(uint32_t, data)]; break;
110*c54f35caSApple OSS Distributions	case KC_ST_INT64: obj =  [NSNumber numberWithLongLong:read_unaligned(int64_t, data)]; break;
111*c54f35caSApple OSS Distributions	case KC_ST_UINT64: obj = [NSNumber numberWithUnsignedLongLong:read_unaligned(uint64_t, data)]; break;
112*c54f35caSApple OSS Distributions
113*c54f35caSApple OSS Distributions	default: obj = @"<Unknown error occurred>"; break;
114*c54f35caSApple OSS Distributions	}
115*c54f35caSApple OSS Distributions
116*c54f35caSApple OSS Distributions	return obj;
117*c54f35caSApple OSS Distributions}
118*c54f35caSApple OSS Distributions
119*c54f35caSApple OSS Distributions- (NSDictionary *)parseData:(void *)dataBuffer ofLength:(uint32_t)length
120*c54f35caSApple OSS Distributions{
121*c54f35caSApple OSS Distributions	NSMutableDictionary * retval = [[NSMutableDictionary alloc] init];
122*c54f35caSApple OSS Distributions	if (length <= _subtype_desc.kcs_elem_offset)
123*c54f35caSApple OSS Distributions		return retval;
124*c54f35caSApple OSS Distributions	uint8_t * data = (uint8_t *)dataBuffer;
125*c54f35caSApple OSS Distributions	/*
126*c54f35caSApple OSS Distributions	 * Calculate the maximum number of data elements we can parse, Taking into
127*c54f35caSApple OSS Distributions	 * account the maximum size specified by the type description, and also the
128*c54f35caSApple OSS Distributions	 * actual length of the data buffer and the offset into the buffer where we
129*c54f35caSApple OSS Distributions	 * begin parsing.
130*c54f35caSApple OSS Distributions	 */
131*c54f35caSApple OSS Distributions	uint32_t elem_count = MIN(_count, (length - _subtype_desc.kcs_elem_offset) / (_size / _count));
132*c54f35caSApple OSS Distributions	uint32_t elem_size = _size / _count;
133*c54f35caSApple OSS Distributions	if (elem_count == 0) {
134*c54f35caSApple OSS Distributions		return retval;
135*c54f35caSApple OSS Distributions	}  else if (elem_count == 1) {
136*c54f35caSApple OSS Distributions		retval[_name] = [self objectForType:_subtype_desc.kcs_elem_type withData:&data[_subtype_desc.kcs_elem_offset]];
137*c54f35caSApple OSS Distributions	} else if (_subtype_desc.kcs_elem_type == KC_ST_CHAR) {
138*c54f35caSApple OSS Distributions		char *s = (char *)&data[_subtype_desc.kcs_elem_offset];
139*c54f35caSApple OSS Distributions		retval[_name] = [NSString stringWithFormat:@"%.*s", (int)elem_count, s];
140*c54f35caSApple OSS Distributions	} else {
141*c54f35caSApple OSS Distributions		NSMutableArray * objArray = [NSMutableArray arrayWithCapacity:elem_count];
142*c54f35caSApple OSS Distributions		for (unsigned int i = 0; i < elem_count; i++) {
143*c54f35caSApple OSS Distributions			[objArray addObject:[self objectForType:_subtype_desc.kcs_elem_type
144*c54f35caSApple OSS Distributions			                                  withData:&data[(_subtype_desc.kcs_elem_offset + (elem_size * i))]]];
145*c54f35caSApple OSS Distributions		}
146*c54f35caSApple OSS Distributions		retval[_name] = objArray;
147*c54f35caSApple OSS Distributions	}
148*c54f35caSApple OSS Distributions	return retval;
149*c54f35caSApple OSS Distributions}
150*c54f35caSApple OSS Distributions
151*c54f35caSApple OSS Distributions- (NSString *)description
152*c54f35caSApple OSS Distributions{
153*c54f35caSApple OSS Distributions    if (_subtype_desc.kcs_flags & KCS_SUBTYPE_FLAGS_ARRAY) {
154*c54f35caSApple OSS Distributions        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) ];
155*c54f35caSApple OSS Distributions    }else {
156*c54f35caSApple OSS Distributions        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 ];
157*c54f35caSApple OSS Distributions    }
158*c54f35caSApple OSS Distributions	//return [NSString stringWithFormat:@"type: %d => \"%@\" ", [self typeID], [self name]];
159*c54f35caSApple OSS Distributions}
160*c54f35caSApple OSS Distributions
161*c54f35caSApple OSS Distributions- (NSString *)name
162*c54f35caSApple OSS Distributions{
163*c54f35caSApple OSS Distributions	return _name;
164*c54f35caSApple OSS Distributions}
165*c54f35caSApple OSS Distributions
166*c54f35caSApple OSS Distributions- (uint32_t)count
167*c54f35caSApple OSS Distributions{
168*c54f35caSApple OSS Distributions	return _count;
169*c54f35caSApple OSS Distributions}
170*c54f35caSApple OSS Distributions
171*c54f35caSApple OSS Distributions- (unsigned int)typeID
172*c54f35caSApple OSS Distributions{
173*c54f35caSApple OSS Distributions	return _typeID;
174*c54f35caSApple OSS Distributions}
175*c54f35caSApple OSS Distributions
176*c54f35caSApple OSS Distributions- (BOOL) shouldMergeData
177*c54f35caSApple OSS Distributions{
178*c54f35caSApple OSS Distributions	return TRUE;
179*c54f35caSApple OSS Distributions}
180*c54f35caSApple OSS Distributions
181*c54f35caSApple OSS Distributions@end
182