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 8, 2024
1 parent 9dc51ac commit 74f1e9d
Show file tree
Hide file tree
Showing 6 changed files with 71 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 @@ -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);
31 changes: 31 additions & 0 deletions src/fmt/text2pcap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <time.h>

#include <re_types.h>
#include <re_fmt.h>
#include <re_mbuf.h>


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;
}
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(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;
}
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 74f1e9d

Please sign in to comment.