sha1: provide another level of indirection for the SHA-1 functions

The git source uses git_SHA1_Update() and friends to call into the code that computes the hashes. Traditionally, we used to map these directly to underlying implementation of the SHA-1 hash (e.g. SHA1_Update() from OpenSSL or blk_SHA1_Update() from block-sha1/). This arrangement however makes it hard to tweak behaviour of the underlying implementation without fully replacing. If we want to introduce a tweaked_SHA1_Update() wrapper to implement the "Update" in a slightly different way, for example, the implementation of the wrapper still would want to call into the underlying implementation, but tweaked_SHA1_Update() cannot call git_SHA1_Update() to get to the underlying implementation (often but not always SHA1_Update()). Add another level of indirection that maps platform_SHA1_Update() and friends to their underlying implementations, and by default make git_SHA1_Update() and friends map to platform_SHA1_* functions. Doing it this way will later allow us to map git_SHA1_Update() to tweaked_SHA1_Update(), and the latter can use platform_SHA1_Update() in its implementation. Signed-off-by: Atousa Pahlevan Duprat <apahlevan@ieee.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Atousa Pahlevan Duprat committed Nov 4, 2015 at 22:38 UTC 3bc72fde3f35b988c902abf5a0bc027f2e54d0c1
3 files changed +25 -13
block-sha1/sha1.h
+4 -4
@@ -16,7 +16,7 @@ void blk_SHA1_Init(blk_SHA_CTX *ctx);
16 void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *dataIn, unsigned long len);
17 void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx);
18
19 -#define git_SHA_CTX blk_SHA_CTX
20 -#define git_SHA1_Init blk_SHA1_Init
21 -#define git_SHA1_Update blk_SHA1_Update
22 -#define git_SHA1_Final blk_SHA1_Final
19 +#define platform_SHA_CTX blk_SHA_CTX
20 +#define platform_SHA1_Init blk_SHA1_Init
21 +#define platform_SHA1_Update blk_SHA1_Update
22 +#define platform_SHA1_Final blk_SHA1_Final
cache.h
+17 -5
@@ -11,13 +11,25 @@
11 #include "string-list.h"
12
13 #include SHA1_HEADER
14 -#ifndef git_SHA_CTX
15 -#define git_SHA_CTX SHA_CTX
16 -#define git_SHA1_Init SHA1_Init
17 -#define git_SHA1_Update SHA1_Update
18 -#define git_SHA1_Final SHA1_Final
14 +#ifndef platform_SHA_CTX
15 +/*
16 + * platform's underlying implementation of SHA-1; could be OpenSSL,
17 + * blk_SHA, Apple CommonCrypto, etc... Note that including
18 + * SHA1_HEADER may have already defined platform_SHA_CTX for our
19 + * own implementations like block-sha1 and ppc-sha1, so we list
20 + * the default for OpenSSL compatible SHA-1 implementations here.
21 + */
22 +#define platform_SHA_CTX SHA_CTX
23 +#define platform_SHA1_Init SHA1_Init
24 +#define platform_SHA1_Update SHA1_Update
25 +#define platform_SHA1_Final SHA1_Final
26 #endif
27
28 +#define git_SHA_CTX platform_SHA_CTX
29 +#define git_SHA1_Init platform_SHA1_Init
30 +#define git_SHA1_Update platform_SHA1_Update
31 +#define git_SHA1_Final platform_SHA1_Final
32 +
33 #include <zlib.h>
34 typedef struct git_zstream {
35 z_stream z;
ppc/sha1.h
+4 -4
@@ -19,7 +19,7 @@ int ppc_SHA1_Init(ppc_SHA_CTX *c);
19 int ppc_SHA1_Update(ppc_SHA_CTX *c, const void *p, unsigned long n);
20 int ppc_SHA1_Final(unsigned char *hash, ppc_SHA_CTX *c);
21
22 -#define git_SHA_CTX ppc_SHA_CTX
23 -#define git_SHA1_Init ppc_SHA1_Init
24 -#define git_SHA1_Update ppc_SHA1_Update
25 -#define git_SHA1_Final ppc_SHA1_Final
22 +#define platform_SHA_CTX ppc_SHA_CTX
23 +#define platform_SHA1_Init ppc_SHA1_Init
24 +#define platform_SHA1_Update ppc_SHA1_Update
25 +#define platform_SHA1_Final ppc_SHA1_Final