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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | /* * 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" const char * name_for_subtype(uint8_t elem_type); const char * name_for_subtype(uint8_t elem_type) { char * retval = "unknown"; switch (elem_type) { case KC_ST_CHAR: retval = "char"; break; case KC_ST_INT8: retval = "int8_t"; break; case KC_ST_UINT8: retval = "uint8_t"; break; case KC_ST_INT16: retval = "int16_t"; break; case KC_ST_UINT16: retval = "uint16_t"; break; case KC_ST_INT32: retval = "int32_t"; break; case KC_ST_UINT32: retval = "uint32_t"; break; case KC_ST_INT64: retval = "int64_t"; break; case KC_ST_UINT64: retval = "uint64_t"; break; default: retval = "Unknown"; break; } return retval; } @interface KCDBasicTypeDescription () { unsigned 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; } #define read_unaligned(type, data) ({ \ type x; \ memcpy((void*)&x, (void*)(data), sizeof(type)); \ x; }) - (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:read_unaligned(int8_t, data)]; break; case KC_ST_UINT8: obj = [NSNumber numberWithInt:read_unaligned(uint8_t, data)]; break; case KC_ST_INT16: obj = [NSNumber numberWithShort:read_unaligned(int16_t, data)]; break; case KC_ST_UINT16: obj = [NSNumber numberWithUnsignedShort:read_unaligned(uint16_t, data)]; break; case KC_ST_INT32: obj = [NSNumber numberWithInt:read_unaligned(int32_t, data)]; break; case KC_ST_UINT32: obj = [NSNumber numberWithUnsignedInt:read_unaligned(uint32_t, data)]; break; case KC_ST_INT64: obj = [NSNumber numberWithLongLong:read_unaligned(int64_t, data)]; break; case KC_ST_UINT64: obj = [NSNumber numberWithUnsignedLongLong:read_unaligned(uint64_t, data)]; break; default: obj = @"<Unknown error occurred>"; break; } return obj; } - (NSDictionary *)parseData:(void *)dataBuffer ofLength:(uint32_t)length { NSMutableDictionary * retval = [[NSMutableDictionary alloc] init]; if (length <= _subtype_desc.kcs_elem_offset) return retval; uint8_t * data = (uint8_t *)dataBuffer; /* * Calculate the maximum number of data elements we can parse, Taking into * account the maximum size specified by the type description, and also the * actual length of the data buffer and the offset into the buffer where we * begin parsing. */ uint32_t elem_count = MIN(_count, (length - _subtype_desc.kcs_elem_offset) / (_size / _count)); uint32_t elem_size = _size / _count; if (elem_count == 0) { return retval; } else if (elem_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) { char *s = (char *)&data[_subtype_desc.kcs_elem_offset]; if (!(strnlen(s, length) < length)) { return nil; } retval[_name] = [NSString stringWithFormat:@"%s", s]; } 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 { if (_subtype_desc.kcs_flags & KCS_SUBTYPE_FLAGS_ARRAY) { 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) ]; }else { 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 ]; } //return [NSString stringWithFormat:@"type: %d => \"%@\" ", [self typeID], [self name]]; } - (NSString *)name { return _name; } - (uint32_t)count { return _count; } - (unsigned int)typeID { return _typeID; } - (BOOL) shouldMergeData { return TRUE; } @end |