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