acme-client/src/mimik_acme_client.c
int mk_convert_to_base64(char *src, size_t slen, char *dst64, size_t dsz)
{
   size_t dstlen = 0 ;
   int i = 0;
   int rc = 0;

   if ((!src)||(slen <= 0)||(!dst64)||(dsz < slen)) {
      return -1;
   }

  /*
   mbedtls_base64_encode() Returns 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or
   MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is not correct. *dstlen is
   always updated to reflect the amount of data that has (or would have) been
   written.
  */

  rc = mbedtls_base64_encode((unsigned char *)dst64, dsz, &dstlen, (unsigned char *)src, slen);
  if (rc != 0) {
     MK_LOG(LOG_ERR,"%s() mbedtls_base64_encode failed: slen=%d dsz=%d dstlen=%d rc=%d \n",
                __func__,slen,dsz,dstlen,rc);
     return -1;
  }

  MK_LOG(LOG_DEBUG ,"%s() mbedtls_base64_encode-1: slen=%d dstlen=%d strlen(dst64)=%d \n",
                __func__,slen,dstlen,strlen(dst64));
  /*
   As per logic given in Appendix C of RFC 7515  JSON Web Signature (JWS)
   To implement base64url encoding and decoding functions without padding based upon
   standard base64 encoding and decoding functions that do use padding.  

   The rule to change base64 without padding is:
   // Remove any trailing '=' and change it to 0
   // s.Replace('+', '-');
   // s.Replace('/', '_');

  */
  
  for (i = 0; i <= dstlen; i++) {
     switch(dst64[i]) {
	case '+':
	 dst64[i] = '-';
	 break;
	case '/':
	 dst64[i] = '_';
	 break;
	case  '=':
        {
	 dst64[i] = 0;
         //MK_LOG(LOG_DEBUG ,"%s(_CHANGE) mbedtls_base64_encode= slen=%d dstlen=%d strlen(dst64)=%d \n",
         //       __func__,slen,dstlen,strlen(dst64));
        }
	 break;
	default:
	 break;
     }
  }

  MK_LOG(LOG_DEBUG ,"%s() mbedtls_base64_encode-2 success: slen=%d dstlen=%d strlen(dst64)=%d \n",
                __func__,slen,dstlen,strlen(dst64));
    
  return dstlen;
}
//slen	amount of data to be encoded
//dsz	size of the destination buffer
//returns length of bytes written to dst64 and -1 on error
slen    amount of data to be encoded
dsz     size of the destination buffer
returns length of bytes written to dst64 and -1 on error
slen amount of data to be encoded dsz size of the destination buffer returns length of bytes written to dst64 and - 1 on error
int mk_convert_to_base64(char *src, size_t slen, char *dst64, size_t dsz)
{
   size_t dstlen = 0 ;
   int i = 0;
   int rc = 0;
   if ((!src)||(slen <= 0)||(!dst64)||(dsz < slen)) {
      return -1;
   }
  rc = mbedtls_base64_encode((unsigned char *)dst64, dsz, &dstlen, (unsigned char *)src, slen);
  if (rc != 0) {
     MK_LOG(LOG_ERR,"%s() mbedtls_base64_encode failed: slen=%d dsz=%d dstlen=%d rc=%d \n",
                __func__,slen,dsz,dstlen,rc);
     return -1;
  }
  MK_LOG(LOG_DEBUG ,"%s() mbedtls_base64_encode-1: slen=%d dstlen=%d strlen(dst64)=%d \n",
                __func__,slen,dstlen,strlen(dst64));
  for (i = 0; i <= dstlen; i++) {
     switch(dst64[i]) {
	case '+':
	 dst64[i] = '-';
	 break;
	case '/':
	 dst64[i] = '_';
	 break;
	case  '=':
        {
	 dst64[i] = 0;
        }
	 break;
	default:
	 break;
     }
  }
  MK_LOG(LOG_DEBUG ,"%s() mbedtls_base64_encode-2 success: slen=%d dstlen=%d strlen(dst64)=%d \n",
                __func__,slen,dstlen,strlen(dst64));
  return dstlen;
}
int mk_convert_to_base64 ( char * src , size_t slen , char * dst64 , size_t dsz ) { size_t dstlen = 0 ; int i = 0 ; int rc = 0 ; if ( ( ! src ) || ( slen <= 0 ) || ( ! dst64 ) || ( dsz < slen ) ) { return -1 ; } rc = mbedtls_base64_encode ( ( unsigned char * ) dst64 , dsz , & dstlen , ( unsigned char * ) src , slen ) ; if ( rc != 0 ) { MK_LOG ( LOG_ERR , " \n " , __func__ , slen , dsz , dstlen , rc ) ; return -1 ; } MK_LOG ( LOG_DEBUG , " \n " , __func__ , slen , dstlen , strlen ( dst64 ) ) ; for ( i = 0 ; i <= dstlen ; i ++ ) { switch ( dst64 [ i ] ) { case ' ' : dst64 [ i ] = ' ' ; break ; case ' ' : dst64 [ i ] = ' ' ; break ; case ' ' : { dst64 [ i ] = 0 ; } break ; default : break ; } } MK_LOG ( LOG_DEBUG , " \n " , __func__ , slen , dstlen , strlen ( dst64 ) ) ; return dstlen ; }
slen    amount of data to be encoded
dsz     size of the destination buffer
returns length of bytes written to dst64 and -1 on error
slen amount of data to be encoded dsz size of the destination buffer returns length of bytes written to dst64 and - 1 on error
/*
   mbedtls_base64_encode() Returns 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or
   MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is not correct. *dstlen is
   always updated to reflect the amount of data that has (or would have) been
   written.
  */ /*
   As per logic given in Appendix C of RFC 7515  JSON Web Signature (JWS)
   To implement base64url encoding and decoding functions without padding based upon
   standard base64 encoding and decoding functions that do use padding.  

   The rule to change base64 without padding is:
   // Remove any trailing '=' and change it to 0
   // s.Replace('+', '-');
   // s.Replace('/', '_');

  */ //MK_LOG(LOG_DEBUG ,"%s(_CHANGE) mbedtls_base64_encode= slen=%d dstlen=%d strlen(dst64)=%d \n", //       __func__,slen,dstlen,strlen(dst64));