Simplify checking/parsing of strings using strtoul(), isdigit(), etc.

This commit is contained in:
Archie Cobbs 1999-11-02 23:18:01 +00:00
parent 44b4dd855d
commit 2b70adcbbe
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=52816
4 changed files with 37 additions and 65 deletions

View File

@ -55,6 +55,7 @@
#include <sys/queue.h>
#include <sys/mbuf.h>
#include <sys/socketvar.h>
#include <machine/limits.h>
#include <net/netisr.h>
@ -410,37 +411,20 @@ ng_findname(node_p this, const char *name)
static ng_ID_t
ng_decodeidname(const char *name)
{
ng_ID_t val;
int k, len;
const int len = strlen(name);
const char *eptr;
u_long val;
/* Basic checks */
for (len = 0; name[len] != '\0'; len++) {
const char ch = name[len];
/* Check for proper length, brackets, no leading junk */
if (len < 3 || name[0] != '[' || name[len - 1] != ']'
|| !isxdigit(name[1]) || name[1] == '0') /* "[0]" is not valid */
return (0);
if (len == 0) {
if (ch != '[')
return (NULL);
} else if (name[len + 1] == '\0') {
if (ch != ']')
return (NULL);
} else if (!((ch >= '0' && ch <= '9')
|| (ch >= 'a' && ch <= 'f')
|| (ch >= 'A' && ch <= 'F')))
return (NULL);
}
if (len < 3 || len > (sizeof(val) * 2) + 2 || name[1] == '0')
return (NULL);
/* Convert number to binary */
for (val = 0, k = 1; k < len - 1; k++) {
const char ch = name[k];
if (ch <= '9')
val = (val << 4) | ((ch - '0') & 0xf);
else
val = (val << 4) | (((ch & 0xdf) - 'A' + 10) & 0xf);
}
return (val);
/* Decode number */
val = strtoul(name + 1, &eptr, 16);
if (eptr - name != len - 1 || val == ULONG_MAX)
return (0);
return (ng_ID_t)val;
}
/*

View File

@ -246,9 +246,7 @@ static int
ngfrm_newhook(node_p node, hook_p hook, const char *name)
{
const sc_p sc = node->private;
const char *cp;
char c = '\0';
int digits = 0;
const char *cp, *eptr;
int dlci = 0;
int ctxnum;
@ -285,13 +283,12 @@ ngfrm_newhook(node_p node, hook_p hook, const char *name)
/* Must be a dlci hook at this point */
cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI);
while ((digits < 5) && ((c = *cp++) >= '0') && (c <= '9')) {
dlci *= 10;
dlci += c - '0';
digits++;
}
if ((c != 0) || (digits == 5) || (dlci < 0) || (dlci > 1023))
if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
return (EINVAL);
dlci = (int)strtoul(cp, &eptr, 10);
if (*eptr != '\0' || dlci < 0 || dlci > 1023)
return (EINVAL);
/*
* We have a dlci, now either find it, or allocate it. It's possible
* that we might have seen packets for it already and made an entry

View File

@ -60,14 +60,14 @@
#include <netgraph/ng_vjc.h>
#define PROT_VALID(p) (((p) & 0x0101) == 0x0001)
#define PROT_COMPRESSIBLE(p) (((p) & 0xff00) == 0x0000)
#define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000)
/* Some PPP protocol numbers we're interested in */
#define PROT_APPLETALK 0x0029
#define PROT_COMPD 0x00fd
#define PROT_CRYPTD 0x0053
#define PROT_IP 0x0021
#define PROT_IPX 0x002B
#define PROT_IPX 0x002b
#define PROT_MP 0x003d
#define PROT_VJCOMP 0x002d
#define PROT_VJUNCOMP 0x002f
@ -267,22 +267,13 @@ ng_ppp_newhook(node_p node, hook_p hook, const char *name)
/* Figure out which hook it is */
if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */
strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
int gotDigit = 0;
const char *cp;
const char *cp, *eptr;
for (cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
*cp >= '0' && *cp <= '9'; cp++) {
if (!gotDigit) {
if (*cp == '0') /* no leading zeros */
return (EINVAL);
linkNum = *cp - '0';
gotDigit = 1;
} else
linkNum = (linkNum * 10) + (*cp - '0');
if (linkNum >= NG_PPP_MAX_LINKS)
return (EINVAL);
}
if (!gotDigit || *cp != '\0')
cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
return (EINVAL);
linkNum = (int)strtoul(cp, &eptr, 10);
if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
return (EINVAL);
hookPtr = &priv->links[linkNum];
hookIndex = ~linkNum;
@ -1292,7 +1283,7 @@ ng_ppp_intcmp(const void *v1, const void *v2)
static struct mbuf *
ng_ppp_addproto(struct mbuf *m, int proto, int compOK)
{
int psize = (PROT_COMPRESSIBLE(proto) && compOK) ? 1 : 2;
int psize = (PROT_COMPRESSABLE(proto) && compOK) ? 1 : 2;
/* Add protocol number */
M_PREPEND(m, psize, M_NOWAIT);

View File

@ -176,17 +176,17 @@ ng_xxx_newhook(node_p node, hook_p hook, const char *name)
* hooks start with 'dlci' and have a decimal trailing channel
* number up to 4 digits Use the leadin defined int he associated .h
* file. */
if (strncmp(name, NG_XXX_HOOK_DLCI_LEADIN, 4) == 0) {
if (strncmp(name,
NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
const char *eptr;
cp = name + sizeof(NG_XXX_HOOK_DLCI_LEADIN);
while ((digits < 5)
&& ((c = *cp++) > '0') && (c < '9')) {
dlci *= 10;
dlci += c - '0';
digits++;
}
if ((c != 0) || (digits == 5)
|| (dlci <= 0) || (dlci > 1023))
if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
return (EINVAL);
dlci = (int)strtoul(cp, &eptr, 10);
if (*eptr != '\0' || dlci < 0 || dlci > 1023)
return (EINVAL);
/* We have a dlci, now either find it, or allocate it */
for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
if (xxxp->channel[chan].dlci == dlci)