21 bool operator ==(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
24 bool operator !=(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
27 bool operator >(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
30 bool operator <(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
33 bool operator >=(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
36 bool operator <=(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
57 template<
bool...>
struct bool_pack;
59 using all_true = std::is_same< bool_pack<bs...,
true>, bool_pack<
true, bs...> >;
61 template<
typename Word,
size_t NumWords>
62 static void set_from_word_sequence(
const Word* arr_begin,
const Word* arr_end,
fixed_bytes<Size>& key)
64 auto itr = key._data.begin();
66 const size_t sub_word_shift = 8 *
sizeof(Word);
67 const size_t num_sub_words =
sizeof(
word_t) /
sizeof(Word);
68 auto sub_words_left = num_sub_words;
69 for(
auto w_itr = arr_begin; w_itr != arr_end; ++w_itr ) {
70 if( sub_words_left > 1 ) {
71 temp_word |=
static_cast<word_t>(*w_itr);
72 temp_word <<= sub_word_shift;
77 eosio::check( sub_words_left == 1,
"unexpected error in fixed_bytes constructor" );
78 temp_word |=
static_cast<word_t>(*w_itr);
79 sub_words_left = num_sub_words;
85 if( sub_words_left != num_sub_words ) {
86 if( sub_words_left > 1 )
87 temp_word <<= 8 * (sub_words_left-1);
119 std::copy(arr.begin(), arr.end(), _data.begin());
127 template<
typename Word,
size_t NumWords,
128 typename Enable =
typename std::enable_if<std::is_integral<Word>::value &&
129 std::is_unsigned<Word>::value &&
130 !std::is_same<Word, bool>::value &&
131 std::less<size_t>{}(
sizeof(Word),
sizeof(
word_t))>::type >
134 static_assert(
sizeof(
word_t) == (
sizeof(
word_t)/
sizeof(Word)) *
sizeof(Word),
135 "size of the backing word size is not divisible by the size of the array element" );
136 static_assert(
sizeof(Word) * NumWords <= Size,
"too many words supplied to fixed_bytes constructor" );
138 set_from_word_sequence<Word, NumWords>(arr.data(), arr.data() + arr.size(), *
this);
146 template<
typename Word,
size_t NumWords,
147 typename Enable =
typename std::enable_if<std::is_integral<Word>::value &&
148 std::is_unsigned<Word>::value &&
149 !std::is_same<Word, bool>::value &&
150 std::less<size_t>{}(
sizeof(Word),
sizeof(
word_t))>::type >
153 static_assert(
sizeof(
word_t) == (
sizeof(
word_t)/
sizeof(Word)) *
sizeof(Word),
154 "size of the backing word size is not divisible by the size of the array element" );
155 static_assert(
sizeof(Word) * NumWords <= Size,
"too many words supplied to fixed_bytes constructor" );
157 set_from_word_sequence<Word, NumWords>(arr, arr + NumWords, *
this);
168 template<
typename FirstWord,
typename... Rest>
172 std::is_unsigned<FirstWord>::value &&
173 !std::is_same<FirstWord, bool>::value &&
174 sizeof(FirstWord) <=
sizeof(
word_t) &&
175 all_true<(std::is_same<FirstWord, Rest>::value)...>::value,
176 FirstWord>::type first_word,
179 static_assert(
sizeof(
word_t) == (
sizeof(
word_t)/
sizeof(FirstWord)) *
sizeof(FirstWord),
180 "size of the backing word size is not divisible by the size of the words supplied as arguments" );
181 static_assert(
sizeof(FirstWord) * (1 +
sizeof...(Rest)) <= Size,
"too many words supplied to make_from_word_sequence" );
184 std::array<FirstWord, 1+
sizeof...(Rest)> arr{{ first_word, rest... }};
185 set_from_word_sequence<FirstWord, 1+
sizeof...(Rest)>(arr.data(), arr.data() + arr.size(), key);
197 auto data() {
return _data.data(); }
204 auto data()
const {
return _data.data(); }
211 auto size()
const {
return _data.size(); }
220 std::array<uint8_t, Size> arr;
222 const size_t num_sub_words =
sizeof(
word_t);
224 auto arr_itr = arr.begin();
225 auto data_itr = _data.begin();
227 for(
size_t counter = _data.size(); counter > 0; --counter, ++data_itr ) {
228 size_t sub_words_left = num_sub_words;
230 auto temp_word = *data_itr;
235 for( ; sub_words_left > 0; --sub_words_left ) {
236 *(arr_itr + sub_words_left - 1) =
static_cast<uint8_t
>(temp_word & 0xFF);
239 arr_itr += num_sub_words;
251 printhex(
static_cast<const void*
>(arr.data()), arr.size());
284 template<
size_t Size>
285 bool operator ==(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2) {
286 return c1._data == c2._data;
296 template<
size_t Size>
297 bool operator !=(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2) {
298 return c1._data != c2._data;
308 template<
size_t Size>
309 bool operator >(
const fixed_bytes<Size>& c1,
const fixed_bytes<Size>& c2) {
310 return c1._data > c2._data;
320 template<
size_t Size>
321 bool operator <(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2) {
322 return c1._data < c2._data;
332 template<
size_t Size>
333 bool operator >=(
const fixed_bytes<Size>& c1,
const fixed_bytes<Size>& c2) {
334 return c1._data >= c2._data;
344 template<
size_t Size>
345 bool operator <=(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2) {
346 return c1._data <= c2._data;
350 using checksum160 = fixed_bytes<20>;
351 using checksum256 = fixed_bytes<32>;
352 using checksum512 = fixed_bytes<64>;
363 template<
typename DataStream,
size_t Size>
364 inline DataStream&
operator<<(DataStream& ds,
const fixed_bytes<Size>& d) {
365 auto arr = d.extract_as_byte_array();
366 ds.write( (
const char*)arr.data(), arr.size() );
379 template<
typename DataStream,
size_t Size>
380 inline DataStream&
operator>>(DataStream& ds, fixed_bytes<Size>& d) {
381 std::array<uint8_t, Size> arr;
382 ds.read( (
char*)arr.data(), arr.size() );
383 d = fixed_bytes<Size>( arr );