From 63ef9aa8f48e234109378360967d20536b33bd04 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Wed, 6 Aug 2025 14:16:06 +0800 Subject: [PATCH] add functions short2hex, int2hex, long2hex etc. --- HISTORY | 3 +- src/shared_func.c | 12 ++-- src/shared_func.h | 108 ++++++++++++++++++++++++++++++++ src/tests/Makefile | 2 +- src/tests/test_server_id_func.c | 2 +- 5 files changed, 118 insertions(+), 9 deletions(-) diff --git a/HISTORY b/HISTORY index dd0893a..e597bc5 100644 --- a/HISTORY +++ b/HISTORY @@ -1,8 +1,9 @@ -Version 1.78 2025-08-03 +Version 1.78 2025-08-06 * getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6 * add files: spinlock.[hc] * shared_func.[hc]: change int2buff, buff2int etc. functions to static inline + * shared_func.[hc]: add functions short2hex, int2hex, long2hex etc. Version 1.77 2025-03-18 * impl. shorten_path for /./ and /../ diff --git a/src/shared_func.c b/src/shared_func.c index 7c075c3..6fb11e0 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -46,6 +46,7 @@ #endif bool g_set_cloexec = false; +const char *g_lower_hex_chars = "0123456789abcdef"; char *formatDatetime(const time_t nTime, \ const char *szDateFormat, \ @@ -528,7 +529,6 @@ void daemon_init(bool bCloseFiles) char *bin2hex(const char *s, const int len, char *szHexBuff) { - const char *hex_chars = "0123456789abcdef"; unsigned char *p; unsigned char *pEnd; char *dest; @@ -537,8 +537,8 @@ char *bin2hex(const char *s, const int len, char *szHexBuff) pEnd = (unsigned char *)s + len; for (p=(unsigned char *)s; p> 4) & 0x0F]; - *dest++ = hex_chars[*p & 0x0F]; + *dest++ = g_lower_hex_chars[*p >> 4]; + *dest++ = g_lower_hex_chars[*p & 0x0F]; } *dest = '\0'; @@ -2568,7 +2568,7 @@ int get_time_item_from_str(const char *pValue, const char *item_name, char *urlencode(const char *src, const int src_len, char *dest, int *dest_len) { - static unsigned char hex_chars[] = "0123456789ABCDEF"; + const unsigned char upper_hex_chars[] = "0123456789ABCDEF"; const unsigned char *pSrc; const unsigned char *pEnd; char *pDest; @@ -2591,8 +2591,8 @@ char *urlencode(const char *src, const int src_len, char *dest, int *dest_len) else { *pDest++ = '%'; - *pDest++ = hex_chars[(*pSrc) >> 4]; - *pDest++ = hex_chars[(*pSrc) & 0x0F]; + *pDest++ = upper_hex_chars[(*pSrc) >> 4]; + *pDest++ = upper_hex_chars[(*pSrc) & 0x0F]; } } diff --git a/src/shared_func.h b/src/shared_func.h index 5e0d83c..c713866 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -46,6 +46,7 @@ extern "C" { #endif extern bool g_set_cloexec; + extern const char *g_lower_hex_chars; static inline void fc_enable_fd_cloexec(const bool cloexec) { @@ -339,6 +340,97 @@ static inline double buff2double(const char *buff) return *p; } +static inline void padding_hex(char *buff, const int len, const int padding_len) +{ + char *p; + char *end; + char *stop; + int new_len; + int fill_len; + + if (padding_len == len) { + return; + } else if (padding_len > len) { + fill_len = padding_len - len; + memmove(buff + fill_len, buff, len + 1); + memset(buff, '0', fill_len); + return; + } else if (*buff != '0') { + return; + } + + end = buff + len; + if (padding_len <= 0) { + stop = end - 1; + } else { + stop = end - padding_len; + } + + p = buff + 1; + while (p < stop && *p == '0') { + ++p; + } + + new_len = end - p; + memmove(buff, p, new_len + 1); +} + +static inline void short2hex(const short n, char *buff, const int padding_len) +{ + unsigned char *p; + + p = (unsigned char *)buff; + *p++ = g_lower_hex_chars[(n >> 12) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 8) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 4) & 0x0F]; + *p++ = g_lower_hex_chars[n & 0x0F]; + *p = '\0'; + padding_hex(buff, 4, padding_len); +} + +static inline void int2hex(const int n, char *buff, const int padding_len) +{ + unsigned char *p; + + p = (unsigned char *)buff; + *p++ = g_lower_hex_chars[(n >> 28) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 24) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 20) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 16) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 12) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 8) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 4) & 0x0F]; + *p++ = g_lower_hex_chars[n & 0x0F]; + *p = '\0'; + padding_hex(buff, 8, padding_len); +} + +static inline void long2hex(const int64_t n, + char *buff, const int padding_len) +{ + unsigned char *p; + + p = (unsigned char *)buff; + *p++ = g_lower_hex_chars[(n >> 60) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 56) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 52) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 48) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 44) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 40) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 36) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 32) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 28) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 24) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 20) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 16) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 12) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 8) & 0x0F]; + *p++ = g_lower_hex_chars[(n >> 4) & 0x0F]; + *p++ = g_lower_hex_chars[n & 0x0F]; + *p = '\0'; + padding_hex(buff, 16, padding_len); +} + /** trim leading spaces ( \t\r\n) * parameters: * pStr: the string to trim @@ -1273,6 +1365,22 @@ bool fc_path_contains(const string_t *path, const string_t *needle, */ int fc_itoa(int64_t n, char *buff); +static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size) +{ + int len; + + len = strlen(src); + if (len < size) { + memcpy(dest, src, len + 1); + } else { + len = size - 1; + memcpy(dest, src, len); + *(dest + len) = '\0'; + } + + return len; +} + /** sleep in microseconds * parameters: * microseconds: microseconds to sleep diff --git a/src/tests/Makefile b/src/tests/Makefile index 7aa22a8..3ea0d10 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -11,7 +11,7 @@ ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blo test_server_id_func test_pipe test_atomic test_file_write_hole test_file_lock \ test_pthread_wait test_thread_pool test_data_visible test_mutex_lock_perf \ test_queue_perf test_normalize_path test_sorted_array test_sorted_queue \ - test_thread_local test_memcpy mblock_benchmark cpool_benchmark + test_thread_local test_memcpy test_fast_buffer mblock_benchmark cpool_benchmark all: $(ALL_PRGS) diff --git a/src/tests/test_server_id_func.c b/src/tests/test_server_id_func.c index 064148b..8f03608 100644 --- a/src/tests/test_server_id_func.c +++ b/src/tests/test_server_id_func.c @@ -112,7 +112,7 @@ int main(int argc, char *argv[]) setup_mblock_stat_task(); - if ((result=fast_buffer_init_ex(&buffer, 1024)) != 0) { + if ((result=fast_buffer_init1(&buffer, 1024)) != 0) { return result; } fc_server_to_config_string(&ctx, &buffer);