xref: /xnu-10063.141.1/libkdd/kdd.h (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions /*
2*d8b80295SApple OSS Distributions  * Copyright (c) 2015 Apple Inc. All rights reserved.
3*d8b80295SApple OSS Distributions  *
4*d8b80295SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*d8b80295SApple OSS Distributions  *
6*d8b80295SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*d8b80295SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*d8b80295SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*d8b80295SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*d8b80295SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*d8b80295SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*d8b80295SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*d8b80295SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*d8b80295SApple OSS Distributions  *
15*d8b80295SApple OSS Distributions  * Please obtain a copy of the License at
16*d8b80295SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*d8b80295SApple OSS Distributions  *
18*d8b80295SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*d8b80295SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*d8b80295SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*d8b80295SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*d8b80295SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*d8b80295SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*d8b80295SApple OSS Distributions  * limitations under the License.
25*d8b80295SApple OSS Distributions  *
26*d8b80295SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*d8b80295SApple OSS Distributions  */
28*d8b80295SApple OSS Distributions 
29*d8b80295SApple OSS Distributions #ifndef _KDD_H_
30*d8b80295SApple OSS Distributions #define _KDD_H_
31*d8b80295SApple OSS Distributions 
32*d8b80295SApple OSS Distributions #import <Foundation/Foundation.h>
33*d8b80295SApple OSS Distributions #import <kcdata.h>
34*d8b80295SApple OSS Distributions 
35*d8b80295SApple OSS Distributions /*!
36*d8b80295SApple OSS Distributions  * @class KCDataType
37*d8b80295SApple OSS Distributions  * A basic abstraction that allows for parsing data provided by kernel chunked
38*d8b80295SApple OSS Distributions  * data library.
39*d8b80295SApple OSS Distributions  *
40*d8b80295SApple OSS Distributions  * @discussion
41*d8b80295SApple OSS Distributions  * Each type object has a name and a method to parse and populate data in memory to
42*d8b80295SApple OSS Distributions  * a dictionary. The dictionary will have keys as NSStrings and values could be NSObject
43*d8b80295SApple OSS Distributions  *
44*d8b80295SApple OSS Distributions  */
45*d8b80295SApple OSS Distributions @interface KCDataType : NSObject
46*d8b80295SApple OSS Distributions - (NSDictionary * _Nullable)parseData:(void * _Nonnull)dataBuffer ofLength:(uint32_t)length NS_RETURNS_RETAINED;
47*d8b80295SApple OSS Distributions - (NSString * _Nonnull)name;
48*d8b80295SApple OSS Distributions - (unsigned int)typeID;
49*d8b80295SApple OSS Distributions - (BOOL) shouldMergeData;
50*d8b80295SApple OSS Distributions @end
51*d8b80295SApple OSS Distributions 
52*d8b80295SApple OSS Distributions /*!
53*d8b80295SApple OSS Distributions  * @function getKCDataTypeForID
54*d8b80295SApple OSS Distributions  *
55*d8b80295SApple OSS Distributions  * @abstract
56*d8b80295SApple OSS Distributions  * Find a type description for give TypeID
57*d8b80295SApple OSS Distributions  *
58*d8b80295SApple OSS Distributions  * @param typeID
59*d8b80295SApple OSS Distributions  * A unsinged int type specified by the KCDATA.
60*d8b80295SApple OSS Distributions  *
61*d8b80295SApple OSS Distributions  * @discussion
62*d8b80295SApple OSS Distributions  * This routine queries the system for a give type. If a known type description is found it will be used to
63*d8b80295SApple OSS Distributions  * initialize a KCDataType object. If no known type is found it assumes the data is uint8_t[].
64*d8b80295SApple OSS Distributions  */
65*d8b80295SApple OSS Distributions KCDataType * _Nullable getKCDataTypeForID(uint32_t typeID);
66*d8b80295SApple OSS Distributions 
67*d8b80295SApple OSS Distributions /*!
68*d8b80295SApple OSS Distributions  * @function KCDataTypeNameForID
69*d8b80295SApple OSS Distributions  *
70*d8b80295SApple OSS Distributions  * @abstract
71*d8b80295SApple OSS Distributions  * Get a name for the type.
72*d8b80295SApple OSS Distributions  *
73*d8b80295SApple OSS Distributions  * @param typeID
74*d8b80295SApple OSS Distributions  * A unsinged int type specified by the KCDATA.
75*d8b80295SApple OSS Distributions  *
76*d8b80295SApple OSS Distributions  * @return NSString *
77*d8b80295SApple OSS Distributions  * Returns name of the type. If a type is not found the return
78*d8b80295SApple OSS Distributions  * value will be string object of the passed value.
79*d8b80295SApple OSS Distributions  */
80*d8b80295SApple OSS Distributions NSString * _Nonnull KCDataTypeNameForID(uint32_t typeID) NS_RETURNS_NOT_RETAINED;
81*d8b80295SApple OSS Distributions 
82*d8b80295SApple OSS Distributions /*!
83*d8b80295SApple OSS Distributions  * @function parseKCDataArray
84*d8b80295SApple OSS Distributions  *
85*d8b80295SApple OSS Distributions  * @abstract
86*d8b80295SApple OSS Distributions  * Parse the given KCDATA buffer as an Array of element. The buffer should begin with header
87*d8b80295SApple OSS Distributions  * of type KCDATA_TYPE_ARRAY.
88*d8b80295SApple OSS Distributions  *
89*d8b80295SApple OSS Distributions  * @param iter
90*d8b80295SApple OSS Distributions  * An iterator into the input buffer
91*d8b80295SApple OSS Distributions  *
92*d8b80295SApple OSS Distributions  * @param error
93*d8b80295SApple OSS Distributions  * Error return.
94*d8b80295SApple OSS Distributions  *
95*d8b80295SApple OSS Distributions  * @return
96*d8b80295SApple OSS Distributions  * A dictionary with  key specifying name of the type of each elements and value is an Array of data.
97*d8b80295SApple OSS Distributions  *
98*d8b80295SApple OSS Distributions  */
99*d8b80295SApple OSS Distributions 
100*d8b80295SApple OSS Distributions NSMutableDictionary * _Nullable parseKCDataArray(kcdata_iter_t iter, NSError * _Nullable * _Nullable error) NS_RETURNS_RETAINED;
101*d8b80295SApple OSS Distributions 
102*d8b80295SApple OSS Distributions /*!
103*d8b80295SApple OSS Distributions  * @function parseKCDataContainer
104*d8b80295SApple OSS Distributions  *
105*d8b80295SApple OSS Distributions  * @abstract
106*d8b80295SApple OSS Distributions  * Parse the given KCDATA buffer as a container and convert each sub structures as fields in a dictionary.
107*d8b80295SApple OSS Distributions  *
108*d8b80295SApple OSS Distributions  * @param iter
109*d8b80295SApple OSS Distributions  * A pointer to an iterator into the input buffer.  The iterator will be updated
110*d8b80295SApple OSS Distributions  * to point at the container end marker.
111*d8b80295SApple OSS Distributions  *
112*d8b80295SApple OSS Distributions  * @param error
113*d8b80295SApple OSS Distributions  * Error return.
114*d8b80295SApple OSS Distributions  *
115*d8b80295SApple OSS Distributions  * @return NSDictionary *
116*d8b80295SApple OSS Distributions  * containing each field and potentially sub containers within the provided container.
117*d8b80295SApple OSS Distributions  *
118*d8b80295SApple OSS Distributions  * @discussion
119*d8b80295SApple OSS Distributions  * This function tries to parse one container. If it encounters sub containers
120*d8b80295SApple OSS Distributions  * they will be parsed and collected within the same dictionary.
121*d8b80295SApple OSS Distributions  * Other data type fields will also be parsed based on their type.
122*d8b80295SApple OSS Distributions  *
123*d8b80295SApple OSS Distributions  */
124*d8b80295SApple OSS Distributions 
125*d8b80295SApple OSS Distributions NSMutableDictionary * _Nullable parseKCDataContainer(kcdata_iter_t * _Nonnull iter, NSError * _Nullable * _Nullable error) NS_RETURNS_RETAINED;
126*d8b80295SApple OSS Distributions 
127*d8b80295SApple OSS Distributions /*!
128*d8b80295SApple OSS Distributions  * @function parseKCDataBuffer
129*d8b80295SApple OSS Distributions  *
130*d8b80295SApple OSS Distributions  * @abstract
131*d8b80295SApple OSS Distributions  * Parse complete KCDATA buffer into NSMutableDictionary. Depending on the size of buffer and elements
132*d8b80295SApple OSS Distributions  * this routine makes allocations for objects and strings.
133*d8b80295SApple OSS Distributions  *
134*d8b80295SApple OSS Distributions  * @param dataBuffer
135*d8b80295SApple OSS Distributions  * A pointer in memory where KCDATA is allocated. The data should be of type
136*d8b80295SApple OSS Distributions  * kcdata_item_t and have KCDATA_BUFFER_BEGIN_* tags (see kern_cdata.h)
137*d8b80295SApple OSS Distributions  *
138*d8b80295SApple OSS Distributions  * @param size
139*d8b80295SApple OSS Distributions  * Size of the buffer as provided by kernel api.
140*d8b80295SApple OSS Distributions  *
141*d8b80295SApple OSS Distributions  * @return NSDictionary *
142*d8b80295SApple OSS Distributions  * Dictionary with key:value pairs for each data item. KCDATA_TYPE_ARRAY and KCDATA_TYPE_CONTAINERS will
143*d8b80295SApple OSS Distributions  * grouped and recursed as much possible. For unknown types NSData object is returned with "Type_0x123"
144*d8b80295SApple OSS Distributions  * as keys.
145*d8b80295SApple OSS Distributions  *
146*d8b80295SApple OSS Distributions  * @discussion
147*d8b80295SApple OSS Distributions  * This function tries to parse KCDATA buffer with known type description. If an error occurs,
148*d8b80295SApple OSS Distributions  * NULL is returned, and error (if not NULL) will have the error string.
149*d8b80295SApple OSS Distributions  *
150*d8b80295SApple OSS Distributions  * Iff the buffer does begin with a known kcdata magic number, the error code
151*d8b80295SApple OSS Distributions  * will be KERN_INVALID_VALUE.
152*d8b80295SApple OSS Distributions  *
153*d8b80295SApple OSS Distributions  */
154*d8b80295SApple OSS Distributions NSDictionary * _Nullable parseKCDataBuffer(void * _Nonnull dataBuffer, uint32_t size, NSError * _Nullable * _Nullable error) NS_RETURNS_RETAINED;
155*d8b80295SApple OSS Distributions 
156*d8b80295SApple OSS Distributions 
157*d8b80295SApple OSS Distributions #endif /* _KDD_H_ */
158