[libnftables PATCH 1/2] set_elem: xml: avoid code duplication

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch removes 3x code duplication in the XML snprintf function.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@xxxxxxxxx>
---
 src/set_elem.c |   32 +++++++++-----------------------
 1 file changed, 9 insertions(+), 23 deletions(-)

diff --git a/src/set_elem.c b/src/set_elem.c
index a9e0751..885893b 100644
--- a/src/set_elem.c
+++ b/src/set_elem.c
@@ -519,7 +519,7 @@ static int nft_set_elem_snprintf_default(char *buf, size_t size,
 static int nft_set_elem_snprintf_xml(char *buf, size_t size,
 				     struct nft_set_elem *e, uint32_t flags)
 {
-	int ret, len = size, offset = 0;
+	int ret, len = size, offset = 0, type = DATA_NONE;
 
 	ret = snprintf(buf, size, "<set_elem>"
 				"<flags>%u</flags><key>",
@@ -533,33 +533,19 @@ static int nft_set_elem_snprintf_xml(char *buf, size_t size,
 	ret = snprintf(buf+offset, size, "</key>");
 	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 
-	if (e->flags & (1 << NFT_SET_ELEM_ATTR_DATA)) {
-		ret = snprintf(buf+offset, size, "<data>");
-		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-
-		ret = nft_data_reg_snprintf(buf+offset, len, &e->data,
-					    NFT_RULE_O_XML, flags, DATA_VALUE);
-		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-
-		ret = snprintf(buf+offset, size, "</data>");
-		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-	} else if (e->flags & (1 << NFT_SET_ELEM_ATTR_VERDICT)) {
-		ret = snprintf(buf+offset, size, "<data>");
-		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-
-		ret = nft_data_reg_snprintf(buf+offset, len, &e->data,
-					    NFT_RULE_O_XML, flags,
-					    DATA_VERDICT);
-		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	if (e->flags & (1 << NFT_SET_ELEM_ATTR_DATA))
+		type = DATA_VALUE;
+	else if (e->flags & (1 << NFT_SET_ELEM_ATTR_CHAIN))
+		type = DATA_CHAIN;
+	else if (e->flags & (1 << NFT_SET_ELEM_ATTR_VERDICT))
+		type = DATA_VERDICT;
 
-		ret = snprintf(buf+offset, size, "</data>");
-		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-	} else if (e->flags & (1 << NFT_SET_ELEM_ATTR_CHAIN)) {
+	if (type != DATA_NONE) {
 		ret = snprintf(buf+offset, size, "<data>");
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 
 		ret = nft_data_reg_snprintf(buf+offset, len, &e->data,
-					    NFT_RULE_O_XML, flags, DATA_CHAIN);
+					    NFT_RULE_O_XML, flags, type);
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 
 		ret = snprintf(buf+offset, size, "</data>");

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux