hex: introduce functions to print arbitrary hashes

Currently, we have functions that turn an arbitrary SHA-1 value or an
object ID into hex format, either using a static buffer or with a
user-provided buffer.  Add variants of these functions that can handle
an arbitrary hash algorithm, specified by constant.  Update the
documentation as well.

While we're at it, remove the "extern" declaration from this family of
functions, since it's not needed and our style now recommends against
it.

We use the variant taking the algorithm structure pointer as the
internal variant, since taking an algorithm pointer is the easiest way
to handle all of the variants in use.

Note that we maintain these functions because there are hashes which
must change based on the hash algorithm in use but are not object IDs
(such as pack checksums).

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
brian m. carlson 2018-11-14 04:09:29 +00:00 committed by Junio C Hamano
parent 2f90b9d9b4
commit 47edb64997
2 changed files with 33 additions and 14 deletions

15
cache.h
View File

@ -1361,9 +1361,9 @@ extern int get_oid_hex(const char *hex, struct object_id *sha1);
extern int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
/*
* Convert a binary sha1 to its hex equivalent. The `_r` variant is reentrant,
* Convert a binary hash to its hex equivalent. The `_r` variant is reentrant,
* and writes the NUL-terminated output to the buffer `out`, which must be at
* least `GIT_SHA1_HEXSZ + 1` bytes, and returns a pointer to out for
* least `GIT_MAX_HEXSZ + 1` bytes, and returns a pointer to out for
* convenience.
*
* The non-`_r` variant returns a static buffer, but uses a ring of 4
@ -1371,10 +1371,13 @@ extern int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
*
* printf("%s -> %s", sha1_to_hex(one), sha1_to_hex(two));
*/
extern char *sha1_to_hex_r(char *out, const unsigned char *sha1);
extern char *oid_to_hex_r(char *out, const struct object_id *oid);
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as sha1_to_hex */
char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, const struct git_hash_algo *);
char *sha1_to_hex_r(char *out, const unsigned char *sha1);
char *oid_to_hex_r(char *out, const struct object_id *oid);
char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *); /* static buffer result! */
char *sha1_to_hex(const unsigned char *sha1); /* same static buffer */
char *hash_to_hex(const unsigned char *hash); /* same static buffer */
char *oid_to_hex(const struct object_id *oid); /* same static buffer */
/*
* Parse a 40-character hexadecimal object ID starting from hex, updating the

32
hex.c
View File

@ -73,14 +73,15 @@ int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
return ret;
}
char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash,
const struct git_hash_algo *algop)
{
static const char hex[] = "0123456789abcdef";
char *buf = buffer;
int i;
for (i = 0; i < the_hash_algo->rawsz; i++) {
unsigned int val = *sha1++;
for (i = 0; i < algop->rawsz; i++) {
unsigned int val = *hash++;
*buf++ = hex[val >> 4];
*buf++ = hex[val & 0xf];
}
@ -89,20 +90,35 @@ char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
return buffer;
}
char *oid_to_hex_r(char *buffer, const struct object_id *oid)
char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
{
return sha1_to_hex_r(buffer, oid->hash);
return hash_to_hex_algop_r(buffer, sha1, &hash_algos[GIT_HASH_SHA1]);
}
char *sha1_to_hex(const unsigned char *sha1)
char *oid_to_hex_r(char *buffer, const struct object_id *oid)
{
return hash_to_hex_algop_r(buffer, oid->hash, the_hash_algo);
}
char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *algop)
{
static int bufno;
static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
return sha1_to_hex_r(hexbuffer[bufno], sha1);
return hash_to_hex_algop_r(hexbuffer[bufno], hash, algop);
}
char *sha1_to_hex(const unsigned char *sha1)
{
return hash_to_hex_algop(sha1, &hash_algos[GIT_HASH_SHA1]);
}
char *hash_to_hex(const unsigned char *hash)
{
return hash_to_hex_algop(hash, the_hash_algo);
}
char *oid_to_hex(const struct object_id *oid)
{
return sha1_to_hex(oid->hash);
return hash_to_hex_algop(oid->hash, the_hash_algo);
}