From 63d7365ae6d1be6facaabad6ecc80cf6650f5d6d Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sun, 17 May 2015 23:14:57 +0100 Subject: [PATCH] Gratuitous simplification of commasep_string functions. in_commasep_string() is now implemented in terms of first_in_commasep_string(), memchr(), and tail recursion. --- ssh.c | 60 ++++++++++++++++++++++++----------------------------------- 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/ssh.c b/ssh.c index e6acec62..47f904eb 100644 --- a/ssh.c +++ b/ssh.c @@ -6058,39 +6058,7 @@ static void ssh1_protocol(Ssh ssh, const void *vin, int inlen, } /* - * Utility routine for decoding comma-separated strings in KEXINIT. - */ -static int 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); - while (1) { - /* - * Is it at the start of the string? - */ - if (haylen >= needlen && /* haystack is long enough */ - !memcmp(needle, haystack, needlen) && /* initial match */ - (haylen == needlen || haystack[needlen] == ',') - /* either , or EOS follows */ - ) - return 1; - /* - * If not, search for the next comma and resume after that. - * If no comma found, terminate. - */ - while (haylen > 0 && *haystack != ',') - haylen--, haystack++; - if (haylen == 0) - return 0; - haylen--, haystack++; /* skip over comma itself */ - } -} - -/* - * Similar routine for checking whether we have the first string in a list. + * Utility routines for decoding comma-separated strings in KEXINIT. */ static int first_in_commasep_string(char const *needle, char const *haystack, int haylen) @@ -6099,9 +6067,7 @@ static int first_in_commasep_string(char const *needle, char const *haystack, if (!needle || !haystack) /* protect against null pointers */ return 0; needlen = strlen(needle); - /* - * Is it at the start of the string? - */ + if (haylen >= needlen && /* haystack is long enough */ !memcmp(needle, haystack, needlen) && /* initial match */ (haylen == needlen || haystack[needlen] == ',') @@ -6111,6 +6077,28 @@ static int first_in_commasep_string(char const *needle, char const *haystack, 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 the comma-separated string at the end of the packet. * If the value is already in the string, don't bother adding it again.