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