From 19479c5f60b2d0531dd806ccf7c1b28e137dccda Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Wed, 25 Jun 2008 21:17:08 +0200 Subject: [PATCH] Hexdumping functions can also use separators. --- lib/str-hex.c | 23 ++++++++++++++++------- lib/string.h | 4 ++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/str-hex.c b/lib/str-hex.c index 3dd3e438..8485f0c8 100644 --- a/lib/str-hex.c +++ b/lib/str-hex.c @@ -18,12 +18,14 @@ hex_make(uns x) } void -mem_to_hex(char *dest, const byte *src, uns bytes) +mem_to_hex(char *dest, const byte *src, uns bytes, uns sep) { while (bytes--) { *dest++ = hex_make(*src >> 4); *dest++ = hex_make(*src & 0x0f); + if (sep && bytes) + *dest++ = sep; src++; } *dest = 0; @@ -38,12 +40,18 @@ hex_parse(uns c) } const char * -hex_to_mem(byte *dest, const char *src, uns max_bytes) +hex_to_mem(byte *dest, const char *src, uns max_bytes, uns sep) { while (max_bytes-- && Cxdigit(src[0]) && Cxdigit(src[1])) { *dest++ = (hex_parse(src[0]) << 4) | hex_parse(src[1]); src += 2; + if (sep && *src && max_bytes) + { + if (*src != (char)sep) + return src; + src++; + } } return src; } @@ -56,14 +64,15 @@ int main(void) { byte x[4] = { 0xfe, 0xed, 0xf0, 0x0d }; byte y[4]; - char a[10]; + char a[16]; - mem_to_hex(a, x, 4); + mem_to_hex(a, x, 4, ':'); puts(a); - const char *z = hex_to_mem(y, a, 4); + const char *z = hex_to_mem(y, a, 4, ':'); if (*z) - return 1; - printf("%02x%02x%02x%02x\n", y[0], y[1], y[2], y[3]); + puts("BAD"); + else + printf("%02x%02x%02x%02x\n", y[0], y[1], y[2], y[3]); return 0; } diff --git a/lib/string.h b/lib/string.h index 6b0ed593..409d17fa 100644 --- a/lib/string.h +++ b/lib/string.h @@ -31,8 +31,8 @@ int str_match_pattern_nocase(const char *patt, const char *str); /* str-hex.c */ -void mem_to_hex(char *dest, const byte *src, uns bytes); -const char *hex_to_mem(byte *dest, const char *src, uns max_bytes); +void mem_to_hex(char *dest, const byte *src, uns bytes, uns sep); +const char *hex_to_mem(byte *dest, const char *src, uns max_bytes, uns sep); /* md5hex.c */ -- 2.39.2