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#include <kcdata.h> 30*2c2f96dcSApple OSS Distributions#import <Foundation/Foundation.h> 31*2c2f96dcSApple OSS Distributions#import "kdd.h" 32*2c2f96dcSApple OSS Distributions#import "KCDBasicTypeDescription.h" 33*2c2f96dcSApple OSS Distributions#import "KCDStructTypeDescription.h" 34*2c2f96dcSApple OSS Distributions#import "KCDEmbeddedBufferDescription.h" 35*2c2f96dcSApple OSS Distributions 36*2c2f96dcSApple OSS Distributions#define LIB_KCD_ERR_DOMAIN @"KCDataError" 37*2c2f96dcSApple OSS Distributions 38*2c2f96dcSApple OSS Distributions#define GEN_ERROR(code, msg) gen_error(__LINE__, code, @msg) 39*2c2f96dcSApple OSS Distributions#define GEN_ERRORF(code, msg, ...) gen_error(__LINE__, code, [NSString stringWithFormat:@msg, __VA_ARGS__]) 40*2c2f96dcSApple OSS Distributions 41*2c2f96dcSApple OSS Distributions#define MAX_KCDATATYPE_BUFFER_SIZE 2048 42*2c2f96dcSApple OSS Distributionsextern struct kcdata_type_definition * kcdata_get_typedescription(unsigned type_id, uint8_t * buffer, uint32_t buffer_size); 43*2c2f96dcSApple OSS Distributions 44*2c2f96dcSApple OSS DistributionsBOOL setKCDataTypeForID(uint32_t newTypeID, KCDataType *newTypeObj); 45*2c2f96dcSApple OSS Distributions 46*2c2f96dcSApple OSS Distributionsstatic NSError * 47*2c2f96dcSApple OSS Distributionsgen_error(int line, NSInteger code, NSString *message) 48*2c2f96dcSApple OSS Distributions{ 49*2c2f96dcSApple OSS Distributions return [NSError errorWithDomain:LIB_KCD_ERR_DOMAIN 50*2c2f96dcSApple OSS Distributions code:code 51*2c2f96dcSApple OSS Distributions userInfo:@{ @"line": @(line), @"message": message }]; 52*2c2f96dcSApple OSS Distributions} 53*2c2f96dcSApple OSS Distributions 54*2c2f96dcSApple OSS Distributionsstatic BOOL 55*2c2f96dcSApple OSS Distributionsmergedict(NSMutableDictionary * container, NSDictionary * object, NSError ** error) 56*2c2f96dcSApple OSS Distributions{ 57*2c2f96dcSApple OSS Distributions for (id key in object) { 58*2c2f96dcSApple OSS Distributions id existing = container[key]; 59*2c2f96dcSApple OSS Distributions id new = object[key]; 60*2c2f96dcSApple OSS Distributions if (existing) { 61*2c2f96dcSApple OSS Distributions if ([existing isKindOfClass:[NSMutableArray class]] && [new isKindOfClass:[ NSArray class ]]) { 62*2c2f96dcSApple OSS Distributions [existing addObjectsFromArray:new]; 63*2c2f96dcSApple OSS Distributions } else { 64*2c2f96dcSApple OSS Distributions if (error) { 65*2c2f96dcSApple OSS Distributions *error = GEN_ERRORF(KERN_INVALID_OBJECT, "repeated key: %@", key); 66*2c2f96dcSApple OSS Distributions } 67*2c2f96dcSApple OSS Distributions return FALSE; 68*2c2f96dcSApple OSS Distributions } 69*2c2f96dcSApple OSS Distributions } else { 70*2c2f96dcSApple OSS Distributions [container setValue:new forKey:key]; 71*2c2f96dcSApple OSS Distributions } 72*2c2f96dcSApple OSS Distributions } 73*2c2f96dcSApple OSS Distributions return TRUE; 74*2c2f96dcSApple OSS Distributions} 75*2c2f96dcSApple OSS Distributions 76*2c2f96dcSApple OSS Distributions/*! 77*2c2f96dcSApple OSS Distributions * @function getTypeFromTypeDef 78*2c2f96dcSApple OSS Distributions * 79*2c2f96dcSApple OSS Distributions * @abstract 80*2c2f96dcSApple OSS Distributions * Build a KCDataType from a type definition. 81*2c2f96dcSApple OSS Distributions * 82*2c2f96dcSApple OSS Distributions * @param typeDef 83*2c2f96dcSApple OSS Distributions * A pointer to kcdata_type_definition_t that specifies the type fields and has subtype definitions 84*2c2f96dcSApple OSS Distributions * in the memory immediately following the type_definition. 85*2c2f96dcSApple OSS Distributions * 86*2c2f96dcSApple OSS Distributions * @return KCDataType * type object which can be used to parse data into dictionaries. 87*2c2f96dcSApple OSS Distributions * This may return nil if it finds the data to be invalid. 88*2c2f96dcSApple OSS Distributions * 89*2c2f96dcSApple OSS Distributions * @discussion 90*2c2f96dcSApple OSS Distributions * This routine tries to decode the typeDef structure and create either a basic type (KCDBasicTypeDescription) 91*2c2f96dcSApple OSS Distributions * or a struct type. 92*2c2f96dcSApple OSS Distributions */ 93*2c2f96dcSApple OSS Distributionsstatic KCDataType * getTypeFromTypeDef(struct kcdata_type_definition * typeDef); 94*2c2f96dcSApple OSS Distributions 95*2c2f96dcSApple OSS Distributionsstatic KCDataType * 96*2c2f96dcSApple OSS DistributionsgetTypeFromTypeDef(struct kcdata_type_definition * typeDef) 97*2c2f96dcSApple OSS Distributions{ 98*2c2f96dcSApple OSS Distributions if (typeDef == NULL) { 99*2c2f96dcSApple OSS Distributions return nil; 100*2c2f96dcSApple OSS Distributions } 101*2c2f96dcSApple OSS Distributions NSString * kct_name = [NSString stringWithFormat:@"%s", typeDef->kct_name]; 102*2c2f96dcSApple OSS Distributions if (typeDef->kct_num_elements == 1 && !(typeDef->kct_elements[0].kcs_flags & KCS_SUBTYPE_FLAGS_STRUCT)) { 103*2c2f96dcSApple OSS Distributions KCDBasicTypeDescription * retval = [[KCDBasicTypeDescription alloc] initWithKCTypeDesc:&typeDef->kct_elements[0]]; 104*2c2f96dcSApple OSS Distributions return retval; 105*2c2f96dcSApple OSS Distributions } else { 106*2c2f96dcSApple OSS Distributions KCDStructTypeDescription * retval = 107*2c2f96dcSApple OSS Distributions [[KCDStructTypeDescription alloc] initWithType:typeDef->kct_type_identifier withName:kct_name]; 108*2c2f96dcSApple OSS Distributions /* need to do work here to get the array of elements setup here */ 109*2c2f96dcSApple OSS Distributions KCDBasicTypeDescription * curField = nil; 110*2c2f96dcSApple OSS Distributions for (unsigned int i = 0; i < typeDef->kct_num_elements; i++) { 111*2c2f96dcSApple OSS Distributions curField = [[KCDBasicTypeDescription alloc] initWithKCTypeDesc:&typeDef->kct_elements[i]]; 112*2c2f96dcSApple OSS Distributions [retval addFieldBasicType:curField]; 113*2c2f96dcSApple OSS Distributions if (typeDef->kct_elements[i].kcs_flags & KCS_SUBTYPE_FLAGS_MERGE) { 114*2c2f96dcSApple OSS Distributions [retval setFlagsRequestedMerge]; 115*2c2f96dcSApple OSS Distributions } 116*2c2f96dcSApple OSS Distributions } 117*2c2f96dcSApple OSS Distributions return retval; 118*2c2f96dcSApple OSS Distributions } 119*2c2f96dcSApple OSS Distributions return nil; 120*2c2f96dcSApple OSS Distributions} 121*2c2f96dcSApple OSS Distributions 122*2c2f96dcSApple OSS Distributionsstatic dispatch_once_t onceToken; 123*2c2f96dcSApple OSS Distributionsstatic NSMutableDictionary * knownTypes = nil; 124*2c2f96dcSApple OSS Distributions 125*2c2f96dcSApple OSS DistributionsKCDataType * 126*2c2f96dcSApple OSS DistributionsgetKCDataTypeForID(uint32_t typeID) 127*2c2f96dcSApple OSS Distributions{ 128*2c2f96dcSApple OSS Distributions dispatch_once(&onceToken, ^{ 129*2c2f96dcSApple OSS Distributions if (!knownTypes) { 130*2c2f96dcSApple OSS Distributions knownTypes = [[NSMutableDictionary alloc] init]; 131*2c2f96dcSApple OSS Distributions } 132*2c2f96dcSApple OSS Distributions }); 133*2c2f96dcSApple OSS Distributions 134*2c2f96dcSApple OSS Distributions NSNumber * type = [NSNumber numberWithUnsignedInt:typeID]; 135*2c2f96dcSApple OSS Distributions if (!knownTypes[type]) { 136*2c2f96dcSApple OSS Distributions if (typeID == KCDATA_TYPE_NESTED_KCDATA) { 137*2c2f96dcSApple OSS Distributions knownTypes[type] = [[KCDEmbeddedBufferDescription alloc] init]; 138*2c2f96dcSApple OSS Distributions return knownTypes[type]; 139*2c2f96dcSApple OSS Distributions } 140*2c2f96dcSApple OSS Distributions /* code to query system for type information */ 141*2c2f96dcSApple OSS Distributions uint8_t buffer[MAX_KCDATATYPE_BUFFER_SIZE]; 142*2c2f96dcSApple OSS Distributions struct kcdata_type_definition * sys_def = kcdata_get_typedescription(typeID, buffer, MAX_KCDATATYPE_BUFFER_SIZE); 143*2c2f96dcSApple OSS Distributions if (sys_def == NULL) { 144*2c2f96dcSApple OSS Distributions knownTypes[type] = [[KCDBasicTypeDescription alloc] createDefaultForType:typeID]; 145*2c2f96dcSApple OSS Distributions } else { 146*2c2f96dcSApple OSS Distributions knownTypes[type] = getTypeFromTypeDef(sys_def); 147*2c2f96dcSApple OSS Distributions } 148*2c2f96dcSApple OSS Distributions } 149*2c2f96dcSApple OSS Distributions assert(knownTypes[type] != nil); 150*2c2f96dcSApple OSS Distributions return knownTypes[type]; 151*2c2f96dcSApple OSS Distributions} 152*2c2f96dcSApple OSS Distributions 153*2c2f96dcSApple OSS DistributionsBOOL 154*2c2f96dcSApple OSS DistributionssetKCDataTypeForID(uint32_t newTypeID, KCDataType *newTypeObj) { 155*2c2f96dcSApple OSS Distributions if (newTypeObj == NULL || newTypeID == 0) { 156*2c2f96dcSApple OSS Distributions return FALSE; 157*2c2f96dcSApple OSS Distributions } 158*2c2f96dcSApple OSS Distributions 159*2c2f96dcSApple OSS Distributions dispatch_once(&onceToken, ^{ 160*2c2f96dcSApple OSS Distributions if (!knownTypes) { 161*2c2f96dcSApple OSS Distributions knownTypes = [[NSMutableDictionary alloc] init]; 162*2c2f96dcSApple OSS Distributions } 163*2c2f96dcSApple OSS Distributions }); 164*2c2f96dcSApple OSS Distributions 165*2c2f96dcSApple OSS Distributions NSNumber * type = [NSNumber numberWithUnsignedInt:newTypeID]; 166*2c2f96dcSApple OSS Distributions 167*2c2f96dcSApple OSS Distributions if (!knownTypes[type]) { 168*2c2f96dcSApple OSS Distributions knownTypes[type] = newTypeObj; 169*2c2f96dcSApple OSS Distributions return TRUE; 170*2c2f96dcSApple OSS Distributions } 171*2c2f96dcSApple OSS Distributions 172*2c2f96dcSApple OSS Distributions return FALSE; 173*2c2f96dcSApple OSS Distributions} 174*2c2f96dcSApple OSS Distributions 175*2c2f96dcSApple OSS Distributions 176*2c2f96dcSApple OSS DistributionsNSString * 177*2c2f96dcSApple OSS DistributionsKCDataTypeNameForID(uint32_t typeID) 178*2c2f96dcSApple OSS Distributions{ 179*2c2f96dcSApple OSS Distributions NSString * retval = [NSString stringWithFormat:@"%u", typeID]; 180*2c2f96dcSApple OSS Distributions KCDataType * t = getKCDataTypeForID(typeID); 181*2c2f96dcSApple OSS Distributions 182*2c2f96dcSApple OSS Distributions if (![[t name] containsString:@"Type_"]) { 183*2c2f96dcSApple OSS Distributions retval = [t name]; 184*2c2f96dcSApple OSS Distributions } 185*2c2f96dcSApple OSS Distributions return retval; 186*2c2f96dcSApple OSS Distributions} 187*2c2f96dcSApple OSS Distributions 188*2c2f96dcSApple OSS DistributionsNSMutableDictionary * 189*2c2f96dcSApple OSS DistributionsparseKCDataArray(kcdata_iter_t iter, NSError **error) 190*2c2f96dcSApple OSS Distributions{ 191*2c2f96dcSApple OSS Distributions if (!kcdata_iter_array_valid(iter)) { 192*2c2f96dcSApple OSS Distributions if (error) 193*2c2f96dcSApple OSS Distributions *error = GEN_ERROR(KERN_INVALID_OBJECT, "invalid array"); 194*2c2f96dcSApple OSS Distributions return NULL; 195*2c2f96dcSApple OSS Distributions } 196*2c2f96dcSApple OSS Distributions 197*2c2f96dcSApple OSS Distributions uint32_t typeID = kcdata_iter_array_elem_type(iter); 198*2c2f96dcSApple OSS Distributions uint32_t count = kcdata_iter_array_elem_count(iter); 199*2c2f96dcSApple OSS Distributions uint32_t size = kcdata_iter_array_elem_size(iter); 200*2c2f96dcSApple OSS Distributions uint8_t * buffer = (uint8_t *)kcdata_iter_payload(iter); 201*2c2f96dcSApple OSS Distributions KCDataType * datatype = getKCDataTypeForID(typeID); 202*2c2f96dcSApple OSS Distributions NSMutableDictionary * retval = [[NSMutableDictionary alloc] initWithCapacity:1]; 203*2c2f96dcSApple OSS Distributions NSMutableArray * arr = [[NSMutableArray alloc] initWithCapacity:count]; 204*2c2f96dcSApple OSS Distributions retval[[datatype name]] = arr; 205*2c2f96dcSApple OSS Distributions NSDictionary * tmpdict = NULL; 206*2c2f96dcSApple OSS Distributions for (uint32_t i = 0; i < count; i++) { 207*2c2f96dcSApple OSS Distributions tmpdict = [datatype parseData:(void *)&buffer[i * size] ofLength:size]; 208*2c2f96dcSApple OSS Distributions if (!tmpdict) { 209*2c2f96dcSApple OSS Distributions if (error) 210*2c2f96dcSApple OSS Distributions *error = GEN_ERRORF(KERN_INVALID_OBJECT, "failed to parse array element. type=0x%x", (int)typeID); 211*2c2f96dcSApple OSS Distributions return NULL; 212*2c2f96dcSApple OSS Distributions } 213*2c2f96dcSApple OSS Distributions if ([datatype shouldMergeData]) { 214*2c2f96dcSApple OSS Distributions assert([tmpdict count] == 1); 215*2c2f96dcSApple OSS Distributions [arr addObject: [tmpdict allValues][0]]; 216*2c2f96dcSApple OSS Distributions } else { 217*2c2f96dcSApple OSS Distributions [arr addObject:tmpdict]; 218*2c2f96dcSApple OSS Distributions } 219*2c2f96dcSApple OSS Distributions } 220*2c2f96dcSApple OSS Distributions return retval; 221*2c2f96dcSApple OSS Distributions} 222*2c2f96dcSApple OSS Distributions 223*2c2f96dcSApple OSS DistributionsNSMutableDictionary * 224*2c2f96dcSApple OSS DistributionsparseKCDataContainer(kcdata_iter_t *iter_p, NSError **error) 225*2c2f96dcSApple OSS Distributions{ 226*2c2f96dcSApple OSS Distributions kcdata_iter_t iter = *iter_p; 227*2c2f96dcSApple OSS Distributions 228*2c2f96dcSApple OSS Distributions if (!kcdata_iter_container_valid(iter)) { 229*2c2f96dcSApple OSS Distributions if (error) 230*2c2f96dcSApple OSS Distributions *error = GEN_ERROR(KERN_INVALID_OBJECT, "invalid container"); 231*2c2f96dcSApple OSS Distributions return NULL; 232*2c2f96dcSApple OSS Distributions } 233*2c2f96dcSApple OSS Distributions uint64_t containerID = kcdata_iter_container_id(iter); 234*2c2f96dcSApple OSS Distributions 235*2c2f96dcSApple OSS Distributions /* setup collection object for sub containers */ 236*2c2f96dcSApple OSS Distributions NSMutableDictionary * sub_containers = [[NSMutableDictionary alloc] init]; 237*2c2f96dcSApple OSS Distributions NSMutableDictionary * retval = [[NSMutableDictionary alloc] init]; 238*2c2f96dcSApple OSS Distributions NSMutableDictionary * container = [[NSMutableDictionary alloc] init]; 239*2c2f96dcSApple OSS Distributions 240*2c2f96dcSApple OSS Distributions KCDataType * tmptype; 241*2c2f96dcSApple OSS Distributions uint32_t _t; 242*2c2f96dcSApple OSS Distributions void * _d; 243*2c2f96dcSApple OSS Distributions BOOL ok; 244*2c2f96dcSApple OSS Distributions NSDictionary * tmpdict; 245*2c2f96dcSApple OSS Distributions BOOL found_end = FALSE; 246*2c2f96dcSApple OSS Distributions retval[KCDataTypeNameForID(kcdata_iter_container_type(iter))] = container; 247*2c2f96dcSApple OSS Distributions 248*2c2f96dcSApple OSS Distributions iter = kcdata_iter_next(iter); 249*2c2f96dcSApple OSS Distributions 250*2c2f96dcSApple OSS Distributions KCDATA_ITER_FOREACH(iter) 251*2c2f96dcSApple OSS Distributions { 252*2c2f96dcSApple OSS Distributions _t = kcdata_iter_type(iter); 253*2c2f96dcSApple OSS Distributions _d = kcdata_iter_payload(iter); 254*2c2f96dcSApple OSS Distributions if (_t == KCDATA_TYPE_CONTAINER_END) { 255*2c2f96dcSApple OSS Distributions if (kcdata_iter_container_id(iter) != containerID) { 256*2c2f96dcSApple OSS Distributions if (error) 257*2c2f96dcSApple OSS Distributions *error = GEN_ERROR(KERN_INVALID_ARGUMENT, "container marker mismatch"); 258*2c2f96dcSApple OSS Distributions return NULL; 259*2c2f96dcSApple OSS Distributions } 260*2c2f96dcSApple OSS Distributions found_end = TRUE; 261*2c2f96dcSApple OSS Distributions break; 262*2c2f96dcSApple OSS Distributions } 263*2c2f96dcSApple OSS Distributions 264*2c2f96dcSApple OSS Distributions if (_t == KCDATA_TYPE_ARRAY) { 265*2c2f96dcSApple OSS Distributions tmpdict = parseKCDataArray(iter, error); 266*2c2f96dcSApple OSS Distributions if (!tmpdict) 267*2c2f96dcSApple OSS Distributions return NULL; 268*2c2f96dcSApple OSS Distributions 269*2c2f96dcSApple OSS Distributions ok = mergedict(container, tmpdict, error); 270*2c2f96dcSApple OSS Distributions if (!ok) 271*2c2f96dcSApple OSS Distributions return NULL; 272*2c2f96dcSApple OSS Distributions 273*2c2f96dcSApple OSS Distributions continue; 274*2c2f96dcSApple OSS Distributions } 275*2c2f96dcSApple OSS Distributions 276*2c2f96dcSApple OSS Distributions if (_t == KCDATA_TYPE_CONTAINER_BEGIN) { 277*2c2f96dcSApple OSS Distributions NSString * subcontainerID = [NSString stringWithFormat:@"%llu", kcdata_iter_container_id(iter)]; 278*2c2f96dcSApple OSS Distributions tmpdict = parseKCDataContainer(&iter, error); 279*2c2f96dcSApple OSS Distributions if (!tmpdict) 280*2c2f96dcSApple OSS Distributions return NULL; 281*2c2f96dcSApple OSS Distributions assert([tmpdict count] == 1); 282*2c2f96dcSApple OSS Distributions for (NSString * k in [tmpdict keyEnumerator]) { 283*2c2f96dcSApple OSS Distributions if (sub_containers[k] == nil) { 284*2c2f96dcSApple OSS Distributions sub_containers[k] = [[NSMutableDictionary alloc] init]; 285*2c2f96dcSApple OSS Distributions } 286*2c2f96dcSApple OSS Distributions if (sub_containers[k][subcontainerID] != nil) { 287*2c2f96dcSApple OSS Distributions if (error) 288*2c2f96dcSApple OSS Distributions *error = GEN_ERRORF(KERN_INVALID_OBJECT, "repeated container id: %@", subcontainerID); 289*2c2f96dcSApple OSS Distributions return NULL; 290*2c2f96dcSApple OSS Distributions } 291*2c2f96dcSApple OSS Distributions sub_containers[k][subcontainerID] = tmpdict[k]; 292*2c2f96dcSApple OSS Distributions } 293*2c2f96dcSApple OSS Distributions continue; 294*2c2f96dcSApple OSS Distributions } 295*2c2f96dcSApple OSS Distributions 296*2c2f96dcSApple OSS Distributions tmptype = getKCDataTypeForID(_t); 297*2c2f96dcSApple OSS Distributions tmpdict = [tmptype parseData:_d ofLength:kcdata_iter_size(iter)]; 298*2c2f96dcSApple OSS Distributions if (!tmpdict) { 299*2c2f96dcSApple OSS Distributions if (error) 300*2c2f96dcSApple OSS Distributions *error = GEN_ERRORF(KERN_INVALID_OBJECT, "failed to parse. type=0x%x", (int)_t); 301*2c2f96dcSApple OSS Distributions return NULL; 302*2c2f96dcSApple OSS Distributions } 303*2c2f96dcSApple OSS Distributions if (![tmptype shouldMergeData]) { 304*2c2f96dcSApple OSS Distributions tmpdict = @{[tmptype name] : tmpdict}; 305*2c2f96dcSApple OSS Distributions } 306*2c2f96dcSApple OSS Distributions ok = mergedict(container, tmpdict, error); 307*2c2f96dcSApple OSS Distributions if (!ok) 308*2c2f96dcSApple OSS Distributions return NULL; 309*2c2f96dcSApple OSS Distributions } 310*2c2f96dcSApple OSS Distributions 311*2c2f96dcSApple OSS Distributions if (!found_end) { 312*2c2f96dcSApple OSS Distributions if (error) 313*2c2f96dcSApple OSS Distributions *error = GEN_ERROR(KERN_INVALID_ARGUMENT, "missing container end"); 314*2c2f96dcSApple OSS Distributions return NULL; 315*2c2f96dcSApple OSS Distributions } 316*2c2f96dcSApple OSS Distributions 317*2c2f96dcSApple OSS Distributions ok = mergedict(container, sub_containers, error); 318*2c2f96dcSApple OSS Distributions if (!ok) 319*2c2f96dcSApple OSS Distributions return NULL; 320*2c2f96dcSApple OSS Distributions 321*2c2f96dcSApple OSS Distributions *iter_p = iter; 322*2c2f96dcSApple OSS Distributions return retval; 323*2c2f96dcSApple OSS Distributions} 324*2c2f96dcSApple OSS Distributions 325*2c2f96dcSApple OSS DistributionsNSDictionary * 326*2c2f96dcSApple OSS DistributionsparseKCDataBuffer(void * dataBuffer, uint32_t size, NSError ** error) 327*2c2f96dcSApple OSS Distributions{ 328*2c2f96dcSApple OSS Distributions if (dataBuffer == NULL) { 329*2c2f96dcSApple OSS Distributions if (error) 330*2c2f96dcSApple OSS Distributions *error = GEN_ERROR(KERN_INVALID_ARGUMENT, "buffer is null"); 331*2c2f96dcSApple OSS Distributions return NULL; 332*2c2f96dcSApple OSS Distributions } 333*2c2f96dcSApple OSS Distributions 334*2c2f96dcSApple OSS Distributions uint32_t _type = (size >= sizeof(uint32_t)) ? *(uint32_t*)dataBuffer : 0; 335*2c2f96dcSApple OSS Distributions uint32_t _size = 0; 336*2c2f96dcSApple OSS Distributions uint64_t _flags = 0; 337*2c2f96dcSApple OSS Distributions void * _datap = NULL; 338*2c2f96dcSApple OSS Distributions KCDataType * kcd_type = NULL; 339*2c2f96dcSApple OSS Distributions NSString * rootKey = NULL; 340*2c2f96dcSApple OSS Distributions uint32_t rootType = _type; 341*2c2f96dcSApple OSS Distributions BOOL ok; 342*2c2f96dcSApple OSS Distributions 343*2c2f96dcSApple OSS Distributions /* validate begin tag and get root key */ 344*2c2f96dcSApple OSS Distributions switch (_type) { 345*2c2f96dcSApple OSS Distributions case KCDATA_BUFFER_BEGIN_CRASHINFO: 346*2c2f96dcSApple OSS Distributions rootKey = @"kcdata_crashinfo"; 347*2c2f96dcSApple OSS Distributions break; 348*2c2f96dcSApple OSS Distributions case KCDATA_BUFFER_BEGIN_STACKSHOT: 349*2c2f96dcSApple OSS Distributions rootKey = @"kcdata_stackshot"; 350*2c2f96dcSApple OSS Distributions break; 351*2c2f96dcSApple OSS Distributions case KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT: 352*2c2f96dcSApple OSS Distributions rootKey = @"kcdata_delta_stackshot"; 353*2c2f96dcSApple OSS Distributions break; 354*2c2f96dcSApple OSS Distributions case KCDATA_BUFFER_BEGIN_OS_REASON: 355*2c2f96dcSApple OSS Distributions rootKey = @"kcdata_reason"; 356*2c2f96dcSApple OSS Distributions break; 357*2c2f96dcSApple OSS Distributions case KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG: 358*2c2f96dcSApple OSS Distributions rootKey = @"xnupost_testconfig"; 359*2c2f96dcSApple OSS Distributions break; 360*2c2f96dcSApple OSS Distributions default: { 361*2c2f96dcSApple OSS Distributions if (error) 362*2c2f96dcSApple OSS Distributions *error = GEN_ERROR(KERN_INVALID_VALUE, "invalid magic number"); 363*2c2f96dcSApple OSS Distributions return NULL; 364*2c2f96dcSApple OSS Distributions break; 365*2c2f96dcSApple OSS Distributions } 366*2c2f96dcSApple OSS Distributions } 367*2c2f96dcSApple OSS Distributions assert(rootKey != NULL); 368*2c2f96dcSApple OSS Distributions 369*2c2f96dcSApple OSS Distributions kcdata_iter_t iter = kcdata_iter(dataBuffer, size); 370*2c2f96dcSApple OSS Distributions 371*2c2f96dcSApple OSS Distributions if (!kcdata_iter_valid(iter)) { 372*2c2f96dcSApple OSS Distributions if (error) { 373*2c2f96dcSApple OSS Distributions *error = GEN_ERROR(KERN_INVALID_OBJECT, "initial item is invalid"); 374*2c2f96dcSApple OSS Distributions } 375*2c2f96dcSApple OSS Distributions return NULL; 376*2c2f96dcSApple OSS Distributions } 377*2c2f96dcSApple OSS Distributions 378*2c2f96dcSApple OSS Distributions NSMutableDictionary * rootObject = [NSMutableDictionary dictionary]; 379*2c2f96dcSApple OSS Distributions NSDictionary * retval = [NSMutableDictionary dictionaryWithObject:rootObject forKey:rootKey]; 380*2c2f96dcSApple OSS Distributions 381*2c2f96dcSApple OSS Distributions /* iterate over each kcdata item */ 382*2c2f96dcSApple OSS Distributions KCDATA_ITER_FOREACH(iter) 383*2c2f96dcSApple OSS Distributions { 384*2c2f96dcSApple OSS Distributions _type = kcdata_iter_type(iter); 385*2c2f96dcSApple OSS Distributions _size = kcdata_iter_size(iter); 386*2c2f96dcSApple OSS Distributions _flags = kcdata_iter_flags(iter); 387*2c2f96dcSApple OSS Distributions _datap = kcdata_iter_payload(iter); 388*2c2f96dcSApple OSS Distributions 389*2c2f96dcSApple OSS Distributions if (_type == rootType) 390*2c2f96dcSApple OSS Distributions continue; 391*2c2f96dcSApple OSS Distributions 392*2c2f96dcSApple OSS Distributions if (_type == KCDATA_TYPE_ARRAY) { 393*2c2f96dcSApple OSS Distributions NSDictionary * dict = parseKCDataArray(iter, error); 394*2c2f96dcSApple OSS Distributions if (!dict) 395*2c2f96dcSApple OSS Distributions return nil; 396*2c2f96dcSApple OSS Distributions 397*2c2f96dcSApple OSS Distributions ok = mergedict(rootObject, dict, error); 398*2c2f96dcSApple OSS Distributions if (!ok) 399*2c2f96dcSApple OSS Distributions return NULL; 400*2c2f96dcSApple OSS Distributions 401*2c2f96dcSApple OSS Distributions continue; 402*2c2f96dcSApple OSS Distributions } 403*2c2f96dcSApple OSS Distributions 404*2c2f96dcSApple OSS Distributions if (_type == KCDATA_TYPE_CONTAINER_BEGIN) { 405*2c2f96dcSApple OSS Distributions NSString * containerID = [NSString stringWithFormat:@"%llu", kcdata_iter_container_id(iter)]; 406*2c2f96dcSApple OSS Distributions NSMutableDictionary *container = parseKCDataContainer(&iter, error); 407*2c2f96dcSApple OSS Distributions if (!container) 408*2c2f96dcSApple OSS Distributions return nil; 409*2c2f96dcSApple OSS Distributions assert([container count] == 1); 410*2c2f96dcSApple OSS Distributions for (NSString * k in [container keyEnumerator]) { 411*2c2f96dcSApple OSS Distributions if (rootObject[k] == nil) { 412*2c2f96dcSApple OSS Distributions rootObject[k] = [[NSMutableDictionary alloc] init]; 413*2c2f96dcSApple OSS Distributions } 414*2c2f96dcSApple OSS Distributions if (rootObject[k][containerID] != nil) { 415*2c2f96dcSApple OSS Distributions if (error) 416*2c2f96dcSApple OSS Distributions *error = GEN_ERRORF(KERN_INVALID_OBJECT, "repeated container id: %@", containerID); 417*2c2f96dcSApple OSS Distributions return NULL; 418*2c2f96dcSApple OSS Distributions } 419*2c2f96dcSApple OSS Distributions rootObject[k][containerID] = container[k]; 420*2c2f96dcSApple OSS Distributions } 421*2c2f96dcSApple OSS Distributions continue; 422*2c2f96dcSApple OSS Distributions } 423*2c2f96dcSApple OSS Distributions 424*2c2f96dcSApple OSS Distributions if (_type == KCDATA_TYPE_TYPEDEFINTION) { 425*2c2f96dcSApple OSS Distributions KCDataType *new_type = getTypeFromTypeDef((struct kcdata_type_definition *)_datap); 426*2c2f96dcSApple OSS Distributions if (new_type != NULL) { 427*2c2f96dcSApple OSS Distributions setKCDataTypeForID([new_type typeID], new_type); 428*2c2f96dcSApple OSS Distributions kcd_type = getKCDataTypeForID(_type); 429*2c2f96dcSApple OSS Distributions NSDictionary * tmpdict = [kcd_type parseData:_datap ofLength:_size]; 430*2c2f96dcSApple OSS Distributions if (!tmpdict) { 431*2c2f96dcSApple OSS Distributions if (error) 432*2c2f96dcSApple OSS Distributions *error = GEN_ERRORF(KERN_INVALID_OBJECT, "failed to parse. type=0x%x", (int)_type); 433*2c2f96dcSApple OSS Distributions return NULL; 434*2c2f96dcSApple OSS Distributions } 435*2c2f96dcSApple OSS Distributions NSString *k = [NSString stringWithFormat:@"typedef[%@]", [new_type name]]; 436*2c2f96dcSApple OSS Distributions rootObject[k] = tmpdict; 437*2c2f96dcSApple OSS Distributions }else { 438*2c2f96dcSApple OSS Distributions if (error) 439*2c2f96dcSApple OSS Distributions *error = GEN_ERRORF(KERN_INVALID_OBJECT, "Failed to parse type definition for type %u", _type); 440*2c2f96dcSApple OSS Distributions return NULL; 441*2c2f96dcSApple OSS Distributions } 442*2c2f96dcSApple OSS Distributions continue; 443*2c2f96dcSApple OSS Distributions } 444*2c2f96dcSApple OSS Distributions 445*2c2f96dcSApple OSS Distributions kcd_type = getKCDataTypeForID(_type); 446*2c2f96dcSApple OSS Distributions NSDictionary * tmpdict = [kcd_type parseData:_datap ofLength:_size]; 447*2c2f96dcSApple OSS Distributions if (!tmpdict) { 448*2c2f96dcSApple OSS Distributions if (error) 449*2c2f96dcSApple OSS Distributions *error = GEN_ERRORF(KERN_INVALID_OBJECT, "failed to parse. type=0x%x", (int)_type); 450*2c2f96dcSApple OSS Distributions return NULL; 451*2c2f96dcSApple OSS Distributions } 452*2c2f96dcSApple OSS Distributions if (![kcd_type shouldMergeData]) { 453*2c2f96dcSApple OSS Distributions tmpdict = @{[kcd_type name] : tmpdict}; 454*2c2f96dcSApple OSS Distributions } 455*2c2f96dcSApple OSS Distributions ok = mergedict(rootObject, tmpdict, error); 456*2c2f96dcSApple OSS Distributions if (!ok) 457*2c2f96dcSApple OSS Distributions return NULL; 458*2c2f96dcSApple OSS Distributions } 459*2c2f96dcSApple OSS Distributions 460*2c2f96dcSApple OSS Distributions if (KCDATA_ITER_FOREACH_FAILED(iter)) { 461*2c2f96dcSApple OSS Distributions retval = nil; 462*2c2f96dcSApple OSS Distributions if (error) { 463*2c2f96dcSApple OSS Distributions *error = GEN_ERROR(KERN_INVALID_OBJECT, "invalid item or missing buffer end marker"); 464*2c2f96dcSApple OSS Distributions } 465*2c2f96dcSApple OSS Distributions } 466*2c2f96dcSApple OSS Distributions 467*2c2f96dcSApple OSS Distributions return retval; 468*2c2f96dcSApple OSS Distributions} 469