1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-03-22 06:38:37 -05:00

Gratuitous simplification of commasep_string functions.

in_commasep_string() is now implemented in terms of
first_in_commasep_string(), memchr(), and tail recursion.
This commit is contained in:
Ben Harris 2015-05-17 23:14:57 +01:00
parent 454fe4fdf7
commit 63d7365ae6

60
ssh.c
View File

@ -6058,39 +6058,7 @@ static void ssh1_protocol(Ssh ssh, const void *vin, int inlen,
} }
/* /*
* Utility routine for decoding comma-separated strings in KEXINIT. * Utility routines 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.
*/ */
static int first_in_commasep_string(char const *needle, char const *haystack, static int first_in_commasep_string(char const *needle, char const *haystack,
int haylen) 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 */ if (!needle || !haystack) /* protect against null pointers */
return 0; return 0;
needlen = strlen(needle); needlen = strlen(needle);
/*
* Is it at the start of the string?
*/
if (haylen >= needlen && /* haystack is long enough */ if (haylen >= needlen && /* haystack is long enough */
!memcmp(needle, haystack, needlen) && /* initial match */ !memcmp(needle, haystack, needlen) && /* initial match */
(haylen == needlen || haystack[needlen] == ',') (haylen == needlen || haystack[needlen] == ',')
@ -6111,6 +6077,28 @@ static int first_in_commasep_string(char const *needle, char const *haystack,
return 0; 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. * 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. * If the value is already in the string, don't bother adding it again.