diff options
author | Anders Broman <anders.broman@ericsson.com> | 2006-06-19 15:53:03 +0000 |
---|---|---|
committer | Anders Broman <anders.broman@ericsson.com> | 2006-06-19 15:53:03 +0000 |
commit | 764e3ac16725573ef633ae160f3afb74a88a9ed6 (patch) | |
tree | deceb77733ae0b9c9c59aff9c4d5fb0cc204b76d /epan | |
parent | 0eb577e17b75658cf23c7e328ad02477fb81accf (diff) |
New rutine tvb_format_text_wsp() which will change "whiite space" characters to space
before output.
svn path=/trunk/; revision=18519
Diffstat (limited to 'epan')
-rw-r--r-- | epan/libwireshark.def | 1 | ||||
-rw-r--r-- | epan/strutil.c | 119 | ||||
-rw-r--r-- | epan/strutil.h | 2 | ||||
-rw-r--r-- | epan/tvbuff.c | 21 | ||||
-rw-r--r-- | epan/tvbuff.h | 6 |
5 files changed, 144 insertions, 5 deletions
diff --git a/epan/libwireshark.def b/epan/libwireshark.def index f7252f6485..2d649c3003 100644 --- a/epan/libwireshark.def +++ b/epan/libwireshark.def @@ -671,6 +671,7 @@ tvb_find_guint8 tvb_find_line_end tvb_find_line_end_unquoted tvb_format_text +tvb_format_text_wsp tvb_get_ephemeral_string tvb_get_guid tvb_get_guint8 diff --git a/epan/strutil.c b/epan/strutil.c index 6707d710c2..01a9745cda 100644 --- a/epan/strutil.c +++ b/epan/strutil.c @@ -213,27 +213,136 @@ format_text(const guchar *string, int len) break; case '\b': - fmtbuf[idx][column] = 'b'; + fmtbuf[idx][column] = 'b'; /* BS */ column++; break; case '\f': - fmtbuf[idx][column] = 'f'; + fmtbuf[idx][column] = 'f'; /* FF */ column++; break; case '\n': - fmtbuf[idx][column] = 'n'; + fmtbuf[idx][column] = 'n'; /* NL */ column++; break; case '\r': - fmtbuf[idx][column] = 'r'; + fmtbuf[idx][column] = 'r'; /* CR */ column++; break; case '\t': - fmtbuf[idx][column] = 't'; + fmtbuf[idx][column] = 't'; /* tab */ + column++; + break; + + case '\v': + fmtbuf[idx][column] = 'v'; + column++; + break; + + default: + i = (c>>6)&03; + fmtbuf[idx][column] = i + '0'; + column++; + i = (c>>3)&07; + fmtbuf[idx][column] = i + '0'; + column++; + i = (c>>0)&07; + fmtbuf[idx][column] = i + '0'; + column++; + break; + } + } + } + fmtbuf[idx][column] = '\0'; + return fmtbuf[idx]; +} + +/* + * 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 replaved by a space, and return a pointer to it. + */ +gchar * +format_text_wsp(const guchar *string, int len) +{ + static gchar *fmtbuf[3]; + static int fmtbuf_len[3]; + static int idx; + int column; + const guchar *stringend = string + len; + guchar c; + int i; + + idx = (idx + 1) % 3; + + /* + * Allocate the buffer if it's not already allocated. + */ + if (fmtbuf[idx] == NULL) { + fmtbuf[idx] = g_malloc(INITIAL_FMTBUF_SIZE); + fmtbuf_len[idx] = INITIAL_FMTBUF_SIZE; + } + 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[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] = 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] = '\\'; + column++; + switch (c) { + + case '\a': + fmtbuf[idx][column] = 'a'; + column++; + break; + + case '\b': + fmtbuf[idx][column] = 'b'; /* BS */ + column++; + break; + + case '\f': + fmtbuf[idx][column] = 'f'; /* FF */ + column++; + break; + + case '\n': + fmtbuf[idx][column] = 'n'; /* NL */ + column++; + break; + + case '\r': + fmtbuf[idx][column] = 'r'; /* CR */ + column++; + break; + + case '\t': + fmtbuf[idx][column] = 't'; /* tab */ column++; break; diff --git a/epan/strutil.h b/epan/strutil.h index 7ed68367b6..2067a4179d 100644 --- a/epan/strutil.h +++ b/epan/strutil.h @@ -64,6 +64,8 @@ int get_token_len(const guchar *linep, const guchar *lineend, */ gchar* format_text(const guchar *line, int len); +gchar* format_text_wsp(const guchar *line, int len); + /** Turn an array of bytes into a string showing the bytes in hex. * * @param bd A pointer to the byte array diff --git a/epan/tvbuff.c b/epan/tvbuff.c index 3cee88e99c..5d8492d160 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -1796,6 +1796,27 @@ tvb_format_text(tvbuff_t *tvb, gint offset, gint size) } /* + * Format the data in the tvb from offset for length ... + */ + +gchar * +tvb_format_text_wsp(tvbuff_t *tvb, gint offset, gint size) +{ + const guint8 *ptr; + gint len = size; + + if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) { + + len = tvb_length_remaining(tvb, offset); + ptr = ensure_contiguous(tvb, offset, len); + + } + + return format_text_wsp(ptr, len); + +} + +/* * Like "tvb_format_text()", but for null-padded strings; don't show * the null padding characters as "\000". */ diff --git a/epan/tvbuff.h b/epan/tvbuff.h index b4f809c3a7..7957456302 100644 --- a/epan/tvbuff.h +++ b/epan/tvbuff.h @@ -428,6 +428,12 @@ extern char *tvb_get_ephemeral_faked_unicode(tvbuff_t *tvb, int offset, int len, extern gchar * tvb_format_text(tvbuff_t *tvb, gint offset, gint size); /** + * Like "tvb_format_text()", but for 'wsp'; don't show + * the characters as C-style escapes. + */ +extern gchar * tvb_format_text_wsp(tvbuff_t *tvb, gint offset, gint size); + +/** * Like "tvb_format_text()", but for null-padded strings; don't show * the null padding characters as "\000". */ |