各種エンコード実装
Base32 URL はでっちあげたもの。
ruby>>
static char* sBase16Table1 = "0123456789ABCDEF";
static char* sBase16Table2 = "0123456789abcdef";
static NSString*
StringEncodedByHEX( NSData* p )
{ char* w = (char*)p.bytes;
int wNumOutputBytes = p.length * 2;
char v[ wNumOutputBytes ];
for ( int i = 0; i < p.length; i++ )
{ v[ i * 2 ] = ( w[ i ] >> 4 ) & 0x0f;
v[ i * 2 + 1 ] = w[ i ] & 0x0f;
}
for ( int i = 0; i < wNumOutputBytes; i++ ) v[ i ] = sBase16Table1[ v[ i ] ];
return [ [ NSString alloc ] initWithBytes:v length:wNumOutputBytes encoding:NSUTF8StringEncoding ];
}
static NSData*
DataDecodedByHEX( NSString* p )
// If the number of valid characters is odd number, the last one will be ignored.
{ char wBuffer[ p.length ];
int wValidCharCounter = 0;
for ( int i = 0; i < p.length; i++ )
{ char* wPtr = strchr( sBase16Table1, [ p characterAtIndex:i ] );
if ( wPtr ) wBuffer[ wValidCharCounter++ ] = wPtr - sBase16Table1;
else
{ wPtr = strchr( sBase16Table2, [ p characterAtIndex:i ] );
if ( wPtr ) wBuffer[ wValidCharCounter++ ] = wPtr - sBase16Table2;
}
}
int wSize = wValidCharCounter * 4 / 8;
char v[ wSize ];
for ( int i = 0; i < wSize; i++ ) v[ i ] = ( wBuffer[ i * 2 ] << 4 ) | wBuffer[ i * 2 + 1 ];
return [ NSData dataWithBytes:v length:wSize ];
}
static char* sBase32URLTable1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
static char* sBase32URLTable2 = "abcdefghijklmnopqrstuvwxyz";
static NSString*
StringEncodedByBase32URL( NSData* p )
// Although there is no standard named Base32URL but we need it.
// The difference with Base32 is that it doesn't use padding charater i.e. '='.
{ int wNumInputBits = p.length * 8;
int wNumOutputBytes = ( wNumInputBits + 4 ) / 5;
char* w = (char*)p.bytes;
char v[ wNumOutputBytes ];
for ( int i = 0; i < wNumOutputBytes; i++ ) v[ i ] = 0;
for ( int i = 0; i < wNumInputBits; i++ ) v[ i / 5 ] |= ( ( w[ i / 8 ] >> ( 7 - i % 8 ) ) & 1 ) << ( 4 - i % 5 );
for ( int i = 0; i < wNumOutputBytes; i++ ) v[ i ] = sBase32URLTable1[ v[ i ] ];
return [ [ NSString alloc ] initWithBytes:v length:wNumOutputBytes encoding:NSUTF8StringEncoding ];
}
static NSData*
DataDecodedByBase32URL( NSString* p )
{ char wBuffer[ p.length ];
int wValidCharCounter = 0;
for ( int i = 0; i < p.length; i++ )
{ char* wPtr = strchr( sBase32URLTable1, [ p characterAtIndex:i ] );
if ( wPtr ) wBuffer[ wValidCharCounter++ ] = wPtr - sBase32URLTable1;
else
{ wPtr = strchr( sBase32URLTable2, [ p characterAtIndex:i ] );
if ( wPtr ) wBuffer[ wValidCharCounter++ ] = wPtr - sBase32URLTable2;
}
}
int wSize = wValidCharCounter * 5 / 8;
char v[ wSize ];
for ( int i = 0; i < wSize; i++ ) v[ i ] = 0;
for ( int i = 0; i < wSize * 8; i++ ) v[ i / 8 ] |= ( wBuffer[ i / 5 ] >> ( 4 - i % 5 ) ) << ( 7 - i % 8 );
return [ NSData dataWithBytes:v length:wSize ];
}
static char* sBase64URLTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
static NSString*
StringEncodedByBase64URL( NSData* p )
{ int wNumInputBits = p.length * 8;
int wNumOutputBytes = ( wNumInputBits + 5 ) / 6;
char* w = (char*)p.bytes;
char v[ wNumOutputBytes ];
for ( int i = 0; i < wNumOutputBytes; i++ ) v[ i ] = 0;
for ( int i = 0; i < wNumInputBits; i++ ) v[ i / 6 ] |= ( ( w[ i / 8 ] >> ( 7 - i % 8 ) ) & 1 ) << ( 5 - i % 6 );
for ( int i = 0; i < wNumOutputBytes; i++ ) v[ i ] = sBase64URLTable[ v[ i ] ];
return [ [ NSString alloc ] initWithBytes:v length:wNumOutputBytes encoding:NSUTF8StringEncoding ];
}
static NSData*
DataDecodedByBase64URL( NSString* p )
{ char wBuffer[ p.length ];
int wValidCharCounter = 0;
for ( int i = 0; i < p.length; i++ )
{ char* wPtr = strchr( sBase64URLTable, [ p characterAtIndex:i ] );
if ( wPtr ) wBuffer[ wValidCharCounter++ ] = wPtr - sBase64URLTable;
}
int wSize = wValidCharCounter * 6 / 8;
char v[ wSize ];
for ( int i = 0; i < wSize; i++ ) v[ i ] = 0;
for ( int i = 0; i < wSize * 8; i++ ) v[ i / 8 ] |= ( wBuffer[ i / 6 ] >> ( 5 - i % 6 ) ) << ( 7 - i % 8 );
return [ NSData dataWithBytes:v length:wSize ];
}
<<--