188cc0b97SApple OSS Distributions/*
288cc0b97SApple OSS Distributions * Copyright (c) 2015 Apple Inc. All rights reserved.
388cc0b97SApple OSS Distributions *
488cc0b97SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
588cc0b97SApple OSS Distributions *
688cc0b97SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
788cc0b97SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
888cc0b97SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
988cc0b97SApple OSS Distributions * compliance with the License. The rights granted to you under the License
1088cc0b97SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
1188cc0b97SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
1288cc0b97SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
1388cc0b97SApple OSS Distributions * terms of an Apple operating system software license agreement.
1488cc0b97SApple OSS Distributions *
1588cc0b97SApple OSS Distributions * Please obtain a copy of the License at
1688cc0b97SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
1788cc0b97SApple OSS Distributions *
1888cc0b97SApple OSS Distributions * The Original Code and all software distributed under the License are
1988cc0b97SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
2088cc0b97SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
2188cc0b97SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2288cc0b97SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
2388cc0b97SApple OSS Distributions * Please see the License for the specific language governing rights and
2488cc0b97SApple OSS Distributions * limitations under the License.
2588cc0b97SApple OSS Distributions *
2688cc0b97SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
2788cc0b97SApple OSS Distributions */
2888cc0b97SApple OSS Distributions
2988cc0b97SApple OSS Distributions#import "KCDBasicTypeDescription.h"
3088cc0b97SApple OSS Distributions
3188cc0b97SApple OSS Distributionsconst char * name_for_subtype(uint8_t elem_type);
3288cc0b97SApple OSS Distributions
3388cc0b97SApple OSS Distributionsconst char * name_for_subtype(uint8_t elem_type)
3488cc0b97SApple OSS Distributions{
3588cc0b97SApple OSS Distributions    char * retval = "unknown";
3688cc0b97SApple OSS Distributions
3788cc0b97SApple OSS Distributions    switch (elem_type) {
3888cc0b97SApple OSS Distributions        case KC_ST_CHAR: retval = "char"; break;
3988cc0b97SApple OSS Distributions        case KC_ST_INT8: retval = "int8_t"; break;
4088cc0b97SApple OSS Distributions        case KC_ST_UINT8: retval = "uint8_t"; break;
4188cc0b97SApple OSS Distributions        case KC_ST_INT16: retval = "int16_t"; break;
4288cc0b97SApple OSS Distributions        case KC_ST_UINT16: retval = "uint16_t"; break;
4388cc0b97SApple OSS Distributions        case KC_ST_INT32: retval = "int32_t"; break;
4488cc0b97SApple OSS Distributions        case KC_ST_UINT32: retval = "uint32_t"; break;
4588cc0b97SApple OSS Distributions        case KC_ST_INT64: retval = "int64_t"; break;
4688cc0b97SApple OSS Distributions        case KC_ST_UINT64: retval = "uint64_t"; break;
4788cc0b97SApple OSS Distributions
4888cc0b97SApple OSS Distributions        default: retval = "Unknown"; break;
4988cc0b97SApple OSS Distributions    }
5088cc0b97SApple OSS Distributions
5188cc0b97SApple OSS Distributions    return retval;
5288cc0b97SApple OSS Distributions}
5388cc0b97SApple OSS Distributions
5488cc0b97SApple OSS Distributions
5588cc0b97SApple OSS Distributions@interface
5688cc0b97SApple OSS DistributionsKCDBasicTypeDescription () {
5788cc0b97SApple OSS Distributions	unsigned int _typeID;
5888cc0b97SApple OSS Distributions	uint32_t _size;
5988cc0b97SApple OSS Distributions	uint32_t _count;
6088cc0b97SApple OSS Distributions	NSString * _name;
6188cc0b97SApple OSS Distributions	struct kcdata_subtype_descriptor _subtype_desc;
6288cc0b97SApple OSS Distributions}
6388cc0b97SApple OSS Distributions
6488cc0b97SApple OSS Distributions@end
6588cc0b97SApple OSS Distributions
6688cc0b97SApple OSS Distributions@implementation KCDBasicTypeDescription
6788cc0b97SApple OSS Distributions
6888cc0b97SApple OSS Distributions- (id)initWithKCTypeDesc:(kcdata_subtype_descriptor_t)sub_type_desc
6988cc0b97SApple OSS Distributions{
7088cc0b97SApple OSS Distributions	_typeID = sub_type_desc->kcs_elem_type;
7188cc0b97SApple OSS Distributions	_count = kcs_get_elem_count(sub_type_desc);
7288cc0b97SApple OSS Distributions	_size = kcs_get_elem_size(sub_type_desc);
7388cc0b97SApple OSS Distributions
7488cc0b97SApple OSS Distributions	memcpy(&_subtype_desc, sub_type_desc, sizeof(_subtype_desc));
7588cc0b97SApple OSS Distributions	_name = [NSString stringWithFormat:@"%s", _subtype_desc.kcs_name];
7688cc0b97SApple OSS Distributions
7788cc0b97SApple OSS Distributions	return self;
7888cc0b97SApple OSS Distributions}
7988cc0b97SApple OSS Distributions
8088cc0b97SApple OSS Distributions- (id)createDefaultForType:(uint32_t)typeID
8188cc0b97SApple OSS Distributions{
8288cc0b97SApple OSS Distributions	struct kcdata_subtype_descriptor subtype;
8388cc0b97SApple OSS Distributions	subtype.kcs_flags = KCS_SUBTYPE_FLAGS_ARRAY;
8488cc0b97SApple OSS Distributions	subtype.kcs_elem_type = KC_ST_UINT8;
8588cc0b97SApple OSS Distributions	subtype.kcs_elem_offset = 0;
8688cc0b97SApple OSS Distributions	subtype.kcs_elem_size = KCS_SUBTYPE_PACK_SIZE(UINT16_MAX, (uint16_t)sizeof(uint8_t));
8788cc0b97SApple OSS Distributions	subtype.kcs_name[0] = '\0';
8888cc0b97SApple OSS Distributions	(void)[self initWithKCTypeDesc:&subtype];
8988cc0b97SApple OSS Distributions	_name = [NSString stringWithFormat:@"Type_0x%x", typeID];
9088cc0b97SApple OSS Distributions	return self;
9188cc0b97SApple OSS Distributions}
9288cc0b97SApple OSS Distributions
9376e12aa3SApple OSS Distributions#define read_unaligned(type, data) ({ \
9476e12aa3SApple OSS Distributions    type x; \
9576e12aa3SApple OSS Distributions    memcpy((void*)&x, (void*)(data), sizeof(type)); \
9676e12aa3SApple OSS Distributions    x; })
9776e12aa3SApple OSS Distributions
9888cc0b97SApple OSS Distributions- (NSObject *)objectForType:(kctype_subtype_t)elem_type withData:(uint8_t *)data
9988cc0b97SApple OSS Distributions{
10088cc0b97SApple OSS Distributions	NSObject * obj;
10188cc0b97SApple OSS Distributions
10288cc0b97SApple OSS Distributions	switch (elem_type) {
10388cc0b97SApple OSS Distributions	case KC_ST_CHAR: obj = [NSString stringWithFormat:@"%c", *(char *)data]; break;
10476e12aa3SApple OSS Distributions	case KC_ST_INT8: obj =   [NSNumber numberWithInt:read_unaligned(int8_t, data)]; break;
10576e12aa3SApple OSS Distributions	case KC_ST_UINT8: obj =  [NSNumber numberWithInt:read_unaligned(uint8_t, data)]; break;
10676e12aa3SApple OSS Distributions	case KC_ST_INT16: obj =  [NSNumber numberWithShort:read_unaligned(int16_t, data)]; break;
10776e12aa3SApple OSS Distributions	case KC_ST_UINT16: obj = [NSNumber numberWithUnsignedShort:read_unaligned(uint16_t, data)]; break;
10876e12aa3SApple OSS Distributions	case KC_ST_INT32: obj =  [NSNumber numberWithInt:read_unaligned(int32_t, data)]; break;
10976e12aa3SApple OSS Distributions	case KC_ST_UINT32: obj = [NSNumber numberWithUnsignedInt:read_unaligned(uint32_t, data)]; break;
11076e12aa3SApple OSS Distributions	case KC_ST_INT64: obj =  [NSNumber numberWithLongLong:read_unaligned(int64_t, data)]; break;
11176e12aa3SApple OSS Distributions	case KC_ST_UINT64: obj = [NSNumber numberWithUnsignedLongLong:read_unaligned(uint64_t, data)]; break;
11288cc0b97SApple OSS Distributions
11388cc0b97SApple OSS Distributions	default: obj = @"<Unknown error occurred>"; break;
11488cc0b97SApple OSS Distributions	}
11588cc0b97SApple OSS Distributions
11688cc0b97SApple OSS Distributions	return obj;
11788cc0b97SApple OSS Distributions}
11888cc0b97SApple OSS Distributions
11988cc0b97SApple OSS Distributions- (NSDictionary *)parseData:(void *)dataBuffer ofLength:(uint32_t)length
12088cc0b97SApple OSS Distributions{
12188cc0b97SApple OSS Distributions	NSMutableDictionary * retval = [[NSMutableDictionary alloc] init];
12288cc0b97SApple OSS Distributions	if (length <= _subtype_desc.kcs_elem_offset)
12388cc0b97SApple OSS Distributions		return retval;
12488cc0b97SApple OSS Distributions	uint8_t * data = (uint8_t *)dataBuffer;
12588cc0b97SApple OSS Distributions	/*
12688cc0b97SApple OSS Distributions	 * Calculate the maximum number of data elements we can parse, Taking into
12788cc0b97SApple OSS Distributions	 * account the maximum size specified by the type description, and also the
12888cc0b97SApple OSS Distributions	 * actual length of the data buffer and the offset into the buffer where we
12988cc0b97SApple OSS Distributions	 * begin parsing.
13088cc0b97SApple OSS Distributions	 */
13188cc0b97SApple OSS Distributions	uint32_t elem_count = MIN(_count, (length - _subtype_desc.kcs_elem_offset) / (_size / _count));
13288cc0b97SApple OSS Distributions	uint32_t elem_size = _size / _count;
13388cc0b97SApple OSS Distributions	if (elem_count == 0) {
13488cc0b97SApple OSS Distributions		return retval;
13588cc0b97SApple OSS Distributions	}  else if (elem_count == 1) {
13688cc0b97SApple OSS Distributions		retval[_name] = [self objectForType:_subtype_desc.kcs_elem_type withData:&data[_subtype_desc.kcs_elem_offset]];
13788cc0b97SApple OSS Distributions	} else if (_subtype_desc.kcs_elem_type == KC_ST_CHAR) {
13888cc0b97SApple OSS Distributions		char *s = (char *)&data[_subtype_desc.kcs_elem_offset];
139*e7776783SApple OSS Distributions		retval[_name] = [NSString stringWithFormat:@"%.*s", (int)elem_count, s];
14088cc0b97SApple OSS Distributions	} else {
14188cc0b97SApple OSS Distributions		NSMutableArray * objArray = [NSMutableArray arrayWithCapacity:elem_count];
14288cc0b97SApple OSS Distributions		for (unsigned int i = 0; i < elem_count; i++) {
14388cc0b97SApple OSS Distributions			[objArray addObject:[self objectForType:_subtype_desc.kcs_elem_type
14488cc0b97SApple OSS Distributions			                                  withData:&data[(_subtype_desc.kcs_elem_offset + (elem_size * i))]]];
14588cc0b97SApple OSS Distributions		}
14688cc0b97SApple OSS Distributions		retval[_name] = objArray;
14788cc0b97SApple OSS Distributions	}
14888cc0b97SApple OSS Distributions	return retval;
14988cc0b97SApple OSS Distributions}
15088cc0b97SApple OSS Distributions
15188cc0b97SApple OSS Distributions- (NSString *)description
15288cc0b97SApple OSS Distributions{
15388cc0b97SApple OSS Distributions    if (_subtype_desc.kcs_flags & KCS_SUBTYPE_FLAGS_ARRAY) {
15488cc0b97SApple OSS Distributions        return  [NSString stringWithFormat:@"[%d,%d] %s  %s[%d];", _subtype_desc.kcs_elem_offset, kcs_get_elem_size(&_subtype_desc), name_for_subtype(_subtype_desc.kcs_elem_type), _subtype_desc.kcs_name, kcs_get_elem_count(&_subtype_desc) ];
15588cc0b97SApple OSS Distributions    }else {
15688cc0b97SApple OSS Distributions        return [NSString stringWithFormat:@"[%d,%d] %s  %s;", _subtype_desc.kcs_elem_offset, kcs_get_elem_size(&_subtype_desc), name_for_subtype(_subtype_desc.kcs_elem_type), _subtype_desc.kcs_name ];
15788cc0b97SApple OSS Distributions    }
15888cc0b97SApple OSS Distributions	//return [NSString stringWithFormat:@"type: %d => \"%@\" ", [self typeID], [self name]];
15988cc0b97SApple OSS Distributions}
16088cc0b97SApple OSS Distributions
16188cc0b97SApple OSS Distributions- (NSString *)name
16288cc0b97SApple OSS Distributions{
16388cc0b97SApple OSS Distributions	return _name;
16488cc0b97SApple OSS Distributions}
16588cc0b97SApple OSS Distributions
16688cc0b97SApple OSS Distributions- (uint32_t)count
16788cc0b97SApple OSS Distributions{
16888cc0b97SApple OSS Distributions	return _count;
16988cc0b97SApple OSS Distributions}
17088cc0b97SApple OSS Distributions
17188cc0b97SApple OSS Distributions- (unsigned int)typeID
17288cc0b97SApple OSS Distributions{
17388cc0b97SApple OSS Distributions	return _typeID;
17488cc0b97SApple OSS Distributions}
17588cc0b97SApple OSS Distributions
17688cc0b97SApple OSS Distributions- (BOOL) shouldMergeData
17788cc0b97SApple OSS Distributions{
17888cc0b97SApple OSS Distributions	return TRUE;
17988cc0b97SApple OSS Distributions}
18088cc0b97SApple OSS Distributions
18188cc0b97SApple OSS Distributions@end
182