Skip to content

Commit 246569b

Browse files
committed
Merge branch 'ps/hash-cleanup'
Further code clean-up on the use of hash functions. Now the context object knows what hash function it is working with. * ps/hash-cleanup: global: adapt callers to use generic hash context helpers hash: provide generic wrappers to update hash contexts hash: stop typedeffing the hash context hash: convert hashing context to a structure
2 parents 0ca6b46 + 0578f1e commit 246569b

23 files changed

+226
-202
lines changed

builtin/fast-import.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -954,15 +954,15 @@ static int store_object(
954954
unsigned char hdr[96];
955955
struct object_id oid;
956956
unsigned long hdrlen, deltalen;
957-
git_hash_ctx c;
957+
struct git_hash_ctx c;
958958
git_zstream s;
959959

960960
hdrlen = format_object_header((char *)hdr, sizeof(hdr), type,
961961
dat->len);
962962
the_hash_algo->init_fn(&c);
963-
the_hash_algo->update_fn(&c, hdr, hdrlen);
964-
the_hash_algo->update_fn(&c, dat->buf, dat->len);
965-
the_hash_algo->final_oid_fn(&oid, &c);
963+
git_hash_update(&c, hdr, hdrlen);
964+
git_hash_update(&c, dat->buf, dat->len);
965+
git_hash_final_oid(&oid, &c);
966966
if (oidout)
967967
oidcpy(oidout, &oid);
968968

@@ -1096,7 +1096,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
10961096
struct object_id oid;
10971097
unsigned long hdrlen;
10981098
off_t offset;
1099-
git_hash_ctx c;
1099+
struct git_hash_ctx c;
11001100
git_zstream s;
11011101
struct hashfile_checkpoint checkpoint;
11021102
int status = Z_OK;
@@ -1114,7 +1114,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
11141114
hdrlen = format_object_header((char *)out_buf, out_sz, OBJ_BLOB, len);
11151115

11161116
the_hash_algo->init_fn(&c);
1117-
the_hash_algo->update_fn(&c, out_buf, hdrlen);
1117+
git_hash_update(&c, out_buf, hdrlen);
11181118

11191119
crc32_begin(pack_file);
11201120

@@ -1132,7 +1132,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
11321132
if (!n && feof(stdin))
11331133
die("EOF in data (%" PRIuMAX " bytes remaining)", len);
11341134

1135-
the_hash_algo->update_fn(&c, in_buf, n);
1135+
git_hash_update(&c, in_buf, n);
11361136
s.next_in = in_buf;
11371137
s.avail_in = n;
11381138
len -= n;
@@ -1158,7 +1158,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
11581158
}
11591159
}
11601160
git_deflate_end(&s);
1161-
the_hash_algo->final_oid_fn(&oid, &c);
1161+
git_hash_final_oid(&oid, &c);
11621162

11631163
if (oidout)
11641164
oidcpy(oidout, &oid);

builtin/index-pack.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static unsigned int input_offset, input_len;
151151
static off_t consumed_bytes;
152152
static off_t max_input_size;
153153
static unsigned deepest_delta;
154-
static git_hash_ctx input_ctx;
154+
static struct git_hash_ctx input_ctx;
155155
static uint32_t input_crc32;
156156
static int input_fd, output_fd;
157157
static const char *curr_pack;
@@ -301,7 +301,7 @@ static void flush(void)
301301
if (input_offset) {
302302
if (output_fd >= 0)
303303
write_or_die(output_fd, input_buffer, input_offset);
304-
the_hash_algo->update_fn(&input_ctx, input_buffer, input_offset);
304+
git_hash_update(&input_ctx, input_buffer, input_offset);
305305
memmove(input_buffer, input_buffer + input_offset, input_len);
306306
input_offset = 0;
307307
}
@@ -475,14 +475,14 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
475475
int status;
476476
git_zstream stream;
477477
void *buf;
478-
git_hash_ctx c;
478+
struct git_hash_ctx c;
479479
char hdr[32];
480480
int hdrlen;
481481

