From: Joel Rosdahl Date: Tue, 2 Nov 2010 17:39:40 +0000 (+0100) Subject: Let format_hash_as_string optionally not include the size suffix X-Git-Tag: v3.2~296 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=de36c5490a6aa55b1e3542b4844e397fc0cb02e1;p=thirdparty%2Fccache.git Let format_hash_as_string optionally not include the size suffix --- diff --git a/ccache.h b/ccache.h index 570d74c05..99cdaa89f 100644 --- a/ccache.h +++ b/ccache.h @@ -112,7 +112,7 @@ int create_dir(const char *dir); int create_parent_dirs(const char *path); const char *get_hostname(void); const char *tmp_string(void); -char *format_hash_as_string(const unsigned char *hash, unsigned size); +char *format_hash_as_string(const unsigned char *hash, int size); int create_hash_dir(char **dir, const char *hash, const char *cache_dir); int create_cachedirtag(const char *dir); char *format(const char *format, ...) ATTR_FORMAT(printf, 1, 2); diff --git a/test/test_util.c b/test/test_util.c index b8d24beeb..a92d55c2c 100644 --- a/test/test_util.c +++ b/test/test_util.c @@ -41,4 +41,19 @@ TEST(dirname) CHECK_STR_EQ_FREE2("dir1/dir2", dirname("dir1/dir2/")); } +TEST(format_hash_as_string) +{ + unsigned char hash[16] = { + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"}; + + CHECK_STR_EQ_FREE2("00000000000000000000000000000000", + format_hash_as_string(hash, -1)); + CHECK_STR_EQ_FREE2("00000000000000000000000000000000-0", + format_hash_as_string(hash, 0)); + hash[0] = 17; + hash[15] = 42; + CHECK_STR_EQ_FREE2("1100000000000000000000000000002a-12345", + format_hash_as_string(hash, 12345)); +} + TEST_SUITE_END diff --git a/util.c b/util.c index b9046b6e8..27452a239 100644 --- a/util.c +++ b/util.c @@ -474,9 +474,12 @@ tmp_string(void) return ret; } -/* Return the hash result as a hex string. Caller frees. */ +/* + * Return the hash result as a hex string. Size -1 means don't include size + * suffix. Caller frees. + */ char * -format_hash_as_string(const unsigned char *hash, unsigned size) +format_hash_as_string(const unsigned char *hash, int size) { char *ret; int i; @@ -485,7 +488,9 @@ format_hash_as_string(const unsigned char *hash, unsigned size) for (i = 0; i < 16; i++) { sprintf(&ret[i*2], "%02x", (unsigned) hash[i]); } - sprintf(&ret[i*2], "-%u", size); + if (size >= 0) { + sprintf(&ret[i*2], "-%u", size); + } return ret; }