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