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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
/* lset_t routines, for libreswan
*
* Copyright (C) 2012-2017 Paul Wouters <pwouters@redhat.com>
* Copyright (C) 2012 Avesh Agarwal <avagarwa@redhat.com>
* Copyright (C) 1998-2002,2015 D. Hugh Redelmeier.
* Copyright (C) 2016-2017 Andrew Cagney
* Copyright (C) 2017 Vukasin Karadzic <vukasin.karadzic@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "lset.h"
#include "lswlog.h" /* for passert() */
#include "enum_names.h"
#include "sparse_names.h"
/* test a set by seeing if all bits have names */
bool test_lset(const struct enum_names *en, lset_t val)
{
for (unsigned e = 0; val != 0; e++) {
lset_t bit = LELEM(e);
if (val & bit) {
enum_buf b;
if (!enum_name(en, e, &b)) {
return false;
}
}
val &= ~bit;
}
return true;
}
static size_t jam_lset_pretty(struct jambuf *buf, enum_names *en, lset_t val,
const char *separator, bool shorten)
{
if (val == LEMPTY) {
return jam(buf, "none");
}
size_t s = 0;
const char *sep = "";
const struct enum_names *range = NULL;
const char *prefix = NULL;
for (unsigned e = 0; val != 0; e++) {
lset_t bit = LELEM(e);
if (val & bit) {
/* range!=NULL implies e>=range->en_first */
if (range == NULL || e > range->en_last) {
/* try to find a new range */
range = enum_range(en, e, &prefix);
}
s += jam_string(buf, sep);
sep = separator;
/* can handle range==NULL */
const char *name = enum_range_name(range, e, prefix, shorten);
if (name == NULL) {
/* No name for this bit, use hex. */
s += jam(buf, "0x" PRI_LSET, bit);
} else {
s += jam_string(buf, name);
}
}
val &= ~bit;
}
return s;
}
#define LSET_SEPARATOR "+"
size_t jam_lset(struct jambuf *buf, enum_names *en, lset_t val)
{
return jam_lset_pretty(buf, en, val, LSET_SEPARATOR, /*shorten?*/false);
}
const char *str_lset(enum_names *en, lset_t val, lset_buf *out)
{
struct jambuf buf = ARRAY_AS_JAMBUF(out->buf);
jam_lset(&buf, en, val);
return out->buf;
}
size_t jam_lset_short(struct jambuf *buf, enum_names *en,
const char *separator, lset_t val)
{
return jam_lset_pretty(buf, en, val, separator, /*shorten?*/true);
}
const char *str_lset_short(enum_names *en, const char *separator,
lset_t val, lset_buf *out)
{
struct jambuf buf = ARRAY_AS_JAMBUF(out->buf);
jam_lset_short(&buf, en, separator, val);
return out->buf;
}
size_t jam_sparse_lset(struct jambuf *buf, const struct sparse_names *sd, lset_t val)
{
if (val == LEMPTY) {
return jam(buf, "none");
}
size_t s = 0;
const char *sep = "";
for (unsigned e = 0; val != 0; e++) {
lset_t bit = LELEM(e);
if (val & bit) {
s += jam_string(buf, sep);
sep = LSET_SEPARATOR;
/* can return NULL */
name_buf nb;
if (sparse_short(sd, bit, &nb)) {
s += jam_string(buf, nb.buf);
} else {
/* No name for this bit, use hex. */
s += jam(buf, "0x" PRI_LSET, bit);
}
}
val &= ~bit;
}
return s;
}
|