user3343424
user3343424

Reputation:

Base64 Encript/Decript (C++ Builder XE)

I'm need encrypt a content of strings in C++ Builder XE and found this code on the internet:

AnsiString Base64Encode(AnsiString slToEnc)
      {
        //The Base64 Table
        const char Base64Table[64]=
          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        char * buftoenc = slToEnc.c_str();
        int bufsize = slToEnc.Length() + 1;
        char * encbuf = new char[slToEnc.Length() * 5];
        encbuf[0] = '\0';  int ilStrLen = -1;
        int i=0; int b64byte[5];
        unsigned char *buftemp;
        AnsiString slRetVal = EmptyStr;
        buftemp=(unsigned char *)malloc(bufsize+2);
        strcpy(buftemp,buftoenc);

        if (fmod(bufsize,3)==1)
        {
          buftemp[bufsize]='\0';
          buftemp[bufsize+1]='\0';
        }

        if (fmod(bufsize,3)==2)buftemp[bufsize]='\0';

        while (i<bufsize)
        {
          b64byte[0]=buftemp[i]>>2;
          b64byte[1]=((buftemp[i]&3)<<4)|(buftemp[i+1]>>4);
          b64byte[2]=((buftemp[i+1]&0x0F)<<2)|(buftemp[i+2]>>6);
          b64byte[3]=buftemp[i+2]&0x3F;
          encbuf[i+(i/3)]=Base64Table[b64byte[0]];
          encbuf[i+(i/3)+1]=Base64Table[b64byte[1]];
          encbuf[i+(i/3)+2]=Base64Table[b64byte[2]];
          encbuf[i+(i/3)+3]=Base64Table[b64byte[3]];
          i+=3;
        }

        free(buftemp);

        if (fmod(bufsize,3)==0) ilStrLen = bufsize*8/6;
        else if (fmod(bufsize,3)==1) ilStrLen = ((bufsize+2)*8/6)-2;
        else if (fmod(bufsize,3)==2) ilStrLen = ((bufsize+1)*8/6)-1;
        else ilStrLen = -1;

        if(ilStrLen> 0) slRetVal = AnsiString(encbuf).SubString(1, ilStrLen);
        if(encbuf != NULL) { delete encbuf; encbuf = NULL; }

        return slRetVal;
      }

// Calling function in a button component =>

Base64Encode(Memo1->Text);

But it generates a small erro on compilation making reference to ambiguity between the function std::fmod(double, double) being in this source code and the same function of math.h library.

Some suggestion?

Upvotes: 0

Views: 2946

Answers (3)

tutralex
tutralex

Reputation: 31

My version is a simple fast encoder (decoder) of base64 for C++ Builder.

//---------------------------------------------------------------------------
UnicodeString __fastcall TExample::Base64Encode(void *data,int length)
{
 if (length<=0) return L"";
 static const char set[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 unsigned char *in=(unsigned char*)data;
 char *pos,*out=pos=new char[((length-1)/3+1)<<2];
 while ((length-=3)>=0)
 {
  pos[0]=set[in[0]>>2];
  pos[1]=set[((in[0]&0x03)<<4)|(in[1]>>4)];
  pos[2]=set[((in[1]&0x0F)<<2)|(in[2]>>6)];
  pos[3]=set[in[2]&0x3F];
  pos+=4;
  in+=3;
 };
 if ((length&2)!=0)
 {
  pos[0]=set[in[0]>>2];
  if ((length&1)!=0)
  {
   pos[1]=set[((in[0]&0x03)<<4)|(in[1]>>4)];
   pos[2]=set[(in[1]&0x0F)<<2];
  }
  else
  {
   pos[1]=set[(in[0]&0x03)<<4];
   pos[2]='=';
  };
  pos[3]='=';
  pos+=4;
 };
 UnicodeString code=UnicodeString(out,pos-out);
 delete[] out;
 return code;
};
//---------------------------------------------------------------------------
int __fastcall TExample::Base64Decode(const UnicodeString &code,unsigned char **data)
{
 int length;
 if (((length=code.Length())==0)||((length&3)!=0)) return 0;
 wchar_t *str=code.c_str();
 unsigned char *pos,*out=pos=new unsigned char[(length>>2)*3];
 while (*str!=0)
 {
  length=-1;
  int shift=18,bits=0;
  do
  {
   wchar_t s=str[++length];
   if ((s>=L'A')&&(s<=L'Z')) bits|=(s-L'A')<<shift;
   else if ((s>=L'a')&&(s<=L'z')) bits|=(s-(L'a'-26))<<shift;
   else if (((s>=L'0')&&(s<=L'9'))) bits|=(s-(L'0'-52))<<shift;
   else if (s==L'+') bits|=62<<shift;
   else if (s==L'/') bits|=63<<shift;
   else if (s==L'=')
   {
    length--;
    break;
   }
   else
   {
    delete[] out;
    return 0;
   };
  }
  while ((shift-=6)>=0);
  pos[0]=bits>>16;
  pos[1]=bits>>8;
  pos[2]=bits;
  pos+=length;
  str+=4;
 };
 *data=out;
 return pos-out;
};
//---------------------------------------------------------------------------

Upvotes: 0

relipse
relipse

Reputation: 1820

You can use http://docwiki.embarcadero.com/Libraries/Rio/en/System.NetEncoding.TBase64Encoding

TBase64Encoding* enc = new TBase64Encoding();
UnicodeString decoded = enc->Decode(encoded);
delete enc;

TBase64Encoding* enc = new TBase64Encoding();
UnicodeString encoded = enc->Encode(whatever);
delete enc;

Upvotes: 0

pstrjds
pstrjds

Reputation: 17448

In order to resolve the ambiguity you need to specify the namespace for the fmod function. Just change the line:

if (fmod(bufsize,3)==1)

to:

if (std::fmod(bufsize,3)==1)

and it should compile for you.

Upvotes: 1

Related Questions