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