From 8c6ff9fec2ed8b1eed6a4c35370f62f1bf24a158 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 28 Jun 2018 11:49:27 -0400 Subject: Move tor_escape_str_for_pt_args into or/transports.c --- src/or/transports.c | 37 +++++++++++++++++++++++++++++++++++++ src/or/transports.h | 3 +++ 2 files changed, 40 insertions(+) (limited to 'src/or') diff --git a/src/or/transports.c b/src/or/transports.c index 6cc7f844ab..34161fd16e 100644 --- a/src/or/transports.c +++ b/src/or/transports.c @@ -1699,3 +1699,40 @@ pt_free_all(void) managed_proxy_list=NULL; } } + +/** Return a newly allocated string equal to string, except that every + * character in chars_to_escape is preceded by a backslash. */ +char * +tor_escape_str_for_pt_args(const char *string, const char *chars_to_escape) +{ + char *new_string = NULL; + char *new_cp = NULL; + size_t length, new_length; + + 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; +} diff --git a/src/or/transports.h b/src/or/transports.h index 0bd96e0950..d304dcd485 100644 --- a/src/or/transports.h +++ b/src/or/transports.h @@ -66,6 +66,9 @@ char *pt_stringify_socks_args(const smartlist_t *socks_args); char *pt_get_socks_args_for_proxy_addrport(const tor_addr_t *addr, uint16_t port); +char *tor_escape_str_for_pt_args(const char *string, + const char *chars_to_escape); + #ifdef PT_PRIVATE /** State of the managed proxy configuration protocol. */ enum pt_proto_state { -- cgit v1.2.3-54-g00ecf