1*1b191cb5SApple OSS Distributions// 2*1b191cb5SApple OSS Distributions// main.m 3*1b191cb5SApple OSS Distributions// kdd command 4*1b191cb5SApple OSS Distributions// 5*1b191cb5SApple OSS Distributions// Created by Lawrence D'Anna on 11/9/15. 6*1b191cb5SApple OSS Distributions// Copyright © 2015 Apple Inc. All rights reserved. 7*1b191cb5SApple OSS Distributions// 8*1b191cb5SApple OSS Distributions 9*1b191cb5SApple OSS Distributions#import <Foundation/Foundation.h> 10*1b191cb5SApple OSS Distributions#include <stdio.h> 11*1b191cb5SApple OSS Distributions#include <unistd.h> 12*1b191cb5SApple OSS Distributions#include <zlib.h> 13*1b191cb5SApple OSS Distributions#import "kdd.h" 14*1b191cb5SApple OSS Distributions 15*1b191cb5SApple OSS Distributionsvoid usage(char *const* argv) { 16*1b191cb5SApple OSS Distributions fprintf(stderr, "usage: %s [-p] FILE\n", argv[0]); 17*1b191cb5SApple OSS Distributions exit(1); 18*1b191cb5SApple OSS Distributions} 19*1b191cb5SApple OSS Distributions 20*1b191cb5SApple OSS Distributionsint main(int argc, char *const*argv) { 21*1b191cb5SApple OSS Distributions 22*1b191cb5SApple OSS Distributions int c ; 23*1b191cb5SApple OSS Distributions int plist = 0; 24*1b191cb5SApple OSS Distributions 25*1b191cb5SApple OSS Distributions while ((c = getopt(argc, argv, "p")) != EOF) { 26*1b191cb5SApple OSS Distributions switch(c) { 27*1b191cb5SApple OSS Distributions case 'p': 28*1b191cb5SApple OSS Distributions plist = TRUE; 29*1b191cb5SApple OSS Distributions break; 30*1b191cb5SApple OSS Distributions case '?': 31*1b191cb5SApple OSS Distributions case 'h': 32*1b191cb5SApple OSS Distributions default: 33*1b191cb5SApple OSS Distributions usage(argv); 34*1b191cb5SApple OSS Distributions break; 35*1b191cb5SApple OSS Distributions } 36*1b191cb5SApple OSS Distributions } 37*1b191cb5SApple OSS Distributions 38*1b191cb5SApple OSS Distributions if (optind != argc -1) { 39*1b191cb5SApple OSS Distributions usage(argv); 40*1b191cb5SApple OSS Distributions } 41*1b191cb5SApple OSS Distributions 42*1b191cb5SApple OSS Distributions NSError *error = nil; 43*1b191cb5SApple OSS Distributions NSData *data; 44*1b191cb5SApple OSS Distributions 45*1b191cb5SApple OSS Distributions if (0 == strcmp(argv[optind], "-")) { 46*1b191cb5SApple OSS Distributions data = [[NSFileHandle fileHandleWithStandardInput] readDataToEndOfFile]; 47*1b191cb5SApple OSS Distributions } else { 48*1b191cb5SApple OSS Distributions data = [NSData dataWithContentsOfFile:[NSString stringWithUTF8String:argv[optind]] 49*1b191cb5SApple OSS Distributions options:NSDataReadingMappedIfSafe 50*1b191cb5SApple OSS Distributions error:&error]; 51*1b191cb5SApple OSS Distributions } 52*1b191cb5SApple OSS Distributions 53*1b191cb5SApple OSS Distributions if (!data || error) { 54*1b191cb5SApple OSS Distributions NSLog(@"couldn't read data: %@", error); 55*1b191cb5SApple OSS Distributions return 1; 56*1b191cb5SApple OSS Distributions } 57*1b191cb5SApple OSS Distributions 58*1b191cb5SApple OSS Distributions if (data.length > UINT32_MAX) { 59*1b191cb5SApple OSS Distributions NSLog(@"data too big"); 60*1b191cb5SApple OSS Distributions return 1; 61*1b191cb5SApple OSS Distributions } 62*1b191cb5SApple OSS Distributions 63*1b191cb5SApple OSS Distributions NSDictionary *dict = parseKCDataBuffer((void*)data.bytes, (uint32_t)data.length, &error); 64*1b191cb5SApple OSS Distributions 65*1b191cb5SApple OSS Distributions if (error && error.code == KERN_INVALID_VALUE) { 66*1b191cb5SApple OSS Distributions uint8_t buffer[100]; 67*1b191cb5SApple OSS Distributions z_stream stream; 68*1b191cb5SApple OSS Distributions bzero(&stream, sizeof(stream)); 69*1b191cb5SApple OSS Distributions stream.next_in = (void*) data.bytes; 70*1b191cb5SApple OSS Distributions stream.avail_in = data.length; 71*1b191cb5SApple OSS Distributions stream.next_out = buffer; 72*1b191cb5SApple OSS Distributions stream.avail_out = sizeof(buffer); 73*1b191cb5SApple OSS Distributions inflateInit2(&stream, 16+MAX_WBITS); 74*1b191cb5SApple OSS Distributions NSMutableData *inflated = [[NSMutableData alloc] init]; 75*1b191cb5SApple OSS Distributions while (1) { 76*1b191cb5SApple OSS Distributions int z = inflate(&stream, Z_NO_FLUSH); 77*1b191cb5SApple OSS Distributions if (z == Z_OK || z == Z_STREAM_END) { 78*1b191cb5SApple OSS Distributions [inflated appendBytes:buffer length:sizeof(buffer) - stream.avail_out]; 79*1b191cb5SApple OSS Distributions stream.avail_out = sizeof(buffer); 80*1b191cb5SApple OSS Distributions stream.next_out = buffer; 81*1b191cb5SApple OSS Distributions if (z == Z_STREAM_END) { 82*1b191cb5SApple OSS Distributions break; 83*1b191cb5SApple OSS Distributions } 84*1b191cb5SApple OSS Distributions } else { 85*1b191cb5SApple OSS Distributions inflated = nil; 86*1b191cb5SApple OSS Distributions break; 87*1b191cb5SApple OSS Distributions } 88*1b191cb5SApple OSS Distributions } 89*1b191cb5SApple OSS Distributions if (inflated) { 90*1b191cb5SApple OSS Distributions error = nil; 91*1b191cb5SApple OSS Distributions dict = parseKCDataBuffer((void*)inflated.bytes, (uint32_t)inflated.length, &error); 92*1b191cb5SApple OSS Distributions } 93*1b191cb5SApple OSS Distributions } 94*1b191cb5SApple OSS Distributions 95*1b191cb5SApple OSS Distributions if (error && error.code == KERN_INVALID_VALUE) { 96*1b191cb5SApple OSS Distributions NSData *decoded = [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters]; 97*1b191cb5SApple OSS Distributions if (decoded) { 98*1b191cb5SApple OSS Distributions error = nil; 99*1b191cb5SApple OSS Distributions dict = parseKCDataBuffer((void*)decoded.bytes, (uint32_t)decoded.length, &error); 100*1b191cb5SApple OSS Distributions } 101*1b191cb5SApple OSS Distributions } 102*1b191cb5SApple OSS Distributions 103*1b191cb5SApple OSS Distributions if (!dict || error) { 104*1b191cb5SApple OSS Distributions NSLog(@"error parsing kcdata: %@", error); 105*1b191cb5SApple OSS Distributions return 1; 106*1b191cb5SApple OSS Distributions } 107*1b191cb5SApple OSS Distributions 108*1b191cb5SApple OSS Distributions if (plist) { 109*1b191cb5SApple OSS Distributions NSData *plist = [NSPropertyListSerialization dataWithPropertyList:dict 110*1b191cb5SApple OSS Distributions format:NSPropertyListXMLFormat_v1_0 111*1b191cb5SApple OSS Distributions options:0 112*1b191cb5SApple OSS Distributions error:&error]; 113*1b191cb5SApple OSS Distributions if (!plist || error) { 114*1b191cb5SApple OSS Distributions NSLog(@"couldn't write plist: %@", error); 115*1b191cb5SApple OSS Distributions return 1; 116*1b191cb5SApple OSS Distributions } 117*1b191cb5SApple OSS Distributions 118*1b191cb5SApple OSS Distributions fwrite(plist.bytes, plist.length, 1, stdout); 119*1b191cb5SApple OSS Distributions 120*1b191cb5SApple OSS Distributions } else { 121*1b191cb5SApple OSS Distributions puts([[NSString stringWithFormat: @"%@", dict] UTF8String]); 122*1b191cb5SApple OSS Distributions } 123*1b191cb5SApple OSS Distributions 124*1b191cb5SApple OSS Distributions 125*1b191cb5SApple OSS Distributions return 0; 126*1b191cb5SApple OSS Distributions} 127