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