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/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 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..d637e17 --- /dev/null +++ b/t/t_strchrnul.c @@ -0,0 +1,164 @@ +/*- + * 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 + +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 = 'S', + .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 = 'E', + .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); +}