aboutsummaryrefslogtreecommitdiffstats
path: root/epan/strutil.c
diff options
context:
space:
mode:
authorEvan Huus <eapache@gmail.com>2013-10-12 12:38:56 +0000
committerEvan Huus <eapache@gmail.com>2013-10-12 12:38:56 +0000
commitfa1027a0048c1de11cb01dfdded3a88595a4a8c9 (patch)
treea2aa62eea1a16934e047fab80ae0f58f19ec4b04 /epan/strutil.c
parent522f74e9aa0059e9f2ec681117640d829e11e1c8 (diff)
From RobiOneKenobi via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9229
Add a new strutil function format_text_chr that replaces unprintable characters with a single passed-in char (eg a '.' or a '-') instead of a C-style escape. This is useful for displaying binary data that frequently but not always contains text; otherwise the number of C escape codes makes it unreadable. svn path=/trunk/; revision=52563
Diffstat (limited to 'epan/strutil.c')
-rw-r--r--epan/strutil.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/epan/strutil.c b/epan/strutil.c
index 4446a45979..1265f9f89e 100644
--- a/epan/strutil.c
+++ b/epan/strutil.c
@@ -355,6 +355,71 @@ format_text_wsp(const guchar *string, size_t len)
return fmtbuf[idx];
}
+/*
+ * Given a string, generate a string from it that shows non-printable
+ * characters as the chr parameter passed, except a whitespace character
+ * (space, tab, carriage return, new line, vertical tab, or formfeed)
+ * which will be replaced by a space, and return a pointer to it.
+ */
+gchar *
+format_text_chr(const guchar *string, const size_t len, const guchar chr)
+{
+ static gchar *fmtbuf[3];
+ static int fmtbuf_len[3];
+ static int idx;
+ int column;
+ const guchar *stringend = string + len;
+ guchar c;
+
+ idx = (idx + 1) % 3;
+
+ /*
+ * Allocate the buffer if it's not already allocated.
+ */
+ if (fmtbuf[idx] == NULL) {
+ fmtbuf[idx] = (gchar *)g_malloc(INITIAL_FMTBUF_SIZE);
+ fmtbuf_len[idx] = INITIAL_FMTBUF_SIZE;
+ }
+ column = 0;
+ while (string < stringend)
+ {
+ /*
+ * Is there enough room for this character,
+ * and also enough room for a terminating '\0'?
+ */
+ if (column+1 >= fmtbuf_len[idx])
+ {
+ /*
+ * Double the buffer's size if it's not big enough.
+ * The size of the buffer starts at 128, so doubling its size
+ * adds at least another 128 bytes, which is more than enough
+ * for one more character plus a terminating '\0'.
+ */
+ fmtbuf_len[idx] = fmtbuf_len[idx] * 2;
+ fmtbuf[idx] = (gchar *)g_realloc(fmtbuf[idx], fmtbuf_len[idx]);
+ }
+ c = *string++;
+
+ if (isprint(c))
+ {
+ fmtbuf[idx][column] = c;
+ column++;
+ }
+ else if (isspace(c))
+ {
+ fmtbuf[idx][column] = ' ';
+ column++;
+ }
+ else
+ {
+ fmtbuf[idx][column] = chr;
+ column++;
+ }
+ }
+ fmtbuf[idx][column] = '\0';
+ return fmtbuf[idx];
+}
+
static gboolean
is_byte_sep(guint8 c)
{