From 85f4aa359a8e3266ffa86152b7cadf0d0631217e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 21 Aug 2017 14:17:01 +0200 Subject: [PATCH 1/4] Add CRYB_DEQUAL() to strip qualifiers from pointers. --- include/cryb/defs.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/cryb/defs.h b/include/cryb/defs.h index ee23049..a69a043 100644 --- a/include/cryb/defs.h +++ b/include/cryb/defs.h @@ -50,4 +50,6 @@ #define CRYB_END #endif +#define CRYB_DEQUAL(p) ((void *)(intptr_t)(p)) + #endif From 5c8745138e80180680e4bc24353e948abfe9e78a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 21 Aug 2017 18:31:29 +0200 Subject: [PATCH 2/4] Add strchrnul(), wcschrnul(), and tests for the former. --- configure.ac | 4 +- include/cryb/Makefile.am | 2 + include/cryb/strchrnul.h | 48 +++++++++++ include/cryb/wcschrnul.h | 48 +++++++++++ lib/core/Makefile.am | 2 + lib/core/cryb_strchrnul.c | 48 +++++++++++ lib/core/cryb_wcschrnul.c | 49 +++++++++++ t/.gitignore | 1 + t/Makefile.am | 3 +- t/t_strchrnul.c | 166 ++++++++++++++++++++++++++++++++++++++ 10 files changed, 368 insertions(+), 3 deletions(-) create mode 100644 include/cryb/strchrnul.h create mode 100644 include/cryb/wcschrnul.h create mode 100644 lib/core/cryb_strchrnul.c create mode 100644 lib/core/cryb_wcschrnul.c create mode 100644 t/t_strchrnul.c diff --git a/configure.ac b/configure.ac index 903d63d..a39fee5 100644 --- a/configure.ac +++ b/configure.ac @@ -98,8 +98,8 @@ AC_CHECK_FUNCS([ffs ffsl ffsll fls flsl flsll], [], [], [[ #include #endif ]]) -AC_CHECK_FUNCS([strlcat strlcmp strlcpy]) -AC_CHECK_FUNCS([wcslcat wcslcmp wcslcpy]) +AC_CHECK_FUNCS([strchrnul strlcat strlcmp strlcpy]) +AC_CHECK_FUNCS([wcschrnul wcslcat wcslcmp wcslcpy]) # For tracing allocations in unit tests AC_CHECK_HEADERS([sys/uio.h sys/ktrace.h], [], [], [[ diff --git a/include/cryb/Makefile.am b/include/cryb/Makefile.am index 49872e8..40abdb4 100644 --- a/include/cryb/Makefile.am +++ b/include/cryb/Makefile.am @@ -23,10 +23,12 @@ cryb_HEADERS += \ defs.h \ endian.h \ memset_s.h \ + strchrnul.h \ string.h \ strlcat.h \ strlcmp.h \ strlcpy.h \ + wcschrnul.h \ wcslcat.h \ wcslcmp.h \ wcslcpy.h \ diff --git a/include/cryb/strchrnul.h b/include/cryb/strchrnul.h new file mode 100644 index 0000000..45a082a --- /dev/null +++ b/include/cryb/strchrnul.h @@ -0,0 +1,48 @@ +/*- + * Copyright (c) 2017 The University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef CRYB_STRCHRNUL_H_INCLUDED +#define CRYB_STRCHRNUL_H_INCLUDED + +#ifndef CRYB_TO +#include +#endif + +CRYB_BEGIN + +char *cryb_strchrnul(const char *, int); + +#if !HAVE_STRCHRNUL +#undef strchrnul +#define strchrnul(arg, ...) cryb_strchrnul(arg, __VA_ARGS__) +#endif + +CRYB_END + +#endif diff --git a/include/cryb/wcschrnul.h b/include/cryb/wcschrnul.h new file mode 100644 index 0000000..965d9d7 --- /dev/null +++ b/include/cryb/wcschrnul.h @@ -0,0 +1,48 @@ +/*- + * Copyright (c) 2011-2016 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef CRYB_WCSCHRNUL_H_INCLUDED +#define CRYB_WCSCHRNUL_H_INCLUDED + +#ifndef CRYB_TO +#include +#endif + +CRYB_BEGIN + +wchar_t *cryb_wcschrnul(const wchar_t *, wchar_t); + +#if !HAVE_WCSCHRNUL +#undef wcschrnul +#define wcschrnul(...) cryb_wcschrnul(__VA_ARGS__) +#endif + +CRYB_END + +#endif diff --git a/lib/core/Makefile.am b/lib/core/Makefile.am index f42a975..95ddb28 100644 --- a/lib/core/Makefile.am +++ b/lib/core/Makefile.am @@ -5,10 +5,12 @@ lib_LTLIBRARIES = libcryb-core.la libcryb_core_la_SOURCES = \ cryb_assert.c \ cryb_memset_s.c \ + cryb_strchrnul.c \ cryb_string.c \ cryb_strlcat.c \ cryb_strlcmp.c \ cryb_strlcpy.c \ + cryb_wcschrnul.c \ cryb_wcslcat.c \ cryb_wcslcmp.c \ cryb_wcslcpy.c \ diff --git a/lib/core/cryb_strchrnul.c b/lib/core/cryb_strchrnul.c new file mode 100644 index 0000000..dd99abd --- /dev/null +++ b/lib/core/cryb_strchrnul.c @@ -0,0 +1,48 @@ +/*- + * Copyright (c) 2017 The University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +/* + * Like strchr(3), but returns the terminating NUL on failure. + */ + +char * +cryb_strchrnul(const char *s, int c) +{ + + while (*s != '\0' && *s != c) + s++; + return (CRYB_DEQUAL(s)); +} diff --git a/lib/core/cryb_wcschrnul.c b/lib/core/cryb_wcschrnul.c new file mode 100644 index 0000000..79f4ccd --- /dev/null +++ b/lib/core/cryb_wcschrnul.c @@ -0,0 +1,49 @@ +/*- + * Copyright (c) 2017 The University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include +#include +#include + +#include + +/* + * Like wcschr(3), but returns the terminating NUL on failure. + */ + +wchar_t * +cryb_wcschrnul(const wchar_t *ws, wchar_t wc) +{ + + while (*ws != L'\0' && *ws != wc) + ws++; + return (CRYB_DEQUAL(ws)); +} diff --git a/t/.gitignore b/t/.gitignore index 643f395..9bd7ad9 100644 --- a/t/.gitignore +++ b/t/.gitignore @@ -54,6 +54,7 @@ /t_sha384_openssl /t_sha512 /t_sha512_openssl +/t_strchrnul /t_string /t_strlcat /t_strlcmp diff --git a/t/Makefile.am b/t/Makefile.am index 6c06cf5..92967b6 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -94,10 +94,11 @@ TESTS += t_core t_core_LDADD = $(libt) $(libcore) TESTS += t_assert t_assert_LDADD = $(libt) $(libcore) -TESTS += t_ctype t_endian t_memset_s t_strlcat t_strlcmp t_strlcpy +TESTS += t_ctype t_endian t_memset_s t_strchrnul t_strlcat t_strlcmp t_strlcpy t_ctype_LDADD = $(libt) $(libcore) t_endian_LDADD = $(libt) $(libcore) t_memset_s_LDADD = $(libt) $(libcore) +t_strchrnul_LDADD = $(libt) $(libcore) t_strlcat_LDADD = $(libt) $(libcore) t_strlcmp_LDADD = $(libt) $(libcore) t_strlcpy_LDADD = $(libt) $(libcore) diff --git a/t/t_strchrnul.c b/t/t_strchrnul.c new file mode 100644 index 0000000..8b0bbad --- /dev/null +++ b/t/t_strchrnul.c @@ -0,0 +1,166 @@ +/*- + * Copyright (c) 2017 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "cryb/impl.h" + +#include + +#include +#include +#include + +#include + +#include + +typedef char *(*strchrnul_f)(const char *, int); + +#define T_MAGIC1_STR "squeamish" +#define T_MAGIC1_LEN (sizeof(T_MAGIC1_STR) - 1) +#define T_MAGIC2_STR "ossifrage" +#define T_MAGIC2_LEN (sizeof(T_MAGIC2_STR) - 1) +#define T_MAGIC_STR T_MAGIC1_STR " " T_MAGIC2_STR "!" +#define T_MAGIC_LEN (sizeof(T_MAGIC_STR) - 1) + +const char t_empty_str[] = ""; +const char t_magic_str[] = T_MAGIC_STR; + +struct t_case { + const char *desc; + const char *str; + int chr; + const char *out; +}; + +/*************************************************************************** + * Test cases + */ +static struct t_case t_cases[] = { + { + .desc = "zero in empty", + .str = t_empty_str, + .chr = '\0', + .out = t_empty_str, + }, + { + .desc = "non-zero in empty", + .str = t_empty_str, + .chr = 'q', + .out = t_empty_str, + }, + { + .desc = "zero in non-empty", + .str = t_magic_str, + .chr = '\0', + .out = t_magic_str + T_MAGIC_LEN, + }, + { + .desc = "miss in non-empty", + .str = t_magic_str, + .chr = 'Z', + .out = t_magic_str + T_MAGIC_LEN, + }, + { + .desc = "first in non-empty", + .str = t_magic_str, + .chr = T_MAGIC1_STR[0], + .out = t_magic_str, + }, + { + .desc = "middle in non-empty", + .str = t_magic_str, + .chr = ' ', + .out = t_magic_str + T_MAGIC1_LEN, + }, + { + .desc = "last in non-empty", + .str = t_magic_str, + .chr = '!', + .out = t_magic_str + T_MAGIC_LEN - 1, + }, +}; + +/*************************************************************************** + * Test function + */ +static int +t_strchrnul(strchrnul_f func, const struct t_case *t) +{ + + return (t_compare_ptr(t->out, func(t->str, t->chr))); +} + +static int +t_cryb_strchrnul(char **desc CRYB_UNUSED, void *arg) +{ + const struct t_case *t = arg; + + return (t_strchrnul(cryb_strchrnul, t)); +} + +#if HAVE_STRCHRNUL +static int +t_libc_strchrnul(char **desc CRYB_UNUSED, void *arg) +{ + const struct t_case *t = arg; + + return (t_strchrnul(strchrnul, t)); +} +#endif + + +/*************************************************************************** + * Boilerplate + */ + +static int +t_prepare(int argc, char *argv[]) +{ + int i, n; + + (void)argc; + (void)argv; + n = sizeof t_cases / sizeof t_cases[0]; + for (i = 0; i < n; ++i) + t_add_test(t_cryb_strchrnul, &t_cases[i], + "%s (cryb)", t_cases[i].desc); +#if HAVE_STRCHRNUL + for (i = 0; i < n; ++i) + t_add_test(t_libc_strchrnul, &t_cases[i], + "%s (libc)", t_cases[i].desc); +#endif + return (0); +} + +int +main(int argc, char *argv[]) +{ + + t_main(t_prepare, NULL, argc, argv); +} From d77ba2a45830100438436105a2d457434a035ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 21 Aug 2017 19:25:36 +0200 Subject: [PATCH 3/4] Fix unit test for strchrnul(). --- t/t_strchrnul.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/t/t_strchrnul.c b/t/t_strchrnul.c index 8b0bbad..e9fc045 100644 --- a/t/t_strchrnul.c +++ b/t/t_strchrnul.c @@ -41,11 +41,11 @@ typedef char *(*strchrnul_f)(const char *, int); -#define T_MAGIC1_STR "squeamish" +#define T_MAGIC1_STR "Squeamish" #define T_MAGIC1_LEN (sizeof(T_MAGIC1_STR) - 1) -#define T_MAGIC2_STR "ossifrage" +#define T_MAGIC2_STR "ossifragE" #define T_MAGIC2_LEN (sizeof(T_MAGIC2_STR) - 1) -#define T_MAGIC_STR T_MAGIC1_STR " " T_MAGIC2_STR "!" +#define T_MAGIC_STR T_MAGIC1_STR " " T_MAGIC2_STR #define T_MAGIC_LEN (sizeof(T_MAGIC_STR) - 1) const char t_empty_str[] = ""; @@ -89,7 +89,7 @@ static struct t_case t_cases[] = { { .desc = "first in non-empty", .str = t_magic_str, - .chr = T_MAGIC1_STR[0], + .chr = 'S', .out = t_magic_str, }, { @@ -101,7 +101,7 @@ static struct t_case t_cases[] = { { .desc = "last in non-empty", .str = t_magic_str, - .chr = '!', + .chr = 'E', .out = t_magic_str + T_MAGIC_LEN - 1, }, }; From 7744c45b8b81058c6d5dfddba78b4e974fbb51ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Wed, 23 Aug 2017 13:42:00 +0200 Subject: [PATCH 4/4] Remove unnecessary header. --- t/t_strchrnul.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/t/t_strchrnul.c b/t/t_strchrnul.c index e9fc045..d637e17 100644 --- a/t/t_strchrnul.c +++ b/t/t_strchrnul.c @@ -29,8 +29,6 @@ #include "cryb/impl.h" -#include - #include #include #include