This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
dexter gerrit-no-reply at lists.osmocom.orgReview at https://gerrit.osmocom.org/4200 add function msgb_printf() to print formatted text into msg buf In ASCII string based protocols it a printf() version that prints directly to the message buffer may be useful. Add function msgb_printf(), make sure that msg buffer bounderies are not exceeded. If the end of the tail buffer is hit, return with an error code. Change-Id: I15e1af68616309555d0ed9ac5da027c9833d42e3 --- M include/osmocom/core/msgb.h M src/msgb.c M tests/msgb/msgb_test.c M tests/msgb/msgb_test.ok 4 files changed, 59 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/00/4200/1 diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h index 91b7ec7..9c3ccf0 100644 --- a/include/osmocom/core/msgb.h +++ b/include/osmocom/core/msgb.h @@ -498,5 +498,6 @@ void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size); void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead"); +int msgb_printf(struct msgb *msgb, const char *format, ...); /*! @} */ diff --git a/src/msgb.c b/src/msgb.c index 2e9f4a2..34d03a6 100644 --- a/src/msgb.c +++ b/src/msgb.c @@ -55,6 +55,9 @@ #include <string.h> #include <stdlib.h> #include <inttypes.h> +#include <stdarg.h> +#include <errno.h> + #include <osmocom/core/msgb.h> //#include <openbsc/gsm_data.h> @@ -376,4 +379,32 @@ return buf; } + +/*! Print a string to the end of message buffer + * \param[in] msg message buffer + * \returns 0 on success, -EINVAL on error */ +int msgb_printf(struct msgb *msgb, const char *format, ...) +{ + va_list args; + int str_len; + int rc = 0; + + OSMO_ASSERT(msgb); + OSMO_ASSERT(format); + + va_start(args, format); + + str_len = + vsnprintf((char *)msgb->tail, msgb_tailroom(msgb), format, args); + + if (str_len > msgb_tailroom(msgb)) { + msgb_put(msgb, msgb_tailroom(msgb)); + rc = -EINVAL; + } else + msgb_put(msgb, str_len); + + va_end(args); + return rc; +} + /*! @} */ diff --git a/tests/msgb/msgb_test.c b/tests/msgb/msgb_test.c index ac10382..18f1742 100644 --- a/tests/msgb/msgb_test.c +++ b/tests/msgb/msgb_test.c @@ -277,6 +277,29 @@ osmo_set_panic_handler(NULL); } +static void test_msgb_printf() +{ + struct msgb *msg = msgb_alloc(80, "data"); + int rc; + int total_len; + + rc = msgb_printf(msg, "|this is a test %i, %s, %16x|", 4711, "testme", + 0x4711); + total_len = strlen((char *)msg->data); + printf("rc=%i, total_len=%i, msg->data=%s\n", rc, total_len, msg->data); + OSMO_ASSERT(rc == 0); + + rc = msgb_printf(msg, "|some more text|"); + total_len = strlen((char *)msg->data); + printf("rc=%i, total_len=%i, msg->data=%s\n", rc, total_len, msg->data); + OSMO_ASSERT(rc == 0); + + rc = msgb_printf(msg, "|more %i %x %s|", 23, 0xfee, "text"); + total_len = strlen((char *)msg->data); + printf("rc=%i, total_len=%i, msg->data=%s\n", rc, total_len, msg->data); + OSMO_ASSERT(rc == -EINVAL); +} + static struct log_info info = {}; int main(int argc, char **argv) @@ -287,6 +310,7 @@ test_msgb_api_errors(); test_msgb_copy(); test_msgb_resize_area(); + test_msgb_printf(); printf("Success.\n"); diff --git a/tests/msgb/msgb_test.ok b/tests/msgb/msgb_test.ok index 6603fe7..aae5a63 100644 --- a/tests/msgb/msgb_test.ok +++ b/tests/msgb/msgb_test.ok @@ -32,4 +32,7 @@ Original: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f Extended: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f Shrinked: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f +rc=0, total_len=47, msg->data=|this is a test 4711, testme, 4711| +rc=0, total_len=63, msg->data=|this is a test 4711, testme, 4711||some more text| +rc=-22, total_len=79, msg->data=|this is a test 4711, testme, 4711||some more text||more 23 fee tex Success. -- To view, visit https://gerrit.osmocom.org/4200 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I15e1af68616309555d0ed9ac5da027c9833d42e3 Gerrit-PatchSet: 1 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: dexter <pmaier at sysmocom.de>