Skip to content

Commit

Permalink
utils: string: Add strbytes2mem()
Browse files Browse the repository at this point in the history
    ulpatch/build$ ./src/tests/ulpatch_test -f strbytes -v --lv dbg
    =========================================
    ===
    === ULPatch Testing
    ===
    ===  version: v0.5.12-67-g423b4-dirty
    === ---------------------------
    ===  104/106  Utils_str.strbytes2mem
    0xff : 1 (expect 1)
    0xff, : 1 (expect 1)
    ,,,0xff,,,, : 1 (expect 1)
    0xff,0x11 : 2 (expect 2)
    ,,,,0xff,,,0x11,,, : 2 (expect 2)
    0xff,0x1,0x2 : 3 (expect 3)
    0xff,,,0x1,,,0x2,, : 3 (expect 3)
    0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90 : 8 (expect 8)
    0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90,0xff,0x25,0x02,0x00,0x00 : 13 (expect 13)
    0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90,0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90 : 16 (expect 16)
    0xff; : 1 (expect 1)
    +++0xff+++0x1++++0x3++0x2++ : 4 (expect 4)
    43us OK        ret:0:0
    =========================================
    === Total 1 tested
    ===  Success 1
    ===  Failed 0
    ===  Spend 0ms 0.04ms/per
    =========================================

Signed-off-by: Rong Tao <rtoax@foxmail.com>
  • Loading branch information
Rtoax committed Jan 8, 2025
1 parent 423b42a commit b3dd557
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,13 +828,11 @@ static void init_test_symbols(void)
#define TEST_FUNC_SYM(s) \
if (!strcmp(#s, test_symbols[i].sym) && test_symbols[i].type == TYPE_TST_SYM_FUNC) { \
test_symbols[i].addr = (unsigned long)s; \
ulp_debug("Sym %s addr %lx\n", #s, test_symbols[i].addr); \
}
/* DATA need '&' operater */
#define TEST_DATA_SYM(s) \
if (!strcmp(#s, test_symbols[i].sym) && test_symbols[i].type == TYPE_TST_SYM_DATA) { \
test_symbols[i].addr = (unsigned long)&s; \
ulp_debug("Sym %s addr %lx\n", #s, test_symbols[i].addr); \
}
# include <tests/test-symbols.h>
#undef TEST_FUNC_SYM
Expand Down
98 changes: 98 additions & 0 deletions src/tests/utils/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <sys/types.h>
#include <unistd.h>
#include <utils/log.h>
#include <utils/util.h>
#include <utils/list.h>
#include <utils/disasm.h>
#include <tests/test-api.h>

TEST_STUB(utils_string);
Expand Down Expand Up @@ -97,3 +99,99 @@ TEST(Utils_str, str2addr, 0)

return ret;
}

TEST(Utils_str, strbytes2mem, 0)
{
int ret, i;
char buf[1024];
size_t nbytes;

struct {
char *s;
uint8_t mem[128];
size_t n;
char seperator;
} values[] = {
{
.s = "0xff",
.mem = {0xff},
.n = 1,
},
{
.s = "0xff,",
.mem = {0xff},
.n = 1,
},
{
.s = ",,,0xff,,,,",
.mem = {0xff},
.n = 1,
},
{
.s = "0xff,0x11",
.mem = {0xff,0x11},
.n = 2,
},
{
.s = ",,,,0xff,,,0x11,,,",
.mem = {0xff,0x11},
.n = 2,
},
{
.s = "0xff,0x1,0x2",
.mem = {0xff,0x1,0x2},
.n = 3,
},
{
.s = "0xff,,,0x1,,,0x2,,",
.mem = {0xff,0x1,0x2},
.n = 3,
},
{
.s = "0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90",
.mem = {0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90},
.n = 8,
},
{
.s = "0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90,0xff,0x25,0x02,0x00,0x00",
.mem = {0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90,0xff,0x25,0x02,0x00,0x00},
.n = 13,
},
{
.s = "0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90,0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90",
.mem = {0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90,0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90},
.n = 16,
},
{
.s = "0xff;",
.mem = {0xff},
.n = 1,
.seperator = ';',
},
{
.s = "+++0xff+++0x1++++0x3++0x2++",
.mem = {0xff,0x1,0x3,0x2},
.n = 4,
.seperator = '+',
},
};

ret = 0;

for (i = 0; i < ARRAY_SIZE(values); i++) {
void *mem = strbytes2mem(values[i].s, &nbytes, buf, sizeof(buf),
values[i].seperator);

printf("%s : %ld (expect %ld)\n", values[i].s, nbytes, values[i].n);
#if defined(DEBUG)
fdisasm_arch(stdout, ">> ", 0, (unsigned char *)mem, nbytes);
#endif
if (memcmp(mem, values[i].mem, MAX(nbytes, values[i].n)))
ret++;

if (nbytes != values[i].n)
ret++;
}

return ret;
}
57 changes: 57 additions & 0 deletions src/utils/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,63 @@ int fmembytes(FILE *fp, const void *data, int data_len)
return 0;
}

/**
* convert string to memory bytes, split with ','.
*
* bytes: "0xff,0x25,0x02,0x00,0x00,0x00,0x90,0x90"
* return: 0xff25020000009090, nbytes = 8
*/
void *strbytes2mem(const char *bytes, size_t *nbytes, void *buf, size_t buf_len,
char seperator)
{
size_t n = 0;
uint8_t *u = buf;
char sep = ',', str_sep[2];

if (seperator)
sep = seperator;

sprintf(str_sep, "%c", sep);

const char *s = bytes;

/* Skip seperator prefix */
while (s && *s == sep && *s != '\0')
s++;

while (s && *s != '\0') {
if (s[0] != '0' || s[1] != 'x') {
errno = EINVAL;
*nbytes = 0;
return NULL;
}
*u = (uint8_t)strtoull(s, NULL, 16);
#if defined(DEBUG)
ulp_debug("u %d, s %s\n", *u, s);
#endif
u++;
n++;

if (n > buf_len) {
errno = EINVAL;
*nbytes = 0;
return NULL;
}
s = strstr(s, str_sep);

/* Skip all seperator */
while (s && *s == sep && *s != '\0')
s++;
#if defined(DEBUG)
ulp_debug("loop: s %s\n", s);
#endif
}

*nbytes = n;

return buf;
}

/* Return TRUE if the start of STR matches PREFIX, FALSE otherwise. */
int ulp_startswith(const char *str, const char *prefix)
{
Expand Down
3 changes: 3 additions & 0 deletions src/utils/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <limits.h>

Expand Down Expand Up @@ -226,6 +227,8 @@ void free_strstr_list(struct list_head *list);

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

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

Expand Down

0 comments on commit b3dd557

Please sign in to comment.