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>
This commit is contained in:
parent
3206b6bdf6
commit
18e2588e11
52
sha1_file.c
52
sha1_file.c
@ -786,16 +786,16 @@ void *xmmap(void *start, size_t length,
|
|||||||
int check_sha1_signature(const unsigned char *sha1, void *map,
|
int check_sha1_signature(const unsigned char *sha1, void *map,
|
||||||
unsigned long size, const char *type)
|
unsigned long size, const char *type)
|
||||||
{
|
{
|
||||||
unsigned char real_sha1[20];
|
struct object_id real_oid;
|
||||||
enum object_type obj_type;
|
enum object_type obj_type;
|
||||||
struct git_istream *st;
|
struct git_istream *st;
|
||||||
git_SHA_CTX c;
|
git_hash_ctx c;
|
||||||
char hdr[32];
|
char hdr[32];
|
||||||
int hdrlen;
|
int hdrlen;
|
||||||
|
|
||||||
if (map) {
|
if (map) {
|
||||||
hash_sha1_file(map, size, type, real_sha1);
|
hash_sha1_file(map, size, type, real_oid.hash);
|
||||||
return hashcmp(sha1, real_sha1) ? -1 : 0;
|
return hashcmp(sha1, real_oid.hash) ? -1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
st = open_istream(sha1, &obj_type, &size, NULL);
|
st = open_istream(sha1, &obj_type, &size, NULL);
|
||||||
@ -806,8 +806,8 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
|
|||||||
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(obj_type), size) + 1;
|
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(obj_type), size) + 1;
|
||||||
|
|
||||||
/* Sha1.. */
|
/* Sha1.. */
|
||||||
git_SHA1_Init(&c);
|
the_hash_algo->init_fn(&c);
|
||||||
git_SHA1_Update(&c, hdr, hdrlen);
|
the_hash_algo->update_fn(&c, hdr, hdrlen);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char buf[1024 * 16];
|
char buf[1024 * 16];
|
||||||
ssize_t readlen = read_istream(st, buf, sizeof(buf));
|
ssize_t readlen = read_istream(st, buf, sizeof(buf));
|
||||||
@ -818,11 +818,11 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
|
|||||||
}
|
}
|
||||||
if (!readlen)
|
if (!readlen)
|
||||||
break;
|
break;
|
||||||
git_SHA1_Update(&c, buf, readlen);
|
the_hash_algo->update_fn(&c, buf, readlen);
|
||||||
}
|
}
|
||||||
git_SHA1_Final(real_sha1, &c);
|
the_hash_algo->final_fn(real_oid.hash, &c);
|
||||||
close_istream(st);
|
close_istream(st);
|
||||||
return hashcmp(sha1, real_sha1) ? -1 : 0;
|
return hashcmp(sha1, real_oid.hash) ? -1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_open_cloexec(const char *name, int flags)
|
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,
|
|||||||
const char *type, unsigned char *sha1,
|
const char *type, unsigned char *sha1,
|
||||||
char *hdr, int *hdrlen)
|
char *hdr, int *hdrlen)
|
||||||
{
|
{
|
||||||
git_SHA_CTX c;
|
git_hash_ctx c;
|
||||||
|
|
||||||
/* Generate the header */
|
/* Generate the header */
|
||||||
*hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
|
*hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
|
||||||
|
|
||||||
/* Sha1.. */
|
/* Sha1.. */
|
||||||
git_SHA1_Init(&c);
|
the_hash_algo->init_fn(&c);
|
||||||
git_SHA1_Update(&c, hdr, *hdrlen);
|
the_hash_algo->update_fn(&c, hdr, *hdrlen);
|
||||||
git_SHA1_Update(&c, buf, len);
|
the_hash_algo->update_fn(&c, buf, len);
|
||||||
git_SHA1_Final(sha1, &c);
|
the_hash_algo->final_fn(sha1, &c);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1552,8 +1552,8 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
|
|||||||
int fd, ret;
|
int fd, ret;
|
||||||
unsigned char compressed[4096];
|
unsigned char compressed[4096];
|
||||||
git_zstream stream;
|
git_zstream stream;
|
||||||
git_SHA_CTX c;
|
git_hash_ctx c;
|
||||||
unsigned char parano_sha1[20];
|
struct object_id parano_oid;
|
||||||
static struct strbuf tmp_file = STRBUF_INIT;
|
static struct strbuf tmp_file = STRBUF_INIT;
|
||||||
const char *filename = sha1_file_name(sha1);
|
const char *filename = sha1_file_name(sha1);
|
||||||
|
|
||||||
@ -1569,14 +1569,14 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
|
|||||||
git_deflate_init(&stream, zlib_compression_level);
|
git_deflate_init(&stream, zlib_compression_level);
|
||||||
stream.next_out = compressed;
|
stream.next_out = compressed;
|
||||||
stream.avail_out = sizeof(compressed);
|
stream.avail_out = sizeof(compressed);
|
||||||
git_SHA1_Init(&c);
|
the_hash_algo->init_fn(&c);
|
||||||
|
|
||||||
/* First header.. */
|
/* First header.. */
|
||||||
stream.next_in = (unsigned char *)hdr;
|
stream.next_in = (unsigned char *)hdr;
|
||||||
stream.avail_in = hdrlen;
|
stream.avail_in = hdrlen;
|
||||||
while (git_deflate(&stream, 0) == Z_OK)
|
while (git_deflate(&stream, 0) == Z_OK)
|
||||||
; /* nothing */
|
; /* nothing */
|
||||||
git_SHA1_Update(&c, hdr, hdrlen);
|
the_hash_algo->update_fn(&c, hdr, hdrlen);
|
||||||
|
|
||||||
/* Then the data itself.. */
|
/* Then the data itself.. */
|
||||||
stream.next_in = (void *)buf;
|
stream.next_in = (void *)buf;
|
||||||
@ -1584,7 +1584,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
|
|||||||
do {
|
do {
|
||||||
unsigned char *in0 = stream.next_in;
|
unsigned char *in0 = stream.next_in;
|
||||||
ret = git_deflate(&stream, Z_FINISH);
|
ret = git_deflate(&stream, Z_FINISH);
|
||||||
git_SHA1_Update(&c, in0, stream.next_in - in0);
|
the_hash_algo->update_fn(&c, in0, stream.next_in - in0);
|
||||||
if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
|
if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
|
||||||
die("unable to write sha1 file");
|
die("unable to write sha1 file");
|
||||||
stream.next_out = compressed;
|
stream.next_out = compressed;
|
||||||
@ -1596,8 +1596,8 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
|
|||||||
ret = git_deflate_end_gently(&stream);
|
ret = git_deflate_end_gently(&stream);
|
||||||
if (ret != Z_OK)
|
if (ret != Z_OK)
|
||||||
die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret);
|
die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret);
|
||||||
git_SHA1_Final(parano_sha1, &c);
|
the_hash_algo->final_fn(parano_oid.hash, &c);
|
||||||
if (hashcmp(sha1, parano_sha1) != 0)
|
if (hashcmp(sha1, parano_oid.hash) != 0)
|
||||||
die("confused by unstable object source data for %s", sha1_to_hex(sha1));
|
die("confused by unstable object source data for %s", sha1_to_hex(sha1));
|
||||||
|
|
||||||
close_sha1_file(fd);
|
close_sha1_file(fd);
|
||||||
@ -2091,14 +2091,14 @@ static int check_stream_sha1(git_zstream *stream,
|
|||||||
const char *path,
|
const char *path,
|
||||||
const unsigned char *expected_sha1)
|
const unsigned char *expected_sha1)
|
||||||
{
|
{
|
||||||
git_SHA_CTX c;
|
git_hash_ctx c;
|
||||||
unsigned char real_sha1[GIT_MAX_RAWSZ];
|
unsigned char real_sha1[GIT_MAX_RAWSZ];
|
||||||
unsigned char buf[4096];
|
unsigned char buf[4096];
|
||||||
unsigned long total_read;
|
unsigned long total_read;
|
||||||
int status = Z_OK;
|
int status = Z_OK;
|
||||||
|
|
||||||
git_SHA1_Init(&c);
|
the_hash_algo->init_fn(&c);
|
||||||
git_SHA1_Update(&c, hdr, stream->total_out);
|
the_hash_algo->update_fn(&c, hdr, stream->total_out);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We already read some bytes into hdr, but the ones up to the NUL
|
* 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,
|
|||||||
if (size - total_read < stream->avail_out)
|
if (size - total_read < stream->avail_out)
|
||||||
stream->avail_out = size - total_read;
|
stream->avail_out = size - total_read;
|
||||||
status = git_inflate(stream, Z_FINISH);
|
status = git_inflate(stream, Z_FINISH);
|
||||||
git_SHA1_Update(&c, buf, stream->next_out - buf);
|
the_hash_algo->update_fn(&c, buf, stream->next_out - buf);
|
||||||
total_read += stream->next_out - buf;
|
total_read += stream->next_out - buf;
|
||||||
}
|
}
|
||||||
git_inflate_end(stream);
|
git_inflate_end(stream);
|
||||||
@ -2132,7 +2132,7 @@ static int check_stream_sha1(git_zstream *stream,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
git_SHA1_Final(real_sha1, &c);
|
the_hash_algo->final_fn(real_sha1, &c);
|
||||||
if (hashcmp(expected_sha1, real_sha1)) {
|
if (hashcmp(expected_sha1, real_sha1)) {
|
||||||
error("sha1 mismatch for %s (expected %s)", path,
|
error("sha1 mismatch for %s (expected %s)", path,
|
||||||
sha1_to_hex(expected_sha1));
|
sha1_to_hex(expected_sha1));
|
||||||
|
Loading…
Reference in New Issue
Block a user