]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
dbus-string: Add _dbus_string_append_buffer_as_hex()
authorRalf Habacker <ralf.habacker@freenet.de>
Thu, 7 Apr 2022 10:24:26 +0000 (12:24 +0200)
committerRalf Habacker <ralf.habacker@freenet.de>
Sun, 1 May 2022 16:28:51 +0000 (16:28 +0000)
This function provides a portable way to print data as hex values.

Signed-off-by: Ralf Habacker <ralf.habacker@freenet.de>
[smcv: Only compile this when needed, improve assertions, coding style]
Signed-off-by: Simon McVittie <smcv@collabora.com>
dbus/dbus-string.c
dbus/dbus-string.h

index 9c917cd90ec09bb14fdcfa70768645fc860046b7..6ea1c039566c1822abf8dc40d77df57a58d91aad 100644 (file)
@@ -2345,6 +2345,41 @@ _dbus_string_append_byte_as_hex (DBusString *str,
   return TRUE;
 }
 
+/* Currently only used when embedded tests are enabled */
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+/**
+ * Appends \p size bytes from the buffer \p buf as hex digits to the string \p str
+ *
+ * If \p size is nonzero, then \p buf must be non-NULL.
+ *
+ * @param str the string
+ * @param buf the buffer to add bytes from
+ * @param size the number of bytes to add
+ * @returns #FALSE if no memory
+ */
+dbus_bool_t
+_dbus_string_append_buffer_as_hex (DBusString *str,
+                                   void *buf,
+                                   int size)
+{
+  unsigned char *p;
+  int i;
+
+  _dbus_assert (size >= 0);
+  _dbus_assert (size == 0 || buf != NULL);
+
+  p = (unsigned char *) buf;
+
+  for (i = 0; i < size; i++)
+    {
+      if (!_dbus_string_append_byte_as_hex (str, p[i]))
+        return FALSE;
+    }
+
+  return TRUE;
+}
+#endif
+
 /**
  * Encodes a string in hex, the way MD5 and SHA-1 are usually
  * encoded. (Each byte is two hex digits.)
index b75da1aa30ad5ee742b5140c24c1e827f217ba0e..dc223c61d7400d07a60b133f19214500a8b55dcb 100644 (file)
@@ -31,6 +31,8 @@
 
 #include <stdarg.h>
 
+#include <dbus/dbus-macros-internal.h>
+
 DBUS_BEGIN_DECLS
 
 /**
@@ -358,6 +360,10 @@ DBUS_PRIVATE_EXPORT
 void          _dbus_string_chop_white            (DBusString        *str); 
 dbus_bool_t   _dbus_string_append_byte_as_hex    (DBusString        *str,
                                                   unsigned char      byte);
+DBUS_EMBEDDED_TESTS_EXPORT
+dbus_bool_t _dbus_string_append_buffer_as_hex    (DBusString        *str,
+                                                  void              *buf,
+                                                  int                size);
 DBUS_PRIVATE_EXPORT
 dbus_bool_t   _dbus_string_hex_encode            (const DBusString  *source,
                                                   int                start,