features: add 'feature_bit_sub', which will subtract/unset bits

Given a two sets of featurebits, unset the featurebits in the first set
that are set in the second set
This commit is contained in:
niftynei
2020-10-22 13:18:38 -05:00
committed by neil saitug
parent 0871bf0999
commit ddc9500a64
3 changed files with 66 additions and 0 deletions

View File

@@ -205,6 +205,35 @@ bool feature_set_or(struct feature_set *a,
return true;
}
bool feature_set_sub(struct feature_set *a,
const struct feature_set *b TAKES)
{
/* Check first, before we change anything! */
for (size_t i = 0; i < ARRAY_SIZE(b->bits); i++) {
for (size_t j = 0; j < tal_bytelen(b->bits[i])*8; j++) {
if (feature_is_set(b->bits[i], j)
&& !feature_offered(a->bits[i], j)) {
if (taken(b))
tal_free(b);
return false;
}
}
}
for (size_t i = 0; i < ARRAY_SIZE(a->bits); i++) {
for (size_t j = 0; j < tal_bytelen(b->bits[i])*8; j++) {
if (feature_is_set(b->bits[i], j))
clear_feature_bit(a->bits[i], j);
}
trim_features(&a->bits[i]);
}
if (taken(b))
tal_free(b);
return true;
}
/* BOLT #1:
*
* All data fields are unsigned big-endian unless otherwise specified.