From aea528701521e9abdec8746bee35640d09491601 Mon Sep 17 00:00:00 2001 From: Chris Dunlop Date: Tue, 13 Dec 2011 11:06:56 +1100 Subject: [PATCH] Improve generation of hash file name Use the calculated checksum_len rather than assuming 16 bytes. Use sprintf() rather than hand-rolled binary-to-text. This is not in a critical performance path. Signed-off-by: Chris Dunlop --- hashlink.c | 20 ++++++++------------ 1 files changed, 8 insertions(+), 12 deletions(-) diff --git a/hashlink.c b/hashlink.c index 12cc39b..263f6e3 100644 --- a/hashlink.c +++ b/hashlink.c @@ -26,22 +26,18 @@ extern char *link_by_hash_dir; char *make_hash_name(struct file_struct *file) { - char hash[(MAX_DIGEST_LEN * 2) + 2], *dst; - uchar c, *src = (uchar*)F_SUM(file); + char hash[(MAX_DIGEST_LEN * 2) + 2], *dst = hash; + uchar *src = (uchar*)F_SUM(file); int i; - for (dst = hash, i = 0; i < 4; i++, src++) { - c = *src >> 4; - *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0'); - c = *src & 0x0f; - *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0'); + assert(checksum_len < sizeof(hash)); + + for (i = 0; i < 4; i++) { + dst += sprintf(dst, "%02x", (int)CVAL(src, i)); } *dst++ = '/'; - for (i = 0; i < 12; i++, src++) { - c = *src >> 4; - *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0'); - c = *src & 0x0f; - *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0'); + for ( ; i < checksum_len; i++) { + dst += sprintf(dst, "%02x", (int)CVAL(src, i)); } *dst = 0; -- 1.7.0.4