Skip to content

Commit 18e2588

Browse files
bk2204gitster
authored andcommitted
sha1_file: switch uses of SHA-1 to the_hash_algo
Switch various uses of explicit calls to SHA-1 into references to the_hash_algo for better abstraction. Convert some calls to use struct object_id. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3206b6b commit 18e2588

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

sha1_file.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -786,16 +786,16 @@ void *xmmap(void *start, size_t length,
786786
int check_sha1_signature(const unsigned char *sha1, void *map,
787787
unsigned long size, const char *type)
788788
{
789-
unsigned char real_sha1[20];
789+
struct object_id real_oid;
790790
enum object_type obj_type;
791791
struct git_istream *st;
792-
git_SHA_CTX c;
792+
git_hash_ctx c;
793793
char hdr[32];
794794
int hdrlen;
795795

796796
if (map) {
797-
hash_sha1_file(map, size, type, real_sha1);
798-
return hashcmp(sha1, real_sha1) ? -1 : 0;
797+
hash_sha1_file(map, size, type, real_oid.hash);
798+
return hashcmp(sha1, real_oid.hash) ? -1 : 0;
799799
}
800800

801801
st = open_istream(sha1, &obj_type, &size, NULL);
@@ -806,8 +806,8 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
806806
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(obj_type), size) + 1;
807807

808808
/* Sha1.. */
809-
git_SHA1_Init(&c);
810-
git_SHA1_Update(&c, hdr, hdrlen);
809+
the_hash_algo->init_fn(&c);
810+
the_hash_algo->update_fn(&c, hdr, hdrlen);
811811
for (;;) {
812812
char buf[1024 * 16];
813813
ssize_t readlen = read_istream(st, buf, sizeof(buf));
@@ -818,11 +818,11 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
818818
}
819819
if (!readlen)
820820
break;
821-
git_SHA1_Update(&c, buf, readlen);
821+
the_hash_algo->update_fn(&c, buf, readlen);
822822
}
823-
git_SHA1_Final(real_sha1, &c);
823+
the_hash_algo->final_fn(real_oid.hash, &c);
824824
close_istream(st);
825-
return hashcmp(sha1, real_sha1) ? -1 : 0;
825+
return hashcmp(sha1, real_oid.hash) ? -1 : 0;
826826
}
827827

828828
int git_open_cloexec(const char *name, int flags)
@@ -1421,16 +1421,16 @@ static void write_sha1_file_prepare(const void *buf, unsigned long len,
14211421
const char *type, unsigned char *sha1,
14221422
char *hdr, int *hdrlen)
14231423
{
1424-
git_SHA_CTX c;
1424+
git_hash_ctx c;
14251425

14261426
/* Generate the header */
14271427
*hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
14281428

14291429
/* Sha1.. */
1430-
git_SHA1_Init(&c);
1431-
git_SHA1_Update(&c, hdr, *hdrlen);
1432-
git_SHA1_Update(&c, buf, len);
1433-
git_SHA1_Final(sha1, &c);
1430+
the_hash_algo->init_fn(&c);
1431+
the_hash_algo->update_fn(&c, hdr, *hdrlen);
1432+
the_hash_algo->update_fn(&c, buf, len);
1433+
the_hash_algo->final_fn(sha1, &c);
14341434
}
14351435

14361436
/*
@@ -1552,8 +1552,8 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
15521552
int fd, ret;
15531553
unsigned char compressed[4096];
15541554
git_zstream stream;
1555-
git_SHA_CTX c;
1556-
unsigned char parano_sha1[20];
1555+
git_hash_ctx c;
1556+
struct object_id parano_oid;
15571557
static struct strbuf tmp_file = STRBUF_INIT;
15581558
const char *filename = sha1_file_name(sha1);
15591559

@@ -1569,22 +1569,22 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
15691569
git_deflate_init(&stream, zlib_compression_level);
15701570
stream.next_out = compressed;
15711571
stream.avail_out = sizeof(compressed);
1572-
git_SHA1_Init(&c);
1572+
the_hash_algo->init_fn(&c);
15731573

15741574
/* First header.. */
15751575
stream.next_in = (unsigned char *)hdr;
15761576
stream.avail_in = hdrlen;
15771577
while (git_deflate(&stream, 0) == Z_OK)
15781578
; /* nothing */
1579-
git_SHA1_Update(&c, hdr, hdrlen);
1579+
the_hash_algo->update_fn(&c, hdr, hdrlen);
15801580

15811581
/* Then the data itself.. */
15821582
stream.next_in = (void *)buf;
15831583
stream.avail_in = len;
15841584
do {
15851585
unsigned char *in0 = stream.next_in;
15861586
ret = git_deflate(&stream, Z_FINISH);
1587-
git_SHA1_Update(&c, in0, stream.next_in - in0);
1587+
the_hash_algo->update_fn(&c, in0, stream.next_in - in0);
15881588
if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
15891589
die("unable to write sha1 file");
15901590
stream.next_out = compressed;
@@ -1596,8 +1596,8 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
15961596
ret = git_deflate_end_gently(&stream);
15971597
if (ret != Z_OK)
15981598
die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret);
1599-
git_SHA1_Final(parano_sha1, &c);
1600-
if (hashcmp(sha1, parano_sha1) != 0)
1599+
the_hash_algo->final_fn(parano_oid.hash, &c);
1600+
if (hashcmp(sha1, parano_oid.hash) != 0)
16011601
die("confused by unstable object source data for %s", sha1_to_hex(sha1));
16021602

16031603
close_sha1_file(fd);
@@ -2091,14 +2091,14 @@ static int check_stream_sha1(git_zstream *stream,
20912091
const char *path,
20922092
const unsigned char *expected_sha1)
20932093
{
2094-
git_SHA_CTX c;
2094+
git_hash_ctx c;
20952095
unsigned char real_sha1[GIT_MAX_RAWSZ];
20962096
unsigned char buf[4096];
20972097
unsigned long total_read;
20982098
int status = Z_OK;
20992099

2100-
git_SHA1_Init(&c);
2101-
git_SHA1_Update(&c, hdr, stream->total_out);
2100+
the_hash_algo->init_fn(&c);
2101+
the_hash_algo->update_fn(&c, hdr, stream->total_out);
21022102

21032103
/*
21042104
* We already read some bytes into hdr, but the ones up to the NUL
@@ -2117,7 +2117,7 @@ static int check_stream_sha1(git_zstream *stream,
21172117
if (size - total_read < stream->avail_out)
21182118
stream->avail_out = size - total_read;
21192119
status = git_inflate(stream, Z_FINISH);
2120-
git_SHA1_Update(&c, buf, stream->next_out - buf);
2120+
the_hash_algo->update_fn(&c, buf, stream->next_out - buf);
21212121
total_read += stream->next_out - buf;
21222122
}
21232123
git_inflate_end(stream);
@@ -2132,7 +2132,7 @@ static int check_stream_sha1(git_zstream *stream,
21322132
return -1;
21332133
}
21342134

2135-
git_SHA1_Final(real_sha1, &c);
2135+
the_hash_algo->final_fn(real_sha1, &c);
21362136
if (hashcmp(expected_sha1, real_sha1)) {
21372137
error("sha1 mismatch for %s (expected %s)", path,
21382138
sha1_to_hex(expected_sha1));

0 commit comments

Comments
 (0)