From 323f18b9855c102c627052fe544c2959625136d4 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 13 Feb 2015 18:55:31 +0000 Subject: [PATCH] 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=89109 --- tools/Makefile.am | 5 +++- tools/dbus-print-message.c | 60 ++++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/tools/Makefile.am b/tools/Makefile.am index 294bbc6..80025b8 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -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 \ diff --git a/tools/dbus-print-message.c b/tools/dbus-print-message.c index 6f02ea3..216ebae 100644 --- a/tools/dbus-print-message.c +++ b/tools/dbus-print-message.c @@ -37,6 +37,8 @@ #include #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 -- 2.1.4