update OpenBSD string functions

A lot of the 3rd party code in the string library is around 20 years
old and has been worked on since.
I've updated the OpenBSD functions at least.
This commit is contained in:
Guilherme Janczak 2021-12-31 09:31:59 +00:00 committed by Corinna Vinschen
parent 484d2ebf8d
commit 8e71066cb2
4 changed files with 143 additions and 207 deletions

View File

@ -1,73 +1,57 @@
/* $OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $ */ /* $OpenBSD: strlcat.c,v 1.19 2019/01/25 00:19:25 millert Exp $ */
/* /*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
* All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Permission to use, copy, modify, and distribute this software for any
* modification, are permitted provided that the following conditions * purpose with or without fee is hereby granted, provided that the above
* are met: * copyright notice and this permission notice appear in all copies.
* 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 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* 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.
*/ */
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h> #include <sys/types.h>
#include <string.h> #include <string.h>
/* /*
* Appends src to string dst of size siz (unlike strncat, siz is the * Appends src to string dst of size dsize (unlike strncat, dsize is the
* full size of dst, not space left). At most siz-1 characters * full size of dst, not space left). At most dsize-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)). * will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)). * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
* If retval >= siz, truncation occurred. * If retval >= dsize, truncation occurred.
*/ */
size_t size_t
strlcat (char *dst, strlcat (char *dst,
const char *src, const char *src,
size_t siz) size_t dsize)
{ {
register char *d = dst; const char *odst = dst;
register const char *s = src; const char *osrc = src;
register size_t n = siz; size_t n = dsize;
size_t dlen; size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */ /* Find the end of dst and adjust bytes left but don't go past end. */
while (n-- != 0 && *d != '\0') while (n-- != 0 && *dst != '\0')
d++; dst++;
dlen = d - dst; dlen = dst - odst;
n = siz - dlen; n = dsize - dlen;
if (n == 0) if (n-- == 0)
return(dlen + strlen(s)); return(dlen + strlen(src));
while (*s != '\0') { while (*src != '\0') {
if (n != 1) { if (n != 0) {
*d++ = *s; *dst++ = *src;
n--; n--;
} }
s++; src++;
} }
*d = '\0'; *dst = '\0';
return(dlen + (s - src)); /* count does not include NUL */ return(dlen + (src - osrc)); /* count does not include NUL */
} }

View File

@ -1,69 +1,52 @@
/* $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $ */ /* $OpenBSD: strlcpy.c,v 1.16 2019/01/25 00:19:25 millert Exp $ */
/* /*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
* All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Permission to use, copy, modify, and distribute this software for any
* modification, are permitted provided that the following conditions * purpose with or without fee is hereby granted, provided that the above
* are met: * copyright notice and this permission notice appear in all copies.
* 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 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* 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.
*/ */
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h> #include <sys/types.h>
#include <string.h> #include <string.h>
/* /*
* Copy src to string dst of size siz. At most siz-1 characters * Copy string src to buffer dst of size dsize. At most dsize-1
* will be copied. Always NUL terminates (unless siz == 0). * chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns strlen(src); if retval >= siz, truncation occurred. * Returns strlen(src); if retval >= dsize, truncation occurred.
*/ */
size_t size_t
strlcpy (char *dst, strlcpy (char *dst,
const char *src, const char *src,
size_t siz) size_t dsize)
{ {
register char *d = dst; const char *osrc = src;
register const char *s = src; size_t nleft = dsize;
register size_t n = siz;
/* Copy as many bytes as will fit */ /* Copy as many bytes as will fit. */
if (n != 0 && --n != 0) { if (nleft != 0) {
do { while (--nleft != 0) {
if ((*d++ = *s++) == 0) if ((*dst++ = *src++) == '\0')
break; break;
} while (--n != 0); }
} }
/* Not enough room in dst, add NUL and traverse rest of src */ /* Not enough room in dst, add NUL and traverse rest of src. */
if (n == 0) { if (nleft == 0) {
if (siz != 0) if (dsize != 0)
*d = '\0'; /* NUL-terminate dst */ *dst = '\0'; /* NUL-terminate dst */
while (*s++) while (*src++)
; ;
} }
return(s - src - 1); /* count does not include NUL */ return(src - osrc - 1); /* count does not include NUL */
} }

View File

