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 <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Feb 1, 2018 at 02:18 UTC 18e2588e11a1ae34f9a6141719ae08d13956affd
1 file changed +26 -26
sha1_file.c
+26 -26
@@ -786,16 +786,16 @@ void *xmmap(void *start, size_t length,
786 int check_sha1_signature(const unsigned char *sha1, void *map,
787 unsigned long size, const char *type)
788 {
789 - unsigned char real_sha1[20];
789 + struct object_id real_oid;
790 enum object_type obj_type;
791 struct git_istream *st;
792 - git_SHA_CTX c;
792 + git_hash_ctx c;
793 char hdr[32];
794 int hdrlen;
795
796 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;
799 }
800
801 st = open_istream(sha1, &obj_type, &size, NULL);
@@ -806,8 +806,8 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
806 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(obj_type), size) + 1;
807
808 /* 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);
811 for (;;) {
812 char buf[1024 * 16];
813 ssize_t readlen = read_istream(st, buf, sizeof(buf));
@@ -818,11 +818,11 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
818 }
819 if (!readlen)
820 break;
821 - git_SHA1_Update(&c, buf, readlen);
821 + the_hash_algo->update_fn(&c, buf, readlen);
822 }
823 - git_SHA1_Final(real_sha1, &c);
823 + the_hash_algo->final_fn(real_oid.hash, &c);
824 close_istream(st);
825 - return hashcmp(sha1, real_sha1) ? -1 : 0;
825 + return hashcmp(sha1, real_oid.hash) ? -1 : 0;
826 }
827
828 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,
1421 const char *type, unsigned char *sha1,
1422 char *hdr, int *hdrlen)
1423 {
1424 - git_SHA_CTX c;
1424 + git_hash_ctx c;
1425
1426 /* Generate the header */
1427 *hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
1428
1429 /* 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);
1434 }
1435
1436 /*
@@ -1552,8 +1552,8 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
1552 int fd, ret;
1553 unsigned char compressed[4096];
1554 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;
1557 static struct strbuf tmp_file = STRBUF_INIT;
1558 const char *filename = sha1_file_name(sha1);
1559
@@ -1569,14 +1569,14 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
1569 git_deflate_init(&stream, zlib_compression_level);
1570 stream.next_out = compressed;
1571 stream.avail_out = sizeof(compressed);
1572 - git_SHA1_Init(&c);
1572 + the_hash_algo->init_fn(&c);
1573
1574 /* First header.. */
1575 stream.next_in = (unsigned char *)hdr;
1576 stream.avail_in = hdrlen;
1577 while (git_deflate(&stream, 0) == Z_OK)
1578 ; /* nothing */
1579 - git_SHA1_Update(&c, hdr, hdrlen);
1579 + the_hash_algo->update_fn(&c, hdr, hdrlen);
1580
1581 /* Then the data itself.. */
1582 stream.next_in = (void *)buf;
@@ -1584,7 +1584,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
1584 do {
1585 unsigned char *in0 = stream.next_in;
1586 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);
1588 if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
1589 die("unable to write sha1 file");
1590 stream.next_out = compressed;
@@ -1596,8 +1596,8 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
1596 ret = git_deflate_end_gently(&stream);
1597 if (ret != Z_OK)
1598 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)
1601 die("confused by unstable object source data for %s", sha1_to_hex(sha1));
1602
1603 close_sha1_file(fd);
@@ -2091,14 +2091,14 @@ static int check_stream_sha1(git_zstream *stream,
2091 const char *path,
2092 const unsigned char *expected_sha1)
2093 {
2094 - git_SHA_CTX c;
2094 + git_hash_ctx c;
2095 unsigned char real_sha1[GIT_MAX_RAWSZ];
2096 unsigned char buf[4096];
2097 unsigned long total_read;
2098 int status = Z_OK;
2099
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);
2102
2103 /*
2104 * 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,
2117 if (size - total_read < stream->avail_out)
2118 stream->avail_out = size - total_read;
2119 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);
2121 total_read += stream->next_out - buf;
2122 }
2123 git_inflate_end(stream);
@@ -2132,7 +2132,7 @@ static int check_stream_sha1(git_zstream *stream,
2132 return -1;
2133 }
2134
2135 - git_SHA1_Final(real_sha1, &c);
2135 + the_hash_algo->final_fn(real_sha1, &c);
2136 if (hashcmp(expected_sha1, real_sha1)) {
2137 error("sha1 mismatch for %s (expected %s)", path,
2138 sha1_to_hex(expected_sha1));