5 // Created by Lawrence D'Anna on 11/9/15.
6 // Copyright © 2015 Apple Inc. All rights reserved.
9 #import <Foundation/Foundation.h>
15 void usage(char *const* argv) {
16 fprintf(stderr, "usage: %s [-p] FILE\n", argv[0]);
20 int main(int argc, char *const*argv) {
25 while ((c = getopt(argc, argv, "p")) != EOF) {
38 if (optind != argc -1) {
45 if (0 == strcmp(argv[optind], "-")) {
46 data = [[NSFileHandle fileHandleWithStandardInput] readDataToEndOfFile];
48 data = [NSData dataWithContentsOfFile:[NSString stringWithUTF8String:argv[optind]]
49 options:NSDataReadingMappedIfSafe
54 NSLog(@"couldn't read data: %@", error);
58 if (data.length > UINT32_MAX) {
59 NSLog(@"data too big");
63 NSDictionary *dict = parseKCDataBuffer((void*)data.bytes, (uint32_t)data.length, &error);
65 if (error && error.code == KERN_INVALID_VALUE) {
68 bzero(&stream, sizeof(stream));
69 stream.next_in = (void*) data.bytes;
70 stream.avail_in = data.length;
71 stream.next_out = buffer;
72 stream.avail_out = sizeof(buffer);
73 inflateInit2(&stream, 16+MAX_WBITS);
74 NSMutableData *inflated = [[NSMutableData alloc] init];
76 int z = inflate(&stream, Z_NO_FLUSH);
77 if (z == Z_OK || z == Z_STREAM_END) {
78 [inflated appendBytes:buffer length:sizeof(buffer) - stream.avail_out];
79 stream.avail_out = sizeof(buffer);
80 stream.next_out = buffer;
81 if (z == Z_STREAM_END) {
91 dict = parseKCDataBuffer((void*)inflated.bytes, (uint32_t)inflated.length, &error);
95 if (error && error.code == KERN_INVALID_VALUE) {
96 NSData *decoded = [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
99 dict = parseKCDataBuffer((void*)decoded.bytes, (uint32_t)decoded.length, &error);
103 if (!dict || error) {
104 NSLog(@"error parsing kcdata: %@", error);
109 NSData *plist = [NSPropertyListSerialization dataWithPropertyList:dict
110 format:NSPropertyListXMLFormat_v1_0
113 if (!plist || error) {
114 NSLog(@"couldn't write plist: %@", error);
118 fwrite(plist.bytes, plist.length, 1, stdout);
121 puts([[NSString stringWithFormat: @"%@", dict] UTF8String]);