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>

brian m. carlson committed Nov 14, 2018 at 04:09 UTC 47edb649973cf5cd5f7066b5c936b330f78d687b
2 files changed +32 -13
cache.h
+9 -6
@@ -1361,9 +1361,9 @@ extern int get_oid_hex(const char *hex, struct object_id *sha1);
1361 extern int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
1362
1363 /*
1364 - * Convert a binary sha1 to its hex equivalent. The `_r` variant is reentrant,
1364 + * Convert a binary hash to its hex equivalent. The `_r` variant is reentrant,
1365 * and writes the NUL-terminated output to the buffer `out`, which must be at
1366 - * least `GIT_SHA1_HEXSZ + 1` bytes, and returns a pointer to out for
1366 + * least `GIT_MAX_HEXSZ + 1` bytes, and returns a pointer to out for
1367 * convenience.
1368 *
1369 * 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);
1371 *
1372 * printf("%s -> %s", sha1_to_hex(one), sha1_to_hex(two));
1373 */
1374 -extern char *sha1_to_hex_r(char *out, const unsigned char *sha1);
1375 -extern char *oid_to_hex_r(char *out, const struct object_id *oid);
1376 -extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
1377 -extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as sha1_to_hex */
1374 +char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, const struct git_hash_algo *);
1375 +char *sha1_to_hex_r(char *out, const unsigned char *sha1);
1376 +char *oid_to_hex_r(char *out, const struct object_id *oid);
1377 +char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *); /* static buffer result! */
1378 +char *sha1_to_hex(const unsigned char *sha1); /* same static buffer */
1379 +char *hash_to_hex(const unsigned char *hash); /* same static buffer */
1380 +char *oid_to_hex(const struct object_id *oid); /* same static buffer */
1381
1382 /*
1383 * Parse a 40-character hexadecimal object ID starting from hex, updating the
hex.c
+23 -7
@@ -73,14 +73,15 @@ int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
73 return ret;
74 }
75
76 -char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
76 +char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash,
77 + const struct git_hash_algo *algop)
78 {
79 static const char hex[] = "0123456789abcdef";
80 char *buf = buffer;
81 int i;
82
82 - for (i = 0; i < the_hash_algo->rawsz; i++) {
83 - unsigned int val = *sha1++;
83 + for (i = 0; i < algop->rawsz; i++) {
84 + unsigned int val = *hash++;
85 *buf++ = hex[val >> 4];
86 *buf++ = hex[val & 0xf];
87 }
@@ -89,20 +90,35 @@ char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
90 return buffer;
91 }
92
93 +char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
94 +{
95 + return hash_to_hex_algop_r(buffer, sha1, &hash_algos[GIT_HASH_SHA1]);
96 +}
97 +
98 char *oid_to_hex_r(char *buffer, const struct object_id *oid)
99 {
94 - return sha1_to_hex_r(buffer, oid->hash);
100 + return hash_to_hex_algop_r(buffer, oid->hash, the_hash_algo);
101 }
102
97 -char *sha1_to_hex(const unsigned char *sha1)
103 +char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *algop)
104 {
105 static int bufno;
106 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
107 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
102 - return sha1_to_hex_r(hexbuffer[bufno], sha1);
108 + return hash_to_hex_algop_r(hexbuffer[bufno], hash, algop);
109 +}
110 +
111 +char *sha1_to_hex(const unsigned char *sha1)
112 +{
113 + return hash_to_hex_algop(sha1, &hash_algos[GIT_HASH_SHA1]);
114 +}
115 +
116 +char *hash_to_hex(const unsigned char *hash)
117 +{
118 + return hash_to_hex_algop(hash, the_hash_algo);
119 }
120
121 char *oid_to_hex(const struct object_id *oid)
122 {
107 - return sha1_to_hex(oid->hash);
123 + return hash_to_hex_algop(oid->hash, the_hash_algo);
124 }