• 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 はでっちあげたもの。

   1  
   2  static	char* sBase16Table1 = "0123456789ABCDEF";
   3  static	char* sBase16Table2 = "0123456789abcdef";
   4  
   5  static	NSString*
   6  StringEncodedByHEX( NSData* p )
   7  {	char* w = (char*)p.bytes;
   8  	int wNumOutputBytes = p.length * 2;
   9  	char v[ wNumOutputBytes ];
  10  	for ( int i = 0; i < p.length; i++ )
  11  	{	v[ i * 2 ] = ( w[ i ] >> 4 ) & 0x0f;
  12  		v[ i * 2 + 1 ] = w[ i ] & 0x0f;
  13  	}
  14  	for ( int i = 0; i < wNumOutputBytes; i++ ) v[ i ] = sBase16Table1[ v[ i ] ];
  15  	return [ [ NSString alloc ] initWithBytes:v length:wNumOutputBytes encoding:NSUTF8StringEncoding ];
  16  }
  17  
  18  
  19  static	NSData*
  20  DataDecodedByHEX( NSString* p )
  21  //	If the number of valid characters is odd number, the last one will be ignored.
  22  {	char wBuffer[ p.length ];
  23  	int	wValidCharCounter = 0;
  24  	for ( int i = 0; i < p.length; i++ )
  25  	{	char* wPtr = strchr( sBase16Table1, [ p characterAtIndex:i ] );
  26  		if ( wPtr ) wBuffer[ wValidCharCounter++ ] = wPtr - sBase16Table1;
  27  		else
  28  		{	wPtr = strchr( sBase16Table2, [ p characterAtIndex:i ] );
  29  			if ( wPtr ) wBuffer[ wValidCharCounter++ ] = wPtr - sBase16Table2;
  30  		}
  31  	}
  32  	int wSize = wValidCharCounter * 4 / 8;
  33  	char v[ wSize ];
  34  	for ( int i = 0; i < wSize; i++ ) v[ i ] = ( wBuffer[ i * 2 ] << 4 ) | wBuffer[ i * 2 + 1 ];
  35  	return [ NSData dataWithBytes:v length:wSize ];
  36  }
  37  
  38  static	char* sBase32URLTable1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  39  static	char* sBase32URLTable2 = "abcdefghijklmnopqrstuvwxyz";
  40  
  41  static	NSString*
  42  StringEncodedByBase32URL( NSData* p )
  43  //	Although there is no standard named Base32URL but we need it.
  44  //	The difference with Base32 is that it doesn't use padding charater i.e. '='.
  45  {	int	wNumInputBits = p.length * 8;
  46  	int wNumOutputBytes = ( wNumInputBits + 4 ) / 5;
  47  	char* w = (char*)p.bytes;
  48  	char v[ wNumOutputBytes ];
  49  	for ( int i = 0; i < wNumOutputBytes; i++ ) v[ i ] = 0;
  50  	for ( int i = 0; i < wNumInputBits; i++ ) v[ i / 5 ] |= ( ( w[ i / 8 ] >> ( 7 - i % 8 ) ) & 1 ) << ( 4 - i % 5 );
  51  	for ( int i = 0; i < wNumOutputBytes; i++ ) v[ i ] = sBase32URLTable1[ v[ i ] ];
  52  	return [ [ NSString alloc ] initWithBytes:v length:wNumOutputBytes encoding:NSUTF8StringEncoding ];
  53  }
  54  
  55  static	NSData*
  56  DataDecodedByBase32URL( NSString* p )
  57  {	char wBuffer[ p.length ];
  58  	int	wValidCharCounter = 0;
  59  	for ( int i = 0; i < p.length; i++ )
  60  	{	char* wPtr = strchr( sBase32URLTable1, [ p characterAtIndex:i ] );
  61  		if ( wPtr ) wBuffer[ wValidCharCounter++ ] = wPtr - sBase32URLTable1;
  62  		else
  63  		{	wPtr = strchr( sBase32URLTable2, [ p characterAtIndex:i ] );
  64  			if ( wPtr ) wBuffer[ wValidCharCounter++ ] = wPtr - sBase32URLTable2;
  65  		}
  66  	}
  67  	int wSize = wValidCharCounter * 5 / 8;
  68  	char v[ wSize ];
  69  	for ( int i = 0; i < wSize; i++ ) v[ i ] = 0;
  70  	for ( int i = 0; i < wSize * 8; i++ ) v[ i / 8 ] |= ( wBuffer[ i / 5 ] >> ( 4 - i % 5 ) ) << ( 7 - i  % 8 );
  71  	return [ NSData dataWithBytes:v length:wSize ];
  72  }
  73  
  74  static	char* sBase64URLTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  75  
  76  static	NSString*
  77  StringEncodedByBase64URL( NSData* p )
  78  {	int	wNumInputBits = p.length * 8;
  79  	int wNumOutputBytes = ( wNumInputBits + 5 ) / 6;
  80  	char* w = (char*)p.bytes;
  81  	char v[ wNumOutputBytes ];
  82  	for ( int i = 0; i < wNumOutputBytes; i++ ) v[ i ] = 0;
  83  	for ( int i = 0; i < wNumInputBits; i++ ) v[ i / 6 ] |= ( ( w[ i / 8 ] >> ( 7 - i % 8 ) ) & 1 ) << ( 5 - i % 6 );
  84  	for ( int i = 0; i < wNumOutputBytes; i++ ) v[ i ] = sBase64URLTable[ v[ i ] ];
  85  	return [ [ NSString alloc ] initWithBytes:v length:wNumOutputBytes encoding:NSUTF8StringEncoding ];
  86  }
  87  
  88  static	NSData*
  89  DataDecodedByBase64URL( NSString* p )
  90  {	char wBuffer[ p.length ];
  91  	int	wValidCharCounter = 0;
  92  	for ( int i = 0; i < p.length; i++ )
  93  	{	char* wPtr = strchr( sBase64URLTable, [ p characterAtIndex:i ] );
  94  		if ( wPtr ) wBuffer[ wValidCharCounter++ ] = wPtr - sBase64URLTable;
  95  	}
  96  	int wSize = wValidCharCounter * 6 / 8;
  97  	char v[ wSize ];
  98  	for ( int i = 0; i < wSize; i++ ) v[ i ] = 0;
  99  	for ( int i = 0; i < wSize * 8; i++ ) v[ i / 8 ] |= ( wBuffer[ i / 6 ] >> ( 5 - i % 6 ) ) << ( 7 - i  % 8 );
 100  	return [ NSData dataWithBytes:v length:wSize ];
 101  }
 102  

posted by Face Saturn on Mon 7 May 2012 at 13:22

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry2118
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