Commit e33b2aab authored by Reto Da Forno's avatar Reto Da Forno
Browse files

base64 encoding function added

parent c2495187
......@@ -115,6 +115,9 @@ uint32_t hexstr_to_uint32(const char* str);
uint32_t uint16_to_str(uint16_t val, char* out_buffer);
uint32_t bytes_to_hexstr(const uint8_t* bytes, uint32_t num_bytes, char* out_buffer, uint32_t buffer_size);
int32_t str_to_int32(const char* str);
void base64_encode(const uint8_t* input, uint32_t len, char* out_buf, uint32_t buf_len);
uint32_t base64_decode_char(unsigned char c);
void base64_decode(const char* input, uint32_t len, uint8_t* out_buf, uint32_t buf_len);
void memb_init(memb_t memb);
void* memb_allocate_block(memb_t memb);
......
/*
* Copyright (c) 2017 - 2021, ETH Zurich, Computer Engineering Group (TEC)
* Copyright (c) 2017 - 2022, ETH Zurich, Computer Engineering Group (TEC)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
......@@ -89,7 +89,7 @@ uint32_t bytes_to_hexstr(const uint8_t* bytes, uint32_t num_bytes, char* out_buf
uint32_t len = 0;
buffer_size -= 3;
while (num_bytes && (len < buffer_size)) {
char upper_bits = ((*bytes) >> 4) + '0';
char upper_bits = ((*bytes) >> 4) + '0';
char lower_bits = ((*bytes) & 0x0f) + '0';
if (upper_bits > '9') {
upper_bits += ('A' - '9' - 1);
......@@ -129,3 +129,89 @@ int32_t str_to_int32(const char* str)
return val;
}
/*---------------------------------------------------------------------------*/
void base64_encode(const uint8_t* input, uint16_t len, char* out_buf, uint16_t buf_len)
{
const char* base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char padding = '=';
/* output buffer must be sufficiently large (4 bytes for every 3 bytes of input data, plus padding/termination chars) */
if (buf_len < (len * 4 / 3 + 3)) {
return;
}
while (len >= 3) {
uint32_t val1 = input[0] >> 2;
uint32_t val2 = ((input[0] & 0x3) << 4) | (input[1] >> 4);
uint32_t val3 = ((input[1] & 0xf) << 2) | (input[2] >> 6);
uint32_t val4 = (input[2] & 0x3f);
out_buf[0] = base64_table[val1];
out_buf[1] = base64_table[val2];
out_buf[2] = base64_table[val3];
out_buf[3] = base64_table[val4];
out_buf += 4;
input += 3;
len -= 3;
}
if (len == 2) {
uint32_t val1 = input[0] >> 2;
uint32_t val2 = ((input[0] & 0x3) << 4) | (input[1] >> 4);
uint32_t val3 = (input[1] & 0xf) << 2;
out_buf[0] = base64_table[val1];
out_buf[1] = base64_table[val2];
out_buf[2] = base64_table[val3];
out_buf[3] = padding;
out_buf += 4;
}
else if (len == 1) {
uint32_t val1 = input[0] >> 2;
uint32_t val2 = (input[0] & 0x3) << 4;
out_buf[0] = base64_table[val1];
out_buf[1] = base64_table[val2];
out_buf[2] = padding;
out_buf[3] = padding;
out_buf += 4;
}
*out_buf = 0; /* terminate string */
}
/*---------------------------------------------------------------------------*/
uint32_t base64_decode_char(unsigned char c)
{
if (c >= 'A' && c <= 'Z') {
return c - 'A';
}
if (c >= 'a' && c <= 'z') {
return c - 'a' + 26;
}
if (c >= '0' && c <= '9') {
return c - '0' + 52;
}
if (c == '+') {
return 62;
}
if (c == '/') {
return 63;
}
return 0;
}
/*---------------------------------------------------------------------------*/
void base64_decode(const char* input, uint16_t len, uint8_t* out_buf, uint16_t buf_len)
{
/* output buffer must be sufficiently large (4 bytes for every 3 bytes of input data, plus padding/termination chars) */
if (buf_len < (len * 3 / 4)) {
return;
}
while (len >= 4) {
uint32_t val1 = base64_decode_char(input[0]);
uint32_t val2 = base64_decode_char(input[1]);
uint32_t val3 = base64_decode_char(input[2]);
uint32_t val4 = base64_decode_char(input[3]);
out_buf[0] = (val1 << 2) | val2 >> 4;
out_buf[1] = ((val2 & 0xf) << 4) | val3 >> 2;
out_buf[2] = ((val3 & 0x3) << 6) | val4;
out_buf += 3;
input += 4;
len -= 4;
}
}
/*---------------------------------------------------------------------------*/
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment