update dev300-m58
[ooovba.git] / bridges / source / cpp_uno / cc50_solaris_sparc / hash.cxx
blob381f45b75eb31b8e02d560c86f12fddb4e55b6d9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hash.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_bridges.hxx"
36 #ifndef TEST
37 #ifndef _SAL_TYPES_H_
38 #include <sal/types.h>
39 #endif
40 #else
41 typedef unsigned int sal_uInt32;
42 #endif
44 #include <string.h>
47 * build a hash for a character buffer using the NIST algorithm
50 class NIST_Hash
53 // helper functions
54 sal_uInt32 f1( sal_uInt32 x, sal_uInt32 y, sal_uInt32 z )
56 return z ^ ( x & ( y ^ z ) );
59 sal_uInt32 f2( sal_uInt32 x, sal_uInt32 y, sal_uInt32 z )
61 return x ^ y ^ z;
64 sal_uInt32 f3( sal_uInt32 x, sal_uInt32 y, sal_uInt32 z )
66 return ( x & y ) + ( z & ( x ^ y ) );
69 sal_uInt32 rotl( sal_uInt32 nValue, sal_uInt32 nBits )
71 return ( nValue << nBits ) | ( nValue >> (32-nBits) );
74 sal_uInt32 expand_nostore( sal_uInt32 index )
76 return data[index&15] ^ data[(index-14)&15] ^ data[(index-8)&15] ^ data[(index-3)&15];
79 sal_uInt32 expand_store( sal_uInt32 index )
81 return data[index&15] ^= data[(index-14)&15] ^ data[(index-8)&15] ^ data[(index-3)&15];
84 void subRound( sal_uInt32 a, sal_uInt32& b, sal_uInt32 c, sal_uInt32 d, sal_uInt32& e, sal_uInt32 constant, sal_uInt32 datum, sal_uInt32 nFunction )
86 e += rotl(a,5);
87 switch( nFunction )
89 case 1: e += f1( b, c, d );break;
90 case 2:
91 case 4: e += f2( b, c, d );break;
92 case 3: e += f3( b, c, d );break;
94 e += constant + datum;
95 b = rotl( b, 30 );
98 void transform();
99 void final();
101 // data members
102 sal_uInt32 data[16];
103 sal_uInt32 hashdata[5];
104 public:
105 NIST_Hash( const char* pString, sal_uInt32 nLen );
107 sal_uInt32 *getHash() { return hashdata; }
110 void NIST_Hash::transform()
112 // constants
113 const sal_uInt32 K2 = 0x5A827999;
114 const sal_uInt32 K3 = 0x6ED9EBA1;
115 const sal_uInt32 K5 = 0x8F1BBCDC;
116 const sal_uInt32 K10 = 0xCA62C1D6;
118 sal_uInt32 a, b, c, d, e;
119 a = hashdata[0];
120 b = hashdata[1];
121 c = hashdata[2];
122 d = hashdata[3];
123 e = hashdata[4];
125 subRound( a, b, c, d, e, K2, data[ 0], 1 );
126 subRound( e, a, b, c, d, K2, data[ 1], 1 );
127 subRound( d, e, a, b, c, K2, data[ 2], 1 );
128 subRound( c, d, e, a, b, K2, data[ 3], 1 );
129 subRound( b, c, d, e, a, K2, data[ 4], 1 );
130 subRound( a, b, c, d, e, K2, data[ 5], 1 );
131 subRound( e, a, b, c, d, K2, data[ 6], 1 );
132 subRound( d, e, a, b, c, K2, data[ 7], 1 );
133 subRound( c, d, e, a, b, K2, data[ 8], 1 );
134 subRound( b, c, d, e, a, K2, data[ 9], 1 );
135 subRound( a, b, c, d, e, K2, data[10], 1 );
136 subRound( e, a, b, c, d, K2, data[11], 1 );
137 subRound( d, e, a, b, c, K2, data[12], 1 );
138 subRound( c, d, e, a, b, K2, data[13], 1 );
139 subRound( b, c, d, e, a, K2, data[14], 1 );
140 subRound( a, b, c, d, e, K2, data[15], 1 );
141 subRound( e, a, b, c, d, K2, expand_store( 16 ), 1 );
142 subRound( d, e, a, b, c, K2, expand_store( 17 ), 1 );
143 subRound( c, d, e, a, b, K2, expand_store( 18 ), 1 );
144 subRound( b, c, d, e, a, K2, expand_store( 19 ), 1 );
146 subRound( a, b, c, d, e, K3, expand_store( 20 ), 2 );
147 subRound( e, a, b, c, d, K3, expand_store( 21 ), 2 );
148 subRound( d, e, a, b, c, K3, expand_store( 22 ), 2 );
149 subRound( c, d, e, a, b, K3, expand_store( 23 ), 2 );
150 subRound( b, c, d, e, a, K3, expand_store( 24 ), 2 );
151 subRound( a, b, c, d, e, K3, expand_store( 25 ), 2 );
152 subRound( e, a, b, c, d, K3, expand_store( 26 ), 2 );
153 subRound( d, e, a, b, c, K3, expand_store( 27 ), 2 );
154 subRound( c, d, e, a, b, K3, expand_store( 28 ), 2 );
155 subRound( b, c, d, e, a, K3, expand_store( 29 ), 2 );
156 subRound( a, b, c, d, e, K3, expand_store( 30 ), 2 );
157 subRound( e, a, b, c, d, K3, expand_store( 31 ), 2 );
158 subRound( d, e, a, b, c, K3, expand_store( 32 ), 2 );
159 subRound( c, d, e, a, b, K3, expand_store( 33 ), 2 );
160 subRound( b, c, d, e, a, K3, expand_store( 34 ), 2 );
161 subRound( a, b, c, d, e, K3, expand_store( 35 ), 2 );
162 subRound( e, a, b, c, d, K3, expand_store( 36 ), 2 );
163 subRound( d, e, a, b, c, K3, expand_store( 37 ), 2 );
164 subRound( c, d, e, a, b, K3, expand_store( 38 ), 2 );
165 subRound( b, c, d, e, a, K3, expand_store( 39 ), 2 );
167 subRound( a, b, c, d, e, K5, expand_store( 40 ), 3 );
168 subRound( e, a, b, c, d, K5, expand_store( 41 ), 3 );
169 subRound( d, e, a, b, c, K5, expand_store( 42 ), 3 );
170 subRound( c, d, e, a, b, K5, expand_store( 43 ), 3 );
171 subRound( b, c, d, e, a, K5, expand_store( 44 ), 3 );
172 subRound( a, b, c, d, e, K5, expand_store( 45 ), 3 );
173 subRound( e, a, b, c, d, K5, expand_store( 46 ), 3 );
174 subRound( d, e, a, b, c, K5, expand_store( 47 ), 3 );
175 subRound( c, d, e, a, b, K5, expand_store( 48 ), 3 );
176 subRound( b, c, d, e, a, K5, expand_store( 49 ), 3 );
177 subRound( a, b, c, d, e, K5, expand_store( 50 ), 3 );
178 subRound( e, a, b, c, d, K5, expand_store( 51 ), 3 );
179 subRound( d, e, a, b, c, K5, expand_store( 52 ), 3 );
180 subRound( c, d, e, a, b, K5, expand_store( 53 ), 3 );
181 subRound( b, c, d, e, a, K5, expand_store( 54 ), 3 );
182 subRound( a, b, c, d, e, K5, expand_store( 55 ), 3 );
183 subRound( e, a, b, c, d, K5, expand_store( 56 ), 3 );
184 subRound( d, e, a, b, c, K5, expand_store( 57 ), 3 );
185 subRound( c, d, e, a, b, K5, expand_store( 58 ), 3 );
186 subRound( b, c, d, e, a, K5, expand_store( 59 ), 3 );
188 subRound( a, b, c, d, e, K10, expand_store( 60 ), 4 );
189 subRound( e, a, b, c, d, K10, expand_store( 61 ), 4 );
190 subRound( d, e, a, b, c, K10, expand_store( 62 ), 4 );
191 subRound( c, d, e, a, b, K10, expand_store( 63 ), 4 );
192 subRound( b, c, d, e, a, K10, expand_store( 64 ), 4 );
193 subRound( a, b, c, d, e, K10, expand_store( 65 ), 4 );
194 subRound( e, a, b, c, d, K10, expand_store( 66 ), 4 );
195 subRound( d, e, a, b, c, K10, expand_store( 67 ), 4 );
196 subRound( c, d, e, a, b, K10, expand_store( 68 ), 4 );
197 subRound( b, c, d, e, a, K10, expand_store( 69 ), 4 );
198 subRound( a, b, c, d, e, K10, expand_store( 70 ), 4 );
199 subRound( e, a, b, c, d, K10, expand_store( 71 ), 4 );
200 subRound( d, e, a, b, c, K10, expand_store( 72 ), 4 );
201 subRound( c, d, e, a, b, K10, expand_store( 73 ), 4 );
202 subRound( b, c, d, e, a, K10, expand_store( 74 ), 4 );
203 subRound( a, b, c, d, e, K10, expand_store( 75 ), 4 );
204 subRound( e, a, b, c, d, K10, expand_store( 76 ), 4 );
205 subRound( d, e, a, b, c, K10, expand_nostore( 77 ), 4 );
206 subRound( c, d, e, a, b, K10, expand_nostore( 78 ), 4 );
207 subRound( b, c, d, e, a, K10, expand_nostore( 79 ), 4 );
209 hashdata[0] += a;
210 hashdata[1] += b;
211 hashdata[2] += c;
212 hashdata[3] += d;
213 hashdata[4] += e;
216 #define BLOCKSIZE sizeof( data )
218 NIST_Hash::NIST_Hash( const char* pString, sal_uInt32 nLen )
220 hashdata[0] = 0x67452301;
221 hashdata[1] = 0xefcdab89;
222 hashdata[2] = 0x98badcfe;
223 hashdata[3] = 0x10325476;
224 hashdata[4] = 0xc3d2e1f0;
226 sal_uInt32 nBytes = nLen;
228 while( nLen >= sizeof( data ) )
230 memcpy( data, pString, sizeof( data ) );
231 pString += sizeof( data );
232 nLen -= sizeof( data );
233 transform();
235 memcpy( data, pString, nLen );
236 ((char*)data)[nLen++] = 0x80;
237 if( nLen > sizeof( data ) - 8 )
239 memset( ((char*)data)+nLen, 0, sizeof( data ) - nLen );
240 transform();
241 memset( data, 0, sizeof( data ) - 8 );
243 else
244 memset( ((char*)data)+nLen, 0, sizeof( data ) - 8 - nLen );
245 data[14] = 0;
246 data[15] = nBytes << 3;
247 transform();
250 #ifdef TEST
251 #include <stdio.h>
252 int main( int argc, const char** argv )
254 const char* pHash = argc < 2 ? argv[0] : argv[1];
256 NIST_Hash aHash( pHash, strlen( pHash ) );
257 sal_uInt32* pBits = aHash.getHash();
259 printf( "text : %s\n"
260 "bits : 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
261 pHash,
262 pBits[0], pBits[1], pBits[2],pBits[3],pBits[4]
264 return 0;
267 #endif