#pragma once
#ifndef _UPBASE64_H
#define _UPBASE64_H
class UpBase64
{
public:
static void Base64_Decode(const char* base64Buf, int len, char* out);
static void Base64_Encode(const char* buf, int len, char* out);
static int DecodeLength(int aLength);
static int EncodeLength(int aLength);
static char* LongToInverseBytes(__int64 aPhoneNum);
static char* IntToInverseBytes(int aPhoneNum);
private:
static bool IsBase64(char aChar);
static int PosOfBase64Char(char aChar);
static char CharOfBase64(int aPos);
};
#endif // _UPBASE64_H
#include
#include "UpBase64.h"
const char* pBase64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
bool UpBase64::IsBase64(char aChar)
{
int val= ( unsigned char) aChar;
bool isalphaOrNum = ( val >= 48 && val <=57 ) || ( val >= 65 && val <=90 ) || ( val >= 97 && val <=122 );
return ( isalphaOrNum || (aChar == '-') || (aChar == '_')) ? true
: false;
}
int UpBase64::PosOfBase64Char(char aChar)
{
int pos = -1;
int len = strlen(pBase64Table);
for( int i = 0; i < len; i++ )
{
if(aChar == pBase64Table[i])
{
pos = i;
break;
}
}
return pos;
}
char UpBase64::CharOfBase64(int aPos)
{
return pBase64Table[aPos];
}
int UpBase64::DecodeLength(int aLength)
{
int len = aLength / 4 * 3 + aLength % 4;
return len;
}
int UpBase64::EncodeLength(int aLength)
{
int len = 4 * ((aLength + 2) / 3);
return len;
}
void UpBase64::Base64_Decode(const char* base64Buf, int len, char* out)
{
int inlen = len;
int i = 0;
int j = 0;
int in_ = 0;
int n = 0;
unsigned char char_array_4[4], char_array_3[3];
while (inlen-- && (base64Buf[in_] != ';') && IsBase64(base64Buf[in_]))
{
char_array_4[i++] = base64Buf[in_];
in_++;
if (i == 4)
{
for (i = 0; i < 4; i++)
char_array_4[i] = PosOfBase64Char(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2)
+ ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4)
+ ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; i < 3; i++)
{
out[n] = (char_array_3[i]);
n++;
}
i = 0;
}
}
if (i)
{
for (j = i; j < 4; j++)
char_array_4[j] = 0;
for (j = 0; j < 4; j++)
char_array_4[j] = PosOfBase64Char(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30)
>> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2]
& 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++)
{
out[n] = char_array_3[j];
n++;
}
}
}
void UpBase64::Base64_Encode(const char* buf, int len, char* out)
{
int in_len = len;
int i = 0;
int j = 0;
int k = 0;
int n = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--)
{
char_array_3[i++] = buf[k++];
if (i == 3)
{
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4)
+ ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2)
+ ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; i < 4; ++i)
{
out[n] = CharOfBase64(char_array_4[i]);
n++;
}
i = 0;
}
}
if (i)
{
for (j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1]
& 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2]
& 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
{
out[n] = CharOfBase64(char_array_4[j]);
n++;
}
while (i++ < 3)
{
out[n] = ';';
n++;
}
}
}
char* UpBase64::LongToInverseBytes(__int64 aPhoneNum)
{
char* pBuf = new char[20]();
int i = 0;
while(aPhoneNum)
{
pBuf[i] = (aPhoneNum & 0xFF);
i ++;
aPhoneNum = aPhoneNum >> 8;
}
pBuf[i] = '\0';
return pBuf;
}
char* UpBase64::IntToInverseBytes(int aPhoneNum)
{
int i = 0;
char* pBuf = new char[20];
while(aPhoneNum)
{
pBuf[i] = (aPhoneNum & 0xFF);
i ++;
aPhoneNum = aPhoneNum >> 8;
}
return pBuf;
}