summaryrefslogtreecommitdiff
path: root/epan/strutil.c
diff options
context:
space:
mode:
authorMichael Mann <mmann78@netscape.net>2017-01-29 21:53:49 -0500
committerMichael Mann <mmann78@netscape.net>2017-01-31 02:26:35 +0000
commitd802b5b0ec8e10fbf64f6c76815b759323029bb4 (patch)
tree2071ff718141347ff02c8dc02aafb9090e8fc5e1 /epan/strutil.c
parent60a877167f05644a5a75676798084027accd1c3d (diff)
downloadwireshark-d802b5b0ec8e10fbf64f6c76815b759323029bb4.tar.gz
Add format_text_wmem.
This allows for a wmem_allocator for users of format_text who want it (dissectors for wmem_packet_scope()). This lessens the role of current format_text functionality in hopes that it will eventually be replaced. Change-Id: I970557a65e32aa79634a3fcc654ab641b871178e Reviewed-on: https://code.wireshark.org/review/19855 Reviewed-by: Michael Mann <mmann78@netscape.net>
Diffstat (limited to 'epan/strutil.c')
-rw-r--r--epan/strutil.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/epan/strutil.c b/epan/strutil.c
index c4a65849a4..d4cab96cb6 100644
--- a/epan/strutil.c
+++ b/epan/strutil.c
@@ -245,6 +245,101 @@ format_text(const guchar *string, size_t len)
/*
* Given a string, generate a string from it that shows non-printable
+ * characters as C-style escapes, and return a pointer to it.
+ */
+gchar *
+format_text_wmem(wmem_allocator_t* allocator, const guchar *string, size_t len)
+{
+ gchar *fmtbuf = (gchar*)wmem_alloc(allocator, INITIAL_FMTBUF_SIZE);
+ int fmtbuf_len = INITIAL_FMTBUF_SIZE;
+ int column;
+ const guchar *stringend = string + len;
+ guchar c;
+ int i;
+
+ column = 0;
+ while (string < stringend) {
+ /*
+ * Is there enough room for this character, if it expands to
+ * a backslash plus 3 octal digits (which is the most it can
+ * expand to), and also enough room for a terminating '\0'?
+ */
+ if (column+3+1 >= fmtbuf_len) {
+ /*
+ * 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 *= 2;
+ fmtbuf = (gchar *)wmem_realloc(allocator, fmtbuf, fmtbuf_len);
+ }
+ c = *string++;
+
+ if (g_ascii_isprint(c)) {
+ fmtbuf[column] = c;
+ column++;
+ } else {
+ fmtbuf[column] = '\\';
+ column++;
+ switch (c) {
+
+ case '\a':
+ fmtbuf[column] = 'a';
+ column++;
+ break;
+
+ case '\b':
+ fmtbuf[column] = 'b'; /* BS */
+ column++;
+ break;
+
+ case '\f':
+ fmtbuf[column] = 'f'; /* FF */
+ column++;
+ break;
+
+ case '\n':
+ fmtbuf[column] = 'n'; /* NL */
+ column++;
+ break;
+
+ case '\r':
+ fmtbuf[column] = 'r'; /* CR */
+ column++;
+ break;
+
+ case '\t':
+ fmtbuf[column] = 't'; /* tab */
+ column++;
+ break;
+
+ case '\v':
+ fmtbuf[column] = 'v';
+ column++;
+ break;
+
+ default:
+ i = (c>>6)&03;
+ fmtbuf[column] = i + '0';
+ column++;
+ i = (c>>3)&07;
+ fmtbuf[column] = i + '0';
+ column++;
+ i = (c>>0)&07;
+ fmtbuf[column] = i + '0';
+ column++;
+ break;
+ }
+ }
+ }
+ fmtbuf[column] = '\0';
+ return fmtbuf;
+}
+
+
+/*
+ * Given a string, generate a string from it that shows non-printable
* characters as C-style escapes 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.