Base32 URL はでっちあげたもの。
1 2 static char* sBase16Table1 = "0123456789 ABCDEF"; 3 static char* sBase16Tab le2 = "0123456789 abcdef"; 4 5 static NSString* 6 StringEnco dedByHEX( NSData* p ) 7 { char* w = (char*)p.bytes; 8 int wNumOutput Bytes = p.length * 2; 9 char v[ wNumOutput Bytes ]; 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 < wNumOutput Bytes; i++ ) v[ i ] = sBase16Tab le1[ v[ i ] ]; 15 return [ [ NSString alloc ] initWithBy tes:v length:wNumOutput Bytes encoding:NSUTF8Stri ngEncoding ]; 16 } 17 18 19 static NSData* 20 DataDecode dByHEX( 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 wValidChar Counter = 0; 24 for ( int i = 0; i < p.length; i++ ) 25 { char* wPtr = strchr( sBase16Tab le1, [ p characterA tIndex:i ] ); 26 if ( wPtr ) wBuffer[ wValidChar Counter++ ] = wPtr - sBase16Tab le1; 27 else 28 { wPtr = strchr( sBase16Tab le2, [ p characterA tIndex:i ] ); 29 if ( wPtr ) wBuffer[ wValidChar Counter++ ] = wPtr - sBase16Tab le2; 30 } 31 } 32 int wSize = wValidChar Counter * 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 dataWithBy tes:v length:wSize ]; 36 } 37 38 static char* sBase32URL Table1 = "ABCDEFGHIJ KLMNOPQRST UVWXYZ2345 67"; 39 static char* sBase32URL Table2 = "abcdefghij klmnopqrst uvwxyz"; 40 41 static NSString* 42 StringEnco dedByBase3 2URL( 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 wNumInputB its = p.length * 8; 46 int wNumOutput Bytes = ( wNumInputB its + 4 ) / 5; 47 char* w = (char*)p.bytes; 48 char v[ wNumOutput Bytes ]; 49 for ( int i = 0; i < wNumOutput Bytes; i++ ) v[ i ] = 0; 50 for ( int i = 0; i < wNumInputB its; i++ ) v[ i / 5 ] |= ( ( w[ i / 8 ] >> ( 7 - i % 8 ) ) & 1 ) << ( 4 - i % 5 ); 51 for ( int i = 0; i < wNumOutput Bytes; i++ ) v[ i ] = sBase32URL Table1[ v[ i ] ]; 52 return [ [ NSString alloc ] initWithBy tes:v length:wNu mOutputByt es encoding:N SUTF8Strin gEncoding ]; 53 } 54 55 static NSData* 56 DataDecode dByBase32U RL( NSString* p ) 57 { char wBuffer[ p.length ]; 58 int wValidChar Counter = 0; 59 for ( int i = 0; i < p.length; i++ ) 60 { char* wPtr = strchr( sBase32URL Table1, [ p characterA tIndex:i ] ); 61 if ( wPtr ) wBuffer[ wValidChar Counter++ ] = wPtr - sBase32URL Table1; 62 else 63 { wPtr = strchr( sBase32URL Table2, [ p characterA tIndex:i ] ); 64 if ( wPtr ) wBuffer[ wValidChar Counter++ ] = wPtr - sBase32URL Table2; 65 } 66 } 67 int wSize = wValidChar Counter * 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 dataWithBy tes:v length:wSi ze ]; 72 } 73 74 static char* sBase64URL Table = "ABCDEFGHIJ KLMNOPQRST UVWXYZabcd efghijklmn opqrstuvwx yz01234567 89-_"; 75 76 static NSString* 77 StringEnco dedByBase6 4URL( NSData* p ) 78 { int wNumInputB its = p.length * 8; 79 int wNumOutput Bytes = ( wNumInputB its + 5 ) / 6; 80 char* w = (char*)p.bytes; 81 char v[ wNumOutput Bytes ]; 82 for ( int i = 0; i < wNumOutput Bytes; i++ ) v[ i ] = 0; 83 for ( int i = 0; i < wNumInputB its; i++ ) v[ i / 6 ] |= ( ( w[ i / 8 ] >> ( 7 - i % 8 ) ) & 1 ) << ( 5 - i % 6 ); 84 for ( int i = 0; i < wNumOutput Bytes; i++ ) v[ i ] = sBase64URL Table[ v[ i ] ]; 85 return [ [ NSString alloc ] initWithBy tes:v length:wNu mOutputByt es encoding:N SUTF8Strin gEncoding ]; 86 } 87 88 static NSData* 89 DataDecode dByBase64U RL( NSString* p ) 90 { char wBuffer[ p.length ]; 91 int wValidChar Counter = 0; 92 for ( int i = 0; i < p.length; i++ ) 93 { char* wPtr = strchr( sBase64URL Table, [ p characterA tIndex:i ] ); 94 if ( wPtr ) wBuffer[ wValidChar Counter++ ] = wPtr - sBase64URL Table; 95 } 96 int wSize = wValidChar Counter * 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 dataWithBy tes:v length:wSi ze ]; 101 } 102
posted by
Saturn
on Mon 7 May 2012
at 13:22