| 1 |
/* -*- coding: utf-8 -*- |
| 2 |
* ---------------------------------------------------------------------- |
| 3 |
* Copyright © 2012, libcorkipset authors |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Please see the COPYING file in this distribution for license details. |
| 7 |
* ---------------------------------------------------------------------- |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef LIBCORK_IPSET_BITS_H |
| 11 |
#define LIBCORK_IPSET_BITS_H |
| 12 |
|
| 13 |
#include <libcork/core.h> |
| 14 |
|
| 15 |
/*----------------------------------------------------------------------- |
| 16 |
* Bit arrays |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* Extract the byte that contains a particular bit in an array. |
| 21 |
*/ |
| 22 |
#define IPSET_BIT_GET_BYTE(array, i) \ |
| 23 |
(((uint8_t *) (array))[(i) / 8]) |
| 24 |
|
| 25 |
/** |
| 26 |
* Create a bit mask that extracts a particular bit from the byte that |
| 27 |
* it lives in. |
| 28 |
*/ |
| 29 |
#define IPSET_BIT_ON_MASK(i) \ |
| 30 |
(0x80 >> ((i) % 8)) |
| 31 |
|
| 32 |
/** |
| 33 |
* Create a bit mask that extracts everything except for a particular |
| 34 |
* bit from the byte that it lives in. |
| 35 |
*/ |
| 36 |
#define IPSET_BIT_NEG_MASK(i) \ |
| 37 |
(~IPSET_BIT_ON_MASK(i)) |
| 38 |
|
| 39 |
/** |
| 40 |
* Return whether a particular bit is set in a byte array. Bits are |
| 41 |
* numbered from 0, in a big-endian order. |
| 42 |
*/ |
| 43 |
#define IPSET_BIT_GET(array, i) \ |
| 44 |
((IPSET_BIT_GET_BYTE(array, i) & \ |
| 45 |
IPSET_BIT_ON_MASK(i)) != 0) |
| 46 |
|
| 47 |
/** |
| 48 |
* Set (or unset) a particular bit is set in a byte array. Bits are |
| 49 |
* numbered from 0, in a big-endian order. |
| 50 |
*/ |
| 51 |
#define IPSET_BIT_SET(array, i, val) \ |
| 52 |
(IPSET_BIT_GET_BYTE(array, i) = \ |
| 53 |
(IPSET_BIT_GET_BYTE(array, i) & IPSET_BIT_NEG_MASK(i)) \ |
| 54 |
| ((val)? IPSET_BIT_ON_MASK(i): 0)) |
| 55 |
|
| 56 |
|
| 57 |
#endif /* LIBCORK_IPSET_BITS_H */ |