From c91f759bafcae2a19808b642316d1e2447b6073d Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 26 Sep 2023 09:35:25 +0200 Subject: common: Add gnupg_memstr to replace static versions. * common/stringhelp.c (gnupg_memstr): New. * common/mbox-util.c (my_memstr): Remove. (is_valid_mailbox_mem): Use gnupg_memstr. * common/recsel.c (my_memstr): Remove. (recsel_select): Use gnupg_memstr. --- common/stringhelp.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'common/stringhelp.c') diff --git a/common/stringhelp.c b/common/stringhelp.c index 1049c78e2..5407653de 100644 --- a/common/stringhelp.c +++ b/common/stringhelp.c @@ -161,6 +161,35 @@ ascii_memistr ( const void *buffer, size_t buflen, const char *sub ) } +/* This is a case-sensitive version of our memistr. I wonder why no + * standard function memstr exists but we better do not use the name + * memstr to avoid future conflicts. + */ +const char * +gnupg_memstr (const void *buffer, size_t buflen, const char *sub) +{ + const unsigned char *buf = buffer; + const unsigned char *t = (const unsigned char *)buf; + const unsigned char *s = (const unsigned char *)sub; + size_t n = buflen; + + for ( ; n ; t++, n-- ) + { + if (*t == *s) + { + for (buf = t++, buflen = n--, s++; n && *t ==*s; t++, s++, n--) + ; + if (!*s) + return (const char*)buf; + t = (const unsigned char *)buf; + s = (const unsigned char *)sub ; + n = buflen; + } + } + return NULL; +} + + /* This function is similar to strncpy(). However it won't copy more * than N - 1 characters and makes sure that a '\0' is appended. With * N given as 0, nothing will happen. With DEST given as NULL, memory -- cgit v1.2.3