Commit 2a713da1 by Edward Thomson

hash: accept the algorithm in inputs

parent 3fff5970
......@@ -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;
......
......@@ -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,
......
......@@ -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,
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -12,16 +12,21 @@ 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)
......@@ -43,7 +48,8 @@ int git_hash_init(git_hash_ctx *ctx)
default:
/* unreachable */ ;
}
GIT_ASSERT(0);
git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
return -1;
}
......@@ -55,7 +61,8 @@ int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
default:
/* unreachable */ ;
}
GIT_ASSERT(0);
git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
return -1;
}
......@@ -67,16 +74,21 @@ int git_hash_final(git_oid *out, git_hash_ctx *ctx)
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++) {
......
......@@ -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
......@@ -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)
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -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)
......
......@@ -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));
......
......@@ -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);
}
......
......@@ -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]);
......
......@@ -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));
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment