xref: /titanic_52/usr/src/cmd/ipf/lib/common/count4bits.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: count4bits.c,v 1.1 2002/06/15 04:46:39 darrenr Exp $
7  */
8 
9 #include "ipf.h"
10 
11 
12 /*
13  * count consecutive 1's in bit mask.  If the mask generated by counting
14  * consecutive 1's is different to that passed, return -1, else return #
15  * of bits.
16  */
17 int	count4bits(ip)
18 u_int	ip;
19 {
20 	int cnt = 0, i, j;
21 	u_int ipn;
22 
23 	ip = ipn = ntohl(ip);
24 	for (i = 32; i; i--, ipn *= 2)
25 		if (ipn & 0x80000000)
26 			cnt++;
27 		else
28 			break;
29 	ipn = 0;
30 	for (i = 32, j = cnt; i; i--, j--) {
31 		ipn *= 2;
32 		if (j > 0)
33 			ipn++;
34 	}
35 	if (ipn == ip)
36 		return cnt;
37 	return -1;
38 }
39