static char exchargeTable[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static unsigned char base64DecodeLookup[256] = {
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx,
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx,
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, 62,xx, xx, xx, 63,
52, 53,54, 55, 56, 57, 58,59, 60, 61, xx, xx,xx, xx, xx, xx,
xx, 0, 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, xx,xx, xx, xx, xx,
xx, 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, xx,xx, xx, xx, xx,
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx,
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx,
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx,
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx,
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx,
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx,
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx,
xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx, xx,xx, xx, xx, xx,
};
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation NSString (Base64)
- (NSString*)base64Encode{
NSString *ret = nil;
NSMutableString *codeString = [NSMutableStringstring];
int lenMod = [selflength]%3;
for (int i =0; i <(int)([selflength]/3) ; i++) {
int index = i *3;
[codeString appendFormat:@"%c",exchargeTable[[selfcharacterAtIndex:index]>>2]];
[codeString appendFormat:@"%c",exchargeTable[(([selfcharacterAtIndex:index]&0x03)<<4)|(([selfcharacterAtIndex:index+1]&0xF0)>>4)]];
[codeString appendFormat:@"%c",exchargeTable[(([selfcharacterAtIndex:index+1]&0x0F)<<2)|([selfcharacterAtIndex:index+2]>>6)]];
[codeString appendFormat:@"%c",exchargeTable[[selfcharacterAtIndex:index+2]&0x3F]];
}
if (lenMod != 0) {
int startIndex = ((int)[selflength])/3 *3;
[codeString appendFormat:@"%c",exchargeTable[[selfcharacterAtIndex:startIndex]>>2]];
if (lenMod > 1) {
[codeString appendFormat:@"%c",exchargeTable[(([selfcharacterAtIndex:startIndex]&0x03)<<4)|(([selfcharacterAtIndex:startIndex+1]&0xF0)>>4)]];
[codeString appendFormat:@"%c",exchargeTable[([selfcharacterAtIndex:startIndex+1]&0x0F)<<2]];
}else{
[codeString appendFormat:@"%c",exchargeTable[([selfcharacterAtIndex:startIndex]&0x03)<<4]];
[codeString appendFormat:@"%c",'='];
}
[codeString appendFormat:@"%c",'='];
}
ret = [NSStringstringWithString:codeString];
return ret;
}
- (NSString*)base64Decode{
NSString *ret = nil;
NSMutableString *decodeString = [NSMutableStringstring];
int lenMod = [selflength]%4;
if (lenMod == 0) {
for (int i =0; i < (int)([selflength]/4); i++) {
int index = i*4;
int char1Pos = base64DecodeLookup[[selfcharacterAtIndex:index]];
int char2Pos = base64DecodeLookup[[selfcharacterAtIndex:index+1]];
int char3Pos = base64DecodeLookup[[selfcharacterAtIndex:index+2]];
int char4Pos = base64DecodeLookup[[selfcharacterAtIndex:index+3]];
[decodeString appendFormat:@"%c",((char1Pos<<2)&0xFC)|((char2Pos&0x30)>>4)];
if ([selfcharacterAtIndex:index+2] !='=') {
[decodeString appendFormat:@"%c",((char2Pos&0x0F)<<4)|((char3Pos>>2)&0x0F)];
if ([selfcharacterAtIndex:index+3] =='=') {
[decodeString appendFormat:@"%c",(char3Pos&0x03)<<6];
}else{
[decodeString appendFormat:@"%c",((char3Pos&0x03)<<6)|(char4Pos&0x3F)];
}
}else{
[decodeString appendFormat:@"%c",(char2Pos&0x0F)<<4];
}
}
}
ret = [NSStringstringWithString:decodeString];
return ret;
}
@end