byte table(byte ot)
{
if(ot>=0 && ot<=25)return a + ot;
else if(ot>=26 && ot<=51)return a + (ot-26);
else if(ot>=52 && ot<=61)return 0 + (ot-52);
else if(ot==62)return +;
else if(ot==63)return /;
else return =;
}
void base64(char* pszsource, char* pszcode)
{
byte ot;
int i;
i=0;
while(pszsource[i])
{
ot = (0xfc & pszsource[i])>>2; *pszcode = table(ot); i++; pszcode++;
if(!pszsource[i])
{
ot = (0x03 & pszsource[i-1])<<4; *pszcode = table(ot); pszcode++;
break;
}
ot = (0x03 & pszsource[i-1])<<4 | (0xf0 & pszsource[i])>>4; *pszcode = table(ot); pszcode++; i++;
if(!pszsource[i])
{
ot = (0x0f & pszsource[i-1])<<2; *pszcode = table(ot); pszcode++;
break;
}
ot = (0x0f & pszsource[i-1])<<2 | (0xc0 & pszsource[i])>>6; *pszcode = table(ot); pszcode++;
ot = 0x3f & pszsource[i]; *pszcode = table(ot), i++; pszcode++;
}
*pszcode = null;
}
byte getsixbits(char* pszstr, int& i)
{
char c;
while(1)
{
if(i>=strlen(pszstr))break;
c = pszstr[i++];
if(isupper(c)) return c-a;
else if(islower(c)) return c-a+26;
else if(isdigit(c)) return c-0+52;
else if(c==+) return 62;
else if(c==/) return 63;
else if(c===) return 0;
}
return 0;
}
void unbase64(char* psztext, char* pszcode)
{
int nlen, i;
byte bysixbits0, bysixbits1, bysixbits2, bysixbits3, c;
nlen = strlen(pszcode);
for(i=0; i<nlen; )
{
bysixbits0 = getsixbits(pszcode, i);
if(i<nlen)bysixbits1 = getsixbits(pszcode, i);
if(i<nlen)bysixbits2 = getsixbits(pszcode, i);
if(i<nlen)bysixbits3 = getsixbits(pszcode, i);
c = bysixbits0<<2 | bysixbits1>>4; *psztext = c; psztext++;
c = bysixbits1<<4 | bysixbits2>>2; *psztext = c; psztext++;
c = bysixbits2<<6 | bysixbits3; *psztext = c; psztext++;
}
*psztext = null;
}
{
if(ot>=0 && ot<=25)return a + ot;
else if(ot>=26 && ot<=51)return a + (ot-26);
else if(ot>=52 && ot<=61)return 0 + (ot-52);
else if(ot==62)return +;
else if(ot==63)return /;
else return =;
}
void base64(char* pszsource, char* pszcode)
{
byte ot;
int i;
i=0;
while(pszsource[i])
{
ot = (0xfc & pszsource[i])>>2; *pszcode = table(ot); i++; pszcode++;
if(!pszsource[i])
{
ot = (0x03 & pszsource[i-1])<<4; *pszcode = table(ot); pszcode++;
break;
}
ot = (0x03 & pszsource[i-1])<<4 | (0xf0 & pszsource[i])>>4; *pszcode = table(ot); pszcode++; i++;
if(!pszsource[i])
{
ot = (0x0f & pszsource[i-1])<<2; *pszcode = table(ot); pszcode++;
break;
}
ot = (0x0f & pszsource[i-1])<<2 | (0xc0 & pszsource[i])>>6; *pszcode = table(ot); pszcode++;
ot = 0x3f & pszsource[i]; *pszcode = table(ot), i++; pszcode++;
}
*pszcode = null;
}
byte getsixbits(char* pszstr, int& i)
{
char c;
while(1)
{
if(i>=strlen(pszstr))break;
c = pszstr[i++];
if(isupper(c)) return c-a;
else if(islower(c)) return c-a+26;
else if(isdigit(c)) return c-0+52;
else if(c==+) return 62;
else if(c==/) return 63;
else if(c===) return 0;
}
return 0;
}
void unbase64(char* psztext, char* pszcode)
{
int nlen, i;
byte bysixbits0, bysixbits1, bysixbits2, bysixbits3, c;
nlen = strlen(pszcode);
for(i=0; i<nlen; )
{
bysixbits0 = getsixbits(pszcode, i);
if(i<nlen)bysixbits1 = getsixbits(pszcode, i);
if(i<nlen)bysixbits2 = getsixbits(pszcode, i);
if(i<nlen)bysixbits3 = getsixbits(pszcode, i);
c = bysixbits0<<2 | bysixbits1>>4; *psztext = c; psztext++;
c = bysixbits1<<4 | bysixbits2>>2; *psztext = c; psztext++;
c = bysixbits2<<6 | bysixbits3; *psztext = c; psztext++;
}
*psztext = null;
}