/* 
*  base64 functions for use with the crypto bone 
*  License: BSD-3-clause (https://senderek.ie/bsd.html)
*  Author:  Ralf Senderek
*/

#include <b64/cencode.h>
#include <b64/cdecode.h>

int b64encode(const char* input, const int input_length, const int max_output_bytes , int* num,  char* out)
{
     /* base64-encodes the fixed size input and writes into output */

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

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

     /* keep track of our encoded position
        position points to the next byte that can be changed. */

     char* position = out;

     /* store the number of bytes encoded by a single call */
     int cnt = 0;
     *num = 0;
     base64_encodestate s;
     base64_init_encodestate(&s);

     /* first multiple 3-byte chunks are encoded */
     cnt = base64_encode_block(input, input_length, position, &s);
     position += cnt;
     *num += cnt;

     /* finalise the encoding  and add the remaining bytes but not the trailing newline */
     cnt = base64_encode_blockend(position, &s);
     /* ignore the trailing \n */
     *num += cnt-1;

     /* so null-terminate output */
     out[*num] = 0;	
     return 0;
}

/*-------------------------------------------------------*/

int validation (const char* input)
{
     char CODES[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
     unsigned char c;
     int OK = -32 ;
     for (int j = 0; j < strlen(input); j++) {
          c = input[j];
          OK = -1;
          for (int i = 0; i<64; i++) {
               if ( c == CODES[i] ) OK = 0 ;
               if ( c == '=' ) OK = 0 ;
               if ( c == '\n' ) OK = 0 ;
          }		    
          if (OK == -1) {
               /* found a non-base64 character, return bad data error */
               return -32;
          }	    
     }
     return 0;
}

/*-------------------------------------------------------*/

int b64decode(const char* input, const int input_length, const int max_output_bytes , int* num,  char* out)
{
     
     /* base64-decodes the fixed size input and writes into output */
     int valid = 0;

     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 ) {
          /* 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 (plus newline) */

     valid = validation(input);
     if (valid != 0) {
          *num = 0;
          return valid;
     }

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

     /* keep track of our encoded position
        position points to the next byte that can be changed. */

     char* position = out;
	  
     /* store the number of bytes encoded by a single call */
     int cnt = 0;
     *num = 0;
     base64_decodestate s;
     base64_init_decodestate(&s);

     cnt = base64_decode_block(input, input_length, position, &s);
     position += cnt;
     *num += cnt;
 
     /* null-terminate output */
     out[*num] = 0;	

     return 0;
}

