diff options
author | Nick Mathewson <nickm@torproject.org> | 2013-03-19 13:25:45 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2013-03-19 13:25:45 -0400 |
commit | c101ecc8dcc733fda31c0834f19c48d717ebe4c7 (patch) | |
tree | 53cbcd8c6b4290bd7d8c78fd2f58e791241864ab /src/common | |
parent | 6e94d2fb3a11d7cba5796c1662e0c5089ad3e509 (diff) | |
parent | 9bdd33eae68b93db688c4537e5c11841a5d37a3b (diff) | |
download | tor-c101ecc8dcc733fda31c0834f19c48d717ebe4c7.tar tor-c101ecc8dcc733fda31c0834f19c48d717ebe4c7.tar.gz |
Merge remote-tracking branch 'asn/bug3594_rebased_and_fixed'
Conflicts:
src/common/util.c
src/or/entrynodes.h
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util.c | 73 | ||||
-rw-r--r-- | src/common/util.h | 4 |
2 files changed, 77 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c index 17fb9496c..144f472d9 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -865,6 +865,39 @@ 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. Log at logging + * <b>severity</b> if something ugly happens. */ +int +string_is_key_value(int severity, const char *string) +{ + /* position of equal sign in string */ + const char *equal_sign_pos = NULL; + + tor_assert(string); + + if (strlen(string) < 2) { /* "x=" is shortest args string */ + tor_log(severity, LD_GENERAL, "'%s' is too short to be a k=v value.", + escaped(string)); + return 0; + } + + equal_sign_pos = strchr(string, '='); + if (!equal_sign_pos) { + tor_log(severity, LD_GENERAL, "'%s' is not a k=v value.", escaped(string)); + return 0; + } + + /* validate that the '=' is not in the beginning of the string. */ + if (equal_sign_pos == string) { + tor_log(severity, LD_GENERAL, "'%s' is not a valid k=v value.", + escaped(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) @@ -1176,6 +1209,46 @@ escaped(const char *s) return escaped_val_; } +/** Escape every ";" or "\" character of <b>string</b>. Use + * <b>escape_char</b> as the character to use for escaping. + * The returned string is allocated on the heap and it's the + * responsibility of the caller to free it. */ +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) /* If we were given the empty string, return the same. */ + return tor_strdup(""); + /* (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(new_length); + + while (*string) { + if (strchr(chars_to_escape, *string)) + *new_cp++ = '\\'; + + *new_cp++ = *string++; + } + + *new_cp = '\0'; /* NUL-terminate the new string */ + + return new_string; +} + /* ===== * Time * ===== */ diff --git a/src/common/util.h b/src/common/util.h index 8206a6d8a..11a50ebf1 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -208,12 +208,16 @@ const char *find_whitespace_eos(const char *s, const char *eos); const char *find_str_at_start_of_line(const char *haystack, const char *needle); int string_is_C_identifier(const char *string); +int string_is_key_value(int severity, const char *string); int tor_mem_is_zero(const char *mem, size_t len); int tor_digest_is_zero(const char *digest); int tor_digest256_is_zero(const char *digest); char *esc_for_log(const char *string) ATTR_MALLOC; const char *escaped(const char *string); + +char *tor_escape_str_for_socks_arg(const char *string); + struct smartlist_t; int tor_vsscanf(const char *buf, const char *pattern, va_list ap) #ifdef __GNUC__ |