diff options
| author | ruki <[email protected]> | 2025-09-30 00:54:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-09-30 00:54:25 +0800 |
| commit | e225e4e8abf4a6fbf27343737a642c54f7c93643 (patch) | |
| tree | 710c99d40c9f09f8896e6c5d601f848d1ad9f185 | |
| parent | d673b3732b66ddae22a5c2b3c613af5447d23dca (diff) | |
improve hash cstr
| -rw-r--r-- | core/src/xmake/hash/md5.c | 10 | ||||
| -rw-r--r-- | core/src/xmake/hash/prefix.h | 3 | ||||
| -rw-r--r-- | core/src/xmake/hash/sha.c | 11 | ||||
| -rw-r--r-- | core/src/xmake/hash/xxhash.c | 8 |
4 files changed, 15 insertions, 17 deletions
diff --git a/core/src/xmake/hash/md5.c b/core/src/xmake/hash/md5.c index 0f6a9f598..39fa82b15 100644 --- a/core/src/xmake/hash/md5.c +++ b/core/src/xmake/hash/md5.c @@ -57,10 +57,10 @@ tb_int_t xm_hash_md5(lua_State* lua) // make md5 string tb_char_t s[256]; - xm_hash_make_cstr(s, buffer, 16); + tb_size_t n = xm_hash_make_cstr(s, buffer, 16); // save result - lua_pushstring(lua, s); + lua_pushlstring(lua, s, n); return 1; } @@ -109,12 +109,10 @@ tb_int_t xm_hash_md5(lua_State* lua) // make md5 string tb_char_t s[256]; - xm_hash_make_cstr(s, buffer, 16); + tb_size_t n = xm_hash_make_cstr(s, buffer, 16); // save result - lua_pushstring(lua, s); - - // ok + lua_pushlstring(lua, s, n); ok = tb_true; } diff --git a/core/src/xmake/hash/prefix.h b/core/src/xmake/hash/prefix.h index f5374a2d1..cf4cb082d 100644 --- a/core/src/xmake/hash/prefix.h +++ b/core/src/xmake/hash/prefix.h @@ -30,7 +30,7 @@ * 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_inline__ tb_size_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; @@ -44,6 +44,7 @@ static __tb_inline__ tb_void_t xm_hash_make_cstr(tb_char_t hash[256], tb_byte_t s += 2; } *s = '\0'; + return s - hash; } #endif diff --git a/core/src/xmake/hash/sha.c b/core/src/xmake/hash/sha.c index 4ffc2651e..ba5408279 100644 --- a/core/src/xmake/hash/sha.c +++ b/core/src/xmake/hash/sha.c @@ -62,13 +62,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]); + tb_size_t len = xm_hash_make_cstr(s, buffer, n); // save result - lua_pushstring(lua, s); + lua_pushlstring(lua, s, len); return 1; } @@ -118,10 +117,10 @@ tb_int_t xm_hash_sha(lua_State* lua) // make sha string tb_char_t s[256]; tb_size_t n = sha.digest_len << 2; - xm_hash_make_cstr(s, buffer, n); + tb_size_t len = xm_hash_make_cstr(s, buffer, n); // save result - lua_pushstring(lua, s); + lua_pushlstring(lua, s, len); ok = tb_true; } diff --git a/core/src/xmake/hash/xxhash.c b/core/src/xmake/hash/xxhash.c index 8f2cc630c..0c9dbdad7 100644 --- a/core/src/xmake/hash/xxhash.c +++ b/core/src/xmake/hash/xxhash.c @@ -88,10 +88,10 @@ tb_int_t xm_hash_xxhash(lua_State* lua) // make xxhash string tb_char_t s[256]; tb_size_t n = mode >> 3; - xm_hash_make_cstr(s, buffer, n); + tb_size_t len = xm_hash_make_cstr(s, buffer, n); // save result - lua_pushstring(lua, s); + lua_pushlstring(lua, s, len); return 1; } @@ -157,10 +157,10 @@ tb_int_t xm_hash_xxhash(lua_State* lua) // make xxhash string tb_char_t s[256]; tb_size_t n = mode >> 3; - xm_hash_make_cstr(s, buffer, n); + tb_size_t len = xm_hash_make_cstr(s, buffer, n); // save result - lua_pushstring(lua, s); + lua_pushlstring(lua, s, len); ok = tb_true; } |
