From d844fa06e43bc80487acfe5647cef4d4994c95fd Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Thu, 4 Jul 2013 14:50:22 +0200 Subject: src: consolidate XML parsing of expressions via nft_mxml_expr_parse Move common code for XML parsing of expressions to the new nft_mxml_expr_parse function. This patch reduces the XML parsing code in 300 LOC. Signed-off-by: Pablo Neira Ayuso --- src/expr/lookup.c | 42 +++++++----------------------------------- 1 file changed, 7 insertions(+), 35 deletions(-) (limited to 'src/expr/lookup.c') diff --git a/src/expr/lookup.c b/src/expr/lookup.c index 8591d4e..ca10cab 100644 --- a/src/expr/lookup.c +++ b/src/expr/lookup.c @@ -153,36 +153,19 @@ nft_rule_expr_lookup_parse(struct nft_rule_expr *e, struct nlattr *attr) } static int -nft_rule_expr_lookup_xml_parse(struct nft_rule_expr *e, char *xml) +nft_rule_expr_lookup_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree) { #ifdef XML_PARSING struct nft_expr_lookup *lookup = (struct nft_expr_lookup *)e->data; - mxml_node_t *tree = NULL; mxml_node_t *node = NULL; uint64_t tmp; char *endptr; - tree = mxmlLoadString(NULL, xml, MXML_OPAQUE_CALLBACK); - if (tree == NULL) - return -1; - - if (mxmlElementGetAttr(tree, "type") == NULL) { - mxmlDelete(tree); - return -1; - } - - if (strcmp("lookup", mxmlElementGetAttr(tree, "type")) != 0) { - mxmlDelete(tree); - return -1; - } - /* get and set . Is mandatory */ node = mxmlFindElement(tree, tree, "set", NULL, NULL, MXML_DESCEND_FIRST); - if (node == NULL) { - mxmlDelete(tree); + if (node == NULL) return -1; - } memcpy(lookup->set_name, node->child->value.opaque, IFNAMSIZ); lookup->set_name[IFNAMSIZ-1] = '\0'; @@ -191,23 +174,17 @@ nft_rule_expr_lookup_xml_parse(struct nft_rule_expr *e, char *xml) /* get and set . Is mandatory */ node = mxmlFindElement(tree, tree, "sreg", NULL, NULL, MXML_DESCEND); - if (node == NULL) { - mxmlDelete(tree); + if (node == NULL) return -1; - } errno = 0; tmp = strtoull(node->child->value.opaque, &endptr, 10); - if (tmp > UINT32_MAX || tmp < 0 || *endptr) { - mxmlDelete(tree); + if (tmp > UINT32_MAX || tmp < 0 || *endptr) return -1; - } - if (tmp > NFT_REG_MAX) { - mxmlDelete(tree); + if (tmp > NFT_REG_MAX) return -1; - } lookup->sreg = (uint32_t)tmp; e->flags |= (1 << NFT_EXPR_LOOKUP_SREG); @@ -217,20 +194,15 @@ nft_rule_expr_lookup_xml_parse(struct nft_rule_expr *e, char *xml) MXML_DESCEND); if (node != NULL) { tmp = strtoull(node->child->value.opaque, &endptr, 10); - if (tmp > UINT32_MAX || tmp < 0 || *endptr) { - mxmlDelete(tree); + if (tmp > UINT32_MAX || tmp < 0 || *endptr) return -1; - } - if (tmp > NFT_REG_MAX) { - mxmlDelete(tree); + if (tmp > NFT_REG_MAX) return -1; - } lookup->dreg = (uint32_t)tmp; e->flags |= (1 << NFT_EXPR_LOOKUP_DREG); } - mxmlDelete(tree); return 0; #else errno = EOPNOTSUPP; -- cgit v1.2.3