summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-09-29 21:31:17 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2021-10-01 08:12:07 -0400
commit2a713da1ec2e9f74c9edc75b06540ab095c68c34 (patch)
tree488891e9b76cf54582738c48674e7dfde81cc638
parent3fff59705fec852b97639364ca7b3e84ff7040b7 (diff)
downloadlibgit2-2a713da1ec2e9f74c9edc75b06540ab095c68c34.tar.gz
hash: accept the algorithm in inputs
-rw-r--r--src/commit_graph.c4
-rw-r--r--src/config_file.c4
-rw-r--r--src/diff.c2
-rw-r--r--src/filebuf.c2
-rw-r--r--src/futils.c2
-rw-r--r--src/hash.c76
-rw-r--r--src/hash.h6
-rw-r--r--src/index.c2
-rw-r--r--src/indexer.c4
-rw-r--r--src/midx.c4
-rw-r--r--src/odb.c6
-rw-r--r--src/odb_loose.c2
-rw-r--r--src/pack-objects.c2
-rw-r--r--tests/core/sha1.c2
-rw-r--r--tests/object/raw/hash.c6
-rw-r--r--tests/object/raw/short.c2
-rw-r--r--tests/odb/largefiles.c2
-rw-r--r--tests/pack/packbuilder.c2
18 files changed, 73 insertions, 57 deletions
diff --git a/src/commit_graph.c b/src/commit_graph.c
index f663fc5d3..8bb2397d7 100644
--- a/src/commit_graph.c
+++ b/src/commit_graph.c
@@ -230,7 +230,7 @@ int git_commit_graph_file_parse(
return commit_graph_error("wrong commit-graph size");
git_oid_cpy(&file->checksum, (git_oid *)(data + trailer_offset));
- if (git_hash_buf(&cgraph_checksum, data, (size_t)trailer_offset) < 0)
+ if (git_hash_buf(&cgraph_checksum, data, (size_t)trailer_offset, GIT_HASH_ALGORITHM_SHA1) < 0)
return commit_graph_error("could not calculate signature");
if (!git_oid_equal(&cgraph_checksum, &file->checksum))
return commit_graph_error("index signature mismatch");
@@ -986,7 +986,7 @@ static int commit_graph_write(
hash_cb_data.cb_data = cb_data;
hash_cb_data.ctx = &ctx;
- error = git_hash_ctx_init(&ctx);
+ error = git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1);
if (error < 0)
return error;
cb_data = &hash_cb_data;
diff --git a/src/config_file.c b/src/config_file.c
index 3588e6be6..e1adb2cbc 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -144,7 +144,7 @@ static int config_file_is_modified(int *modified, config_file *file)
if ((error = git_futils_readbuffer(&buf, file->path)) < 0)
goto out;
- if ((error = git_hash_buf(&hash, buf.ptr, buf.size)) < 0)
+ if ((error = git_hash_buf(&hash, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0)
goto out;
if (!git_oid_equal(&hash, &file->checksum)) {
@@ -869,7 +869,7 @@ static int config_file_read(
goto out;
git_futils_filestamp_set_from_stat(&file->stamp, &st);
- if ((error = git_hash_buf(&file->checksum, contents.ptr, contents.size)) < 0)
+ if ((error = git_hash_buf(&file->checksum, contents.ptr, contents.size, GIT_HASH_ALGORITHM_SHA1)) < 0)
goto out;
if ((error = config_file_read_buffer(entries, repo, file, level, depth,
diff --git a/src/diff.c b/src/diff.c
index 30b9f647a..4434d37d4 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -352,7 +352,7 @@ int git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opt
memset(&args, 0, sizeof(args));
args.first_file = 1;
- if ((error = git_hash_ctx_init(&args.ctx)) < 0)
+ if ((error = git_hash_ctx_init(&args.ctx, GIT_HASH_ALGORITHM_SHA1)) < 0)
goto out;
if ((error = git_diff_print(diff,
diff --git a/src/filebuf.c b/src/filebuf.c
index a3f6b1483..40cd9a445 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -305,7 +305,7 @@ int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mo
if (flags & GIT_FILEBUF_HASH_CONTENTS) {
file->compute_digest = 1;
- if (git_hash_ctx_init(&file->digest) < 0)
+ if (git_hash_ctx_init(&file->digest, GIT_HASH_ALGORITHM_SHA1) < 0)
goto cleanup;
}
diff --git a/src/futils.c b/src/futils.c
index a44820875..5d120eaed 100644
--- a/src/futils.c
+++ b/src/futils.c
@@ -216,7 +216,7 @@ int git_futils_readbuffer_updated(
p_close(fd);
if (checksum) {
- if ((error = git_hash_buf(&checksum_new, buf.ptr, buf.size)) < 0) {
+ if ((error = git_hash_buf(&checksum_new, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0) {
git_buf_dispose(&buf);
return error;
}
diff --git a/src/hash.c b/src/hash.c
index d56c77221..2f11a88e8 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -12,71 +12,83 @@ int git_hash_global_init(void)
return git_hash_sha1_global_init();
}
-int git_hash_ctx_init(git_hash_ctx *ctx)
+int git_hash_ctx_init(git_hash_ctx *ctx, git_hash_algorithm_t algorithm)
{
int error;
- if ((error = git_hash_sha1_ctx_init(&ctx->ctx.sha1)) < 0)
- return error;
-
- ctx->algorithm = GIT_HASH_ALGORITHM_SHA1;
+ switch (algorithm) {
+ case GIT_HASH_ALGORITHM_SHA1:
+ error = git_hash_sha1_ctx_init(&ctx->ctx.sha1);
+ break;
+ default:
+ git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
+ error = -1;
+ }
- return 0;
+ ctx->algorithm = algorithm;
+ return error;
}
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
{
switch (ctx->algorithm) {
- case GIT_HASH_ALGORITHM_SHA1:
- git_hash_sha1_ctx_cleanup(&ctx->ctx.sha1);
- return;
- default:
- /* unreachable */ ;
+ case GIT_HASH_ALGORITHM_SHA1:
+ git_hash_sha1_ctx_cleanup(&ctx->ctx.sha1);
+ return;
+ default:
+ /* unreachable */ ;
}
}
int git_hash_init(git_hash_ctx *ctx)
{
switch (ctx->algorithm) {
- case GIT_HASH_ALGORITHM_SHA1:
- return git_hash_sha1_init(&ctx->ctx.sha1);
- default:
- /* unreachable */ ;
+ case GIT_HASH_ALGORITHM_SHA1:
+ return git_hash_sha1_init(&ctx->ctx.sha1);
+ default:
+ /* unreachable */ ;
}
- GIT_ASSERT(0);
+
+ git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
return -1;
}
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
{
switch (ctx->algorithm) {
- case GIT_HASH_ALGORITHM_SHA1:
- return git_hash_sha1_update(&ctx->ctx.sha1, data, len);
- default:
- /* unreachable */ ;
+ case GIT_HASH_ALGORITHM_SHA1:
+ return git_hash_sha1_update(&ctx->ctx.sha1, data, len);
+ default:
+ /* unreachable */ ;
}
- GIT_ASSERT(0);
+
+ git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
return -1;
}
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
{
switch (ctx->algorithm) {
- case GIT_HASH_ALGORITHM_SHA1:
- return git_hash_sha1_final(out, &ctx->ctx.sha1);
- default:
- /* unreachable */ ;
+ case GIT_HASH_ALGORITHM_SHA1:
+ return git_hash_sha1_final(out, &ctx->ctx.sha1);
+ default:
+ /* unreachable */ ;
}
- GIT_ASSERT(0);
+
+ git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
return -1;
}
-int git_hash_buf(git_oid *out, const void *data, size_t len)
+int git_hash_buf(
+ git_oid *out,
+ const void *data,
+ size_t len,
+ git_hash_algorithm_t algorithm)
{
git_hash_ctx ctx;
int error = 0;
- if (git_hash_ctx_init(&ctx) < 0)
+ if (git_hash_ctx_init(&ctx, algorithm) < 0)
return -1;
if ((error = git_hash_update(&ctx, data, len)) >= 0)
@@ -87,13 +99,17 @@ int git_hash_buf(git_oid *out, const void *data, size_t len)
return error;
}
-int git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n)
+int git_hash_vec(
+ git_oid *out,
+ git_buf_vec *vec,
+ size_t n,
+ git_hash_algorithm_t algorithm)
{
git_hash_ctx ctx;
size_t i;
int error = 0;
- if (git_hash_ctx_init(&ctx) < 0)
+ if (git_hash_ctx_init(&ctx, algorithm) < 0)
return -1;
for (i = 0; i < n; i++) {
diff --git a/src/hash.h b/src/hash.h
index 31a31b325..7938e3b22 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -33,14 +33,14 @@ typedef struct git_hash_ctx {
int git_hash_global_init(void);
-int git_hash_ctx_init(git_hash_ctx *ctx);
+int git_hash_ctx_init(git_hash_ctx *ctx, git_hash_algorithm_t algorithm);
void git_hash_ctx_cleanup(git_hash_ctx *ctx);
int git_hash_init(git_hash_ctx *c);
int git_hash_update(git_hash_ctx *c, const void *data, size_t len);
int git_hash_final(git_oid *out, git_hash_ctx *c);
-int git_hash_buf(git_oid *out, const void *data, size_t len);
-int git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n);
+int git_hash_buf(git_oid *out, const void *data, size_t len, git_hash_algorithm_t algorithm);
+int git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n, git_hash_algorithm_t algorithm);
#endif
diff --git a/src/index.c b/src/index.c
index 5c3305170..838f43c2d 100644
--- a/src/index.c
+++ b/src/index.c
@@ -2648,7 +2648,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
/* Precalculate the SHA1 of the files's contents -- we'll match it to
* the provided SHA1 in the footer */
- git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE);
+ git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE, GIT_HASH_ALGORITHM_SHA1);
/* Parse header */
if ((error = read_header(&header, buffer)) < 0)
diff --git a/src/indexer.c b/src/indexer.c
index ce7737500..c3b1aee32 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -152,8 +152,8 @@ int git_indexer_new(
idx->mode = mode ? mode : GIT_PACK_FILE_MODE;
git_buf_init(&idx->entry_data, 0);
- if ((error = git_hash_ctx_init(&idx->hash_ctx)) < 0 ||
- (error = git_hash_ctx_init(&idx->trailer)) < 0 ||
+ if ((error = git_hash_ctx_init(&idx->hash_ctx, GIT_HASH_ALGORITHM_SHA1)) < 0 ||
+ (error = git_hash_ctx_init(&idx->trailer, GIT_HASH_ALGORITHM_SHA1)) < 0 ||
(error = git_oidmap_new(&idx->expected_oids)) < 0)
goto cleanup;
diff --git a/src/midx.c b/src/midx.c
index 6a885eddc..7ef85248c 100644
--- a/src/midx.c
+++ b/src/midx.c
@@ -212,7 +212,7 @@ int git_midx_parse(
return midx_error("wrong index size");
git_oid_cpy(&idx->checksum, (git_oid *)(data + trailer_offset));
- if (git_hash_buf(&idx_checksum, data, (size_t)trailer_offset) < 0)
+ if (git_hash_buf(&idx_checksum, data, (size_t)trailer_offset, GIT_HASH_ALGORITHM_SHA1) < 0)
return midx_error("could not calculate signature");
if (!git_oid_equal(&idx_checksum, &idx->checksum))
return midx_error("index signature mismatch");
@@ -668,7 +668,7 @@ static int midx_write(
hash_cb_data.cb_data = cb_data;
hash_cb_data.ctx = &ctx;
- error = git_hash_ctx_init(&ctx);
+ error = git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1);
if (error < 0)
return error;
cb_data = &hash_cb_data;
diff --git a/src/odb.c b/src/odb.c
index 7834e5f15..c24780834 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -136,7 +136,7 @@ int git_odb__hashobj(git_oid *id, git_rawobj *obj)
vec[1].data = obj->data;
vec[1].len = obj->len;
- return git_hash_vec(id, vec, 2);
+ return git_hash_vec(id, vec, 2, GIT_HASH_ALGORITHM_SHA1);
}
@@ -210,7 +210,7 @@ int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_object_t type)
return -1;
}
- if ((error = git_hash_ctx_init(&ctx)) < 0)
+ if ((error = git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1)) < 0)
return error;
if ((error = git_odb__format_object_header(&hdr_len, hdr,
@@ -1561,7 +1561,7 @@ int git_odb_open_wstream(
ctx = git__malloc(sizeof(git_hash_ctx));
GIT_ERROR_CHECK_ALLOC(ctx);
- if ((error = git_hash_ctx_init(ctx)) < 0 ||
+ if ((error = git_hash_ctx_init(ctx, GIT_HASH_ALGORITHM_SHA1)) < 0 ||
(error = hash_header(ctx, size, type)) < 0)
goto done;
diff --git a/src/odb_loose.c b/src/odb_loose.c
index b0abbbf4c..1f8a0bb21 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -1023,7 +1023,7 @@ static int loose_backend__readstream(
hash_ctx = git__malloc(sizeof(git_hash_ctx));
GIT_ERROR_CHECK_ALLOC(hash_ctx);
- if ((error = git_hash_ctx_init(hash_ctx)) < 0 ||
+ if ((error = git_hash_ctx_init(hash_ctx, GIT_HASH_ALGORITHM_SHA1)) < 0 ||
(error = git_futils_mmap_ro_file(&stream->map, object_path.ptr)) < 0 ||
(error = git_zstream_init(&stream->zstream, GIT_ZSTREAM_INFLATE)) < 0)
goto done;
diff --git a/src/pack-objects.c b/src/pack-objects.c
index faff310b4..560f57d03 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -141,7 +141,7 @@ int git_packbuilder_new(git_packbuilder **out, git_repository *repo)
pb->repo = repo;
pb->nr_threads = 1; /* do not spawn any thread by default */
- if (git_hash_ctx_init(&pb->ctx) < 0 ||
+ if (git_hash_ctx_init(&pb->ctx, GIT_HASH_ALGORITHM_SHA1) < 0 ||
git_zstream_init(&pb->zstream, GIT_ZSTREAM_DEFLATE) < 0 ||
git_repository_odb(&pb->odb, repo) < 0 ||
packbuilder_config(pb) < 0)
diff --git a/tests/core/sha1.c b/tests/core/sha1.c
index 196b00352..aa351935b 100644
--- a/tests/core/sha1.c
+++ b/tests/core/sha1.c
@@ -23,7 +23,7 @@ static int sha1_file(git_oid *oid, const char *filename)
fd = p_open(filename, O_RDONLY);
cl_assert(fd >= 0);
- cl_git_pass(git_hash_ctx_init(&ctx));
+ cl_git_pass(git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1));
while ((read_len = p_read(fd, buf, 2048)) > 0)
cl_git_pass(git_hash_update(&ctx, buf, (size_t)read_len));
diff --git a/tests/object/raw/hash.c b/tests/object/raw/hash.c
index 830f1caa1..e9a647627 100644
--- a/tests/object/raw/hash.c
+++ b/tests/object/raw/hash.c
@@ -26,7 +26,7 @@ void test_object_raw_hash__hash_by_blocks(void)
git_hash_ctx ctx;
git_oid id1, id2;
- cl_git_pass(git_hash_ctx_init(&ctx));
+ cl_git_pass(git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1));
/* should already be init'd */
cl_git_pass(git_hash_update(&ctx, hello_text, strlen(hello_text)));
@@ -49,7 +49,7 @@ void test_object_raw_hash__hash_buffer_in_single_call(void)
git_oid id1, id2;
cl_git_pass(git_oid_fromstr(&id1, hello_id));
- git_hash_buf(&id2, hello_text, strlen(hello_text));
+ git_hash_buf(&id2, hello_text, strlen(hello_text), GIT_HASH_ALGORITHM_SHA1);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
@@ -65,7 +65,7 @@ void test_object_raw_hash__hash_vector(void)
vec[1].data = hello_text+4;
vec[1].len = strlen(hello_text)-4;
- git_hash_vec(&id2, vec, 2);
+ git_hash_vec(&id2, vec, 2, GIT_HASH_ALGORITHM_SHA1);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
diff --git a/tests/object/raw/short.c b/tests/object/raw/short.c
index 813cd86b6..626c9654b 100644
--- a/tests/object/raw/short.c
+++ b/tests/object/raw/short.c
@@ -33,7 +33,7 @@ static int insert_sequential_oids(
for (i = 0; i < n; ++i) {
p_snprintf(numbuf, sizeof(numbuf), "%u", (unsigned int)i);
- git_hash_buf(&oid, numbuf, strlen(numbuf));
+ git_hash_buf(&oid, numbuf, strlen(numbuf), GIT_HASH_ALGORITHM_SHA1);
oids[i] = git__malloc(GIT_OID_HEXSZ + 1);
cl_assert(oids[i]);
diff --git a/tests/odb/largefiles.c b/tests/odb/largefiles.c
index 8be2cfd02..03b183cd5 100644
--- a/tests/odb/largefiles.c
+++ b/tests/odb/largefiles.c
@@ -107,7 +107,7 @@ void test_odb_largefiles__streamread(void)
cl_assert_equal_sz(LARGEFILE_SIZE, len);
cl_assert_equal_i(GIT_OBJECT_BLOB, type);
- cl_git_pass(git_hash_ctx_init(&hash));
+ cl_git_pass(git_hash_ctx_init(&hash, GIT_HASH_ALGORITHM_SHA1));
cl_git_pass(git_odb__format_object_header(&hdr_len, hdr, sizeof(hdr), len, type));
cl_git_pass(git_hash_update(&hash, hdr, hdr_len));
diff --git a/tests/pack/packbuilder.c b/tests/pack/packbuilder.c
index 5f5441a5d..42d446a8b 100644
--- a/tests/pack/packbuilder.c
+++ b/tests/pack/packbuilder.c
@@ -126,7 +126,7 @@ void test_pack_packbuilder__create_pack(void)
cl_git_pass(git_futils_readbuffer(&buf, git_buf_cstr(&path)));
- cl_git_pass(git_hash_ctx_init(&ctx));
+ cl_git_pass(git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1));
cl_git_pass(git_hash_update(&ctx, buf.ptr, buf.size));
cl_git_pass(git_hash_final(&hash, &ctx));
git_hash_ctx_cleanup(&ctx);