Skip to content

Commit b7187ed

Browse files
committed
hash: add ability to distinguish algorithms
Create an enum that allows us to distinguish between different hashing algorithms. This enum is embedded into each `git_hash_ctx` and will instruct the code to which hashing function the particular request shall be dispatched. As we do not yet have multiple hashing algorithms, we simply initialize the hash algorithm to always be SHA1. At a later point, we will have to extend the `git_hash_init_ctx` function to get as parameter which algorithm shall be used.
1 parent 8832172 commit b7187ed

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/hash.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,55 @@ int git_hash_global_init(void)
1414

1515
int git_hash_ctx_init(git_hash_ctx *ctx)
1616
{
17-
return git_hash_sha1_ctx_init(&ctx->sha1);
17+
int error;
18+
19+
if ((error = git_hash_sha1_ctx_init(&ctx->sha1)) < 0)
20+
return error;
21+
22+
ctx->algo = GIT_HASH_ALGO_SHA1;
23+
24+
return 0;
1825
}
1926

2027
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
2128
{
22-
git_hash_sha1_ctx_cleanup(&ctx->sha1);
29+
switch (ctx->algo) {
30+
case GIT_HASH_ALGO_SHA1:
31+
git_hash_sha1_ctx_cleanup(&ctx->sha1);
32+
return;
33+
default:
34+
assert(0);
35+
}
2336
}
2437

2538
int git_hash_init(git_hash_ctx *ctx)
2639
{
27-
return git_hash_sha1_init(&ctx->sha1);
40+
switch (ctx->algo) {
41+
case GIT_HASH_ALGO_SHA1:
42+
return git_hash_sha1_init(&ctx->sha1);
43+
default:
44+
assert(0);
45+
}
2846
}
2947

3048
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
3149
{
32-
return git_hash_sha1_update(&ctx->sha1, data, len);
50+
switch (ctx->algo) {
51+
case GIT_HASH_ALGO_SHA1:
52+
return git_hash_sha1_update(&ctx->sha1, data, len);
53+
default:
54+
assert(0);
55+
}
3356
}
3457

3558
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
3659
{
37-
return git_hash_sha1_final(out, &ctx->sha1);
60+
switch (ctx->algo) {
61+
case GIT_HASH_ALGO_SHA1:
62+
return git_hash_sha1_final(out, &ctx->sha1);
63+
default:
64+
assert(0);
65+
}
3866
}
3967

4068
int git_hash_buf(git_oid *out, const void *data, size_t len)

src/hash.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ typedef struct {
1717
size_t len;
1818
} git_buf_vec;
1919

20+
typedef enum {
21+
GIT_HASH_ALGO_UNKNOWN = 0,
22+
GIT_HASH_ALGO_SHA1,
23+
} git_hash_algo_t;
24+
2025
#include "hash/sha1.h"
2126

2227
typedef struct git_hash_ctx {
2328
union {
2429
git_hash_sha1_ctx sha1;
2530
};
31+
git_hash_algo_t algo;
2632
} git_hash_ctx;
2733

2834
int git_hash_global_init(void);

0 commit comments

Comments
 (0)