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