diff options
author | Dmitry Rakhchev <rda@pigeonpoint.com> | 2016-07-21 16:52:55 +0300 |
---|---|---|
committer | Zdenek Styblik <stybla@turnovfree.net> | 2016-07-31 08:52:40 +0200 |
commit | 9a6ba646511b6290214158f96ce93340db90f7a1 (patch) | |
tree | 4476f827ffd46289646f9fd01ae010096c079fe4 /lib | |
parent | f8a711b9e8a06bd73c8565634be69bc37066683d (diff) | |
download | ipmitool-9a6ba646511b6290214158f96ce93340db90f7a1.tar.gz |
Extend buf2str to allow separator
Diffstat (limited to 'lib')
-rw-r--r-- | lib/helper.c | 67 |
1 files changed, 55 insertions, 12 deletions
diff --git a/lib/helper.c b/lib/helper.c index 022a9c9..04a5e0d 100644 --- a/lib/helper.c +++ b/lib/helper.c @@ -79,24 +79,67 @@ uint16_t buf2short(uint8_t * buf) return (uint16_t)(buf[1] << 8 | buf[0]); } -const char * buf2str(uint8_t * buf, int len) +/* buf2str_extended - convert sequence of bytes to hexadecimal string with + * optional separator + * + * @param buf - data to convert + * @param len - size of data + * @param sep - optional separator (can be NULL) + * + * @returns buf representation in hex, possibly truncated to fit + * allocated static memory + */ +const char * +buf2str_extended(const uint8_t *buf, int len, const char *sep) { - static char str[2049]; + static char str[BUF2STR_MAXIMUM_OUTPUT_SIZE]; + char *cur; int i; - - if (len <= 0 || len > 1024) - return NULL; - - memset(str, 0, 2049); - - for (i=0; i<len; i++) - sprintf(str+i+i, "%2.2x", buf[i]); - - str[len*2] = '\0'; + int sz; + int left; + int sep_len; + + if (buf == NULL) { + snprintf(str, sizeof(str), "<NULL>"); + return (const char *)str; + } + cur = str; + left = sizeof(str); + if (sep) { + sep_len = strlen(sep); + } else { + sep_len = 0; + } + for (i = 0; i < len; i++) { + /* may return more than 2, depending on locale */ + sz = snprintf(cur, left, "%2.2x", buf[i]); + if (sz >= left) { + /* buffer overflow, truncate */ + break; + } + cur += sz; + left -= sz; + /* do not write separator after last byte */ + if (sep && i != (len - 1)) { + if (sep_len >= left) { + break; + } + strncpy(cur, sep, left - sz); + cur += sep_len; + left -= sep_len; + } + } + *cur = '\0'; return (const char *)str; } +const char * +buf2str(const uint8_t *buf, int len) +{ + return buf2str_extended(buf, len, NULL); +} + void printbuf(const uint8_t * buf, int len, const char * desc) { int i; |