Skip to content

Commit

Permalink
utils/string: Add mem2strbytes()
Browse files Browse the repository at this point in the history
    ulpatch/build$ ./src/tests/ulpatch_test -f Utils_str.mem2strbytes -v --lv dbg
    =========================================
    ===
    === ULPatch Testing
    ===
    ===  version: v0.5.12-69-g16a40-dirty
    === ---------------------------
    ===  105/107  Utils_str.mem2strbytes
    s = <0x11> (0x11)
    s = <(null)> (0x11)
    s = <0x11,0x22> (0x11,0x22)
    s = <0x11#0x22> (0x11#0x22)
    s = <0x11,0x22,0x33,0x44,0x55,0x66,0x77> (0x11,0x22,0x33,0x44,0x55,0x66,0x77)
    13us OK        ret:0:0
    =========================================
    === Total 1 tested
    ===  Success 1
    ===  Failed 0
    ===  Spend 0ms 0.01ms/per
    =========================================

Signed-off-by: Rong Tao <[email protected]>
  • Loading branch information
Rtoax committed Jan 16, 2025
1 parent 16a4008 commit 3741637
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/tests/utils/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,71 @@ TEST(Utils_str, strbytes2mem, 0)

return ret;
}

TEST(Utils_str, mem2strbytes, 0)
{
int i, err = 0;
char buf[1024];

struct {
uint8_t mem[128];
size_t mem_len;
char *expect_buf;
size_t buf_len;
char seperator;
int expect_errno;
} tests[] = {
{
.mem = {0x11},
.mem_len = 1,
.expect_buf = "0x11",
.buf_len = 5,
.seperator = ',',
},
{
.mem = {0x11},
.mem_len = 1,
.expect_buf = "0x11",
.buf_len = 4,
.seperator = ',',
.expect_errno = EINVAL,
},
{
.mem = {0x11,0x22},
.mem_len = 2,
.expect_buf = "0x11,0x22",
.buf_len = 10,
.seperator = ',',
},
{
.mem = {0x11,0x22},
.mem_len = 2,
.expect_buf = "0x11#0x22",
.buf_len = 10,
.seperator = '#',
},
{
.mem = {0x11,0x22,0x33,0x44,0x55,0x66,0x77},
.mem_len = 7,
.expect_buf = "0x11,0x22,0x33,0x44,0x55,0x66,0x77",
.buf_len = 35,
.seperator = ',',
},
};

for (i = 0; i < ARRAY_SIZE(tests); i++) {
char *s = mem2strbytes(tests[i].mem, tests[i].mem_len, buf,
tests[i].buf_len, tests[i].seperator);

if (strcmp(buf, tests[i].expect_buf)) {
err++;
}

if (tests[i].expect_errno != errno)
err++;

printf("s = <%s> (%s)\n", s, tests[i].expect_buf);
}

return err;
}
47 changes: 47 additions & 0 deletions src/utils/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,53 @@ void *strbytes2mem(const char *bytes, size_t *nbytes, void *buf, size_t buf_len,
return buf;
}

/**
* memory to bytes string buffer.
*/
char *mem2strbytes(const void *mem, size_t mem_len, char *bytes_buf,
size_t buf_len, char seperator)
{
int i;

errno = 0;

if (!mem || mem_len < 1 || !bytes_buf || buf_len < 5) {
errno = EINVAL;
return NULL;
}

/**
* mem = 0x123456
* mem_len = 3
* buf_len = "0x12,0x34,0x56\0"
* ^^^^^ 5
*/
if (buf_len < 5 * mem_len) {
errno = EINVAL;
return NULL;
}

bytes_buf[0] = '\0';
for (i = 0; i < mem_len; i++) {
uint8_t u8 = *(uint8_t *)(mem + i);
char *s = bytes_buf + i * 5;
/**
* "0x12,0x34,0x56\0"
* ^^^^^
*/
if (i == mem_len - 1)
sprintf(s, "0x%02x", u8);
/**
* "0x12,0x34,0x56\0"
* ^^^^^
*/
else
sprintf(s, "0x%02x%c", u8, seperator);
}

return bytes_buf;
}

/* Return TRUE if the start of STR matches PREFIX, FALSE otherwise. */
int ulp_startswith(const char *str, const char *prefix)
{
Expand Down
2 changes: 2 additions & 0 deletions src/utils/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ unsigned long str2size(const char *str);
unsigned long str2addr(const char *str);
void *strbytes2mem(const char *bytes, size_t *nbytes, void *buf, size_t buf_len,
char seperator);
char *mem2strbytes(const void *mem, size_t mem_len, char *bytes_buf,
size_t buf_len, char seperator);

int fmembytes(FILE *fp, const void *data, int data_len);

Expand Down

0 comments on commit 3741637

Please sign in to comment.