Add strchrnul(), wcschrnul(), and tests for the former.

This commit is contained in:
Dag-Erling Smørgrav 2017-08-21 18:31:29 +02:00
parent 85f4aa359a
commit 5c8745138e
10 changed files with 368 additions and 3 deletions

View file

@ -98,8 +98,8 @@ AC_CHECK_FUNCS([ffs ffsl ffsll fls flsl flsll], [], [], [[
#include <strings.h>
#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], [], [], [[

View file

@ -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 \

48
include/cryb/strchrnul.h Normal file
View file

@ -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 <cryb/to.h>
#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

48
include/cryb/wcschrnul.h Normal file
View file

@ -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 <cryb/to.h>
#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

View file

@ -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 \

48
lib/core/cryb_strchrnul.c Normal file
View file

@ -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 <stddef.h>
#include <stdint.h>
#include <cryb/strchrnul.h>
/*
* 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));
}

49
lib/core/cryb_wcschrnul.c Normal file
View file

@ -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 <stddef.h>
#include <stdint.h>
#include <wchar.h>
#include <cryb/wcschrnul.h>
/*
* 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));
}

1
t/.gitignore vendored
View file

@ -54,6 +54,7 @@
/t_sha384_openssl
/t_sha512
/t_sha512_openssl
/t_strchrnul
/t_string
/t_strlcat
/t_strlcmp

View file

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

166
t/t_strchrnul.c Normal file
View file

@ -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 <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <cryb/strchrnul.h>
#include <cryb/test.h>
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);
}