diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2021-09-29 21:31:17 -0400 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2021-10-01 08:12:07 -0400 |
commit | 2a713da1ec2e9f74c9edc75b06540ab095c68c34 (patch) | |
tree | 488891e9b76cf54582738c48674e7dfde81cc638 /src | |
parent | 3fff59705fec852b97639364ca7b3e84ff7040b7 (diff) | |
download | libgit2-2a713da1ec2e9f74c9edc75b06540ab095c68c34.tar.gz |
hash: accept the algorithm in inputs
Diffstat (limited to 'src')
-rw-r--r-- | src/commit_graph.c | 4 | ||||
-rw-r--r-- | src/config_file.c | 4 | ||||
-rw-r--r-- | src/diff.c | 2 | ||||
-rw-r--r-- | src/filebuf.c | 2 | ||||
-rw-r--r-- | src/futils.c | 2 | ||||
-rw-r--r-- | src/hash.c | 76 | ||||
-rw-r--r-- | src/hash.h | 6 | ||||
-rw-r--r-- | src/index.c | 2 | ||||
-rw-r--r-- | src/indexer.c | 4 | ||||
-rw-r--r-- | src/midx.c | 4 | ||||
-rw-r--r-- | src/odb.c | 6 | ||||
-rw-r--r-- | src/odb_loose.c | 2 | ||||
-rw-r--r-- | src/pack-objects.c | 2 |
13 files changed, 66 insertions, 50 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; @@ -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) |