Welcome to little lamb

Code » limb » commit 7f2b2b9

buffer_putmsg: Add PUTMSG_DUMP() for hexa-dumping..

author Olivier Brunel
2023-04-30 07:03:11 UTC
committer Olivier Brunel
2023-05-20 18:06:38 UTC
parent 5ac9c43d3bb76302b2e8c6345205495f475f85a8

buffer_putmsg: Add PUTMSG_DUMP() for hexa-dumping..

..that is, using HEX_SP | HEX_1BLOCK_SP | HEX_3BLOCK_LF | HEX_LF to
have spaces between each byte & after every (8 bytes) blocks, as well as
newlines added after each 3 blocks and (always) a trailing newline.

src/doc/buffer.h/buffer_putmsg.3.md +11 -0
src/liblimb/buffer.h/buffer_putmsg.c +3 -2
src/liblimb/include/limb/buffer.h +7 -4

diff --git a/src/doc/buffer.h/buffer_putmsg.3.md b/src/doc/buffer.h/buffer_putmsg.3.md
index e294ef2..4c86f11 100644
--- a/src/doc/buffer.h/buffer_putmsg.3.md
+++ b/src/doc/buffer.h/buffer_putmsg.3.md
@@ -51,6 +51,12 @@ a pointer to a string).
 The content of the byte array shall be written into the buffer as an hexadecimal
 dump, as described on [buffer_puthex](3).
 
+When *PUTMSG_DUMP_NEXT* was given as one of the strings, it acts similarly to
+when *PUTMSG_HEX_NEXT* is given, only with options `HEX_SP | HEX_1BLOCK_SP |
+HEX_3BLOCK_LF | HEX_LF` given to [buffer_puthex](3), so a space is added after
+each byte, another space after every 8 bytes, a newline after every 24 bytes,
+and a trailing new line is added.
+
 When *PUTMSG_LEN_NEXT* was given as one of the strings, it acts as an indicator
 that the next element in `strings` is a string that may not be NUL-terminated,
 and whose length to be written into `buf` is to be obtained for the following
@@ -73,6 +79,11 @@ element, treated as an unsigned integer (instead of a pointer to s string).
 ! :: coma-separated, as it expands to:
 ! :: `PUTMSG_HEX_NEXT, data, PUTMSG_ASUINT(dlen)`
 !
+! : *PUTMSG_DUMP(`data`, `dlen`)*, *DUMP(`data`, `dlen`)*
+! :: Shorthand to easily add an hexadecimal dump of a data blob/byte array. This
+! :: is intended for use in a coma-separated list, as it expands to:
+! :: `PUTMSG_DUMP_NEXT, data, PUTMSG_ASUINT(dlen)`
+!
 ! : *PUTMSG_LEN(`data`, `dlen`)*, *LEN(`data`, `dlen`)*
 ! :: Shorthand to easily write (part of) a string that's not NUL terminated This
 ! :: is intended for use in a coma-separated list, as it expands to:
diff --git a/src/liblimb/buffer.h/buffer_putmsg.c b/src/liblimb/buffer.h/buffer_putmsg.c
index 972c4c6..7b1d147 100644
--- a/src/liblimb/buffer.h/buffer_putmsg.c
+++ b/src/liblimb/buffer.h/buffer_putmsg.c
@@ -33,8 +33,9 @@ buffer_putmsg(buffer *b, const char * const *as, unsigned int n)
 
             buf[u64_fmt(buf, u)] = 0;
             buffer_puts(b, buf_);
-        } else if (as[i] == PUTMSG_HEX_NEXT && i + 2 < n) {
-            buffer_puthex(b, as[i + 1], (uintptr_t) as[i + 2], 0);
+        } else if ((as[i] == PUTMSG_HEX_NEXT || as[i] == PUTMSG_DUMP_NEXT) && i + 2 < n) {
+            buffer_puthex(b, as[i + 1], (uintptr_t) as[i + 2],
+                          (as[i] == PUTMSG_DUMP_NEXT) ? HEX_SP | HEX_1BLOCK_SP | HEX_3BLOCK_LF | HEX_LF : 0);
             i += 2;
         } else if (as[i] == PUTMSG_LEN_NEXT && i + 2 < n) {
             if (puts == buffer_puts)
diff --git a/src/liblimb/include/limb/buffer.h b/src/liblimb/include/limb/buffer.h
index d44da5c..89fddc4 100644
--- a/src/liblimb/include/limb/buffer.h
+++ b/src/liblimb/include/limb/buffer.h
@@ -16,18 +16,21 @@
 #define PUTMSG_INT_NEXT     ((void *) 6)    /* next string is an int */
 #define PUTMSG_HEX_NEXT     ((void *) 7)    /* next strings are data, dlen */
 #define PUTMSG_LEN_NEXT     ((void *) 8)    /* next strings are data, dlen */
+#define PUTMSG_DUMP_NEXT    ((void *) 9)    /* next strings are data, dlen */
 
 #define PUTMSG_ASUINT(n)    ((const char *) (uintptr_t) (n))
 #define PUTMSG_ASINT(n)     ((const char *) ( intptr_t) (n))
 
-#define PUTMSG_UINT(n)      PUTMSG_UINT_NEXT, PUTMSG_ASUINT(n)
-#define PUTMSG_INT(n)       PUTMSG_INT_NEXT,  PUTMSG_ASINT(n)
-#define PUTMSG_HEX(d,l)     PUTMSG_HEX_NEXT, d, PUTMSG_ASUINT(l)
-#define PUTMSG_LEN(d,l)     PUTMSG_LEN_NEXT, d, PUTMSG_ASUINT(l)
+#define PUTMSG_UINT(n)      PUTMSG_UINT_NEXT,    PUTMSG_ASUINT(n)
+#define PUTMSG_INT(n)       PUTMSG_INT_NEXT,     PUTMSG_ASINT(n)
+#define PUTMSG_HEX(d,l)     PUTMSG_HEX_NEXT, d,  PUTMSG_ASUINT(l)
+#define PUTMSG_LEN(d,l)     PUTMSG_LEN_NEXT, d,  PUTMSG_ASUINT(l)
+#define PUTMSG_DUMP(d,l)    PUTMSG_DUMP_NEXT, d, PUTMSG_ASUINT(l)
 
 #define ESC                 PUTMSG_ESC
 #define HEX(d,l)            PUTMSG_HEX(d,l)
 #define LEN(d,l)            PUTMSG_LEN(d,l)
+#define DUMP(d,l)           PUTMSG_DUMP(d,l)
 
 /* Writing */