From 86f0bdf1ee4661d1b66638c18d8cffad8173460a Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Thu, 22 Jun 2023 02:27:14 +0200 Subject: [PATCH] libdpkg: Add varbuf_add_varbuf() function --- lib/dpkg/libdpkg.map | 1 + lib/dpkg/t/t-varbuf.c | 28 +++++++++++++++++++++++++++- lib/dpkg/varbuf.c | 8 ++++++++ lib/dpkg/varbuf.h | 1 + 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/dpkg/libdpkg.map b/lib/dpkg/libdpkg.map index 75918e903..d874a142d 100644 --- a/lib/dpkg/libdpkg.map +++ b/lib/dpkg/libdpkg.map @@ -114,6 +114,7 @@ LIBDPKG_PRIVATE { varbuf_reset; varbuf_grow; varbuf_trunc; + varbuf_add_varbuf; varbuf_add_char; varbuf_dup_char; varbuf_map_char; diff --git a/lib/dpkg/t/t-varbuf.c b/lib/dpkg/t/t-varbuf.c index ee1fdbced..da9dd9a9f 100644 --- a/lib/dpkg/t/t-varbuf.c +++ b/lib/dpkg/t/t-varbuf.c @@ -155,6 +155,31 @@ test_varbuf_trunc(void) } static void +test_varbuf_add_varbuf(void) +{ + struct varbuf vb, cb; + + varbuf_init(&vb, 5); + varbuf_init(&cb, 0); + + varbuf_add_str(&vb, "1234567890"); + varbuf_add_varbuf(&cb, &vb); + test_pass(cb.used == 10); + test_pass(cb.size >= cb.used); + test_mem(cb.buf, ==, "1234567890", 10); + + varbuf_reset(&vb); + varbuf_add_str(&vb, "abcde"); + varbuf_add_varbuf(&cb, &vb); + test_pass(cb.used == 15); + test_pass(cb.size >= cb.used); + test_mem(cb.buf, ==, "1234567890abcde", 15); + + varbuf_destroy(&cb); + varbuf_destroy(&vb); +} + +static void test_varbuf_add_buf(void) { struct varbuf vb; @@ -452,13 +477,14 @@ test_varbuf_detach(void) TEST_ENTRY(test) { - test_plan(152); + test_plan(158); test_varbuf_init(); test_varbuf_prealloc(); test_varbuf_new(); test_varbuf_grow(); test_varbuf_trunc(); + test_varbuf_add_varbuf(); test_varbuf_add_buf(); test_varbuf_add_char(); test_varbuf_dup_char(); diff --git a/lib/dpkg/varbuf.c b/lib/dpkg/varbuf.c index b605c1c4e..47636d793 100644 --- a/lib/dpkg/varbuf.c +++ b/lib/dpkg/varbuf.c @@ -100,6 +100,14 @@ varbuf_get_str(struct varbuf *v) } void +varbuf_add_varbuf(struct varbuf *v, const struct varbuf *other) +{ + varbuf_grow(v, other->used); + memcpy(v->buf + v->used, other->buf, other->used); + v->used += other->used; +} + +void varbuf_add_char(struct varbuf *v, int c) { varbuf_grow(v, 1); diff --git a/lib/dpkg/varbuf.h b/lib/dpkg/varbuf.h index c5c9458e8..9b10bdc8b 100644 --- a/lib/dpkg/varbuf.h +++ b/lib/dpkg/varbuf.h @@ -83,6 +83,7 @@ void varbuf_reset(struct varbuf *v); void varbuf_destroy(struct varbuf *v); void varbuf_free(struct varbuf *v); +void varbuf_add_varbuf(struct varbuf *v, const struct varbuf *other); void varbuf_add_char(struct varbuf *v, int c); void varbuf_dup_char(struct varbuf *v, int c, size_t n); void varbuf_map_char(struct varbuf *v, int c_src, int c_dst); -- 2.11.4.GIT