似乎base64编码和解码不再是问题但是......
有人有ascii85编码器/解码器的实现或知道一个库吗?
PS:BASE85 用于例如RFC1924
这是我自己做的一个objective-c移植。有用。
static unsigned long pow85[] = {
85*85*85*85, 85*85*85, 85*85, 85, 1
};
void wput(unsigned char* output, unsigned long tuple, int bytes) {
for( int i=0; i<bytes; i++ )
output[i] = (tuple>>((3-i)*8)) & 0xFF;
}
int decode85(const char* input, unsigned char *output) {
unsigned long tuple = 0;
int c, count = 0, posInput = 0, posOutput = 0;
for (;;)
switch (c = input[posInput++]) {
default:
if (c < '!' || c > 'u') {
NSLog(@"bad character in ascii85 region: %#o", c);
return -1;
}
tuple += (c - '!') * pow85[count++];
if (count == 5) {
wput(output+posOutput, tuple, 4);
posOutput += 4;
count = 0;
tuple = 0;
}
break;
case 'z':
if (count != 0) {
NSLog(@"z inside ascii85 5-tuple");
return -1;
}
output[posOutput++] = '\0';
output[posOutput++] = '\0';
output[posOutput++] = '\0';
output[posOutput++] = '\0';
break;
case '~':
if ((input[posInput] == '>')||(input[posInput] == '\0')) {
posInput++;
if (count > 0) {
count--;
tuple += pow85[count];
wput(output+posOutput, tuple, count);
posOutput += count;
}
c = input[posInput++];
return posOutput;
}
NSLog(@"~ without > in ascii85 section");
return -1;
case '\n': case '\r': case '\t': case ' ':
case '\0': case '\f': case '\b': case 0177:
break;
case EOF:
NSLog(@"EOF inside ascii85 section");
return -1;
}
}
这是要包含在您的类中的 Objective-C 包装器:
+(NSData*)decodeAscii85:(NSString*) sEncoded
{
char* sBuffer = (char*)malloc(sEncoded.length+1);
if( ![sEncoded getCString:sBuffer maxLength:(sEncoded.length+1) encoding:NSASCIIStringEncoding] )
{
free( sBuffer );
return nil;
}
unsigned char* sOutBuffer = (unsigned char*)malloc(sEncoded.length+1);
int nLength = decode85( sBuffer, sOutBuffer );
free(sBuffer);
if( nLength == -1)
{
free( sOutBuffer );
return nil;
}
return [NSData dataWithBytesNoCopy:sOutBuffer length:nLength freeWhenDone:YES];
}