From: Ralf Habacker Date: Thu, 7 Apr 2022 10:24:26 +0000 (+0200) Subject: dbus-string: Add _dbus_string_append_buffer_as_hex() X-Git-Tag: dbus-1.15.0~63^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2e63543b932ad72234e2cb84e29714d56b247dd1;p=thirdparty%2Fdbus.git dbus-string: Add _dbus_string_append_buffer_as_hex() This function provides a portable way to print data as hex values. Signed-off-by: Ralf Habacker [smcv: Only compile this when needed, improve assertions, coding style] Signed-off-by: Simon McVittie --- diff --git a/dbus/dbus-string.c b/dbus/dbus-string.c index 9c917cd90..6ea1c0395 100644 --- a/dbus/dbus-string.c +++ b/dbus/dbus-string.c @@ -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.) diff --git a/dbus/dbus-string.h b/dbus/dbus-string.h index b75da1aa3..dc223c61d 100644 --- a/dbus/dbus-string.h +++ b/dbus/dbus-string.h @@ -31,6 +31,8 @@ #include +#include + 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,