diff mbox series

[nft,v2,1/2] datatype: convert chain name from gmp value to string

Message ID 20200704004452.2347-1-pablo@netfilter.org
State Accepted
Delegated to: Pablo Neira
Headers show
Series [nft,v2,1/2] datatype: convert chain name from gmp value to string | expand

Commit Message

Pablo Neira Ayuso July 4, 2020, 12:44 a.m. UTC
Add expr_chain_export() helper function to convert the chain name that
is stored in a gmp value variable to string.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 include/datatype.h |  2 ++
 src/datatype.c     | 21 +++++++++++++--------
 src/datatype.cc    |  0
 3 files changed, 15 insertions(+), 8 deletions(-)
 create mode 100644 src/datatype.cc
diff mbox series

Patch

diff --git a/include/datatype.h b/include/datatype.h
index 04b4892b29ac..1061a389b0f0 100644
--- a/include/datatype.h
+++ b/include/datatype.h
@@ -305,4 +305,6 @@  extern struct error_record *rate_parse(const struct location *loc,
 extern struct error_record *data_unit_parse(const struct location *loc,
 					    const char *str, uint64_t *rate);
 
+extern void expr_chain_export(const struct expr *e, char *chain);
+
 #endif /* NFTABLES_DATATYPE_H */
diff --git a/src/datatype.c b/src/datatype.c
index 90905258bc30..7382307e9909 100644
--- a/src/datatype.c
+++ b/src/datatype.c
@@ -247,20 +247,25 @@  const struct datatype invalid_type = {
 	.print		= invalid_type_print,
 };
 
+void expr_chain_export(const struct expr *e, char *chain_name)
+{
+	unsigned int len;
+
+	len = e->len / BITS_PER_BYTE;
+	if (len >= NFT_CHAIN_MAXNAMELEN)
+		BUG("verdict expression length %u is too large (%u bits max)",
+		    e->len, NFT_CHAIN_MAXNAMELEN * BITS_PER_BYTE);
+
+	mpz_export_data(chain_name, e->value, BYTEORDER_HOST_ENDIAN, len);
+}
+
 static void verdict_jump_chain_print(const char *what, const struct expr *e,
 				     struct output_ctx *octx)
 {
 	char chain[NFT_CHAIN_MAXNAMELEN];
-	unsigned int len;
 
 	memset(chain, 0, sizeof(chain));
-
-	len = e->len / BITS_PER_BYTE;
-	if (len >= sizeof(chain))
-		BUG("verdict expression length %u is too large (%lu bits max)",
-		    e->len, (unsigned long)sizeof(chain) * BITS_PER_BYTE);
-
-	mpz_export_data(chain, e->value, BYTEORDER_HOST_ENDIAN, len);
+	expr_chain_export(e, chain);
 	nft_print(octx, "%s %s", what, chain);
 }
 
diff --git a/src/datatype.cc b/src/datatype.cc
new file mode 100644
index 000000000000..e69de29bb2d1