482482
if (!is_delta_type(type)) {
483483
hdrlen = format_object_header(hdr, sizeof(hdr), type, size);
484484
the_hash_algo->init_fn(&c);
485-
the_hash_algo->update_fn(&c, hdr, hdrlen);
485+
git_hash_update(&c, hdr, hdrlen);
486486
} else
487487
oid = NULL;
488488
if (type == OBJ_BLOB && size > big_file_threshold)
@@ -502,7 +502,7 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
502502
status = git_inflate(&stream, 0);
503503
use(input_len - stream.avail_in);
504504
if (oid)
505-
the_hash_algo->update_fn(&c, last_out, stream.next_out - last_out);
505+
git_hash_update(&c, last_out, stream.next_out - last_out);
506506
if (buf == fixed_buf) {
507507
stream.next_out = buf;
508508
stream.avail_out = sizeof(fixed_buf);
@@ -512,7 +512,7 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
512512
bad_object(offset, _("inflate returned %d"), status);
513513
git_inflate_end(&stream);
514514
if (oid)
515-
the_hash_algo->final_oid_fn(oid, &c);
515+
git_hash_final_oid(oid, &c);
516516
return buf == fixed_buf ? NULL : buf;
517517
}
518518

@@ -1248,7 +1248,7 @@ static void parse_pack_objects(unsigned char *hash)
12481248
struct ofs_delta_entry *ofs_delta = ofs_deltas;
12491249
struct object_id ref_delta_oid;
12501250
struct stat st;
1251-
git_hash_ctx tmp_ctx;
1251+
struct git_hash_ctx tmp_ctx;
12521252

