From 49d73b733e45e346d3750be5a4a513af8afbe5ba Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Thu, 6 Jul 2023 22:20:33 +0800 Subject: [PATCH] Build sucessfully by clang with higher version. --- lib_acl/src/stdlib/acl_debug.c | 2 +- lib_acl_cpp/include/acl_cpp/stdlib/md5.hpp | 19 +++++---- lib_acl_cpp/src/stdlib/md5.cpp | 48 +++++++++------------- lib_protocol/src/icmp/icmp_timer.c | 2 +- 4 files changed, 31 insertions(+), 40 deletions(-) diff --git a/lib_acl/src/stdlib/acl_debug.c b/lib_acl/src/stdlib/acl_debug.c index 35d5572a7..f01240a9d 100644 --- a/lib_acl/src/stdlib/acl_debug.c +++ b/lib_acl/src/stdlib/acl_debug.c @@ -22,7 +22,7 @@ static int __max_sections = DEF_DEBUG_SECTIONS; static int *__debug_levels = NULL; -void acl_debug_end() +void acl_debug_end(void) { if (__debug_levels != NULL) { acl_myfree(__debug_levels); diff --git a/lib_acl_cpp/include/acl_cpp/stdlib/md5.hpp b/lib_acl_cpp/include/acl_cpp/stdlib/md5.hpp index be87ed11a..e182a5495 100644 --- a/lib_acl_cpp/include/acl_cpp/stdlib/md5.hpp +++ b/lib_acl_cpp/include/acl_cpp/stdlib/md5.hpp @@ -6,8 +6,7 @@ namespace acl { class istream; -class ACL_CPP_API md5 : public noncopyable -{ +class ACL_CPP_API md5 : public noncopyable { public: md5(void); ~md5(void); @@ -112,15 +111,17 @@ public: #endif /** - * 将 16 字节长度的 MD5 二进制结果转换为 32 字节长度的字符串 - * @param in {const void*} 128 位(即 16 字节)的 md5 值,即 in 的数据长度 - * 至少应该 >= 16,否则会引起内存起越界 + * 通过将一个字节变成两个字节, 将二进制数据转换为字符串数据. + * @param in {const void*} 输入的二进制数据地址 + * @param len {size_t} in 的长度 * @param out {char*} 存储字符串形式的结果 - * @param size {size_t} out 内存大小,至少为 33 字节,否则内部产生断言 - * @return {const char*} 返回存储结果的地址(即 out 地址), - * 且返回值为以 \0 结尾的 32 字节长度(不含 \0)字符串 + * @param size {size_t} out 内存大小, 至少为 len * 2 + 1, + * 最后一个字节存 \0 + * @return {const char*} 返回存储结果的地址(即 out 地址), + * 且返回值为以 \0 结尾的字符串. */ - static const char* hex_encode(const void *in, char* out, size_t size); + static const char* hex_encode(const void *in, size_t len, + char* out, size_t size); private: unsigned int buf_[4]; diff --git a/lib_acl_cpp/src/stdlib/md5.cpp b/lib_acl_cpp/src/stdlib/md5.cpp index 21171c2cb..cfec0d5c5 100644 --- a/lib_acl_cpp/src/stdlib/md5.cpp +++ b/lib_acl_cpp/src/stdlib/md5.cpp @@ -247,7 +247,7 @@ const char* md5::get_digest(void) const const char* md5::get_string(void) const { - const_cast(this)->hex_encode(digest_, + const_cast(this)->hex_encode(digest_, 16, (char*) digest_s_, sizeof(digest_s_)); return (const char*) digest_s_; } @@ -280,7 +280,7 @@ const char* md5::md5_string(const void *dat, size_t dlen, md5.finish(); const unsigned char* d = (const unsigned char*) md5.get_digest(); - hex_encode(d, out, size); + hex_encode(d, 16, out, size); return out; } @@ -327,43 +327,33 @@ acl_int64 md5::md5_file(istream& in, const void *key, size_t klen, md5.finish(); const char* ptr = md5.get_digest(); - hex_encode(ptr, out, size); + hex_encode(ptr, 16, out, size); return n; } -const char* md5::hex_encode(const void *in, char* out, size_t size) -{ - size_t i; - char buf[34]; // xxx: 必须是 34 个字节 - char *ptr; - unsigned char digest[16]; +static const unsigned char hex_chars[] = "0123456789abcdef"; - if (size < 33) { +#define UCHAR_PTR(x) ((const unsigned char *)(x)) + +const char* md5::hex_encode(const void* in, size_t len, char* out, size_t size) +{ + // size 长度至少应该为: len * 2 + 1 + if (size < 2 * len + 1) { abort(); } - memcpy(digest, in, 16); + const unsigned char *cp; + int ch; + size_t count, i = 0; - for (i = 0; i < 16; i++) { -#if _MSC_VER >= 1500 - sprintf_s(&(buf[2 * i]), 3, "%02x", (unsigned char) digest[i]); - sprintf_s(&(buf[2 * i + 1]), 3, "%02x", - (unsigned char) (digest[i] << 4)); -#else - sprintf(&(buf[2 * i]), "%02x", (unsigned char) digest[i]); - sprintf(&(buf[2 * i + 1]), "%02x", - (unsigned char) (digest[i] << 4)); -#endif + for (cp = UCHAR_PTR(in), count = len; count > 0; count--, cp++) { + ch = *cp; + out[i++] = hex_chars[(ch >> 4) & 0xf]; + out[i++] = hex_chars[ch & 0xf]; } - ptr = out; - - for (i = 0; i < 32; i++) { - *ptr++ = buf[i]; - } - - *ptr = '\0'; - return (out); + out[i] = 0; + return out; } } // namespace acl diff --git a/lib_protocol/src/icmp/icmp_timer.c b/lib_protocol/src/icmp/icmp_timer.c index b9e15cd8e..93bff07cd 100644 --- a/lib_protocol/src/icmp/icmp_timer.c +++ b/lib_protocol/src/icmp/icmp_timer.c @@ -113,7 +113,7 @@ static ICMP_PKT* timer_popup(ICMP_TIMER* timer) return (pkt); } -ICMP_TIMER *icmp_timer_new() +ICMP_TIMER *icmp_timer_new(void) { ICMP_TIMER *timer;