-#if defined(__cplusplus)
-extern "C"
-{
-#endif
-
-#define SHA1_BLOCK_SIZE 64
-
-#define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
-#define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
-
-#define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
-
-#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
-#define bsw_32(p,n) \
- { int _i = (n); while(_i--) ((uint32_t*)p)[_i] = bswap_32(((uint32_t*)p)[_i]); }
-#else
-#define bsw_32(p,n)
-#endif
-
-#define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
-
-#if 0
-
-#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
-#define parity(x,y,z) ((x) ^ (y) ^ (z))
-#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
-
-#else /* Discovered by Rich Schroeppel and Colin Plumb */
-
-#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
-#define parity(x,y,z) ((x) ^ (y) ^ (z))
-#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
-
-#endif
-
-/* Compile 64 bytes of hash data into SHA1 context. Note */
-/* that this routine assumes that the byte order in the */
-/* ctx->wbuf[] at this point is in such an order that low */
-/* address bytes in the ORIGINAL byte stream will go in */
-/* this buffer to the high end of 32-bit words on BOTH big */
-/* and little endian systems */
-
-#ifdef ARRAY
-#define q(v,n) v[n]
-#else
-#define q(v,n) v##n
-#endif
-
-#define one_cycle(v,a,b,c,d,e,f,k,h) \
- q(v,e) += rotr32(q(v,a),27) + \
- f(q(v,b),q(v,c),q(v,d)) + k + h; \
- q(v,b) = rotr32(q(v,b), 2)
-
-#define five_cycle(v,f,k,i) \
- one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \
- one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \
- one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \
- one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \
- one_cycle(v, 1,2,3,4,0, f,k,hf(i+4))
-
-static void sha1_compile(sha1_ctx ctx[1])
-{ uint32_t *w = ctx->wbuf;
-
-#ifdef ARRAY
- uint32_t v[5];
- memcpy(v, ctx->hash, 5 * sizeof(uint32_t));
-#else
- uint32_t v0, v1, v2, v3, v4;
- v0 = ctx->hash[0]; v1 = ctx->hash[1];
- v2 = ctx->hash[2]; v3 = ctx->hash[3];
- v4 = ctx->hash[4];
-#endif
-
-#define hf(i) w[i]
-
- five_cycle(v, ch, 0x5a827999, 0);
- five_cycle(v, ch, 0x5a827999, 5);
- five_cycle(v, ch, 0x5a827999, 10);
- one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \
-
-#undef hf
-#define hf(i) (w[(i) & 15] = rotl32( \
- w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \
- ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1))
-
- one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16));
- one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17));
- one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18));
- one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19));
-
- five_cycle(v, parity, 0x6ed9eba1, 20);
- five_cycle(v, parity, 0x6ed9eba1, 25);
- five_cycle(v, parity, 0x6ed9eba1, 30);
- five_cycle(v, parity, 0x6ed9eba1, 35);
-
- five_cycle(v, maj, 0x8f1bbcdc, 40);
- five_cycle(v, maj, 0x8f1bbcdc, 45);
- five_cycle(v, maj, 0x8f1bbcdc, 50);
- five_cycle(v, maj, 0x8f1bbcdc, 55);
-
- five_cycle(v, parity, 0xca62c1d6, 60);
- five_cycle(v, parity, 0xca62c1d6, 65);
- five_cycle(v, parity, 0xca62c1d6, 70);
- five_cycle(v, parity, 0xca62c1d6, 75);
-
-#ifdef ARRAY
- ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
- ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
- ctx->hash[4] += v[4];
-#else
- ctx->hash[0] += v0; ctx->hash[1] += v1;
- ctx->hash[2] += v2; ctx->hash[3] += v3;
- ctx->hash[4] += v4;
-#endif
-}