16
16
17
17
#include " llvm/Support/SHA1.h"
18
18
#include " llvm/ADT/ArrayRef.h"
19
+ #include " llvm/Support/Endian.h"
19
20
#include " llvm/Support/Host.h"
20
21
using namespace llvm ;
21
22
@@ -26,45 +27,45 @@ using namespace llvm;
26
27
#define SHA_BIG_ENDIAN
27
28
#endif
28
29
29
- static uint32_t rol (uint32_t Number, int Bits) {
30
+ static inline uint32_t rol (uint32_t Number, int Bits) {
30
31
return (Number << Bits) | (Number >> (32 - Bits));
31
32
}
32
33
33
- static uint32_t blk0 (uint32_t *Buf, int I) { return Buf[I]; }
34
+ static inline uint32_t blk0 (uint32_t *Buf, int I) { return Buf[I]; }
34
35
35
- static uint32_t blk (uint32_t *Buf, int I) {
36
+ static inline uint32_t blk (uint32_t *Buf, int I) {
36
37
Buf[I & 15 ] = rol (Buf[(I + 13 ) & 15 ] ^ Buf[(I + 8 ) & 15 ] ^ Buf[(I + 2 ) & 15 ] ^
37
38
Buf[I & 15 ],
38
39
1 );
39
40
return Buf[I & 15 ];
40
41
}
41
42
42
- static void r0 (uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E ,
43
- int I, uint32_t *Buf) {
43
+ static inline void r0 (uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D,
44
+ uint32_t &E, int I, uint32_t *Buf) {
44
45
E += ((B & (C ^ D)) ^ D) + blk0 (Buf, I) + 0x5A827999 + rol (A, 5 );
45
46
B = rol (B, 30 );
46
47
}
47
48
48
- static void r1 (uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E ,
49
- int I, uint32_t *Buf) {
49
+ static inline void r1 (uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D,
50
+ uint32_t &E, int I, uint32_t *Buf) {
50
51
E += ((B & (C ^ D)) ^ D) + blk (Buf, I) + 0x5A827999 + rol (A, 5 );
51
52
B = rol (B, 30 );
52
53
}
53
54
54
- static void r2 (uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E ,
55
- int I, uint32_t *Buf) {
55
+ static inline void r2 (uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D,
56
+ uint32_t &E, int I, uint32_t *Buf) {
56
57
E += (B ^ C ^ D) + blk (Buf, I) + 0x6ED9EBA1 + rol (A, 5 );
57
58
B = rol (B, 30 );
58
59
}
59
60
60
- static void r3 (uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E ,
61
- int I, uint32_t *Buf) {
61
+ static inline void r3 (uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D,
62
+ uint32_t &E, int I, uint32_t *Buf) {
62
63
E += (((B | C) & D) | (B & C)) + blk (Buf, I) + 0x8F1BBCDC + rol (A, 5 );
63
64
B = rol (B, 30 );
64
65
}
65
66
66
- static void r4 (uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E ,
67
- int I, uint32_t *Buf) {
67
+ static inline void r4 (uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D,
68
+ uint32_t &E, int I, uint32_t *Buf) {
68
69
E += (B ^ C ^ D) + blk (Buf, I) + 0xCA62C1D6 + rol (A, 5 );
69
70
B = rol (B, 30 );
70
71
}
@@ -210,8 +211,31 @@ void SHA1::writebyte(uint8_t Data) {
210
211
}
211
212
212
213
void SHA1::update (ArrayRef<uint8_t > Data) {
213
- for (auto &C : Data)
214
- writebyte (C);
214
+ InternalState.ByteCount += Data.size ();
215
+
216
+ // Finish the current block.
217
+ if (InternalState.BufferOffset > 0 ) {
218
+ const size_t Remainder = std::min<size_t >(
219
+ Data.size (), BLOCK_LENGTH - InternalState.BufferOffset );
220
+ for (size_t I = 0 ; I < Remainder; ++I)
221
+ addUncounted (Data[I]);
222
+ Data = Data.drop_front (Remainder);
223
+ }
224
+
225
+ // Fast buffer filling for large inputs.
226
+ while (Data.size () >= BLOCK_LENGTH) {
227
+ assert (InternalState.BufferOffset == 0 );
228
+ assert (BLOCK_LENGTH % 4 == 0 );
229
+ constexpr size_t BLOCK_LENGTH_32 = BLOCK_LENGTH / 4 ;
230
+ for (size_t I = 0 ; I < BLOCK_LENGTH_32; ++I)
231
+ InternalState.Buffer .L [I] = support::endian::read32be (&Data[I * 4 ]);
232
+ hashBlock ();
233
+ Data = Data.drop_front (BLOCK_LENGTH);
234
+ }
235
+
236
+ // Finish the remainder.
237
+ for (uint8_t C : Data)
238
+ addUncounted (C);
215
239
}
216
240
217
241
void SHA1::pad () {
0 commit comments