• 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

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 ];
}

<<--

posted by Saturn Saturn on Mon 7 May 2012 at 13:22 with 0 comments
Contents rssrss
Value Transformer
AES & Blowfish in swift (CBC)
OreOre.pem の作り方
JSON Chat Server Skelton in GoLang TLS Ver.
JSON Chat Server Skelton in GoLang
UITextView と、UITextField でキーボードが出た時の Notification の順番が違う
Making window which covers iPhone's Status Bar
UIImagePickerController の編集領域
UIView の tag property
UILocationManager
Services from s21g
MagicBag
MagicBagは、Cloudflare R2やS3互換ストレージをMacのFinderから扱えるファイル同期クライアントです。
アンテナショップめぐり
東京にある全国のアンテナショップ、イベント、特産品を地図や条件から探せるサービスです。
YOMU Web小説リーダー
YOMUは、Web小説の読み上げに特化したiPhoneアプリです。
補助探
公開されている補助金・助成金情報を集約し、条件に合う制度を探しやすくするサービスです。
jotter.me
jotter.meは、考えやアイデア、情報を書き留めるためのノートサービスです。