Image

Imagevolatilevoid wrote in Imagecpp

atob64 -- string to 64-bit encoded string

Hey -- this function "works" but doesn't produce the correct output and I'm not sure why. Any help would be greatly appreciated... :\
char* atob64 (const char * szSrc, char * szDest)
{
  // szDest must be length (strlen (szSrc) / 3) * 4 + ((strlen (szSrc) % 3) + 1) + 1 (NULL)
  int bit24;
  int len = strlen (szSrc);
  for (int i = 0; i < ((len / 3) + (len % 3)); i++)
  {
    bit24 = 0;
    for (int j = i*3; j < (i*3)+3; j++)
    {
      bit24 <<= 8;
      bit24 += szSrc[j];
    }
    int bit6 = 0x3F;
    for (int k = i*4; k < (i*4) + 4; k++) {
      szDest[k] = bit24 & bit6;
      bit24 >>= 6;
    }
  }
  
  const char* mItoB64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  for (i = 0; i < (((len / 3) * 4) + ((len % 3) + 1)); i++) {
		  szDest[i] = mItoB64 [szDest[i]];
  }
  
  i--;
  szDest[i] = 0;
  return szDest;
}


Thanks in advance. :)
EDIT: I meant BASE-64 encoded string. Whoops. :)