From 74f1e9d1c3ce836b813c060c3c4c855c2aef4534 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Wed, 7 Feb 2024 11:52:46 +0100 Subject: [PATCH] fmt: add text2pcap format helper --- CMakeLists.txt | 1 + include/re_fmt.h | 10 ++++++++++ src/fmt/text2pcap.c | 31 +++++++++++++++++++++++++++++++ test/fmt.c | 27 +++++++++++++++++++++++++++ test/test.c | 1 + test/test.h | 1 + 6 files changed, 71 insertions(+) create mode 100644 src/fmt/text2pcap.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 84a507fd8..6f9af1f6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/include/re_fmt.h b/include/re_fmt.h index 2f4cdfbe2..33580a2b8 100644 --- a/include/re_fmt.h +++ b/include/re_fmt.h @@ -203,3 +203,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); diff --git a/src/fmt/text2pcap.c b/src/fmt/text2pcap.c new file mode 100644 index 000000000..f95cd8c68 --- /dev/null +++ b/src/fmt/text2pcap.c @@ -0,0 +1,31 @@ +#ifdef HAVE_SYS_TIME_H +#include +#endif +#include + +#include +#include +#include + + +int re_text2pcap(struct re_printf *pf, struct re_text2pcap *pcap) +{ + if (!pcap) + return EINVAL; + + uint8_t *buf = mbuf_buf(pcap->mb); + if (!buf) + return EINVAL; + + re_hprintf(pf, "%s %H 000000", pcap->in ? "I" : "O", fmt_timestamp_us, + NULL); + + size_t sz = mbuf_get_left(pcap->mb); + for (size_t i = 0; i < sz; i++) { + re_hprintf(pf, " %02x", buf[i]); + } + + re_hprintf(pf, " %s", pcap->id); + + return 0; +} diff --git a/test/fmt.c b/test/fmt.c index 7b82deebb..297346c3f 100644 --- a/test/fmt.c +++ b/test/fmt.c @@ -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(2); + 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(35, ret); + +out: + mem_deref(mb); + return err; +} diff --git a/test/test.c b/test/test.c index 261c1d96f..6bfc024f9 100644 --- a/test/test.c +++ b/test/test.c @@ -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), diff --git a/test/test.h b/test/test.h index 68a681e2c..cee4a3401 100644 --- a/test/test.h +++ b/test/test.h @@ -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);