From cb6743bace7a0a7377db57a59375a8c81330d7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Sat, 17 Sep 2016 21:08:07 +0200 Subject: [PATCH] Add wide-character versions of strlcat(), strlcmp(), strlcpy(). --- configure.ac | 1 + include/cryb/Makefile.am | 3 +++ include/cryb/wcslcat.h | 44 ++++++++++++++++++++++++++++++++++ include/cryb/wcslcmp.h | 44 ++++++++++++++++++++++++++++++++++ include/cryb/wcslcpy.h | 44 ++++++++++++++++++++++++++++++++++ lib/core/Makefile.am | 5 +++- lib/core/cryb_wcslcat.c | 52 ++++++++++++++++++++++++++++++++++++++++ lib/core/cryb_wcslcmp.c | 50 ++++++++++++++++++++++++++++++++++++++ lib/core/cryb_wcslcpy.c | 49 +++++++++++++++++++++++++++++++++++++ 9 files changed, 291 insertions(+), 1 deletion(-) create mode 100644 include/cryb/wcslcat.h create mode 100644 include/cryb/wcslcmp.h create mode 100644 include/cryb/wcslcpy.h create mode 100644 lib/core/cryb_wcslcat.c create mode 100644 lib/core/cryb_wcslcmp.c create mode 100644 lib/core/cryb_wcslcpy.c diff --git a/configure.ac b/configure.ac index 9eadec9..58caf41 100644 --- a/configure.ac +++ b/configure.ac @@ -68,6 +68,7 @@ AC_CHECK_DECLS([ #endif ]]) AC_CHECK_FUNCS([strlcat strlcmp strlcpy]) +AC_CHECK_FUNCS([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 2a5a5f0..47a4231 100644 --- a/include/cryb/Makefile.am +++ b/include/cryb/Makefile.am @@ -23,6 +23,9 @@ cryb_HEADERS += \ strlcat.h \ strlcmp.h \ strlcpy.h \ + wcslcat.h \ + wcslcmp.h \ + wcslcpy.h \ wstring.h \ \ to.h diff --git a/include/cryb/wcslcat.h b/include/cryb/wcslcat.h new file mode 100644 index 0000000..767fcce --- /dev/null +++ b/include/cryb/wcslcat.h @@ -0,0 +1,44 @@ +/*- + * 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_WCSLCAT_H_INCLUDED +#define CRYB_WCSLCAT_H_INCLUDED + +#ifndef CRYB_TO +#include +#endif + +size_t cryb_wcslcat(wchar_t *, const wchar_t *, size_t); + +#if !HAVE_WCSLCAT +#undef wcslcat +#define wcslcat(arg, ...) cryb_wcslcat(arg, __VA_ARGS__) +#endif + +#endif diff --git a/include/cryb/wcslcmp.h b/include/cryb/wcslcmp.h new file mode 100644 index 0000000..c8bb697 --- /dev/null +++ b/include/cryb/wcslcmp.h @@ -0,0 +1,44 @@ +/*- + * 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_WCSLCMP_H_INCLUDED +#define CRYB_WCSLCMP_H_INCLUDED + +#ifndef CRYB_TO +#include +#endif + +int cryb_wcslcmp(const wchar_t *, const wchar_t *, size_t); + +#if !HAVE_WCSLCMP +#undef wcslcmp +#define wcslcmp(...) cryb_wcslcmp(__VA_ARGS__) +#endif + +#endif diff --git a/include/cryb/wcslcpy.h b/include/cryb/wcslcpy.h new file mode 100644 index 0000000..0920ea2 --- /dev/null +++ b/include/cryb/wcslcpy.h @@ -0,0 +1,44 @@ +/*- + * 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_WCSLCPY_H_INCLUDED +#define CRYB_WCSLCPY_H_INCLUDED + +#ifndef CRYB_TO +#include +#endif + +size_t cryb_wcslcpy(wchar_t *, const wchar_t *, size_t); + +#if !HAVE_WCSLCPY +#undef wcslcpy +#define wcslcpy(arg, ...) cryb_wcslcpy(arg, __VA_ARGS__) +#endif + +#endif diff --git a/lib/core/Makefile.am b/lib/core/Makefile.am index 32dbb8c..654462b 100644 --- a/lib/core/Makefile.am +++ b/lib/core/Makefile.am @@ -7,8 +7,11 @@ libcryb_core_la_SOURCES = \ cryb_memset_s.c \ cryb_string.c \ cryb_strlcat.c \ - cryb_strlcpy.c \ cryb_strlcmp.c \ + cryb_strlcpy.c \ + cryb_wcslcat.c \ + cryb_wcslcmp.c \ + cryb_wcslcpy.c \ cryb_wstring.c EXTRA_DIST = cryb_string_impl.c diff --git a/lib/core/cryb_wcslcat.c b/lib/core/cryb_wcslcat.c new file mode 100644 index 0000000..b59dc84 --- /dev/null +++ b/lib/core/cryb_wcslcat.c @@ -0,0 +1,52 @@ +/*- + * 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. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +/* like wcscat(3), but always NUL-terminates; returns wcslen(src) */ +size_t +cryb_wcslcat(wchar_t *dst, const wchar_t *src, size_t size) +{ + size_t len; + + for (len = 0; *dst && len < size; ++len, ++dst) + /* nothing */; + for (; *src && len + 1 < size; ++len, ++src, ++dst) + *dst = *src; + if (len < size) + *dst = '\0'; + while (*src) + ++len, ++src; + return (len); +} diff --git a/lib/core/cryb_wcslcmp.c b/lib/core/cryb_wcslcmp.c new file mode 100644 index 0000000..f5d32bd --- /dev/null +++ b/lib/core/cryb_wcslcmp.c @@ -0,0 +1,50 @@ +/*- + * Copyright (c) 2014-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. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +/* + * Like wcsncmp, but also asserts that s1[len] == '\0'. Useful in parsers + * to verify that the next len characters of the input (s2) match a + * specific keyword (s1). + */ +int +cryb_wcslcmp(const wchar_t *s1, const wchar_t *s2, size_t len) +{ + + for (; len && *s2; --len, ++s1, ++s2) + if (*s1 != *s2) + return ((unsigned int)*s1 - (unsigned int)*s2); + return ((unsigned int)*s1); +} diff --git a/lib/core/cryb_wcslcpy.c b/lib/core/cryb_wcslcpy.c new file mode 100644 index 0000000..67e04e9 --- /dev/null +++ b/lib/core/cryb_wcslcpy.c @@ -0,0 +1,49 @@ +/*- + * 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. + */ + +#include "cryb/impl.h" + +#include +#include + +#include + +/* like wcscpy(3), but always NUL-terminates; returns wcslen(src) */ +size_t +cryb_wcslcpy(wchar_t *dst, const wchar_t *src, size_t size) +{ + size_t len; + + for (len = 0; *src && size > 1; ++len, --size) + *dst++ = *src++; + *dst = '\0'; + while (*src) + ++len, ++src; + return (len); +}