]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Lib: Extend bsnprintf() for byte strings
authorOndrej Zajicek <santiago@crfreenet.org>
Mon, 29 May 2023 03:25:40 +0000 (05:25 +0200)
committerOndrej Zajicek <santiago@crfreenet.org>
Sat, 14 Jun 2025 16:16:28 +0000 (18:16 +0200)
Add support for %Xb directive to print fixed-length byte strings,
formatted as hexadecimal sequences separated by colon.

lib/printf.c

index 0d2f95e8013cd79693c61a098b403394561d76f1..01cf20fe1d19b2f3fc933a5872a2c7d0bbf46c1e 100644 (file)
@@ -230,6 +230,24 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
                if (field_width > size)
                        return -1;
                switch (*fmt) {
+               case 'b': {
+                       const char *digits="0123456789abcdef";
+                       const byte *bs = va_arg(args, const byte *);
+                       len = field_width;
+
+                       if (3*len > size)
+                               return -1;
+
+                       for (i = 0; i < len; i++) {
+                               const byte b = *bs++;
+                               *str++ = digits[b >> 4];
+                               *str++ = digits[b & 0xf];
+                               *str++ = ':';
+                       }
+
+                       str -= !!i;
+                       continue;
+               }
                case 'c':
                        if (!(flags & LEFT))
                                while (--field_width > 0)