xref: /freebsd/sbin/ipf/libipf/fill6bits.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  * $Id$
8  */
9 
10 #include "ipf.h"
11 
12 
13 void
14 fill6bits(int bits, u_int *msk)
15 {
16 	if (bits == 0) {
17 		msk[0] = 0;
18 		msk[1] = 0;
19 		msk[2] = 0;
20 		msk[3] = 0;
21 		return;
22 	}
23 
24 	msk[0] = 0xffffffff;
25 	msk[1] = 0xffffffff;
26 	msk[2] = 0xffffffff;
27 	msk[3] = 0xffffffff;
28 
29 	if (bits == 128)
30 		return;
31 	if (bits > 96) {
32 		msk[3] = htonl(msk[3] << (128 - bits));
33 	} else if (bits > 64) {
34 		msk[3] = 0;
35 		msk[2] = htonl(msk[2] << (96 - bits));
36 	} else if (bits > 32) {
37 		msk[3] = 0;
38 		msk[2] = 0;
39 		msk[1] = htonl(msk[1] << (64 - bits));
40 	} else {
41 		msk[3] = 0;
42 		msk[2] = 0;
43 		msk[1] = 0;
44 		msk[0] = htonl(msk[0] << (32 - bits));
45 	}
46 }
47