C++Builder logo
Base64 Encoding Encodage en Base64

This function encodes a string in buftoenc into a base64 format. You have to pass three parameters to this function. The first is a pointer to the string to encode, the second is the length of this string and the third is a pointer to the string where the function has to put the result. The return value is the length of the encoded string. Be aware that the encoded string has a length greater than the original one. Cette fonction encode un string contenu dans buftoenc en un nouveau string au format base64. Le résultat de la conversion se retrouve dans le troisième paramètre de la fonction. Le paramètre bufsize contient la taille du string à encoder. La fonction retourne le nombre de caractères du string encodé en base64. La taille du string est bien entendu supérieure à la taille du string à encoder.

const char Base64Table[64]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int __stdcall encode_b64(const unsigned char *buftoenc,int bufsize,unsigned char *encbuf) 

  //Allocate space for the temporary buffer 
  unsigned char *buftemp=new unsigned char[bufsize+3]; 
  memset(buftemp,'\0',bufsize+3);
  memcpy(buftemp,buftoenc,bufsize); 
  int i=0;
  int b64byte[5];
  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;
    if(b64byte[0] == 0)encbuf[i + (i/3)] = '=';
    else encbuf[i + (i/3)] = Base64Table[b64byte[0]];
    if(b64byte[1] == 0)encbuf[i + (i/3) + 1] = '=';
    else encbuf[i + (i/3) + 1] = Base64Table[b64byte[1]];
    if(b64byte[2] == 0)encbuf[i + (i/3) + 2] = '='; 
    else encbuf[i + (i/3) + 2] = Base64Table[b64byte[2]];
    if(b64byte[3] == 0)encbuf[i + (i/3) + 3] = '='; 
    else encbuf[i + (i/3) + 3] = Base64Table[b64byte[3]]; 
    i+=3; 
  } 
  delete buftemp; 
  return strlen(encbuf); 
}


Base64 Decoding Décodage Base64

Decodes the string buftodec in base64 format. You have to pass to the function the size of the string and a pointer to the string where to put the decoded one. This function returns the number of characters contained in the decoded string. Cette fonction décode un string codé en base64. Passez à la fonction le string à décoder et sa taille ainsi qu'un pointeur vers le string destiné à contenir le string décodé. La fonction retourne le nombre de caractères contenu dans le string décodé.

const char Base64Table[64]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int __stdcall decode_b64(const unsigned char *buftodec,int bufsize,unsigned char *decbuf) 
{    
  //Allocate space for the temporary buffer 
  unsigned char *buftemp=new unsigned char [bufsize];
  memset(buftemp,'\0',bufsize); 
  memcpy(buftemp,buftodec,bufsize);  
  int i=0;
  int cpos[5];
  unsigned char binbyte[4];
  while (i < bufsize) 
  { 
    if(buftemp[i] == '=')cpos[0] = 0;
    else cpos[0] = strchr(Base64Table,buftemp[i]) - Base64Table;
    if(buftemp[i+1 ]== '=')cpos[1] = 0; 
    else cpos[1] = strchr(Base64Table,buftemp[i + 1]) - Base64Table;
    if(buftemp[i+2] == '=')cpos[2] = 0; 
    else cpos[2] = strchr(Base64Table,buftemp[i + 2]) - Base64Table; 
    if(buftemp[i+3] == '=')cpos[3] = 0;
    else cpos[3] = strchr(Base64Table,buftemp[i + 3]) - Base64Table; 
    binbyte[0] = ((cpos[0] << 2) | (cpos[1] >> 4)); 
    binbyte[1] = ((cpos[1] << 4) | (cpos[2] >> 2)); 
    binbyte[2] = (((cpos[2] & 0x03 )<< 6) | (cpos[3] & 0x3f)); 
    decbuf[i - (i/4)] = binbyte[0]; 
    decbuf[i - (i/4) + 1] = binbyte[1]; 
    decbuf[i - (i/4) + 2] = binbyte[2]; 
    i += 4; 
  }
  delete buftemp; 
  return strlen(decbuf); 
}