Move comma-separated string functions into sshcommon.c.

These are just string handling, after all. They could even move into
misc.c if any non-SSH-related code ever had a need for them.
This commit is contained in:
Simon Tatham 2018-09-19 20:39:25 +01:00
Родитель 968252bbdc
Коммит 26364bb6a1
3 изменённых файлов: 56 добавлений и 53 удалений

53
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).
*/

5
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);

Просмотреть файл

@ -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));
}