Welcome to little lamb

Code » limb » commit 53e5c66

buffer_putmsg(): Add PUTMSG_LEN(data, dlen) macro..

author Olivier Brunel
2023-04-20 15:05:36 UTC
committer Olivier Brunel
2023-05-20 18:06:37 UTC
parent d9349dd612acf8dd19fcc453d0ef756b0d31895c

buffer_putmsg(): Add PUTMSG_LEN(data, dlen) macro..

..that allows to have one of the strings passed not be NUL-terminated,
and instead have its length specified on the following argument.

src/doc/buffer.h.0.md +11 -1
src/doc/buffer.h/buffer_putmsg.3.md +11 -1
src/liblimb/buffer.h/buffer_putmsg.c +6 -0
src/liblimb/include/limb/buffer.h +3 -0

diff --git a/src/doc/buffer.h.0.md b/src/doc/buffer.h.0.md
index 378e856..3163cac 100644
--- a/src/doc/buffer.h.0.md
+++ b/src/doc/buffer.h.0.md
@@ -34,6 +34,11 @@ The following constants are defined :
 :: integer. The byte array's content should be written into the buffer as
 :: hexadecimal.
 
+: *PUTMSG_LEN_NEXT*
+:: Can be used as special string given to [buffer_putmsg](3) to indicate that
+:: the next element is a pointer to a string whose length to be printed is
+:: specified as the following element, to be treated as an unsigned integer.
+
 : *PUTMSG_SYS*
 :: Can be used as special string given to [buffer_putmsg](3) to add error
 :: description of `errno`
@@ -65,9 +70,14 @@ The following macros are defined :
 : *PUTMSG_HEX(`data`,`dlen`)*, *HEX(`data`,`dlen`)*
 :: Shorthand to easily add a blob/byte array whose content shall be written out
 :: in hexadecimal form into a message. This is intended for use in a
-:: coma-separated, as it expands to:
+:: coma-separated list, as it expands to:
 :: `PUTMSG_HEX_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:
+:: `PUTMSG_LEN_NEXT, data, PUTMSG_ASUINT(dlen)`
+
 : *PUTMSG_UINT(`n`)*
 :: Shorthand to easily add an unsigned number into a message. This is intended
 :: for use in a coma-separated list, as it expands to:
diff --git a/src/doc/buffer.h/buffer_putmsg.3.md b/src/doc/buffer.h/buffer_putmsg.3.md
index e817112..e294ef2 100644
--- a/src/doc/buffer.h/buffer_putmsg.3.md
+++ b/src/doc/buffer.h/buffer_putmsg.3.md
@@ -45,12 +45,17 @@ When *PUTMSG_INT_NEXT* was given as one of the strings, it does the same as
 *PUTMSG_UINT_NEXT* only treating the next element as a signed integer.
 
 When *PUTMSG_HEX_NEXT* was given as one of the strings, it acts as an indicator
-that the next element in `strings` in a byte-array whose length in to be
+that the next element in `strings` is a byte-array whose length in to be
 obtained from the following element, treated as an unsigned integer (instead of
 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_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
+element, treated as an unsigned integer (instead of a pointer to s string).
+
 ! INFO:
 ! A few macros are available, in order to simply things:
 ! : *PUTMSG_ASUINT(`n`)*, *PUTMSG_ASINT(`n`)*
@@ -67,6 +72,11 @@ dump, as described on [buffer_puthex](3).
 ! :: out in hexadecimal form into a message. This is intended for use in a
 ! :: coma-separated, as it expands to:
 ! :: `PUTMSG_HEX_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:
+! :: `PUTMSG_LEN_NEXT, data, PUTMSG_ASUINT(dlen)`
 
 # ESCAPING
 
diff --git a/src/liblimb/buffer.h/buffer_putmsg.c b/src/liblimb/buffer.h/buffer_putmsg.c
index 435a272..4e1130b 100644
--- a/src/liblimb/buffer.h/buffer_putmsg.c
+++ b/src/liblimb/buffer.h/buffer_putmsg.c
@@ -36,6 +36,12 @@ buffer_putmsg(buffer *b, const char * const *as, unsigned int n)
         } else if (as[i] == PUTMSG_HEX_NEXT && i + 2 < n) {
             buffer_puthex(b, as[i + 1], (uintptr_t) as[i + 2]);
             i += 2;
+        } else if (as[i] == PUTMSG_LEN_NEXT && i + 2 < n) {
+            if (puts == buffer_puts)
+                buffer_put(b, as[i + 1], (uintptr_t) as[i + 2]);
+            else
+                buffer_putesc(b, as[i + 1], (uintptr_t) as[i + 2]);
+            i += 2;
         } else if (as[i] && as[i][0]) {
             puts(b, as[i]);
         }
diff --git a/src/liblimb/include/limb/buffer.h b/src/liblimb/include/limb/buffer.h
index b7b4b06..d854c46 100644
--- a/src/liblimb/include/limb/buffer.h
+++ b/src/liblimb/include/limb/buffer.h
@@ -14,6 +14,7 @@
 #define PUTMSG_UINT_NEXT    ((void *) 5)    /* next string is an uint */
 #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_ASUINT(n)    ((const char *) (uintptr_t) (n))
 #define PUTMSG_ASINT(n)     ((const char *) ( intptr_t) (n))
@@ -21,9 +22,11 @@
 #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 ESC                 PUTMSG_ESC
 #define HEX(d,l)            PUTMSG_HEX(d,l)
+#define LEN(d,l)            PUTMSG_LEN(d,l)
 
 /* Writing */