1*0f4c859eSApple OSS Distributions/* 2*0f4c859eSApple OSS Distributions * Copyright (c) 2015 Apple Inc. All rights reserved. 3*0f4c859eSApple OSS Distributions * 4*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*0f4c859eSApple OSS Distributions * 6*0f4c859eSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*0f4c859eSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*0f4c859eSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*0f4c859eSApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*0f4c859eSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*0f4c859eSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*0f4c859eSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*0f4c859eSApple OSS Distributions * terms of an Apple operating system software license agreement. 14*0f4c859eSApple OSS Distributions * 15*0f4c859eSApple OSS Distributions * Please obtain a copy of the License at 16*0f4c859eSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*0f4c859eSApple OSS Distributions * 18*0f4c859eSApple OSS Distributions * The Original Code and all software distributed under the License are 19*0f4c859eSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*0f4c859eSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*0f4c859eSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*0f4c859eSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*0f4c859eSApple OSS Distributions * Please see the License for the specific language governing rights and 24*0f4c859eSApple OSS Distributions * limitations under the License. 25*0f4c859eSApple OSS Distributions * 26*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*0f4c859eSApple OSS Distributions */ 28*0f4c859eSApple OSS Distributions 29*0f4c859eSApple OSS Distributions#import "KCDStructTypeDescription.h" 30*0f4c859eSApple OSS Distributions 31*0f4c859eSApple OSS Distributions#ifndef KCDATA_TYPE_MAX_WITH_DESC 32*0f4c859eSApple OSS Distributions#define KCDATA_TYPE_MAX_WITH_DESC 0x6 33*0f4c859eSApple OSS Distributions#endif 34*0f4c859eSApple OSS Distributions 35*0f4c859eSApple OSS Distributions@interface 36*0f4c859eSApple OSS DistributionsKCDStructTypeDescription () { 37*0f4c859eSApple OSS Distributions unsigned int _typeID; 38*0f4c859eSApple OSS Distributions NSString * _name; 39*0f4c859eSApple OSS Distributions NSMutableArray * _fields; 40*0f4c859eSApple OSS Distributions BOOL _needDescriptionAsKey; 41*0f4c859eSApple OSS Distributions BOOL _flagsRequestedMerge; 42*0f4c859eSApple OSS Distributions} 43*0f4c859eSApple OSS Distributions 44*0f4c859eSApple OSS Distributions@end 45*0f4c859eSApple OSS Distributions 46*0f4c859eSApple OSS Distributions@implementation KCDStructTypeDescription 47*0f4c859eSApple OSS Distributions 48*0f4c859eSApple OSS Distributions- (id)initWithType:(unsigned int)typeID withName:(NSString *)name 49*0f4c859eSApple OSS Distributions{ 50*0f4c859eSApple OSS Distributions if ((self = [super init])) { 51*0f4c859eSApple OSS Distributions _typeID = typeID; 52*0f4c859eSApple OSS Distributions _name = name; 53*0f4c859eSApple OSS Distributions _needDescriptionAsKey = NO; 54*0f4c859eSApple OSS Distributions if (typeID >= 0x1 && typeID <= KCDATA_TYPE_MAX_WITH_DESC) 55*0f4c859eSApple OSS Distributions _needDescriptionAsKey = YES; 56*0f4c859eSApple OSS Distributions 57*0f4c859eSApple OSS Distributions _fields = [[NSMutableArray alloc] init]; 58*0f4c859eSApple OSS Distributions _flagsRequestedMerge = NO; 59*0f4c859eSApple OSS Distributions return self; 60*0f4c859eSApple OSS Distributions } 61*0f4c859eSApple OSS Distributions return NULL; 62*0f4c859eSApple OSS Distributions} 63*0f4c859eSApple OSS Distributions 64*0f4c859eSApple OSS Distributions- (void)addFieldBasicType:(KCDBasicTypeDescription *)fieldType 65*0f4c859eSApple OSS Distributions{ 66*0f4c859eSApple OSS Distributions [_fields addObject:fieldType]; 67*0f4c859eSApple OSS Distributions} 68*0f4c859eSApple OSS Distributions 69*0f4c859eSApple OSS Distributions- (void)setFlagsRequestedMerge 70*0f4c859eSApple OSS Distributions{ 71*0f4c859eSApple OSS Distributions _flagsRequestedMerge = YES; 72*0f4c859eSApple OSS Distributions} 73*0f4c859eSApple OSS Distributions 74*0f4c859eSApple OSS Distributions- (NSDictionary *)parseData:(void *)dataBuffer ofLength:(uint32_t)length 75*0f4c859eSApple OSS Distributions{ 76*0f4c859eSApple OSS Distributions NSMutableDictionary * retval = [[NSMutableDictionary alloc] init]; 77*0f4c859eSApple OSS Distributions for (KCDataType * fi in _fields) { 78*0f4c859eSApple OSS Distributions NSDictionary * _d = [fi parseData:dataBuffer ofLength:length]; 79*0f4c859eSApple OSS Distributions if (!_d) { 80*0f4c859eSApple OSS Distributions return nil; 81*0f4c859eSApple OSS Distributions } 82*0f4c859eSApple OSS Distributions for (NSString * k in [_d keyEnumerator]) { 83*0f4c859eSApple OSS Distributions retval[k] = _d[k]; 84*0f4c859eSApple OSS Distributions } 85*0f4c859eSApple OSS Distributions } 86*0f4c859eSApple OSS Distributions if (_typeID == KCDATA_TYPE_TYPEDEFINTION){ 87*0f4c859eSApple OSS Distributions uint32_t elem_size = sizeof(struct kcdata_subtype_descriptor); 88*0f4c859eSApple OSS Distributions uint32_t elem_count = (length - offsetof(struct kcdata_type_definition, kct_elements))/elem_size; 89*0f4c859eSApple OSS Distributions NSMutableArray * fields_array = [NSMutableArray arrayWithCapacity:elem_count]; 90*0f4c859eSApple OSS Distributions struct kcdata_subtype_descriptor *fields_dsc = (struct kcdata_subtype_descriptor *) ((uintptr_t)dataBuffer + offsetof(struct kcdata_type_definition, kct_elements)); 91*0f4c859eSApple OSS Distributions int i = 0; 92*0f4c859eSApple OSS Distributions for (i = 0; i < elem_count; i++) { 93*0f4c859eSApple OSS Distributions KCDBasicTypeDescription * tmpdsc = [[KCDBasicTypeDescription alloc] initWithKCTypeDesc:&fields_dsc[i]]; 94*0f4c859eSApple OSS Distributions NSString *field_desc_str = [tmpdsc description]; 95*0f4c859eSApple OSS Distributions 96*0f4c859eSApple OSS Distributions [fields_array addObject:field_desc_str]; 97*0f4c859eSApple OSS Distributions } 98*0f4c859eSApple OSS Distributions retval[@"fields"] = fields_array; 99*0f4c859eSApple OSS Distributions } 100*0f4c859eSApple OSS Distributions if (_needDescriptionAsKey) { 101*0f4c859eSApple OSS Distributions NSString * desc = retval[@"desc"]; 102*0f4c859eSApple OSS Distributions NSObject * obj = retval[@"data"]; 103*0f4c859eSApple OSS Distributions retval[desc] = obj; 104*0f4c859eSApple OSS Distributions [retval removeObjectForKey:@"desc"]; 105*0f4c859eSApple OSS Distributions [retval removeObjectForKey:@"data"]; 106*0f4c859eSApple OSS Distributions } 107*0f4c859eSApple OSS Distributions return retval; 108*0f4c859eSApple OSS Distributions} 109*0f4c859eSApple OSS Distributions 110*0f4c859eSApple OSS Distributions- (BOOL)shouldMergeData 111*0f4c859eSApple OSS Distributions{ 112*0f4c859eSApple OSS Distributions /* 113*0f4c859eSApple OSS Distributions * If this is a type where the kcdata item itself carries the key name, or 114*0f4c859eSApple OSS Distributions * KCS_SUBTYPE_FLAGS_MERGE was used to define the type, then a member of 115*0f4c859eSApple OSS Distributions * this type should have it's dict merged into the parent container, 116*0f4c859eSApple OSS Distributions * instead of being represented as typename => dict. 117*0f4c859eSApple OSS Distributions */ 118*0f4c859eSApple OSS Distributions return _needDescriptionAsKey || _flagsRequestedMerge; 119*0f4c859eSApple OSS Distributions} 120*0f4c859eSApple OSS Distributions 121*0f4c859eSApple OSS Distributions- (NSString *)description 122*0f4c859eSApple OSS Distributions{ 123*0f4c859eSApple OSS Distributions return [NSString stringWithFormat:@"type: %d => \"%@\" ", _typeID, _name]; 124*0f4c859eSApple OSS Distributions} 125*0f4c859eSApple OSS Distributions 126*0f4c859eSApple OSS Distributions- (NSString *)name 127*0f4c859eSApple OSS Distributions{ 128*0f4c859eSApple OSS Distributions return _name; 129*0f4c859eSApple OSS Distributions} 130*0f4c859eSApple OSS Distributions 131*0f4c859eSApple OSS Distributions- (uint32_t)count 132*0f4c859eSApple OSS Distributions{ 133*0f4c859eSApple OSS Distributions return (uint32_t)[_fields count]; 134*0f4c859eSApple OSS Distributions} 135*0f4c859eSApple OSS Distributions 136*0f4c859eSApple OSS Distributions- (unsigned int)typeID 137*0f4c859eSApple OSS Distributions{ 138*0f4c859eSApple OSS Distributions return _typeID; 139*0f4c859eSApple OSS Distributions} 140*0f4c859eSApple OSS Distributions 141*0f4c859eSApple OSS Distributions@end 142