/* 
*  base64 functions for use with the Crypto Bone (https://crypto-bone.com) 
*  License: BSD-3-clause (https://senderek.ie/bsd.html)
*  Author:  Ralf Senderek
*  Version: 1.0 (November 2026)
*/


#define BYTE unsigned char

BYTE CODES[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


BYTE b64encode(const BYTE *input_bytes, const unsigned int input_length, const unsigned int max_output_bytes , unsigned int *num, BYTE *output_bytes)
{
     /*  base64-encodes a fixed size input and writes into output 
     *
     *   input is a byte string of at least input_length BYTES (unsigned chars)
     *   bytes beyond input_length are ignored
     *   max_output_bytes must be large enough to hold the (null-terminated) base64 encoded string
     *   num returns the number of base64 characters in the output byte string 
     *   i and o indexes the in_bytes and out_bytes arrays
     */

     unsigned int rounds, leftover = 0;
     int i = 0 ;
     int o = 0 ;
     BYTE b1, b2, b3 = 0;
     BYTE A, B, C, D ;

     /* compute necessary rounds and left over bytes */
     rounds = input_length / 3 ;
     leftover = input_length % 3 ;
     *num = 0;

     if ((input_length*4/3) > max_output_bytes - 5) {
          /* the input is too large, return overflow error */
	  *num = 0;
	  return 30;
     }

     /* init the output buffer */
     for (int x=0; x < max_output_bytes; x++)
          output_bytes[x] = 0;

     printf("ENCODING %i rounds plus %i leftover bytes\n",rounds, leftover);

     for (int n = 0; n < rounds; n++) {
          b1 = input_bytes[i++];
          b2 = input_bytes[i++];
          b3 = input_bytes[i++];
          
          A = CODES[ b1 >> 2 ] ;
          B = CODES[ ((b1 << 4) | (b2 >> 4)) & 0x3F ] ;
          C = CODES[ ((b2 << 2) | (b3 >> 6)) & 0x3F ] ;
          D = CODES[b3 & 0x3F] ;

	  output_bytes[o++] = A;
	  output_bytes[o++] = B;
	  output_bytes[o++] = C;
	  output_bytes[o++] = D;
     }

     // process leftover bytes
     if(leftover > 0) {
          b1 = input_bytes[i++];
          A = CODES[ b1 >> 2 ] ;
	  output_bytes[o++] = A;
          if (leftover == 1) {
               // one byte
               B = CODES[ (b1 << 4)  & 0x3F ] ;
	       output_bytes[o++] = B;
	       output_bytes[o++] = '=';
          } else {
               // two bytes
               b2 = input_bytes[i++];
               B = CODES[ ((b1 << 4) | (b2 >> 4)) & 0x3F ] ;
               C = CODES[ (b2 << 2)  & 0x3F ] ;
	       output_bytes[o++] = B;
	       output_bytes[o++] = C;
          }
	  output_bytes[o++] = '=';
     }

     *num = o;

     /* null-terminate output */
     output_bytes[*num] = 0;	

     return 0;
}

/*-------------------------------------------------------*/
unsigned int validation (const char* input, const int length)
{
     unsigned char c;
     int OK = 32 ;
     for (int j=0; j < length; j++) {
          c = input[j];
          OK = 32;
          for (int i=0; i<64; i++) {
               if ( c == CODES[i] ) { OK = 0 ;}
	       // accept = if at the end of the input
               if ( ( c == '=' ) && ((j == length-1) || (j == length-2)) ) { OK = 0 ;}
          }		    
          if (OK == 32) {
               /* found a non-base64 character, return bad data error */
               return 32;
          }	    
     }
     return 0;
}

/*-------------------------------------------------------*/
unsigned int position(char c){
     for (int i=0; i<64; i++){
          if ( c == CODES[i] ) return i;
     }
     return 255;
}

/*-------------------------------------------------------*/
BYTE b64decode(const BYTE *input_bytes, const unsigned int input_length, const unsigned int max_output_bytes , int *num,  BYTE *output_bytes)
{
     
     /*  base64-decodes the fixed size input and writes the decoded bytes into output_bytes 
     *
     *   input is a byte string of at least input_length BYTES, all bytes beyond input_length are 
     *   ignored
     *   max_output_bytes must be large enough to hold the (null-terminated) base64 decoded bytes
     *   num returns the number of base64 characters in the output_bytes string 
     *   i and o indexes the in_bytes and out_bytes arrays
     */

     unsigned int length = 0 ;
     unsigned int rounds, leftover = 0;
     unsigned int valid = 0;
     int i = 0 ;
     int o = 0 ;
     BYTE b1, b2, b3 = 0;
     BYTE A, B, C, D ;

     if ((input_length % 4) != 0) {
         printf("Bad data (%i bytes)\n",input_length);
         return 32;
     }
     /* compute necessary rounds and left over bytes */
     length = input_length;
     if (input_bytes[input_length-1] == '=') {length--;}
     if (input_bytes[input_length-2] == '=') {length--;}
     
     rounds = length / 4 ;
     leftover = length % 4 ;
     *num = 0;
     
     printf("DE-CODING %i rounds plus %i leftover bytes\n",rounds, leftover);

     if (input_length < 4 ) {
          /* the input is too small, return bad data error */
          *num = 0;
          return 32;
     }

     if ((input_length*3/4 ) > max_output_bytes -5) {
          /* the input is too large, return overflow error */
          *num = 0;
          return 30;
     }
 
     /* before decoding can start it must be assured, that all characters in 
        input are valid base64 characters */

     valid = validation(input_bytes, input_length);
     if (valid != 0) {
          *num = 0;
          return valid;
     }
     
     /* input_bytes does not contain invalid characters (including newlines) */

     /* init the output buffer */
     for (int x=0; x < max_output_bytes; x++)
          output_bytes[x] = 0;

     for (int n = 0; n < rounds; n++) {
          A = position(input_bytes[i++]);
          B = position(input_bytes[i++]);
          C = position(input_bytes[i++]);
          D = position(input_bytes[i++]);

          b1 = ((A << 2) | (B >> 4)) & 0xFF;
          b2 = ((B << 4) | (C >> 2)) & 0xFF;
          b3 = ((C << 6) | D) & 0xFF;

	  output_bytes[o++] = b1;
	  output_bytes[o++] = b2;
	  output_bytes[o++] = b3;
     }

     // process leftover bytes
     if(leftover > 0) {
          A = position(input_bytes[i++]);
          B = position(input_bytes[i++]);
          b1 = ((A << 2) | (B >> 4)) & 0xFF;
	  output_bytes[o++] = b1;
          if (leftover == 3) {
               // three bytes
               C =  position(input_bytes[i++]);
               //b2 = (B << 4)  & 0xFF;
               b2 = ((B << 4) | (C >> 2)) & 0xFF;
	       output_bytes[o++] = b2;
          }
     }

     *num = o;

     /* null-terminate output */
     output_bytes[*num] = 0;	

     return 0;
}

