aboutsummaryrefslogtreecommitdiff
path: root/src/common/util.c
diff options
context:
space:
mode:
authorGeorge Kadianakis <desnacked@riseup.net>2012-12-17 14:14:09 +0200
committerGeorge Kadianakis <desnacked@riseup.net>2013-02-09 16:30:16 +0000
commitb8532bcb1ec51fcfae4ceff869be116fec4ccbb9 (patch)
treea7b042c5ecadcbe9de92ef9cda232c32a0129b83 /src/common/util.c
parentd86a45f991693cf2367a6ccb94fc29c22f5f7b45 (diff)
downloadtor-b8532bcb1ec51fcfae4ceff869be116fec4ccbb9.tar
tor-b8532bcb1ec51fcfae4ceff869be116fec4ccbb9.tar.gz
Add utility functions needed for SOCKS argument parsing.
Diffstat (limited to 'src/common/util.c')
-rw-r--r--src/common/util.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 93e2ba8e1..b2f12bfb6 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -865,6 +865,36 @@ tor_digest_is_zero(const char *digest)
return tor_memeq(digest, ZERO_DIGEST, DIGEST_LEN);
}
+/** Return true if <b>string</b> is a valid '<key>=<value>' string.
+ * <value> is optional, to indicate the empty string. */
+int
+string_is_key_value(const char *string)
+{
+ /* position of equal sign in string */
+ char *equal_sign_pos = NULL;
+
+ tor_assert(string);
+
+ if (strlen(string) < 2) { /* "x=a" is shortest args string */
+ log_warn(LD_GENERAL, "'%s' is too short to be a k=v value.", string);
+ return 0;
+ }
+
+ equal_sign_pos = strchr(string, '=');
+ if (!equal_sign_pos) {
+ log_warn(LD_GENERAL, "'%s' is not a k=v value.", string);
+ return 0;
+ }
+
+ /* validate that the '=' is not in the beginning of the string. */
+ if (equal_sign_pos == string) {
+ log_warn(LD_GENERAL, "'%s' is not a valid k=v value.", string);
+ return 0;
+ }
+
+ return 1;
+}
+
/** Return true iff the DIGEST256_LEN bytes in digest are all zero. */
int
tor_digest256_is_zero(const char *digest)
@@ -1249,6 +1279,43 @@ wrap_string(smartlist_t *out, const char *string, size_t width,
}
}
+/** Escape every character of <b>string</b> that belongs to the set of
+ * characters <b>set</b>. Use <b>escape_char</b> as the character to
+ * use for escaping. */
+char *
+tor_escape_str_for_socks_arg(const char *string)
+{
+ char *new_string = NULL;
+ char *new_cp = NULL;
+ size_t length, new_length;
+ static const char *chars_to_escape = ";\\";
+
+ tor_assert(string);
+
+ length = strlen(string);
+
+ if (!length)
+ return NULL;
+ /* (new_length > SIZE_MAX) => ((length * 2) + 1 > SIZE_MAX) =>
+ (length*2 > SIZE_MAX - 1) => (length > (SIZE_MAX - 1)/2) */
+ if (length > (SIZE_MAX - 1)/2) /* check for overflow */
+ return NULL;
+
+ /* this should be enough even if all characters must be escaped */
+ new_length = (length * 2) + 1;
+
+ new_string = new_cp = tor_malloc_zero(new_length);
+
+ while (*string) {
+ if (strchr(chars_to_escape, *string))
+ *new_cp++ = '\\';
+
+ *new_cp++ = *string++;
+ }
+
+ return new_string;
+}
+
/* =====
* Time
* ===== */