diff --git a/ssh.c b/ssh.c index d5f203b0..a8d93ad1 100644 --- a/ssh.c +++ b/ssh.c @@ -3464,59 +3464,6 @@ static void ssh1_protocol_setup(Ssh ssh) ssh->packet_dispatch[SSH1_MSG_DEBUG] = ssh1_msg_debug; } -/* - * Utility routines for decoding comma-separated strings in KEXINIT. - */ -static int first_in_commasep_string(char const *needle, char const *haystack, - int haylen) -{ - int needlen; - if (!needle || !haystack) /* protect against null pointers */ - return 0; - needlen = strlen(needle); - - if (haylen >= needlen && /* haystack is long enough */ - !memcmp(needle, haystack, needlen) && /* initial match */ - (haylen == needlen || haystack[needlen] == ',') - /* either , or EOS follows */ - ) - return 1; - return 0; -} - -static int in_commasep_string(char const *needle, char const *haystack, - int haylen) -{ - char *p; - - if (!needle || !haystack) /* protect against null pointers */ - return 0; - /* - * Is it at the start of the string? - */ - if (first_in_commasep_string(needle, haystack, haylen)) - return 1; - /* - * If not, search for the next comma and resume after that. - * If no comma found, terminate. - */ - p = memchr(haystack, ',', haylen); - if (!p) return 0; - /* + 1 to skip over comma */ - return in_commasep_string(needle, p + 1, haylen - (p + 1 - haystack)); -} - -/* - * Add a value to a strbuf containing a comma-separated list. - */ -static void add_to_commasep(strbuf *buf, const char *data) -{ - if (buf->len > 0) - put_byte(buf, ','); - put_data(buf, data, strlen(data)); -} - - /* * SSH-2 key derivation (RFC 4253 section 7.2). */ diff --git a/ssh.h b/ssh.h index fc61aa31..16224330 100644 --- a/ssh.h +++ b/ssh.h @@ -1334,3 +1334,8 @@ unsigned alloc_channel_id_general(tree234 *channels, size_t localid_offset); #define alloc_channel_id(tree, type) \ TYPECHECK(&((type *)0)->localid == (unsigned *)0, \ alloc_channel_id_general(tree, offsetof(type, localid))) + +int first_in_commasep_string(char const *needle, char const *haystack, + int haylen); +int in_commasep_string(char const *needle, char const *haystack, int haylen); +void add_to_commasep(strbuf *buf, const char *data); diff --git a/sshcommon.c b/sshcommon.c index 28f28cf9..371d6c30 100644 --- a/sshcommon.c +++ b/sshcommon.c @@ -503,3 +503,54 @@ unsigned alloc_channel_id_general(tree234 *channels, size_t localid_offset) */ return ss.index + CHANNEL_NUMBER_OFFSET; } + +/* ---------------------------------------------------------------------- + * Functions for handling the comma-separated strings used to store + * lists of protocol identifiers in SSH-2. + */ + +int first_in_commasep_string(char const *needle, char const *haystack, + int haylen) +{ + int needlen; + if (!needle || !haystack) /* protect against null pointers */ + return 0; + needlen = strlen(needle); + + if (haylen >= needlen && /* haystack is long enough */ + !memcmp(needle, haystack, needlen) && /* initial match */ + (haylen == needlen || haystack[needlen] == ',') + /* either , or EOS follows */ + ) + return 1; + return 0; +} + +int in_commasep_string(char const *needle, char const *haystack, int haylen) +{ + char *p; + + if (!needle || !haystack) /* protect against null pointers */ + return FALSE; + /* + * Is it at the start of the string? + */ + if (first_in_commasep_string(needle, haystack, haylen)) + return TRUE; + /* + * If not, search for the next comma and resume after that. + * If no comma found, terminate. + */ + p = memchr(haystack, ',', haylen); + if (!p) + return FALSE; + /* + 1 to skip over comma */ + return in_commasep_string(needle, p + 1, haylen - (p + 1 - haystack)); +} + +void add_to_commasep(strbuf *buf, const char *data) +{ + if (buf->len > 0) + put_byte(buf, ','); + put_data(buf, data, strlen(data)); +}