summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-09-28 13:56:49 +0800
committerruki <[email protected]>2025-09-28 13:56:49 +0800
commit21b740673ab4b53d7baea2c890ddad31e32524a5 (patch)
tree77a95fe4ca941279cd30456d460c8a03d878679d
parent17a0a62258b65c266599e6fb53f77bbdf61e86ee (diff)
improve hash
-rw-r--r--core/src/xmake/hash/md5.c5
-rw-r--r--core/src/xmake/hash/prefix.h19
-rw-r--r--core/src/xmake/hash/sha.c7
-rw-r--r--core/src/xmake/hash/xxhash.c10
4 files changed, 27 insertions, 14 deletions
diff --git a/core/src/xmake/hash/md5.c b/core/src/xmake/hash/md5.c
index f9b125364..1ca594941 100644
--- a/core/src/xmake/hash/md5.c
+++ b/core/src/xmake/hash/md5.c
@@ -56,9 +56,8 @@ tb_int_t xm_hash_md5(lua_State* lua)
tb_md5_make(data, size, buffer, sizeof(buffer));
// make md5 string
- tb_size_t i = 0;
- tb_char_t s[256] = {0};
- for (i = 0; i < 16; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
+ tb_char_t s[256];
+ xm_hash_make_cstr(s, buffer, 16);
// save result
lua_pushstring(lua, s);
diff --git a/core/src/xmake/hash/prefix.h b/core/src/xmake/hash/prefix.h
index 6141c99aa..f5374a2d1 100644
--- a/core/src/xmake/hash/prefix.h
+++ b/core/src/xmake/hash/prefix.h
@@ -26,6 +26,25 @@
*/
#include "../prefix.h"
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * helper implementation
+ */
+
+static __tb_inline__ tb_void_t xm_hash_make_cstr(tb_char_t hash[256], tb_byte_t const* data, tb_size_t size)
+{
+ static tb_char_t const* digits_table = "0123456789abcdef";
+ tb_size_t i = 0;
+ tb_byte_t value;
+ tb_char_t* s = hash;
+ for (i = 0; i < size; ++i)
+ {
+ value = data[i];
+ s[0] = digits_table[(value >> 4) & 15];
+ s[1] = digits_table[value & 15];
+ s += 2;
+ }
+ *s = '\0';
+}
#endif
diff --git a/core/src/xmake/hash/sha.c b/core/src/xmake/hash/sha.c
index db51a93fd..4ffc2651e 100644
--- a/core/src/xmake/hash/sha.c
+++ b/core/src/xmake/hash/sha.c
@@ -116,15 +116,12 @@ tb_int_t xm_hash_sha(lua_State* lua)
tb_sha_exit(&sha, buffer, sizeof(buffer));
// make sha string
- tb_size_t i = 0;
+ tb_char_t s[256];
tb_size_t n = sha.digest_len << 2;
- tb_char_t s[256] = {0};
- for (i = 0; i < n; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
+ xm_hash_make_cstr(s, buffer, n);
// save result
lua_pushstring(lua, s);
-
- // ok
ok = tb_true;
}
diff --git a/core/src/xmake/hash/xxhash.c b/core/src/xmake/hash/xxhash.c
index 1804e9b74..8f2cc630c 100644
--- a/core/src/xmake/hash/xxhash.c
+++ b/core/src/xmake/hash/xxhash.c
@@ -86,10 +86,9 @@ tb_int_t xm_hash_xxhash(lua_State* lua)
}
// make xxhash string
- tb_size_t i = 0;
+ tb_char_t s[256];
tb_size_t n = mode >> 3;
- tb_char_t s[256] = {0};
- for (i = 0; i < n; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
+ xm_hash_make_cstr(s, buffer, n);
// save result
lua_pushstring(lua, s);
@@ -156,10 +155,9 @@ tb_int_t xm_hash_xxhash(lua_State* lua)
}
// make xxhash string
- tb_size_t i = 0;
+ tb_char_t s[256];
tb_size_t n = mode >> 3;
- tb_char_t s[256] = {0};
- for (i = 0; i < n; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
+ xm_hash_make_cstr(s, buffer, n);
// save result
lua_pushstring(lua, s);