Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::fixed_bytes< Size > Class Template Reference

#include <fixed_bytes.hpp>

Public Types

typedef uint128_t word_t
 

Public Member Functions

constexpr fixed_bytes ()
 
 fixed_bytes (const std::array< word_t, num_words()> &arr)
 
template<typename Word , size_t NumWords, typename Enable = typename std::enable_if<std::is_integral<Word>::value && std::is_unsigned<Word>::value && !std::is_same<Word, bool>::value && std::less<size_t>{}(sizeof(Word), sizeof(word_t))>::type>
 fixed_bytes (const std::array< Word, NumWords > &arr)
 
template<typename Word , size_t NumWords, typename Enable = typename std::enable_if<std::is_integral<Word>::value && std::is_unsigned<Word>::value && !std::is_same<Word, bool>::value && std::less<size_t>{}(sizeof(Word), sizeof(word_t))>::type>
 fixed_bytes (const Word(&arr)[NumWords])
 
const auto & get_array () const
 
auto data ()
 
auto size () const
 
std::array< uint8_t, Size > extract_as_byte_array () const
 
void print () const
 

Static Public Member Functions

static constexpr size_t num_words ()
 
static constexpr size_t padded_bytes ()
 
template<typename FirstWord , typename... Rest>
static fixed_bytes< Size > make_from_word_sequence (typename std::enable_if< std::is_integral< FirstWord >::value &&std::is_unsigned< FirstWord >::value &&!std::is_same< FirstWord, bool >::value &&sizeof(FirstWord)<=sizeof(word_t) &&all_true<(std::is_same< FirstWord, Rest >::value)... >::value, FirstWord >::type first_word, Rest... rest)
 

Detailed Description

template<size_t Size>
class sysio::fixed_bytes< Size >

Fixed size byte array sorted lexicographically

Template Parameters
Size- Size of the fixed_bytes object

Definition at line 57 of file fixed_bytes.hpp.

Member Typedef Documentation

◆ word_t

template<size_t Size>
uint128_t sysio::fixed_bytes< Size >::word_t

Definition at line 96 of file fixed_bytes.hpp.

Constructor & Destructor Documentation

◆ fixed_bytes() [1/4]

template<size_t Size>
sysio::fixed_bytes< Size >::fixed_bytes ( )
inlineconstexpr

Default constructor to fixed_bytes object which initializes all bytes to zero

Definition at line 113 of file fixed_bytes.hpp.

113: _data() {}

◆ fixed_bytes() [2/4]

template<size_t Size>
sysio::fixed_bytes< Size >::fixed_bytes ( const std::array< word_t, num_words()> & arr)
inline

Constructor to fixed_bytes object from std::array of num_words() word_t types

Parameters
arrdata

Definition at line 120 of file fixed_bytes.hpp.

121 {
122 std::copy(arr.begin(), arr.end(), _data.begin());
123 }

◆ fixed_bytes() [3/4]

template<size_t Size>
template<typename Word , size_t NumWords, typename Enable = typename std::enable_if<std::is_integral<Word>::value && std::is_unsigned<Word>::value && !std::is_same<Word, bool>::value && std::less<size_t>{}(sizeof(Word), sizeof(word_t))>::type>
sysio::fixed_bytes< Size >::fixed_bytes ( const std::array< Word, NumWords > & arr)
inline

Constructor to fixed_bytes object from std::array of Word types smaller in size than word_t

Parameters
arr- Source data

Definition at line 135 of file fixed_bytes.hpp.

136 {
137 static_assert( sizeof(word_t) == (sizeof(word_t)/sizeof(Word)) * sizeof(Word),
138 "size of the backing word size is not divisible by the size of the array element" );
139 static_assert( sizeof(Word) * NumWords <= Size, "too many words supplied to fixed_bytes constructor" );
140
141 set_from_word_sequence<Word, NumWords>(arr.data(), arr.data() + arr.size(), *this);
142 }

◆ fixed_bytes() [4/4]

template<size_t Size>
template<typename Word , size_t NumWords, typename Enable = typename std::enable_if<std::is_integral<Word>::value && std::is_unsigned<Word>::value && !std::is_same<Word, bool>::value && std::less<size_t>{}(sizeof(Word), sizeof(word_t))>::type>
sysio::fixed_bytes< Size >::fixed_bytes ( const Word(&) arr[NumWords])
inline

Constructor to fixed_bytes object from fixed-sized C array of Word types smaller in size than word_t

Parameters
arr- Source data

Definition at line 154 of file fixed_bytes.hpp.

155 {
156 static_assert( sizeof(word_t) == (sizeof(word_t)/sizeof(Word)) * sizeof(Word),
157 "size of the backing word size is not divisible by the size of the array element" );
158 static_assert( sizeof(Word) * NumWords <= Size, "too many words supplied to fixed_bytes constructor" );
159
160 set_from_word_sequence<Word, NumWords>(arr, arr + NumWords, *this);
161 }

Member Function Documentation

