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