Commit 3ca9aa9c authored by Simon McVittie's avatar Simon McVittie

dbus-send: pretty-print GVariant-style bytestrings

dbus-send could already pretty-print bytestrings that do not have
\0 termination, but those are awkward to work with (they need copying),
so they are now discouraged. Teach it to print bytestrings that
do have \0 termination as well.

In the process, rewrite this part of the message parser
to use dbus_message_iter_get_fixed_array(), which is the Right way
to get arrays of numbers out of a message.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=89109Reviewed-by: Philip Withnall's avatarPhilip Withnall <philip.withnall@collabora.co.uk>
parent 890b1dd5
......@@ -29,7 +29,10 @@ endif
dbus_send_SOURCES= \
dbus-print-message.c \
dbus-print-message.h \
dbus-send.c
dbus-send.c \
tool-common.c \
tool-common.h \
$(NULL)
dbus_monitor_SOURCES = \
dbus-monitor.c \
......
......@@ -37,6 +37,8 @@
#include <stdlib.h>
#include "config.h"
#include "tool-common.h"
static const char*
type_to_name (int message_type)
{
......@@ -65,7 +67,9 @@ indent (int depth)
}
static void
print_hex (unsigned char *bytes, unsigned int len, int depth)
print_hex (const unsigned char *bytes,
int len,
int depth)
{
unsigned int i, columns;
......@@ -110,47 +114,45 @@ print_hex (unsigned char *bytes, unsigned int len, int depth)
static void
print_ay (DBusMessageIter *iter, int depth)
{
/* Not using DBusString because it's not public API. It's 2009, and I'm
* manually growing a string chunk by chunk.
*/
unsigned char *bytes = malloc (DEFAULT_SIZE + 1);
unsigned int len = 0;
unsigned int max = DEFAULT_SIZE;
/* True if every byte in the bytestring (so far) is printable
* ASCII, with one exception: the last byte is also allowed to be \0. */
dbus_bool_t all_ascii = TRUE;
int current_type;
while ((current_type = dbus_message_iter_get_arg_type (iter))
!= DBUS_TYPE_INVALID)
{
unsigned char val;
dbus_message_iter_get_basic (iter, &val);
bytes[len] = val;
len++;
const unsigned char *bytes;
int len;
int i;
if (val < 32 || val > 126)
all_ascii = FALSE;
dbus_message_iter_get_fixed_array (iter, &bytes, &len);
if (len == max)
for (i = 0; i < len; i++)
{
if ((bytes[i] < 32 || bytes[i] > 126) &&
(i < len - 1 || bytes[i] != '\0'))
{
max *= 2;
bytes = realloc (bytes, max + 1);
all_ascii = FALSE;
break;
}
dbus_message_iter_next (iter);
}
if (all_ascii)
if (all_ascii && len > 0 && bytes[len - 1] == '\0')
{
printf ("array of bytes \"%s\" + \\0\n", bytes);
}
else if (all_ascii)
{
bytes[len] = '\0';
printf ("array of bytes \"%s\"\n", bytes);
unsigned char *copy = dbus_malloc (len + 1);
if (copy == NULL)
tool_oom ("copying bytestring");
memcpy (copy, bytes, len);
copy[len] = '\0';
printf ("array of bytes \"%s\"\n", copy);
dbus_free (copy);
}
else
{
print_hex (bytes, len, depth);
}
free (bytes);
}
static void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment