summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustus Winter <4winter@informatik.uni-hamburg.de>2013-12-13 12:57:55 +0100
committerJustus Winter <4winter@informatik.uni-hamburg.de>2013-12-16 10:48:18 +0100
commit84932431cf4fbd494b4597105faed26ed2ac4efe (patch)
tree338db9d3de6f9f834832c02afd021bd1edb5218a
parentb8bce60ff16e52862ca5a3c5a6018420b4fa3cd1 (diff)
utils/rpctrace: escape non-printable characters in strings
* utils/rpctrace.c (escape_sequences): New char array mapping characters to their escape sequence. (print_data): Escape non-printable characters when printing strings.
-rw-r--r--utils/rpctrace.c48
1 files changed, 46 insertions, 2 deletions
diff --git a/utils/rpctrace.c b/utils/rpctrace.c
index b39b2e31..0f68f444 100644
--- a/utils/rpctrace.c
+++ b/utils/rpctrace.c
@@ -1528,6 +1528,20 @@ print_reply_header (struct send_once_info *info, mig_reply_header_t *reply,
}
}
+static char escape_sequences[0x100] =
+ {
+ ['\0'] = '0',
+ ['\a'] = 'a',
+ ['\b'] = 'b',
+ ['\f'] = 'f',
+ ['\n'] = 'n',
+ ['\r'] = 'r',
+ ['\t'] = 't',
+ ['\v'] = 'v',
+ ['\\'] = '\\',
+ ['\''] = '\'',
+ ['"'] = '"',
+ };
static void
print_data (mach_msg_type_name_t type,
@@ -1555,8 +1569,38 @@ print_data (mach_msg_type_name_t type,
case MACH_MSG_TYPE_CHAR:
if (nelt > strsize)
nelt = strsize;
- fprintf (ostream, "\"%.*s\"",
- (int) (nelt * eltsize), (const char *) data);
+ fprintf (ostream, "\"");
+ /* Scan data for non-printable characters. p always points to
+ the first character that has not yet been printed. */
+ const char *p, *q;
+ p = q = (const char *) data;
+ while (*q && q - (const char *) data < (int) (nelt * eltsize))
+ {
+ if (isgraph (*q) || *q == ' ')
+ {
+ q += 1;
+ continue;
+ }
+
+ /* We encountered a non-printable character. Print anything
+ that has not been printed so far. */
+ if (p < q)
+ fprintf (ostream, "%.*s", q - p, p);
+
+ char c = escape_sequences[*((const unsigned char *) q)];
+ if (c)
+ fprintf (ostream, "\\%c", c);
+ else
+ fprintf (ostream, "\\x%02x", *((const unsigned char *) q));
+
+ q += 1;
+ p = q;
+ }
+
+ /* Print anything that has not been printed so far. */
+ if (p < q)
+ fprintf (ostream, "%.*s", q - p, p);
+ fprintf (ostream, "\"");
return;
#if 0