This forum is in archive mode. You will not be able to post new content.

Author Topic: Base64 Encode  (Read 1523 times)

0 Members and 1 Guest are viewing this topic.

Offline bubzuru

  • Knight
  • **
  • Posts: 395
  • Cookies: 21
  • everything is contained in the data
    • View Profile
    • New School Tools
Base64 Encode
« on: August 05, 2012, 04:17:25 AM »
Code: (c++) [Select]
char* base64_encode(char *dst, const char *src, unsigned int len)
{

  char base64_table[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ""abcdefghijklmnopqrstuvwxyz""0123456789+/"};
  int x, y = 0, i, flag=0, m=0;
  int n = 3;
  char triple[3], quad[4], c;
  static char encode[1024]={0};

    for(x = 0; x < len; x = x + 3)
    {
        if((len - x) / 3 == 0) n = (len - x) % 3;

        for(i=0; i < 3; i++) triple[i] = '0';
        for(i=0; i < n; i++) triple[i] = src[x + i];
        quad[0] = base64_table[(triple[0] & 0xFC) >> 2]; // FC = 11111100
        quad[1] = base64_table[((triple[0] & 0x03) << 4) | ((triple[1] & 0xF0) >> 4)]; // 03 = 11
        quad[2] = base64_table[((triple[1] & 0x0F) << 2) | ((triple[2] & 0xC0) >> 6)]; // 0F = 1111, C0=11110
        quad[3] = base64_table[triple[2] & 0x3F]; // 3F = 111111
        if(n < 3) quad[3] = '=';
        if(n < 2) quad[2] = '=';
        for(i=0; i < 4; i++) dst[y + i] = quad[i];
        y = y + 4;
        m = m + 4;
        if((m != 0)&&((m % 76) == 0))
        {
            dst[y] = '\r';
            dst[y+1] = '\n';
            flag++;
            y += 2;
            m = 0;
        }
    }
  dst[y] = '\0';
  sprintf(encode,dst);
  return encode;
}

not my code but it works well
« Last Edit: August 05, 2012, 04:17:44 AM by bubzuru »
Damm it feels good to be gangsta
http://bubzuru.comule.com

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.