Skip to content

Commit 3bc72fd

Browse files
Atousagitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 441c4a4 commit 3bc72fd

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

block-sha1/sha1.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void blk_SHA1_Init(blk_SHA_CTX *ctx);
1616
void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *dataIn, unsigned long len);
1717
void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx);
1818

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,25 @@
1111
#include "string-list.h"
1212

1313
#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
1926
#endif
2027

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+
2133
#include <zlib.h>
2234
typedef struct git_zstream {
2335
z_stream z;

ppc/sha1.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int ppc_SHA1_Init(ppc_SHA_CTX *c);
1919
int ppc_SHA1_Update(ppc_SHA_CTX *c, const void *p, unsigned long n);
2020
int ppc_SHA1_Final(unsigned char *hash, ppc_SHA_CTX *c);
2121

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

0 commit comments

Comments
 (0)