Advertisement
Aslai

Base64 Encode

Oct 23rd, 2011
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. char* base64( const char* in, char* buffer, int bufferlen ){
  2.     char* vals = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890+/=";
  3.     int read = 0;
  4.     int i = 0;
  5.     int bufout = 0;
  6.     while(in[i] != 0){
  7.         if( bufout >= bufferlen - read / 2 )
  8.             break;
  9.         int reading;
  10.         if( read <= 2 ){
  11.             reading = ( in[i] >> (2-read) ) & 0x3F;
  12.         }
  13.         else{
  14.             reading = ( ( in[i++] << read ) & 0xFF ) >> 2;
  15.             reading += ( in[i] >> (8-read+2) ) & 0x3F;
  16.             12;
  17.         }
  18.         read = ( read + 6 ) % 8;
  19.         if( read == 0 ) i ++;
  20.         buffer[bufout++] = vals[reading];
  21.     }
  22.     while( read != 0 ){
  23.         buffer[bufout++] = '=';
  24.         read = ( read + 6 ) % 8;
  25.     }
  26.     buffer[bufout] = NULL;
  27.     return buffer;
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement