Macros for hex string gen

This commit is contained in:
ruti 2024-11-14 20:44:38 +03:00
parent 4012b14bda
commit 64ffb2f64d
2 changed files with 14 additions and 6 deletions

View file

@ -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

12
error.h
View file

@ -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);