-
Notifications
You must be signed in to change notification settings - Fork 2
/
NSData+QBase64.m
78 lines (63 loc) · 1.62 KB
/
NSData+QBase64.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
//
// NSData+QBase64.m
// QWeiboSDK4iOS
//
// Created on 11-1-12.
//
//
#import "NSData+QBase64.h"
@implementation NSData (QBase64)
#define CHAR64(c) (index_64[(unsigned char)(c)])
#define BASE64_GETC (length > 0 ? (length--, bytes++, (unsigned int)(bytes[-1])) : (unsigned int)EOF)
#define BASE64_PUTC(c) [buffer appendBytes: &c length: 1]
static char basis_64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static inline void output64Chunk( int c1, int c2, int c3, int pads, NSMutableData * buffer )
{
char pad = '=';
BASE64_PUTC(basis_64[c1 >> 2]);
BASE64_PUTC(basis_64[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]);
switch ( pads )
{
case 2:
BASE64_PUTC(pad);
BASE64_PUTC(pad);
break;
case 1:
BASE64_PUTC(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)]);
BASE64_PUTC(pad);
break;
default:
case 0:
BASE64_PUTC(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)]);
BASE64_PUTC(basis_64[c3 & 0x3F]);
break;
}
}
- (NSString *) base64EncodedString
{
NSMutableData * buffer = [NSMutableData data];
const unsigned char * bytes;
NSUInteger length;
unsigned int c1, c2, c3;
bytes = [self bytes];
length = [self length];
while ( (c1 = BASE64_GETC) != (unsigned int)EOF )
{
c2 = BASE64_GETC;
if ( c2 == (unsigned int)EOF )
{
output64Chunk( c1, 0, 0, 2, buffer );
}
else
{
c3 = BASE64_GETC;
if ( c3 == (unsigned int)EOF )
output64Chunk( c1, c2, 0, 1, buffer );
else
output64Chunk( c1, c2, c3, 0, buffer );
}
}
return ( [[[NSString allocWithZone: [self zone]] initWithData: buffer encoding: NSASCIIStringEncoding] autorelease] );
}
@end