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