-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathNSString+Extensions.m
96 lines (73 loc) · 2.35 KB
/
NSString+Extensions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// NSString+Extensions.m
// tetherKit
//
// Created by Kevin Bradley on 4/6/13.
// Copyright 2013 FireCore, LLC. All rights reserved.
//
#import "NSString+Extensions.h"
@implementation NSString (specialData)
- (NSString *)stringToPaddedHex
{
return [NSString stringWithFormat:@"%.016lX", (unsigned long)[self integerValue]];
}
- (NSString *)hexToString
{
NSData *theData = [self stringToHexData];
return [[[NSString alloc] initWithData:theData encoding: NSASCIIStringEncoding] autorelease];
}
- (NSData *) stringToHexData
{
int len = [self length] / 2; // Target length
unsigned char *buf = malloc(len);
unsigned char *whole_byte = buf;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [self length] / 2; i++) {
byte_chars[0] = [self characterAtIndex:i*2];
byte_chars[1] = [self characterAtIndex:i*2+1];
*whole_byte = strtol(byte_chars, NULL, 16);
whole_byte++;
}
NSData *data = [NSData dataWithBytes:buf length:len];
free( buf );
return data;
}
@end
@implementation NSXMLDocument (specialData)
- (NSDictionary *)iFaithDictionaryRepresentation
{
NSArray *convertKeys = [NSArray arrayWithObjects:@"bat1", @"recm", @"glyp", @"chg0", @"ibot", @"dtre", @"bat0", @"logo", @"chg1", @"glyc", @"illb", @"batf", @"krnl", @"apticket",nil];
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
NSArray *val=[self nodesForXPath:@"./iFaith/name" error:nil];
NSMutableArray *convertedKeys = [[NSMutableArray alloc] init];
if ([val count]!=0)
{
NSArray *children = [[val objectAtIndex:0] children];
for (NSXMLNode *child in children)
{
//NSLog(@"name: %@, stringVal: %@", [child name], [child stringValue]);
id object = [child stringValue];
NSString *key = [child name];
if ([convertKeys containsObject:key])
{
NSData *newObject = [object stringToHexData];
NSString *newKey = [TSSManager manifestKeyFromiFaithKey:key];
//NSLog(@"newobject; %@ forkey: %@", newObject, key);
object = newObject;
NSLog(@"key %@ to new key: %@", key, newKey);
if (newKey != nil)
{
key = newKey;
[convertedKeys addObject:newKey];
}
}
//
[dict setObject:object forKey:key];
}
[dict setObject:convertedKeys forKey:@"ifaithSigned"];
}
// NSLog(@"dict: %@", dict);
return [dict autorelease];
}
@end