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