From 563f0e4115d0a7c22242d3f45ee2fdb365d82afc Mon Sep 17 00:00:00 2001 From: Chris Dunlop Date: Tue, 13 Dec 2011 11:39:33 +1100 Subject: [PATCH] Improve generation of hash file name Use the calculated checksum_len rather than assuming 16 bytes. Use assert() to check for buffer overflow Use sprintf() rather than hand-rolled binary-to-text. This is not in a critical performance path. Signed-off-by: Chris Dunlop --- hashlink.c | 21 +++++++++------------ 1 files changed, 9 insertions(+), 12 deletions(-) diff --git a/hashlink.c b/hashlink.c index 12cc39b..3f98beb 100644 --- a/hashlink.c +++ b/hashlink.c @@ -21,27 +21,24 @@ #include "rsync.h" extern char *link_by_hash_dir; +extern int checksum_len; #ifdef HAVE_LINK 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((uint)((checksum_len * 2) + 2) <= 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