summaryrefslogtreecommitdiff
path: root/epan/strutil.c
diff options
context:
space:
mode:
authorAnders Broman <anders.broman@ericsson.com>2009-08-19 18:37:13 +0000
committerAnders Broman <anders.broman@ericsson.com>2009-08-19 18:37:13 +0000
commit7ec476b88a5e97a2c737182d7ed8feee3c09fa89 (patch)
treeb6e58d3812bf4dc4e94bc700c45178a34defb48f /epan/strutil.c
parent2c87da6c664240d67506aaa70e8ab629d76ad61b (diff)
downloadwireshark-7ec476b88a5e97a2c737182d7ed8feee3c09fa89.tar.gz
From Didier Gautheron:
Part 2 Extracted from optimizations patch http://wiki.wireshark.org/Development/Optimization Optimize expert info. Slightly changed by me. svn path=/trunk/; revision=29478
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 1b952443b9..1bf2272f7d 100644
--- a/epan/strutil.c
+++ b/epan/strutil.c
@@ -959,3 +959,68 @@ string_or_null(const char *string)
return string;
return "[NULL]";
}
+
+int
+escape_string_len(const char *string)
+{
+ const char *p;
+ gchar c;
+ int repr_len;
+
+ repr_len = 0;
+ for (p = string; (c = *p) != '\0'; p++) {
+ /* Backslashes and double-quotes must
+ * be escaped */
+ if (c == '\\' || c == '"') {
+ repr_len += 2;
+ }
+ /* Values that can't nicely be represented
+ * in ASCII need to be escaped. */
+ else if (!isprint((unsigned char)c)) {
+ /* c --> \xNN */
+ repr_len += 4;
+ }
+ /* Other characters are just passed through. */
+ else {
+ repr_len++;
+ }
+ }
+ return repr_len + 2; /* string plus leading and trailing quotes */
+}
+
+char *
+escape_string(char *buf, const char *string)
+{
+ const gchar *p;
+ gchar c;
+ char *bufp;
+ char hex[3];
+
+ bufp = buf;
+ *bufp++ = '"';
+ for (p = string; (c = *p) != '\0'; p++) {
+ /* Backslashes and double-quotes must
+ * be escaped. */
+ if (c == '\\' || c == '"') {
+ *bufp++ = '\\';
+ *bufp++ = c;
+ }
+ /* Values that can't nicely be represented
+ * in ASCII need to be escaped. */
+ else if (!isprint((unsigned char)c)) {
+ /* c --> \xNN */
+ g_snprintf(hex,sizeof(hex), "%02x", (unsigned char) c);
+ *bufp++ = '\\';
+ *bufp++ = 'x';
+ *bufp++ = hex[0];
+ *bufp++ = hex[1];
+ }
+ /* Other characters are just passed through. */
+ else {
+ *bufp++ = c;
+ }
+ }
+ *bufp++ = '"';
+ *bufp = '\0';
+ return buf;
+}