Commit 2a713da1ec2e9f74c9edc75b06540ab095c68c34

Edward Thomson 2021-09-29T21:31:17

hash: accept the algorithm in inputs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
diff --git a/src/commit_graph.c b/src/commit_graph.c
index f663fc5..8bb2397 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 3588e6b..e1adb2c 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 30b9f64..4434d37 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 a3f6b14..40cd9a4 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 a448208..5d120ea 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 d56c772..2f11a88 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 31a31b3..7938e3b 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 5c33051..838f43c 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 ce77375..c3b1aee 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 6a885ed..7ef8524 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 7834e5f..c247808 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 b0abbbf..1f8a0bb 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 faff310..560f57d 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 196b003..aa35193 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 830f1ca..e9a6476 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 813cd86..626c965 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 8be2cfd..03b183c 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 5f5441a..42d446a 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);