21 #ifndef G_HASH_STATE__H
22 #define G_HASH_STATE__H
29 template <
unsigned int N,
typename U,
typename S>
class HashState ;
38 template <
unsigned int N,
typename U,
typename S>
45 static std::string
encode(
const uint_type * ) ;
49 static std::string
encode(
const uint_type * , size_type n ) ;
53 static void decode(
const std::string & , uint_type * , size_type & ) ;
60 static void convert( uint_type n ,
char * p ) ;
61 static void convert(
char hi ,
char himid ,
char lomid ,
char lo , uint_type & n ) ;
62 static void convert(
const std::string & str , uint_type & n ) ;
63 static void convert(
const std::string & s , uint_type * state ) ;
64 static void convert(
const std::string & s , uint_type * state , size_type & ) ;
67 template <
unsigned int N,
typename U,
typename S>
70 std::string result( N ,
'\0' ) ;
71 for(
size_t i = 0U ; i < N/4 ; i++ )
73 convert( state[i] , &result[i*4U] ) ;
78 template <
unsigned int N,
typename U,
typename S>
81 std::string result( N+4U ,
'\0' ) ;
82 for(
size_t i = 0U ; i < N/4 ; i++ )
84 convert( state[i] , &result[i*4U] ) ;
86 convert( n , &result[N] ) ;
90 template <
unsigned int N,
typename U,
typename S>
93 if( str.length() < (N+4U) )
94 return decode( str+std::string(N+4U,
'\0') , state_out , n_out ) ;
95 convert( str , state_out , n_out ) ;
98 template <
unsigned int N,
typename U,
typename S>
101 *p_out++ =
static_cast<char>(n&0xffU) ; n >>= 8U ;
102 *p_out++ =
static_cast<char>(n&0xffU) ; n >>= 8U ;
103 *p_out++ =
static_cast<char>(n&0xffU) ; n >>= 8U ;
104 *p_out++ =
static_cast<char>(n&0xffU) ;
107 template <
unsigned int N,
typename U,
typename S>
111 n_out |=
static_cast<unsigned char>(hi) ; n_out <<= 8 ;
112 n_out |=
static_cast<unsigned char>(himid) ; n_out <<= 8 ;
113 n_out |=
static_cast<unsigned char>(lomid) ; n_out <<= 8 ;
114 n_out |=
static_cast<unsigned char>(lo) ;
117 template <
unsigned int N,
typename U,
typename S>
120 convert( str.at(3) , str.at(2) , str.at(1) , str.at(0) , n_out ) ;
123 template <
unsigned int N,
typename U,
typename S>
126 for(
unsigned int i = 0U ; i < (N/4U) ; i++ )
128 convert( str.at(i*4U+3U) , str.at(i*4U+2U) , str.at(i*4U+1U) , str.at(i*4U+0U) , state_out[i] ) ;
132 template <
unsigned int N,
typename U,
typename S>
135 convert( str , state_out ) ;
137 convert( str.at(N+3U) , str.at(N+2U) , str.at(N+1U) , str.at(N) , nn ) ;
138 n_out =
static_cast<size_type
>(nn) ;
Helper functions for representing the state of a hash function as a non-printable string...
static void decode(const std::string &, uint_type *, size_type &)
Converts an encode()d string back into a hash state of N integers and a data size returned by referen...
static std::string encode(const uint_type *)
Returns the hash state as a string, typically containing non-printing characters. ...