This function works like osmo_hexdump() and return a static buffer
containing hex bytes along with markes for the layers.

Sponsored-by: On-Waves ehf
---
 include/osmocom/core/msgb.h |    1 +
 src/msgb.c                  |   48 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h
index c4be0c3..33e8081 100644
--- a/include/osmocom/core/msgb.h
+++ b/include/osmocom/core/msgb.h
@@ -73,6 +73,7 @@ extern void msgb_enqueue(struct llist_head *queue, struct 
msgb *msg);
 extern struct msgb *msgb_dequeue(struct llist_head *queue);
 extern void msgb_reset(struct msgb *m);
 uint16_t msgb_length(const struct msgb *msg);
+extern const char *msgb_hexdump(const struct msgb *msg);
 
 #ifdef MSGB_DEBUG
 #include <osmocom/core/panic.h>
diff --git a/src/msgb.c b/src/msgb.c
index 359a545..98c7d79 100644
--- a/src/msgb.c
+++ b/src/msgb.c
@@ -153,4 +153,52 @@ void msgb_set_talloc_ctx(void *ctx)
        tall_msgb_ctx = ctx;
 }
 
+/*! \brief Return (static) buffer containing a hexdump of the msg
+ * \param[in] msg message buffer
+ * \returns a pointer to a static char array
+ */
+const char *msgb_hexdump(const struct msgb *msg)
+{
+       static char buf[4100];
+       int buf_offs = 0;
+       int nchars;
+       const unsigned char *start = msg->data;
+       const unsigned char *lxhs[4];
+       int i;
+
+       lxhs[0] = msg->l1h;
+       lxhs[1] = msg->l2h;
+       lxhs[2] = msg->l3h;
+       lxhs[3] = msg->l4h;
+
+       for (i = 0; i < ARRAY_SIZE(lxhs); i++) {
+               if (lxhs[i]) {
+                       if (lxhs[i] < msg->data)
+                               goto out_of_range;
+                       if (lxhs[i] > msg->tail)
+                               goto out_of_range;
+                       nchars = snprintf(buf + buf_offs, sizeof(buf) - 
buf_offs,
+                                         "%s[L%d]> ",
+                                         osmo_hexdump(start, lxhs[i] - start),
+                                         i+1);
+                       if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
+                               return "ERROR";
+
+                       buf_offs += nchars;
+                       start = lxhs[i];
+               }
+       }
+       nchars = snprintf(buf + buf_offs, sizeof(buf) - buf_offs,
+                         "%s", osmo_hexdump(start, msg->tail - start));
+       if (nchars < 0 || nchars + buf_offs >= sizeof(buf))
+               return "ERROR";
+
+       return buf;
+
+out_of_range:
+       nchars = snprintf(buf, sizeof(buf) - buf_offs,
+                         "!!! L%d out of range", i+1);
+       return buf;
+}
+
 /*! @} */
-- 
1.7.9.5


Reply via email to