Skip to contentSkip to conversation

src: xml: implement helper function nft_strtol

PATCH New Arturo Borrero 1 participant 1 message Jun 7, 2013 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 src: xml: implement helper function nft_strtol

Add a helper function to handle the XML validations.

The functions works this way:
* input string (which contains the number)
* base the number has (e.g. base 10 or 16)
* pointer to where you want to store the conversed number.
* datatype, to make additional validations like overflows.

Thus, to get a base 10 uint32_t number from a string:

if (nft_strtol(string, 10, &number, NFT_STRTOL_U32) != 0)
return -1

Also, I already have a patch that implements this functions all around the XML parsing code.

Of course, other approach are possibles.

---

 0 files changed
src/Makefile.am +2 -1
index 8a61ce8..bfbcaf7 100644
@@ -4,7 +4,8 @@ lib_LTLIBRARIES = libnftables.la
 libnftables_la_LIBADD = ${LIBMNL_LIBS}
 libnftables_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libnftables.map \
 			 -version-info $(LIBVERSION)
-libnftables_la_SOURCES = table.c		\
+libnftables_la_SOURCES = utils.c		\
+			 table.c		\
 			 chain.c		\
 			 rule.c			\
 			 set.c			\
src/internal.h +16 -0
index 0c5de21..094b198 100644
@@ -58,6 +58,22 @@ struct nft_set_elem {
 	uint32_t	flags;
 };
 
+/* utils */
+enum nft_strtol_type {
+	NFT_STRTOL_U8	= 0,
+	NFT_STRTOL_S8,
+	NFT_STRTOL_U16,
+	NFT_STRTOL_S16,
+	NFT_STRTOL_U32,
+	NFT_STRTOL_S32,
+	NFT_STRTOL_U64,
+	NFT_STRTOL_S64,
+	NFT_STRTOL_INT,
+	NFT_STRTOL_UINT,
+};
+
+int nft_strtol(const char *string, int base, void *number, enum nft_strtol_type type);
+
 #define SNPRINTF_BUFFER_SIZE(ret, size, len, offset)	\
 	size += ret;					\
 	if (ret > len)					\
src/utils.c new file +115 -0

115 lines not on this page · raw message