mirror of
https://github.com/ipmitool/ipmitool.git
synced 2025-05-10 18:47:22 +00:00
Extend buf2str to allow separator
This commit is contained in:
parent
f8a711b9e8
commit
9a6ba64651
@ -97,7 +97,9 @@ void print_valstr_2col(const struct valstr * vs, const char * title, int logleve
|
|||||||
|
|
||||||
uint16_t buf2short(uint8_t * buf);
|
uint16_t buf2short(uint8_t * buf);
|
||||||
uint32_t buf2long(uint8_t * buf);
|
uint32_t buf2long(uint8_t * buf);
|
||||||
const char * buf2str(uint8_t * buf, int len);
|
#define BUF2STR_MAXIMUM_OUTPUT_SIZE (3*1024 + 1)
|
||||||
|
const char * buf2str_extended(const uint8_t *buf, int len, const char *sep);
|
||||||
|
const char * buf2str(const uint8_t *buf, int len);
|
||||||
void printbuf(const uint8_t * buf, int len, const char * desc);
|
void printbuf(const uint8_t * buf, int len, const char * desc);
|
||||||
uint8_t ipmi_csum(uint8_t * d, int s);
|
uint8_t ipmi_csum(uint8_t * d, int s);
|
||||||
FILE * ipmi_open_file(const char * file, int rw);
|
FILE * ipmi_open_file(const char * file, int rw);
|
||||||
|
65
lib/helper.c
65
lib/helper.c
@ -79,24 +79,67 @@ uint16_t buf2short(uint8_t * buf)
|
|||||||
return (uint16_t)(buf[1] << 8 | buf[0]);
|
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;
|
int i;
|
||||||
|
int sz;
|
||||||
|
int left;
|
||||||
|
int sep_len;
|
||||||
|
|
||||||
if (len <= 0 || len > 1024)
|
if (buf == NULL) {
|
||||||
return NULL;
|
snprintf(str, sizeof(str), "<NULL>");
|
||||||
|
return (const char *)str;
|
||||||
memset(str, 0, 2049);
|
}
|
||||||
|
cur = str;
|
||||||
for (i=0; i<len; i++)
|
left = sizeof(str);
|
||||||
sprintf(str+i+i, "%2.2x", buf[i]);
|
if (sep) {
|
||||||
|
sep_len = strlen(sep);
|
||||||
str[len*2] = '\0';
|
} 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;
|
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)
|
void printbuf(const uint8_t * buf, int len, const char * desc)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user