wallet/slash6.h

33 lines
2.3 KiB
C
Raw Normal View History

#pragma once
// Converts a bit buffer, arbitrarily positioned with respect to byte boundaries, into a base sixty four numeral.
// In release mode, returns without doing anything if the output string buffer is too small.
// In debug mode, halts execution with an assert. Should throw, need to put in and unit test.
void bits2base64(const uint8_t * bitBuffer, unsigned int start, unsigned int length, std::span<char>base64Numerals);
// Converts a base64 numeral into a bit buffer. The input string is terminated by the first non base64 character, normally a space or null, or terminated when the bit buffer is full. A correct length base64 numeral does not need a terminating space or null.
// If successful, returns a length equal to the bit buffer length. If the input string is too short, returns a length shorter than the bit buffer. If the bit buffer is not a multiple of six bits, and the last base64 numeral of the bit buffer has trailing binary ones, returns a length greater than the size of the bitbuffer, without writing the trailing binary bits into the bit buffer.
unsigned int base64_to_bits(uint8_t* bitBuffer, unsigned int start, unsigned int length, const char* base64Numerals);
/* Expects pointer to byte buffer and pointer to string.
Expects a string of exactly the correct number of numerals,
terminated by a non base64 character, such as null.
Throws exception if that is not what it gets.
Fills the byte buffer exactly.
Returns a uint8_t containing the excess bits of the last numeral in its low order part.*/
uint8_t base64_to_bytes(uint8_t* byteBuffer, uint_fast32_t byteCount, const char* base64Numerals);
/* Expects reference to a range object over bytes and pointer to string.
Expects a string of exactly the correct number of numerals,
terminated by a non base64 character, such as null.
Throws exception if that is not what it gets.
Fills the byte buffer exactly.
Returns a uint8_t containing the excess bits of the last numeral in its low order part.*/
template < typename T>
std::enable_if_t<
!std::is_pointer<T>::value &&
sizeof(std::declval<T>()[0]) == 1,
uint8_t
>base64_to_bytes( T& byteRange, const char* base64Numerals) {
return base64_to_bytes(static_cast<std::nullptr_t>(&byteRange[0]), static_cast<uint_fast32_t>(std::size(byteRange)), base64Numerals);
}