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 <ralf.habacker@freenet.de>
[smcv: Only compile this when needed, improve assertions, coding style]
Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Ralf Habacker 2022-04-07 12:24:26 +02:00
parent 76fe49b7ab
commit 2e63543b93
2 changed files with 41 additions and 0 deletions

View 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.)

View 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,