#include<string.h>
#define AVal(x) Base64ValTab[x]
int EncodeBase64(unsigned char * pInput,int inbufLen, unsigned char * pOutput)
{
const unsigned char Base64ValTab[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int i = 0;
int loop = 0;
int remain = 0;
int iDstLen = 0;
loop = inbufLen/3;
remain = inbufLen%3;
for (i=0; i < loop; i++)
{
unsigned char a1 = (pInput[i*3] >> 2);
unsigned char a2 = ( ((pInput[i*3] & 0x03) << 4) | (pInput[i*3+1] >> 4) );
unsigned char a3 = ( ((pInput[i*3+1] & 0x0F) << 2) | ((pInput[i*3+2] & 0xC0) >> 6) );
unsigned char a4 = (pInput[i*3+2] & 0x3F);
pOutput[i*4] = AVal(a1);
pOutput[i*4+1] = AVal(a2);
pOutput[i*4+2] = AVal(a3);
pOutput[i*4+3] = AVal(a4);
}
iDstLen = i*4;
if (remain == 1)
{
// should pad two equal sign
i = inbufLen-1;
unsigned char a1 = (pInput[i] >> 2);
unsigned char a2 = ((pInput[i] & 0x03) << 4);
pOutput[iDstLen++] = AVal(a1);
pOutput[iDstLen++] = AVal(a2);
pOutput[iDstLen++] = '=';
pOutput[iDstLen] = '=';
}
else if (remain == 2)
{
// should pad one equal sign
i = inbufLen-2;
unsigned char a1 = (pInput[i] >> 2);
unsigned char a2 = ( ((pInput[i] & 0x03) << 4) | (pInput[i+1] >> 4));
unsigned char a3 = ( (pInput[i+1] & 0x0F) << 2);
pOutput[iDstLen++] = AVal(a1);
pOutput[iDstLen++] = AVal(a2);
pOutput[iDstLen++] = AVal(a3);
pOutput[iDstLen] = '=';
}
return iDstLen;
}
int main()
{
unsigned char a[30] = "admin:";
char *a_ptr = (char *)a;
unsigned char b[30] = {0};
EncodeBase64(a,strlen(a_ptr),b);
return 0;
}
#define AVal(x) Base64ValTab[x]
int EncodeBase64(unsigned char * pInput,int inbufLen, unsigned char * pOutput)
{
const unsigned char Base64ValTab[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int i = 0;
int loop = 0;
int remain = 0;
int iDstLen = 0;
loop = inbufLen/3;
remain = inbufLen%3;
for (i=0; i < loop; i++)
{
unsigned char a1 = (pInput[i*3] >> 2);
unsigned char a2 = ( ((pInput[i*3] & 0x03) << 4) | (pInput[i*3+1] >> 4) );
unsigned char a3 = ( ((pInput[i*3+1] & 0x0F) << 2) | ((pInput[i*3+2] & 0xC0) >> 6) );
unsigned char a4 = (pInput[i*3+2] & 0x3F);
pOutput[i*4] = AVal(a1);
pOutput[i*4+1] = AVal(a2);
pOutput[i*4+2] = AVal(a3);
pOutput[i*4+3] = AVal(a4);
}
iDstLen = i*4;
if (remain == 1)
{
// should pad two equal sign
i = inbufLen-1;
unsigned char a1 = (pInput[i] >> 2);
unsigned char a2 = ((pInput[i] & 0x03) << 4);
pOutput[iDstLen++] = AVal(a1);
pOutput[iDstLen++] = AVal(a2);
pOutput[iDstLen++] = '=';
pOutput[iDstLen] = '=';
}
else if (remain == 2)
{
// should pad one equal sign
i = inbufLen-2;
unsigned char a1 = (pInput[i] >> 2);
unsigned char a2 = ( ((pInput[i] & 0x03) << 4) | (pInput[i+1] >> 4));
unsigned char a3 = ( (pInput[i+1] & 0x0F) << 2);
pOutput[iDstLen++] = AVal(a1);
pOutput[iDstLen++] = AVal(a2);
pOutput[iDstLen++] = AVal(a3);
pOutput[iDstLen] = '=';
}
return iDstLen;
}
int main()
{
unsigned char a[30] = "admin:";
char *a_ptr = (char *)a;
unsigned char b[30] = {0};
EncodeBase64(a,strlen(a_ptr),b);
return 0;
}