@ -27,74 +27,60 @@ PORTABILITY
No supporting OS subroutines are required. No supporting OS subroutines are required.
*/ */
/* $NetBSD: wcslcat.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ */ /* $OpenBSD: wcslcat.c,v 1.7 2019/01/25 00:19:25 millert Exp $ */
/* from OpenBSD: strlcat.c,v 1.3 2000/11/24 11:10:02 itojun Exp */
/* /*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
* All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Permission to use, copy, modify, and distribute this software for any
* modification, are permitted provided that the following conditions * purpose with or without fee is hereby granted, provided that the above
* are met: * copyright notice and this permission notice appear in all copies.
* 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 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* 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 <_ansi.h> #include <sys/types.h>
#include <wchar.h> #include <wchar.h>
/* /*
* Appends src to string dst of size siz (unlike wcsncat, siz is the * Appends src to string dst of size dsize (unlike strncat, dsize is the
* full size of dst, not space left). At most siz-1 characters * full size of dst, not space left). At most dsize-1 characters
* will be copied. Always NUL terminates (unless siz == 0). * will be copied. Always NUL terminates (unless dsize <= wcslen(dst)).
* Returns wcslen(initial dst) + wcslen(src); if retval >= siz, * Returns wcslen(src) + MIN(dsize, wcslen(initial dst)).
* truncation occurred. * If retval >= siz, truncation occurred.
*/ */
size_t size_t
wcslcat (wchar_t *dst, wcslcat (wchar_t *dst,
const wchar_t *src, const wchar_t *src,
size_t siz) size_t dsize)
{ {
wchar_t *d = dst; const wchar_t *odst = dst;
const wchar_t *s = src; const wchar_t *osrc = src;
size_t n = siz; size_t n = dsize;
size_t dlen; size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */ /* Find the end of dst and adjust bytes left but don't go past end. */
while (*d != '\0' && n-- != 0) while (n-- != 0 && *dst != L'\0')
d++; dst++;
dlen = d - dst; dlen = dst - odst;
n = siz - dlen; n = dsize - dlen;
if (n == 0) if (n-- == 0)
return (dlen + wcslen (s)); return(dlen + wcslen(src));
while (*s != '\0') while (*src != L'\0') {
{ if (n != 0) {
if (n != 1) *dst++ = *src;
{
*d++ = *s;
n--; n--;
} }
s++; src++;
} }
*d = '\0'; *dst = L'\0';
return (dlen + (s - src)); /* count does not include NUL */ return(dlen + (src - osrc)); /* count does not include NUL */
} }

View File

@ -23,72 +23,55 @@ PORTABILITY
No supporting OS subroutines are required. No supporting OS subroutines are required.
*/ */
/* $NetBSD: wcslcpy.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ */ /* $OpenBSD: wcslcpy.c,v 1.8 2019/01/25 00:19:25 millert Exp $ */
/* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp */
/* /*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
* All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Permission to use, copy, modify, and distribute this software for any
* modification, are permitted provided that the following conditions * purpose with or without fee is hereby granted, provided that the above
* are met: * copyright notice and this permission notice appear in all copies.
* 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 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* 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 <_ansi.h> #include <sys/types.h>
#include <wchar.h> #include <wchar.h>
/* /*
* Copy src to string dst of size siz. At most siz-1 characters * Copy string src to buffer dst of size dsize. At most dsize-1
* will be copied. Always NUL terminates (unless siz == 0). * chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns wcslen(src); if retval >= siz, truncation occurred. * Returns wcslen(src); if retval >= dsize, truncation occurred.
*/ */
size_t size_t
wcslcpy (wchar_t *dst, wcslcpy (wchar_t *dst,
const wchar_t *src, const wchar_t *src,
size_t siz) size_t dsize)
{ {
wchar_t *d = dst; const wchar_t *osrc = src;
const wchar_t *s = src; size_t nleft = dsize;
size_t n = siz;
/* Copy as many bytes as will fit */ /* Copy as many bytes as will fit. */
if (n != 0 && --n != 0) if (nleft != 0) {
{ while (--nleft != 0) {
do if ((*dst++ = *src++) == L'\0')
{
if ((*d++ = *s++) == 0)
break; break;
} }
while (--n != 0);
} }
/* Not enough room in dst, add NUL and traverse rest of src */ /* Not enough room in dst, add NUL and traverse rest of src. */
if (n == 0) if (nleft == 0) {
{ if (dsize != 0)
if (siz != 0) *dst = L'\0'; /* NUL-terminate dst */
*d = '\0'; /* NUL-terminate dst */ while (*src++)
while (*s++)
; ;
} }
return (s - src - 1); /* count does not include NUL */ return(src - osrc - 1); /* count does not include NUL */
} }