]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Let format_hash_as_string optionally not include the size suffix
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 2 Nov 2010 17:39:40 +0000 (18:39 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 2 Nov 2010 17:39:40 +0000 (18:39 +0100)
ccache.h
test/test_util.c
util.c

index 570d74c05dd83ac0a69fc0662138fb996a645a49..99cdaa89f4458387023c83d2b003b7ef75a9b8cb 100644 (file)
--- 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);
index b8d24beebf56d081d0ace81e01c1ab4ee263f96e..a92d55c2c2b77908b1404b7bcc3ce3101a79e325 100644 (file)
@@ -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 b9046b6e8abcbfb54a8d89d34a32a74cc2aad3e4..27452a2391d9d5d86bd0a0fd80e62fa699e2d892 100644 (file)
--- 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;
 }