kernel/libkern: Add strnlen() (from FreeBSD) and use it.
authorSascha Wildner <saw@online.de>
Sun, 22 Nov 2015 17:19:19 +0000 (18:19 +0100)
committerSascha Wildner <saw@online.de>
Sun, 22 Nov 2015 18:09:51 +0000 (19:09 +0100)
sys/conf/files
sys/libkern/strnlen.c [new file with mode: 0644]
sys/net/sppp/if_spppsubr.c
sys/sys/libkern.h

index 13acd6e..ac24134 100644 (file)
@@ -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 (file)
index 0000000..ac495ee
--- /dev/null
@@ -0,0 +1,41 @@
+/*-
+ * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
+ * 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 <sys/libkern.h>
+
+size_t
+strnlen(const char *s, size_t maxlen)
+{
+       size_t len;
+
+       for (len = 0; len < maxlen; len++, s++) {
+               if (!*s)
+                       break;
+       }
+       return (len);
+}
index 1cc4358..02a9748 100644 (file)
@@ -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)
index 775f709..82cabec 100644 (file)
@@ -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);