Skip to content

Commit

Permalink
fmt: add text2pcap format helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Feb 7, 2024
1 parent 14a3236 commit 19011f0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ set(SRCS
src/fmt/regex.c
src/fmt/str.c
src/fmt/str_error.c
src/fmt/text2pcap.c
src/fmt/time.c
src/fmt/unicode.c

Expand Down
10 changes: 10 additions & 0 deletions include/re_fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,13 @@ void fmt_param_apply(const struct pl *pl, fmt_param_h *ph, void *arg);
int utf8_encode(struct re_printf *pf, const char *str);
int utf8_decode(struct re_printf *pf, const struct pl *pl);
size_t utf8_byteseq(char u[4], unsigned cp);


/* text2pcap */
struct re_text2pcap {
bool in;
const struct mbuf *mb;
char *id;
};

int re_text2pcap(struct re_printf *pf, struct re_text2pcap *pcap);
27 changes: 27 additions & 0 deletions test/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1158,3 +1158,30 @@ int test_fmt_hexdump(void)

return 0;
}


int test_text2pcap(void)
{
char test[64];
struct mbuf *mb;
int err = 0;

mb = mbuf_alloc(1);
if (!mb)
return ENOMEM;

mbuf_write_u8(mb, 42);
mbuf_write_u8(mb, 23);

mbuf_set_pos(mb, 0);

struct re_text2pcap pcap = {.id = "test", .in = true, .mb = mb};

int ret = re_snprintf(test, sizeof(test), "%H", re_text2pcap, &pcap);

TEST_EQUALS(37, ret);

out:
mem_deref(mb);
return err;
}
1 change: 1 addition & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ static const struct test tests[] = {
TEST(test_sys_getenv),
TEST(test_tcp),
TEST(test_telev),
TEST(test_text2pcap),
#ifdef USE_TLS
TEST(test_tls),
TEST(test_tls_ec),
Expand Down
1 change: 1 addition & 0 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ int test_sys_fs_fopen(void);
int test_sys_getenv(void);
int test_tcp(void);
int test_telev(void);
int test_text2pcap(void);
int test_thread(void);
int test_thread_cnd_timedwait(void);
int test_tmr_jiffies(void);
Expand Down

0 comments on commit 19011f0

Please sign in to comment.