#include "base64.h"

//Encodes Base64
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

static char encoding_table[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
	'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
	'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
	'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
	'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
	'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
	'w', 'x', 'y', 'z', '0', '1', '2', '3',
	'4', '5', '6', '7', '8', '9', '+', '/'
};
static int mod_table[] = { 0, 2, 1 };

int base64_encode(
	uint8_t * data,
	uint32_t data_len,
	char *buffer,
	uint32_t * out_len
)
{
	uint32_t encode_len = 4 * ((data_len + 2) / 3);
	int i, j;

	if ((encode_len + 1) > *out_len) {
		printf("Not enough buffer for encoding\n");
		return -1;
	}

	*out_len = encode_len;

	for (i = 0, j = 0; i < data_len;) {

		uint32_t octet_a = i < data_len ? (unsigned char)data[i++] : 0;
		uint32_t octet_b = i < data_len ? (unsigned char)data[i++] : 0;
		uint32_t octet_c = i < data_len ? (unsigned char)data[i++] : 0;

		uint32_t triple =
		    (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

		buffer[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
		buffer[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
		buffer[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
		buffer[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
	}

	for (i = 0; i < mod_table[data_len % 3]; i++)
		buffer[*out_len - 1 - i] = '=';

	buffer[encode_len] = 0;

	return 0;
}
