E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ghashstate.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file ghashstate.h
19 ///
20
21 #ifndef G_HASH_STATE__H
22 #define G_HASH_STATE__H
23
24 #include "gdef.h"
25 #include <string>
26
27 namespace G
28 {
29 template <unsigned int N, typename U, typename S> class HashState ;
30 }
31
32 /// \class G::HashState
33 /// Helper functions for representing the state of a hash function as
34 /// a non-printable string. The hash state must be an array of 'N/4'
35 /// 32-bit values, with encoded strings being N or N+4 characters.
36 /// \see G::Hash::printable
37 ///
38 template <unsigned int N, typename U, typename S>
39 class G::HashState
40 {
41 public:
42 typedef U uint_type ;
43 typedef S size_type ;
44
45 static std::string encode( const uint_type * ) ;
46 ///< Returns the hash state as a string, typically
47 ///< containing non-printing characters.
48
49 static std::string encode( const uint_type * , size_type n ) ;
50 ///< Returns the hash state as a string that also
51 ///< encodes the data size.
52
53 static void decode( const std::string & , uint_type * , size_type & ) ;
54 ///< Converts an encode()d string back into a hash
55 ///< state of N integers and a data size returned
56 ///< by reference.
57
58 private:
59 HashState() ;
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 & ) ;
65 } ;
66
67 template <unsigned int N, typename U, typename S>
68 std::string G::HashState<N,U,S>::encode( const uint_type * state )
69 {
70 std::string result( N , '\0' ) ;
71 for( size_t i = 0U ; i < N/4 ; i++ )
72 {
73 convert( state[i] , &result[i*4U] ) ;
74 }
75 return result ;
76 }
77
78 template <unsigned int N, typename U, typename S>
79 std::string G::HashState<N,U,S>::encode( const uint_type * state , size_type n )
80 {
81 std::string result( N+4U , '\0' ) ;
82 for( size_t i = 0U ; i < N/4 ; i++ )
83 {
84 convert( state[i] , &result[i*4U] ) ;
85 }
86 convert( n , &result[N] ) ;
87 return result ;
88 }
89
90 template <unsigned int N, typename U, typename S>
91 void G::HashState<N,U,S>::decode( const std::string & str , uint_type * state_out , size_type & n_out )
92 {
93 if( str.length() < (N+4U) )
94 return decode( str+std::string(N+4U,'\0') , state_out , n_out ) ; // resurse if too short
95 convert( str , state_out , n_out ) ;
96 }
97
98 template <unsigned int N, typename U, typename S>
99 void G::HashState<N,U,S>::convert( uint_type n , char * p_out )
100 {
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) ;
105 }
106
107 template <unsigned int N, typename U, typename S>
108 void G::HashState<N,U,S>::convert( char hi , char himid , char lomid , char lo , uint_type & n_out )
109 {
110 n_out = 0U ;
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) ;
115 }
116
117 template <unsigned int N, typename U, typename S>
118 void G::HashState<N,U,S>::convert( const std::string & str , uint_type & n_out )
119 {
120 convert( str.at(3) , str.at(2) , str.at(1) , str.at(0) , n_out ) ;
121 }
122
123 template <unsigned int N, typename U, typename S>
124 void G::HashState<N,U,S>::convert( const std::string & str , uint_type * state_out )
125 {
126 for( unsigned int i = 0U ; i < (N/4U) ; i++ )
127 {
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] ) ;
129 }
130 }
131
132 template <unsigned int N, typename U, typename S>
133 void G::HashState<N,U,S>::convert( const std::string & str , uint_type * state_out , size_type & n_out )
134 {
135 convert( str , state_out ) ;
136 uint_type nn ;
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) ;
139 }
140
141 #endif
G::HashState
Helper functions for representing the state of a hash function as a non-printable string...
Definition: ghashstate.h:29
G::HashState::decode
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...
Definition: ghashstate.h:91
G
Low-level classes.
Definition: garg.h:29
G::HashState::encode
static std::string encode(const uint_type *)
Returns the hash state as a string, typically containing non-printing characters. ...
Definition: ghashstate.h:68