From 737a6fa55c1716643bb12afb0363decc65975ffe Mon Sep 17 00:00:00 2001 From: Sascha Wildner Date: Sun, 22 Nov 2015 18:19:19 +0100 Subject: [PATCH] kernel/libkern: Add strnlen() (from FreeBSD) and use it. --- sys/conf/files | 1 + sys/libkern/strnlen.c | 41 +++++++++++++++++++++++++++++++++++++++++ sys/net/sppp/if_spppsubr.c | 31 ++++++++++--------------------- sys/sys/libkern.h | 1 + 4 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 sys/libkern/strnlen.c diff --git a/sys/conf/files b/sys/conf/files index 13acd6ef89..ac24134240 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1915,6 +1915,7 @@ libkern/strcpy.c standard libkern/strlcat.c standard libkern/strlcpy.c standard libkern/strlen.c standard +libkern/strnlen.c standard libkern/strncat.c standard libkern/strncmp.c standard libkern/strncpy.c standard diff --git a/sys/libkern/strnlen.c b/sys/libkern/strnlen.c new file mode 100644 index 0000000000..ac495eecdc --- /dev/null +++ b/sys/libkern/strnlen.c @@ -0,0 +1,41 @@ +/*- + * Copyright (c) 2009 David Schultz + * 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. + * + * 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. + * + * $FreeBSD: head/sys/libkern/strnlen.c 226029 2011-10-04 23:53:47Z jkim $ + */ + +#include + +size_t +strnlen(const char *s, size_t maxlen) +{ + size_t len; + + for (len = 0; len < maxlen; len++, s++) { + if (!*s) + break; + } + return (len); +} diff --git a/sys/net/sppp/if_spppsubr.c b/sys/net/sppp/if_spppsubr.c index 1cc4358192..02a9748a0b 100644 --- a/sys/net/sppp/if_spppsubr.c +++ b/sys/net/sppp/if_spppsubr.c @@ -348,7 +348,6 @@ static const char *sppp_phase_name(enum ppp_phase phase); static const char *sppp_proto_name(u_short proto); static const char *sppp_state_name(int state); static int sppp_params(struct sppp *sp, u_long cmd, void *data); -static int sppp_strnlen(u_char *p, int max); static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask); static void sppp_keepalive(void *dummy); @@ -3947,7 +3946,7 @@ sppp_chap_input(struct sppp *sp, struct mbuf *m) MD5Init(&ctx); MD5Update(&ctx, &h->ident, 1); MD5Update(&ctx, sp->myauth.secret, - sppp_strnlen(sp->myauth.secret, AUTHKEYLEN)); + strnlen(sp->myauth.secret, AUTHKEYLEN)); MD5Update(&ctx, value, value_len); MD5Final(digest, &ctx); dsize = sizeof digest; @@ -3955,7 +3954,7 @@ sppp_chap_input(struct sppp *sp, struct mbuf *m) sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident, sizeof dsize, (const char *)&dsize, sizeof digest, digest, - (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN), + (size_t)strnlen(sp->myauth.name, AUTHNAMELEN), sp->myauth.name, 0); break; @@ -4032,14 +4031,14 @@ sppp_chap_input(struct sppp *sp, struct mbuf *m) h->ident, sp->confid[IDX_CHAP]); break; } - if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) + if (name_len != strnlen(sp->hisauth.name, AUTHNAMELEN) || bcmp(name, sp->hisauth.name, name_len) != 0) { log(LOG_INFO, SPP_FMT "chap response, his name ", SPP_ARGS(ifp)); sppp_print_string(name, name_len); log(-1, " != expected "); sppp_print_string(sp->hisauth.name, - sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)); + strnlen(sp->hisauth.name, AUTHNAMELEN)); log(-1, "\n"); } if (debug) { @@ -4067,7 +4066,7 @@ sppp_chap_input(struct sppp *sp, struct mbuf *m) MD5Init(&ctx); MD5Update(&ctx, &h->ident, 1); MD5Update(&ctx, sp->hisauth.secret, - sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN)); + strnlen(sp->hisauth.secret, AUTHKEYLEN)); MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN); MD5Final(digest, &ctx); @@ -4274,7 +4273,7 @@ sppp_chap_scr(struct sppp *sp) sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP], sizeof clen, (const char *)&clen, (size_t)AUTHKEYLEN, sp->myauth.challenge, - (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN), + (size_t)strnlen(sp->myauth.name, AUTHNAMELEN), sp->myauth.name, 0); } @@ -4352,8 +4351,8 @@ sppp_pap_input(struct sppp *sp, struct mbuf *m) sppp_print_string((char*)passwd, passwd_len); log(-1, ">\n"); } - if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) || - passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) || + if (name_len != strnlen(sp->hisauth.name, AUTHNAMELEN) || + passwd_len != strnlen(sp->hisauth.secret, AUTHKEYLEN) || bcmp(name, sp->hisauth.name, name_len) != 0 || bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) { /* action scn, tld */ @@ -4591,8 +4590,8 @@ sppp_pap_scr(struct sppp *sp) u_char idlen, pwdlen; sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP]; - pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN); - idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN); + pwdlen = strnlen(sp->myauth.secret, AUTHKEYLEN); + idlen = strnlen(sp->myauth.name, AUTHNAMELEN); sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP], sizeof idlen, (const char *)&idlen, @@ -5320,16 +5319,6 @@ sppp_dotted_quad(u_long addr) return s; } -static int -sppp_strnlen(u_char *p, int max) -{ - int len; - - for (len = 0; len < max && *p; ++p) - ++len; - return len; -} - /* a dummy, used to drop uninteresting events */ static void sppp_null(struct sppp *unused) diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h index 775f70996c..82cabec6f7 100644 --- a/sys/sys/libkern.h +++ b/sys/sys/libkern.h @@ -132,6 +132,7 @@ size_t strlen (const char *); int strncmp (const char *, const char *, size_t); int strncasecmp (const char *, const char *, size_t); char *strncpy (char * __restrict, const char * __restrict, size_t); +size_t strnlen(const char *, size_t); char *strsep(char **, const char *); char *strstr(const char *, const char *); int _kfnmatch(const char *, const char *, int, int); -- 2.11.4.GIT