From 64ffb2f64d92d77f2ab03a95ff83b594b7fb4ea6 Mon Sep 17 00:00:00 2001 From: ruti <> Date: Thu, 14 Nov 2024 20:44:38 +0300 Subject: [PATCH] Macros for hex string gen --- desync.c | 8 ++------ error.h | 12 ++++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/desync.c b/desync.c index 4fe70f7..e618bcf 100644 --- a/desync.c +++ b/desync.c @@ -447,12 +447,8 @@ ssize_t desync(int sfd, char *buffer, size_t bfsize, host_pos = host - buffer; } else { - size_t s = n > 16 ? 16 : n - (n % 4); - char hex[s * 2 + 1], *b = buffer; - for (size_t i = 0; i < s; i += 4) - snprintf(hex + i * 2, sizeof(hex) - i * 2, - "%02x%02x%02x%02x", b[i],b[i+1],b[i+2],b[i+3]); - LOG(LOG_S, "bytes: %s (%zd)\n", s ? hex : "", n); + INIT_HEX_STR(buffer, (n > 16 ? 16 : n)); + LOG(LOG_S, "bytes: %s (%zd)\n", HEX_STR, n); } } // modify packet diff --git a/error.h b/error.h index 5eea82b..d65d9ee 100644 --- a/error.h +++ b/error.h @@ -81,3 +81,15 @@ static inline const int unie(int e) if (!p) uniperror("inet_ntop"); #endif + +#define INIT_HEX_STR(b, s) \ + char HEX_STR[s * 2 + 1]; \ + HEX_STR[sizeof(HEX_STR) - 1] = 0; \ + do { \ + size_t i; \ + for (i = 0; i + 4 <= s; i += 4) \ + snprintf(HEX_STR + i * 2, sizeof(HEX_STR) - i * 2, \ + "%02x%02x%02x%02x", b[i],b[i+1],b[i+2],b[i+3]); \ + for (; i < s; i++) \ + snprintf(HEX_STR + i * 2, sizeof(HEX_STR) - i * 2, "%02x", b[i]); \ + } while (0);