summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/lib/count4bits.c
blob: 56b4c86aea0eb1abf074630fcc6c7964520ccce0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*	$FreeBSD$	*/

/*
 * Copyright (C) 1993-2001 by Darren Reed.
 *
 * See the IPFILTER.LICENCE file for details on licencing.
 *
 * Id: count4bits.c,v 1.1 2002/06/15 04:46:39 darrenr Exp
 */

#include "ipf.h"


/*
 * count consecutive 1's in bit mask.  If the mask generated by counting
 * consecutive 1's is different to that passed, return -1, else return #
 * of bits.
 */
int	count4bits(ip)
u_int	ip;
{
	int cnt = 0, i, j;
	u_int ipn;

	ip = ipn = ntohl(ip);
	for (i = 32; i; i--, ipn *= 2)
		if (ipn & 0x80000000)
			cnt++;
		else
			break;
	ipn = 0;
	for (i = 32, j = cnt; i; i--, j--) {
		ipn *= 2;
		if (j > 0)
			ipn++;
	}
	if (ipn == ip)
		return cnt;
	return -1;
}
OpenPOWER on IntegriCloud