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