read-cache: abstract away uses of SHA-1

Convert various uses of direct calls to SHA-1 and 20- and 40-based constants to use the_hash_algo instead. Don't yet convert the on-disk data structures, which will be handled in a future commit. Adjust some comments so as not to refer explicitly to SHA-1. 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 aab6135906707d5d35caeb54307ead9eca96fb2a
1 file changed +29 -29
read-cache.c
+29 -29
@@ -1545,8 +1545,8 @@ int verify_ce_order;
1545
1546 static int verify_hdr(struct cache_header *hdr, unsigned long size)
1547 {
1548 - git_SHA_CTX c;
1549 - unsigned char sha1[20];
1548 + git_hash_ctx c;
1549 + unsigned char hash[GIT_MAX_RAWSZ];
1550 int hdr_version;
1551
1552 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
@@ -1558,10 +1558,10 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
1558 if (!verify_index_checksum)
1559 return 0;
1560
1561 - git_SHA1_Init(&c);
1562 - git_SHA1_Update(&c, hdr, size - 20);
1563 - git_SHA1_Final(sha1, &c);
1564 - if (hashcmp(sha1, (unsigned char *)hdr + size - 20))
1561 + the_hash_algo->init_fn(&c);
1562 + the_hash_algo->update_fn(&c, hdr, size - the_hash_algo->rawsz);
1563 + the_hash_algo->final_fn(hash, &c);
1564 + if (hashcmp(hash, (unsigned char *)hdr + size - the_hash_algo->rawsz))
1565 return error("bad index file sha1 signature");
1566 return 0;
1567 }
@@ -1791,7 +1791,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1791 die_errno("cannot stat the open index");
1792
1793 mmap_size = xsize_t(st.st_size);
1794 - if (mmap_size < sizeof(struct cache_header) + 20)
1794 + if (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
1795 die("index file smaller than expected");
1796
1797 mmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);
@@ -1803,7 +1803,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1803 if (verify_hdr(hdr, mmap_size) < 0)
1804 goto unmap;
1805
1806 - hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
1806 + hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - the_hash_algo->rawsz);
1807 istate->version = ntohl(hdr->hdr_version);
1808 istate->cache_nr = ntohl(hdr->hdr_entries);
1809 istate->cache_alloc = alloc_nr(istate->cache_nr);
@@ -1831,7 +1831,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1831 istate->timestamp.sec = st.st_mtime;
1832 istate->timestamp.nsec = ST_MTIME_NSEC(st);
1833
1834 - while (src_offset <= mmap_size - 20 - 8) {
1834 + while (src_offset <= mmap_size - the_hash_algo->rawsz - 8) {
1835 /* After an array of active_nr index entries,
1836 * there can be arbitrary number of extended
1837 * sections, each of which is prefixed with
@@ -1957,11 +1957,11 @@ int unmerged_index(const struct index_state *istate)
1957 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
1958 static unsigned long write_buffer_len;
1959
1960 -static int ce_write_flush(git_SHA_CTX *context, int fd)
1960 +static int ce_write_flush(git_hash_ctx *context, int fd)
1961 {
1962 unsigned int buffered = write_buffer_len;
1963 if (buffered) {
1964 - git_SHA1_Update(context, write_buffer, buffered);
1964 + the_hash_algo->update_fn(context, write_buffer, buffered);
1965 if (write_in_full(fd, write_buffer, buffered) < 0)
1966 return -1;
1967 write_buffer_len = 0;
@@ -1969,7 +1969,7 @@ static int ce_write_flush(git_SHA_CTX *context, int fd)
1969 return 0;
1970 }
1971
1972 -static int ce_write(git_SHA_CTX *context, int fd, void *data, unsigned int len)
1972 +static int ce_write(git_hash_ctx *context, int fd, void *data, unsigned int len)
1973 {
1974 while (len) {
1975 unsigned int buffered = write_buffer_len;
@@ -1991,7 +1991,7 @@ static int ce_write(git_SHA_CTX *context, int fd, void *data, unsigned int len)
1991 return 0;
1992 }
1993
1994 -static int write_index_ext_header(git_SHA_CTX *context, int fd,
1994 +static int write_index_ext_header(git_hash_ctx *context, int fd,
1995 unsigned int ext, unsigned int sz)
1996 {
1997 ext = htonl(ext);
@@ -2000,26 +2000,26 @@ static int write_index_ext_header(git_SHA_CTX *context, int fd,
2000 (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
2001 }
2002
2003 -static int ce_flush(git_SHA_CTX *context, int fd, unsigned char *sha1)
2003 +static int ce_flush(git_hash_ctx *context, int fd, unsigned char *hash)
2004 {
2005 unsigned int left = write_buffer_len;
2006
2007 if (left) {
2008 write_buffer_len = 0;
2009 - git_SHA1_Update(context, write_buffer, left);
2009 + the_hash_algo->update_fn(context, write_buffer, left);
2010 }
2011
2012 - /* Flush first if not enough space for SHA1 signature */
2013 - if (left + 20 > WRITE_BUFFER_SIZE) {
2012 + /* Flush first if not enough space for hash signature */
2013 + if (left + the_hash_algo->rawsz > WRITE_BUFFER_SIZE) {
2014 if (write_in_full(fd, write_buffer, left) < 0)
2015 return -1;
2016 left = 0;
2017 }
2018
2019 - /* Append the SHA1 signature at the end */
2020 - git_SHA1_Final(write_buffer + left, context);
2021 - hashcpy(sha1, write_buffer + left);
2022 - left += 20;
2019 + /* Append the hash signature at the end */
2020 + the_hash_algo->final_fn(write_buffer + left, context);
2021 + hashcpy(hash, write_buffer + left);
2022 + left += the_hash_algo->rawsz;
2023 return (write_in_full(fd, write_buffer, left) < 0) ? -1 : 0;
2024 }
2025
@@ -2100,7 +2100,7 @@ static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
2100 }
2101 }
2102
2103 -static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,
2103 +static int ce_write_entry(git_hash_ctx *c, int fd, struct cache_entry *ce,
2104 struct strbuf *previous_name, struct ondisk_cache_entry *ondisk)
2105 {
2106 int size;
@@ -2167,7 +2167,7 @@ static int verify_index_from(const struct index_state *istate, const char *path)
2167 int fd;
2168 ssize_t n;
2169 struct stat st;
2170 - unsigned char sha1[20];
2170 + unsigned char hash[GIT_MAX_RAWSZ];
2171
2172 if (!istate->initialized)
2173 return 0;
@@ -2179,14 +2179,14 @@ static int verify_index_from(const struct index_state *istate, const char *path)
2179 if (fstat(fd, &st))
2180 goto out;
2181
2182 - if (st.st_size < sizeof(struct cache_header) + 20)
2182 + if (st.st_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
2183 goto out;
2184
2185 - n = pread_in_full(fd, sha1, 20, st.st_size - 20);
2186 - if (n != 20)
2185 + n = pread_in_full(fd, hash, the_hash_algo->rawsz, st.st_size - the_hash_algo->rawsz);
2186 + if (n != the_hash_algo->rawsz)
2187 goto out;
2188
2189 - if (hashcmp(istate->sha1, sha1))
2189 + if (hashcmp(istate->sha1, hash))
2190 goto out;
2191
2192 close(fd);
@@ -2235,7 +2235,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2235 int strip_extensions)
2236 {
2237 int newfd = tempfile->fd;
2238 - git_SHA_CTX c;
2238 + git_hash_ctx c;
2239 struct cache_header hdr;
2240 int i, err = 0, removed, extended, hdr_version;
2241 struct cache_entry **cache = istate->cache;
@@ -2273,7 +2273,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2273 hdr.hdr_version = htonl(hdr_version);
2274 hdr.hdr_entries = htonl(entries - removed);
2275
2276 - git_SHA1_Init(&c);
2276 + the_hash_algo->init_fn(&c);
2277 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
2278 return -1;
2279