◆ data()

template<size_t Size>
auto sysio::fixed_bytes< Size >::data ( )
inline

Get the underlying data of the contained std::array

Definition at line 200 of file fixed_bytes.hpp.

200{ return _data.data(); }

◆ extract_as_byte_array()

template<size_t Size>
std::array< uint8_t, Size > sysio::fixed_bytes< Size >::extract_as_byte_array ( ) const
inline

Extract the contained data as an array of bytes

Returns
- the extracted data as array of bytes

Definition at line 222 of file fixed_bytes.hpp.

222 {
223 std::array<uint8_t, Size> arr;
224
225 const size_t num_sub_words = sizeof(word_t);
226
227 auto arr_itr = arr.begin();
228 auto data_itr = _data.begin();
229
230 for( size_t counter = _data.size(); counter > 0; --counter, ++data_itr ) {
231 size_t sub_words_left = num_sub_words;
232
233 auto temp_word = *data_itr;
234 if( counter == 1 ) { // If last word in _data array...
235 sub_words_left -= padded_bytes();
236 temp_word >>= 8*padded_bytes();
237 }
238 for( ; sub_words_left > 0; --sub_words_left ) {
239 *(arr_itr + sub_words_left - 1) = static_cast<uint8_t>(temp_word & 0xFF);
240 temp_word >>= 8;
241 }
242 arr_itr += num_sub_words;
243 }
244
245 return arr;
246 }
static constexpr size_t padded_bytes()
unsigned char uint8_t
Definition stdint.h:124
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_array()

template<size_t Size>
const auto & sysio::fixed_bytes< Size >::get_array ( ) const
inline

Get the contained std::array

Definition at line 195 of file fixed_bytes.hpp.

195{ return _data; }
Here is the caller graph for this function:

◆ make_from_word_sequence()

template<size_t Size>
template<typename FirstWord , typename... Rest>
static fixed_bytes< Size > sysio::fixed_bytes< Size >::make_from_word_sequence ( typename std::enable_if< std::is_integral< FirstWord >::value &&std::is_unsigned< FirstWord >::value &&!std::is_same< FirstWord, bool >::value &&sizeof(FirstWord)<=sizeof(word_t) &&all_true<(std::is_same< FirstWord, Rest >::value)... >::value, FirstWord >::type first_word,
Rest... rest )
inlinestatic

Create a new fixed_bytes object from a sequence of words

Template Parameters
FirstWord- The type of the first word in the sequence
Rest- The type of the remaining words in the sequence
Parameters
first_word- The first word in the sequence
rest- The remaining words in the sequence

Definition at line 174 of file fixed_bytes.hpp.

181 {
182 static_assert( sizeof(word_t) == (sizeof(word_t)/sizeof(FirstWord)) * sizeof(FirstWord),
183 "size of the backing word size is not divisible by the size of the words supplied as arguments" );
184 static_assert( sizeof(FirstWord) * (1 + sizeof...(Rest)) <= Size, "too many words supplied to make_from_word_sequence" );
185
187 std::array<FirstWord, 1+sizeof...(Rest)> arr{{ first_word, rest... }};
188 set_from_word_sequence<FirstWord, 1+sizeof...(Rest)>(arr.data(), arr.data() + arr.size(), key);
189 return key;
190 }
constexpr fixed_bytes()
uint8_t key[16]
Definition yubico_otp.c:41

◆ num_words()

template<size_t Size>
static constexpr size_t sysio::fixed_bytes< Size >::num_words ( )
inlinestaticconstexpr

Get number of words contained in this fixed_bytes object. A word is defined to be 16 bytes in size

Definition at line 102 of file fixed_bytes.hpp.

102{ return (Size + sizeof(word_t) - 1) / sizeof(word_t); }
Here is the caller graph for this function:

◆ padded_bytes()

template<size_t Size>
static constexpr size_t sysio::fixed_bytes< Size >::padded_bytes ( )
inlinestaticconstexpr

Get number of padded bytes contained in this fixed_bytes object. Padded bytes are the remaining bytes inside the fixed_bytes object after all the words are allocated

Definition at line 108 of file fixed_bytes.hpp.

108{ return num_words() * sizeof(word_t) - Size; }
static constexpr size_t num_words()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print()

template<size_t Size>
void sysio::fixed_bytes< Size >::print ( ) const
inline

Prints fixed_bytes as a hexidecimal string

Parameters
valto be printed

Definition at line 253 of file fixed_bytes.hpp.

253 {
254 auto arr = extract_as_byte_array();
255 printhex(static_cast<const void*>(arr.data()), arr.size());
256 }
std::array< uint8_t, Size > extract_as_byte_array() const
Here is the call graph for this function:

◆ size()

template<size_t Size>
auto sysio::fixed_bytes< Size >::size ( ) const
inline

Get the size of the contained std::array

Definition at line 214 of file fixed_bytes.hpp.

214{ return _data.size(); }

The documentation for this class was generated from the following file: