xref: /xnu-8796.121.2/libkdd/kdd.h (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 #ifndef _KDD_H_
30*c54f35caSApple OSS Distributions #define _KDD_H_
31*c54f35caSApple OSS Distributions 
32*c54f35caSApple OSS Distributions #import <Foundation/Foundation.h>
33*c54f35caSApple OSS Distributions #import <kcdata.h>
34*c54f35caSApple OSS Distributions 
35*c54f35caSApple OSS Distributions /*!
36*c54f35caSApple OSS Distributions  * @class KCDataType
37*c54f35caSApple OSS Distributions  * A basic abstraction that allows for parsing data provided by kernel chunked
38*c54f35caSApple OSS Distributions  * data library.
39*c54f35caSApple OSS Distributions  *
40*c54f35caSApple OSS Distributions  * @discussion
41*c54f35caSApple OSS Distributions  * Each type object has a name and a method to parse and populate data in memory to
42*c54f35caSApple OSS Distributions  * a dictionary. The dictionary will have keys as NSStrings and values could be NSObject
43*c54f35caSApple OSS Distributions  *
44*c54f35caSApple OSS Distributions  */
45*c54f35caSApple OSS Distributions @interface KCDataType : NSObject
46*c54f35caSApple OSS Distributions - (NSDictionary * _Nullable)parseData:(void * _Nonnull)dataBuffer ofLength:(uint32_t)length NS_RETURNS_RETAINED;
47*c54f35caSApple OSS Distributions - (NSString * _Nonnull)name;
48*c54f35caSApple OSS Distributions - (unsigned int)typeID;
49*c54f35caSApple OSS Distributions - (BOOL) shouldMergeData;
50*c54f35caSApple OSS Distributions @end
51*c54f35caSApple OSS Distributions 
52*c54f35caSApple OSS Distributions /*!
53*c54f35caSApple OSS Distributions  * @function getKCDataTypeForID
54*c54f35caSApple OSS Distributions  *
55*c54f35caSApple OSS Distributions  * @abstract
56*c54f35caSApple OSS Distributions  * Find a type description for give TypeID
57*c54f35caSApple OSS Distributions  *
58*c54f35caSApple OSS Distributions  * @param typeID
59*c54f35caSApple OSS Distributions  * A unsinged int type specified by the KCDATA.
60*c54f35caSApple OSS Distributions  *
61*c54f35caSApple OSS Distributions  * @discussion
62*c54f35caSApple OSS Distributions  * This routine queries the system for a give type. If a known type description is found it will be used to
63*c54f35caSApple OSS Distributions  * initialize a KCDataType object. If no known type is found it assumes the data is uint8_t[].
64*c54f35caSApple OSS Distributions  */
65*c54f35caSApple OSS Distributions KCDataType * _Nullable getKCDataTypeForID(uint32_t typeID);
66*c54f35caSApple OSS Distributions 
67*c54f35caSApple OSS Distributions /*!
68*c54f35caSApple OSS Distributions  * @function KCDataTypeNameForID
69*c54f35caSApple OSS Distributions  *
70*c54f35caSApple OSS Distributions  * @abstract
71*c54f35caSApple OSS Distributions  * Get a name for the type.
72*c54f35caSApple OSS Distributions  *
73*c54f35caSApple OSS Distributions  * @param typeID
74*c54f35caSApple OSS Distributions  * A unsinged int type specified by the KCDATA.
75*c54f35caSApple OSS Distributions  *
76*c54f35caSApple OSS Distributions  * @return NSString *
77*c54f35caSApple OSS Distributions  * Returns name of the type. If a type is not found the return
78*c54f35caSApple OSS Distributions  * value will be string object of the passed value.
79*c54f35caSApple OSS Distributions  */
80*c54f35caSApple OSS Distributions NSString * _Nonnull KCDataTypeNameForID(uint32_t typeID) NS_RETURNS_NOT_RETAINED;
81*c54f35caSApple OSS Distributions 
82*c54f35caSApple OSS Distributions /*!
83*c54f35caSApple OSS Distributions  * @function parseKCDataArray
84*c54f35caSApple OSS Distributions  *
85*c54f35caSApple OSS Distributions  * @abstract
86*c54f35caSApple OSS Distributions  * Parse the given KCDATA buffer as an Array of element. The buffer should begin with header
87*c54f35caSApple OSS Distributions  * of type KCDATA_TYPE_ARRAY.
88*c54f35caSApple OSS Distributions  *
89*c54f35caSApple OSS Distributions  * @param iter
90*c54f35caSApple OSS Distributions  * An iterator into the input buffer
91*c54f35caSApple OSS Distributions  *
92*c54f35caSApple OSS Distributions  * @param error
93*c54f35caSApple OSS Distributions  * Error return.
94*c54f35caSApple OSS Distributions  *
95*c54f35caSApple OSS Distributions  * @return
96*c54f35caSApple OSS Distributions  * A dictionary with  key specifying name of the type of each elements and value is an Array of data.
97*c54f35caSApple OSS Distributions  *
98*c54f35caSApple OSS Distributions  */
99*c54f35caSApple OSS Distributions 
100*c54f35caSApple OSS Distributions NSMutableDictionary * _Nullable parseKCDataArray(kcdata_iter_t iter, NSError * _Nullable * _Nullable error) NS_RETURNS_RETAINED;
101*c54f35caSApple OSS Distributions 
102*c54f35caSApple OSS Distributions /*!
103*c54f35caSApple OSS Distributions  * @function parseKCDataContainer
104*c54f35caSApple OSS Distributions  *
105*c54f35caSApple OSS Distributions  * @abstract
106*c54f35caSApple OSS Distributions  * Parse the given KCDATA buffer as a container and convert each sub structures as fields in a dictionary.
107*c54f35caSApple OSS Distributions  *
108*c54f35caSApple OSS Distributions  * @param iter
109*c54f35caSApple OSS Distributions  * A pointer to an iterator into the input buffer.  The iterator will be updated
110*c54f35caSApple OSS Distributions  * to point at the container end marker.
111*c54f35caSApple OSS Distributions  *
112*c54f35caSApple OSS Distributions  * @param error
113*c54f35caSApple OSS Distributions  * Error return.
114*c54f35caSApple OSS Distributions  *
115*c54f35caSApple OSS Distributions  * @return NSDictionary *
116*c54f35caSApple OSS Distributions  * containing each field and potentially sub containers within the provided container.
117*c54f35caSApple OSS Distributions  *
118*c54f35caSApple OSS Distributions  * @discussion
119*c54f35caSApple OSS Distributions  * This function tries to parse one container. If it encounters sub containers
120*c54f35caSApple OSS Distributions  * they will be parsed and collected within the same dictionary.
121*c54f35caSApple OSS Distributions  * Other data type fields will also be parsed based on their type.
122*c54f35caSApple OSS Distributions  *
123*c54f35caSApple OSS Distributions  */
124*c54f35caSApple OSS Distributions 
125*c54f35caSApple OSS Distributions NSMutableDictionary * _Nullable parseKCDataContainer(kcdata_iter_t * _Nonnull iter, NSError * _Nullable * _Nullable error) NS_RETURNS_RETAINED;
126*c54f35caSApple OSS Distributions 
127*c54f35caSApple OSS Distributions /*!
128*c54f35caSApple OSS Distributions  * @function parseKCDataBuffer
129*c54f35caSApple OSS Distributions  *
130*c54f35caSApple OSS Distributions  * @abstract
131*c54f35caSApple OSS Distributions  * Parse complete KCDATA buffer into NSMutableDictionary. Depending on the size of buffer and elements
132*c54f35caSApple OSS Distributions  * this routine makes allocations for objects and strings.
133*c54f35caSApple OSS Distributions  *
134*c54f35caSApple OSS Distributions  * @param dataBuffer
135*c54f35caSApple OSS Distributions  * A pointer in memory where KCDATA is allocated. The data should be of type
136*c54f35caSApple OSS Distributions  * kcdata_item_t and have KCDATA_BUFFER_BEGIN_* tags (see kern_cdata.h)
137*c54f35caSApple OSS Distributions  *
138*c54f35caSApple OSS Distributions  * @param size
139*c54f35caSApple OSS Distributions  * Size of the buffer as provided by kernel api.
140*c54f35caSApple OSS Distributions  *
141*c54f35caSApple OSS Distributions  * @return NSDictionary *
142*c54f35caSApple OSS Distributions  * Dictionary with key:value pairs for each data item. KCDATA_TYPE_ARRAY and KCDATA_TYPE_CONTAINERS will
143*c54f35caSApple OSS Distributions  * grouped and recursed as much possible. For unknown types NSData object is returned with "Type_0x123"
144*c54f35caSApple OSS Distributions  * as keys.
145*c54f35caSApple OSS Distributions  *
146*c54f35caSApple OSS Distributions  * @discussion
147*c54f35caSApple OSS Distributions  * This function tries to parse KCDATA buffer with known type description. If an error occurs,
148*c54f35caSApple OSS Distributions  * NULL is returned, and error (if not NULL) will have the error string.
149*c54f35caSApple OSS Distributions  *
150*c54f35caSApple OSS Distributions  * Iff the buffer does begin with a known kcdata magic number, the error code
151*c54f35caSApple OSS Distributions  * will be KERN_INVALID_VALUE.
152*c54f35caSApple OSS Distributions  *
153*c54f35caSApple OSS Distributions  */
154*c54f35caSApple OSS Distributions NSDictionary * _Nullable parseKCDataBuffer(void * _Nonnull dataBuffer, uint32_t size, NSError * _Nullable * _Nullable error) NS_RETURNS_RETAINED;
155*c54f35caSApple OSS Distributions 
156*c54f35caSApple OSS Distributions 
157*c54f35caSApple OSS Distributions #endif /* _KDD_H_ */
158