-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNSData+Utils.m
113 lines (95 loc) · 2.79 KB
/
NSData+Utils.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// NSData+Utils.m
// ASN.1 Dump
//
// Created by Chris on 03/08/2008.
// Copyright 2008-2012 Chris Ridd. All rights reserved.
//
#import "NSData+Utils.h"
@implementation NSData (Utils)
- (NSInteger)byteAtOffset:(NSInteger)p
{
char c;
[self getBytes: &c range:NSMakeRange(p,1)];
return (NSInteger)c & 0xff;
}
// PEM files look like (according to OpenSSL crypto/pem/pem_lib.c, and a quick
// look at RFC 1421)
// -----BEGIN(stuff)-----\n
// key:value\n (optional, multiple)
// \n(blank line)
// base64-encoded data
// -----END(stuff)-----\n
- (NSData *)dataFromPEMData
{
NSInteger length = [self length];
const char *bytes = [self bytes];
if (length > strlen("-----BEGIN") &&
strncmp(bytes, "-----BEGIN", strlen("-----BEGIN")) != 0)
return nil;
NSInteger i;
for (i = strlen("-----BEGIN"); i < length - 1; i++) {
if (strncmp(bytes + i, "\n\n", strlen("\n\n")) == 0) {
NSLog(@"Base64 data probably at %ld", i + 2);
NSInteger j;
for (j = length - 1 - strlen("\n-----END"); j > i + 2; j--) {
if (strncmp(bytes + j, "\n-----END", strlen("\n-----END")) == 0) {
NSLog(@"Base64 ends at %ld", j - 1);
NSMutableData *d = [[NSMutableData alloc] initWithBase64Bytes:bytes + i + 2
length: j - (i + 2)];
return [d autorelease];
}
}
// not found the -----END
return nil;
}
}
return nil;
}
- (id)initWithBase64Bytes:(const char *)base64Bytes
length:(unsigned long)lentext
{
unsigned long ixtext = 0;
unsigned char ch = 0;
unsigned char inbuf[4], outbuf[3];
short i = 0, ixinbuf = 0;
BOOL flignore = NO;
BOOL flendtext = NO;
NSMutableData *mutableData = [NSMutableData dataWithCapacity:lentext];
while (YES) {
if (ixtext >= lentext) break;
ch = base64Bytes[ixtext++];
flignore = NO;
if ((ch >= 'A') && (ch <= 'Z')) ch = ch - 'A';
else if ((ch >= 'a') && (ch <= 'z')) ch = ch - 'a' + 26;
else if ((ch >= '0') && (ch <= '9')) ch = ch - '0' + 52;
else if (ch == '+') ch = 62;
else if (ch == '=') flendtext = YES;
else if (ch == '/') ch = 63;
else flignore = YES;
if (!flignore) {
short ctcharsinbuf = 3;
BOOL flbreak = NO;
if (flendtext) {
if (!ixinbuf) break;
if ((ixinbuf == 1) || (ixinbuf == 2)) ctcharsinbuf = 1;
else ctcharsinbuf = 2;
ixinbuf = 3;
flbreak = YES;
}
inbuf[ixinbuf++] = ch;
if (ixinbuf == 4) {
ixinbuf = 0;
outbuf[0] = (inbuf[0] << 2) | ((inbuf[1] & 0x30) >> 4);
outbuf[1] = ((inbuf[1] & 0x0F) << 4) | ((inbuf[2] & 0x3C) >> 2);
outbuf[2] = ((inbuf[2] & 0x03) << 6) | (inbuf[3] & 0x3F);
for (i = 0; i < ctcharsinbuf; i++)
[mutableData appendBytes:&outbuf[i] length:1];
}
if (flbreak) break;
}
}
self = [self initWithData:mutableData];
return self;
}
@end