31 template <
unsigned int N,
typename U,
typename S>
class HashState ;
40 template <
typename U>
static std::string
extension( U n ) ;
45 template <
typename U>
static void convert_( U n , std::string::iterator p ) ;
61template <
unsigned int N,
typename U,
typename S>
67 static_assert( N != 0 && (N%4) == 0 ,
"hash state size must be a multiple of four" ) ;
69 static std::string
encode(
const uint_type * ) ;
73 static std::string
encode(
const uint_type * , size_type n ) ;
78 static std::string
encode( uint_type hi , uint_type low ,
const uint_type * ) ;
81 static std::string
encode( uint_type hi , uint_type low , uint_type v0 , uint_type v1 ,
82 uint_type v2 , uint_type v3 , uint_type v4 = 0 ) ;
86 static void decode(
const std::string & s , uint_type * values_out , size_type & size_out ) ;
92 static void decode(
const std::string & , uint_type & size_hi_out , uint_type & size_low_out ,
93 uint_type * value_0 , uint_type * value_1 , uint_type * value_2 , uint_type * value_3 ,
94 uint_type * value_4 =
nullptr ) ;
98 static void decode(
const std::string & , uint_type & size_hi_out , uint_type & size_low_out ,
99 uint_type * values_out ) ;
106 static void convert(
char hi ,
char himid ,
char lomid ,
char lo , uint_type & n ) ;
107 static void convert(
const std::string & str , uint_type & n ) ;
108 static void convert(
const std::string & s , uint_type * state ) ;
109 static void convert(
const std::string & s , uint_type * state , size_type & ) ;
112template <
unsigned int N,
typename U,
typename S>
115 std::string result( N ,
'\0' ) ;
116 for( std::size_t i = 0U ; i < N/4 ; i++ )
118 convert_( values[i] , result.begin() + (i*4U) ) ;
123template <
unsigned int N,
typename U,
typename S>
126 std::string result( N+4U ,
'\0' ) ;
127 for( std::size_t i = 0U ; i < N/4 ; i++ )
129 convert_( values[i] , result.begin() + (i*4U) ) ;
131 convert_( n , result.begin() + N ) ;
135template <
unsigned int N,
typename U,
typename S>
141 return encode( values , n ) ;
144template <
unsigned int N,
typename U,
typename S>
146 uint_type v1 , uint_type v2 , uint_type v3 , uint_type v4 )
151 std::array<uint_type,N/4> values {} ;
152 if( N > 0 ) values[0] = v0 ;
153 if( N > 4 ) values[1] = v1 ;
154 if( N > 8 ) values[2] = v2 ;
155 if( N > 12 ) values[3] = v3 ;
156 if( N > 16 ) values[4] = v4 ;
157 return encode( &values[0] , n ) ;
163 std::string result( 4U ,
'\0' ) ;
168template <
unsigned int N,
typename U,
typename S>
171 if( str.length() < (N+4U) )
172 return decode( str+std::string(N+4U,
'\0') , values_out , size_out ) ;
173 convert( str , values_out , size_out ) ;
176template <
unsigned int N,
typename U,
typename S>
178 uint_type * v0 , uint_type * v1 , uint_type * v2 , uint_type * v3 , uint_type * v4 )
180 if( str.length() < (N+4U) )
return decode( str+std::string(N+4U,
'\0') , hi , low , v0 , v1 , v2 , v3 , v4 ) ;
181 std::array<uint_type,N/4> values {} ;
183 convert( str , &values[0] , n ) ;
184 if( v0 && N > 0 ) *v0 = values[0] ;
185 if( v1 && N > 4 ) *v1 = values[1] ;
186 if( v2 && N > 8 ) *v2 = values[2] ;
187 if( v3 && N > 12 ) *v3 = values[3] ;
188 if( v4 && N > 16 ) *v4 = values[4] ;
190 low = ( n << 3 ) & 0xffffffffUL ;
193template <
unsigned int N,
typename U,
typename S>
195 uint_type * values_out )
197 if( str.length() < (N+4U) )
return decode( str+std::string(N+4U,
'\0') , hi , low , values_out ) ;
199 convert( str , values_out , n ) ;
201 low = ( n << 3 ) & 0xffffffffUL ;
207 *p_out++ =
static_cast<char>(n&0xffU) ; n >>= 8U ;
208 *p_out++ =
static_cast<char>(n&0xffU) ; n >>= 8U ;
209 *p_out++ =
static_cast<char>(n&0xffU) ; n >>= 8U ;
210 *p_out++ =
static_cast<char>(n&0xffU) ;
213template <
unsigned int N,
typename U,
typename S>
217 n_out |=
static_cast<unsigned char>(hi) ; n_out <<= 8 ;
218 n_out |=
static_cast<unsigned char>(himid) ; n_out <<= 8 ;
219 n_out |=
static_cast<unsigned char>(lomid) ; n_out <<= 8 ;
220 n_out |=
static_cast<unsigned char>(lo) ;
223template <
unsigned int N,
typename U,
typename S>
226 convert( str.at(3) , str.at(2) , str.at(1) , str.at(0) , n_out ) ;
229template <
unsigned int N,
typename U,
typename S>
232 for( std::size_t i = 0U ; i < (N/4U) ; i++ )
234 convert( str.at(i*4U+3U) , str.at(i*4U+2U) , str.at(i*4U+1U) , str.at(i*4U+0U) , state_out[i] ) ;
238template <
unsigned int N,
typename U,
typename S>
241 convert( str , state_out ) ;
243 convert( str.at(N+3U) , str.at(N+2U) , str.at(N+1U) , str.at(N) , nn ) ;
244 n_out =
static_cast<size_type
>(nn) ;
The non-template part of G::HashState.
static std::string extension(U n)
Returns the given data size as a four-character string.
static void convert_(U n, std::string::iterator p)
Encodes the given value into four characters.
Functions for representing the intermediate state of a hash function as a non-printable string.
static std::string encode(const uint_type *)
Returns the hash state as an N-character string of non-printing characters.
static void decode(const std::string &s, uint_type *values_out, size_type &size_out)
Converts an encode()d string back into a hash state of N/4 integers and a data size returned by refer...