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