12531253
if (verbose)
12541254
progress = start_progress(
@@ -1286,9 +1286,8 @@ static void parse_pack_objects(unsigned char *hash)
12861286

12871287
/* Check pack integrity */
12881288
flush();
1289-
the_hash_algo->init_fn(&tmp_ctx);
1290-
the_hash_algo->clone_fn(&tmp_ctx, &input_ctx);
1291-
the_hash_algo->final_fn(hash, &tmp_ctx);
1289+
git_hash_clone(&tmp_ctx, &input_ctx);
1290+
git_hash_final(hash, &tmp_ctx);
12921291
if (!hasheq(fill(the_hash_algo->rawsz), hash, the_repository->hash_algo))
12931292
die(_("pack is corrupted (SHA1 mismatch)"));
12941293
use(the_hash_algo->rawsz);

builtin/patch-id.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static size_t get_one_patchid(struct object_id *next_oid, struct object_id *resu
7070
int before = -1, after = -1;
7171
int diff_is_binary = 0;
7272
char pre_oid_str[GIT_MAX_HEXSZ + 1], post_oid_str[GIT_MAX_HEXSZ + 1];
73-
git_hash_ctx ctx;
73+
struct git_hash_ctx ctx;
7474

7575
the_hash_algo->init_fn(&ctx);
7676
oidclr(result, the_repository->hash_algo);
@@ -85,7 +85,7 @@ static size_t get_one_patchid(struct object_id *next_oid, struct object_id *resu
8585
!skip_prefix(line, "From ", &p) &&
8686
starts_with(line, "\\ ") && 12 < strlen(line)) {
8787
if (verbatim)
88-
the_hash_algo->update_fn(&ctx, line, strlen(line));
88+
git_hash_update(&ctx, line, strlen(line));
8989
continue;
9090
}
9191

@@ -104,10 +104,10 @@ static size_t get_one_patchid(struct object_id *next_oid, struct object_id *resu
104104
starts_with(line, "Binary files")) {
105105
diff_is_binary = 1;
106106
before = 0;
107-
the_hash_algo->update_fn(&ctx, pre_oid_str,
108-
strlen(pre_oid_str));
109-
the_hash_algo->update_fn(&ctx, post_oid_str,
110-
strlen(post_oid_str));
107+
git_hash_update(&ctx, pre_oid_str,
108+
strlen(pre_oid_str));
109+
git_hash_update(&ctx, post_oid_str,
110+
strlen(post_oid_str));
111111
if (stable)
112112
flush_one_hunk(result, &ctx);
113113
continue;
@@ -165,7 +165,7 @@ static size_t get_one_patchid(struct object_id *next_oid, struct object_id *resu
165165
/* Add line to hash algo (possibly removing whitespace) */
166166
len = verbatim ? strlen(line) : remove_space(line);
167167
patchlen += len;
168-
the_hash_algo->update_fn(&ctx, line, len);
168+
git_hash_update(&ctx, line, len);
169169
}
170170

171171
if (!found_next)

builtin/receive-pack.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,14 @@ static void hmac_hash(unsigned char *out,
566566
unsigned char k_ipad[GIT_MAX_BLKSZ];
567567
unsigned char k_opad[GIT_MAX_BLKSZ];
568568
int i;
569-
git_hash_ctx ctx;
569+
struct git_hash_ctx ctx;
570570

571571
/* RFC 2104 2. (1) */
572572
memset(key, '\0', GIT_MAX_BLKSZ);
573573
if (the_hash_algo->blksz < key_len) {
574574
the_hash_algo->init_fn(&ctx);
575-
the_hash_algo->update_fn(&ctx, key_in, key_len);
576-
the_hash_algo->final_fn(key, &ctx);
575+
git_hash_update(&ctx, key_in, key_len);
576+
git_hash_final(key, &ctx);
577577
} else {
578578
memcpy(key, key_in, key_len);
579579
}
@@ -586,15 +586,15 @@ static void hmac_hash(unsigned char *out,
586586

587587
/* RFC 2104 2. (3) & (4) */
588588
the_hash_algo->init_fn(&ctx);
589-
the_hash_algo->update_fn(&ctx, k_ipad, sizeof(k_ipad));
590-
the_hash_algo->update_fn(&ctx, text, text_len);
591-
the_hash_algo->final_fn(out, &ctx);
589+
git_hash_update(&ctx, k_ipad, sizeof(k_ipad));
590+
git_hash_update(&ctx, text, text_len);
591+
git_hash_final(out, &ctx);
592592

593593
/* RFC 2104 2. (6) & (7) */
594594
the_hash_algo->init_fn(&ctx);
595-
the_hash_algo->update_fn(&ctx, k_opad, sizeof(k_opad));
596-
the_hash_algo->update_fn(&ctx, out, the_hash_algo->rawsz);
597-
the_hash_algo->final_fn(out, &ctx);
595+
git_hash_update(&ctx, k_opad, sizeof(k_opad));
596+
git_hash_update(&ctx, out, the_hash_algo->rawsz);
597+
git_hash_final(out, &ctx);
598598
}
599599

600600
static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)

builtin/unpack-objects.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static unsigned char buffer[4096];
2828
static unsigned int offset, len;
2929
static off_t consumed_bytes;
3030
static off_t max_input_size;
31-
static git_hash_ctx ctx;
31+
static struct git_hash_ctx ctx;
3232
static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
3333
static struct progress *progress;
3434

@@ -70,7 +70,7 @@ static void *fill(int min)
7070
if (min > sizeof(buffer))
7171
die("cannot fill %d bytes", min);
7272
if (offset) {
73-
the_hash_algo->update_fn(&ctx, buffer, offset);
73+
git_hash_update(&ctx, buffer, offset);
7474
memmove(buffer, buffer + offset, len);
7575
offset = 0;
7676
}
@@ -614,7 +614,7 @@ int cmd_unpack_objects(int argc,
614614
{
615615
int i;
616616
struct object_id oid;
617-
git_hash_ctx tmp_ctx;
617+
struct git_hash_ctx tmp_ctx;
618618

619619
disable_replace_refs();
620620

@@ -667,10 +667,9 @@ int cmd_unpack_objects(int argc,
667667
}
668668
the_hash_algo->init_fn(&ctx);
669669
unpack_all();
670-
the_hash_algo->update_fn(&ctx, buffer, offset);
671-
the_hash_algo->init_fn(&tmp_ctx);
672-
the_hash_algo->clone_fn(&tmp_ctx, &ctx);
673-
the_hash_algo->final_oid_fn(&oid, &tmp_ctx);
670+
git_hash_update(&ctx, buffer, offset);
671+
git_hash_clone(&tmp_ctx, &ctx);
672+
git_hash_final_oid(&oid, &tmp_ctx);
674673
if (strict) {
675674
write_rest();
676675
if (fsck_finish(&fsck_options))

bulk-checkin.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static int already_written(struct bulk_checkin_packfile *state, struct object_id
162162
* with a new pack.
163163
*/
164164
static int stream_blob_to_pack(struct bulk_checkin_packfile *state,
165-
git_hash_ctx *ctx, off_t *already_hashed_to,
165+
struct git_hash_ctx *ctx, off_t *already_hashed_to,
166166
int fd, size_t size, const char *path,
167167
unsigned flags)
168168
{
@@ -195,7 +195,7 @@ static int stream_blob_to_pack(struct bulk_checkin_packfile *state,
195195
if (rsize < hsize)
196196
hsize = rsize;
197197
if (hsize)
198-
the_hash_algo->update_fn(ctx, ibuf, hsize);
198+
git_hash_update(ctx, ibuf, hsize);
199199
*already_hashed_to = offset;
200200
}
201201
s.next_in = ibuf;
@@ -259,7 +259,7 @@ static int deflate_blob_to_pack(struct bulk_checkin_packfile *state,
259259
const char *path, unsigned flags)
260260
{
261261
off_t seekback, already_hashed_to;
262-
git_hash_ctx ctx;
262+
struct git_hash_ctx ctx;
263263
unsigned char obuf[16384];
264264
unsigned header_len;
265265
struct hashfile_checkpoint checkpoint;
@@ -272,7 +272,7 @@ static int deflate_blob_to_pack(struct bulk_checkin_packfile *state,
272272
header_len = format_object_header((char *)obuf, sizeof(obuf),
273273
OBJ_BLOB, size);
274274
the_hash_algo->init_fn(&ctx);
275-
the_hash_algo->update_fn(&ctx, obuf, header_len);
275+
git_hash_update(&ctx, obuf, header_len);
276276

277277
/* Note: idx is non-NULL when we are writing */
278278
if ((flags & HASH_WRITE_OBJECT) != 0) {
@@ -307,7 +307,7 @@ static int deflate_blob_to_pack(struct bulk_checkin_packfile *state,
307307
if (lseek(fd, seekback, SEEK_SET) == (off_t) -1)
308308
return error("cannot seek back");
309309
}
310-
the_hash_algo->final_oid_fn(result_oid, &ctx);
310+
git_hash_final_oid(result_oid, &ctx);
311311
if (!idx)
312312
return 0;
313313

csum-file.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void hashflush(struct hashfile *f)
5151

5252
if (offset) {
5353
if (!f->skip_hash)
54-
f->algop->update_fn(&f->ctx, f->buffer, offset);
54+
git_hash_update(&f->ctx, f->buffer, offset);
5555
flush(f, f->buffer, offset);
5656
f->offset = 0;
5757
}
@@ -74,7 +74,7 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result,
7474
if (f->skip_hash)
7575
hashclr(f->buffer, f->algop);
7676
else
77-
f->algop->final_fn(f->buffer, &f->ctx);
77+
git_hash_final(f->buffer, &f->ctx);
7878

7979
if (result)
8080
hashcpy(result, f->buffer, f->algop);
@@ -129,7 +129,7 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
129129
* f->offset is necessarily zero.
130130
*/
131131
if (!f->skip_hash)
132-
f->algop->update_fn(&f->ctx, buf, nr);
132+
git_hash_update(&f->ctx, buf, nr);
133133
flush(f, buf, nr);
134134
} else {
135135
/*
@@ -218,7 +218,7 @@ void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpo
218218
{
219219
hashflush(f);
220220
checkpoint->offset = f->total;
221-
f->algop->clone_fn(&checkpoint->ctx, &f->ctx);
221+
git_hash_clone(&checkpoint->ctx, &f->ctx);
222222
}
223223

224224
int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
@@ -229,7 +229,7 @@ int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint
229229
lseek(f->fd, offset, SEEK_SET) != offset)
230230
return -1;
231231
f->total = offset;
232-
f->algop->clone_fn(&f->ctx, &checkpoint->ctx);
232+
git_hash_clone(&f->ctx, &checkpoint->ctx);
233233
f->offset = 0; /* hashflush() was called in checkpoint */
234234
return 0;
235235
}
@@ -249,16 +249,16 @@ uint32_t crc32_end(struct hashfile *f)
249249
int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
250250
{
251251
unsigned char got[GIT_MAX_RAWSZ];
252-
git_hash_ctx ctx;
252+
struct git_hash_ctx ctx;
253253
const struct git_hash_algo *algop = unsafe_hash_algo(the_hash_algo);
254254
size_t data_len = total_len - algop->rawsz;
255255

256256
if (total_len < algop->rawsz)
257257
return 0; /* say "too short"? */
258258

259259
algop->init_fn(&ctx);
260-
algop->update_fn(&ctx, data, data_len);
261-
algop->final_fn(got, &ctx);
260+
git_hash_update(&ctx, data, data_len);
261+
git_hash_final(got, &ctx);
262262

263263
return hasheq(got, data + data_len, algop);
264264
}

csum-file.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct hashfile {
1111
int fd;
1212
int check_fd;
1313
unsigned int offset;
14-
git_hash_ctx ctx;
14+
struct git_hash_ctx ctx;
1515
off_t total;
1616
struct progress *tp;
1717
const char *name;
@@ -33,7 +33,7 @@ struct hashfile {
3333
/* Checkpoint */
3434
struct hashfile_checkpoint {
3535
off_t offset;
36-
git_hash_ctx ctx;
36+
struct git_hash_ctx ctx;
3737
};
3838

3939
void hashfile_checkpoint_init(struct hashfile *, struct hashfile_checkpoint *);

0 commit comments

Comments
 (0)