[PATCH v3 02/19] utils: Rename hostapd_parse_bin to wpabuf_parse_bin and move it

Ilan Peer ilan.peer at intel.com
Wed Apr 6 09:42:02 PDT 2016


From: David Spinadel <david.spinadel at intel.com>

Make the function available as part of the wpabuf API.
Use this renamed function where possible.

Signed-off-by: David Spinadel <david.spinadel at intel.com>
---
 hostapd/config_file.c   | 33 +++++----------------------------
 src/utils/wpabuf.c      | 31 +++++++++++++++++++++++++++++++
 src/utils/wpabuf.h      |  1 +
 wpa_supplicant/config.c | 14 ++------------
 4 files changed, 39 insertions(+), 40 deletions(-)

diff --git a/hostapd/config_file.c b/hostapd/config_file.c
index 9e17388..3e8130b 100644
--- a/hostapd/config_file.c
+++ b/hostapd/config_file.c
@@ -1920,29 +1920,6 @@ static int hs20_parse_osu_service_desc(struct hostapd_bss_config *bss,
 #endif /* CONFIG_HS20 */
 
 
-static struct wpabuf * hostapd_parse_bin(const char *buf)
-{
-	size_t len;
-	struct wpabuf *ret;
-
-	len = os_strlen(buf);
-	if (len & 0x01)
-		return NULL;
-	len /= 2;
-
-	ret = wpabuf_alloc(len);
-	if (ret == NULL)
-		return NULL;
-
-	if (hexstr2bin(buf, wpabuf_put(ret, len), len)) {
-		wpabuf_free(ret);
-		return NULL;
-	}
-
-	return ret;
-}
-
-
 #ifdef CONFIG_ACS
 static int hostapd_config_parse_acs_chan_bias(struct hostapd_config *conf,
 					      char *pos)
@@ -3029,15 +3006,15 @@ static int hostapd_config_fill(struct hostapd_config *conf,
 		bss->wps_nfc_pw_from_config = 1;
 	} else if (os_strcmp(buf, "wps_nfc_dh_pubkey") == 0) {
 		wpabuf_free(bss->wps_nfc_dh_pubkey);
-		bss->wps_nfc_dh_pubkey = hostapd_parse_bin(pos);
+		bss->wps_nfc_dh_pubkey = wpabuf_parse_bin(pos);
 		bss->wps_nfc_pw_from_config = 1;
 	} else if (os_strcmp(buf, "wps_nfc_dh_privkey") == 0) {
 		wpabuf_free(bss->wps_nfc_dh_privkey);
-		bss->wps_nfc_dh_privkey = hostapd_parse_bin(pos);
+		bss->wps_nfc_dh_privkey = wpabuf_parse_bin(pos);
 		bss->wps_nfc_pw_from_config = 1;
 	} else if (os_strcmp(buf, "wps_nfc_dev_pw") == 0) {
 		wpabuf_free(bss->wps_nfc_dev_pw);
-		bss->wps_nfc_dev_pw = hostapd_parse_bin(pos);
+		bss->wps_nfc_dev_pw = wpabuf_parse_bin(pos);
 		bss->wps_nfc_pw_from_config = 1;
 #endif /* CONFIG_WPS_NFC */
 #endif /* CONFIG_WPS */
@@ -3487,10 +3464,10 @@ static int hostapd_config_fill(struct hostapd_config *conf,
 		bss->no_auth_if_seen_on = os_strdup(pos);
 	} else if (os_strcmp(buf, "lci") == 0) {
 		wpabuf_free(conf->lci);
-		conf->lci = hostapd_parse_bin(pos);
+		conf->lci = wpabuf_parse_bin(pos);
 	} else if (os_strcmp(buf, "civic") == 0) {
 		wpabuf_free(conf->civic);
-		conf->civic = hostapd_parse_bin(pos);
+		conf->civic = wpabuf_parse_bin(pos);
 	} else {
 		wpa_printf(MSG_ERROR,
 			   "Line %d: unknown configuration item '%s'",
diff --git a/src/utils/wpabuf.c b/src/utils/wpabuf.c
index 11e7323..7a3061d 100644
--- a/src/utils/wpabuf.c
+++ b/src/utils/wpabuf.c
@@ -310,3 +310,34 @@ void wpabuf_printf(struct wpabuf *buf, char *fmt, ...)
 		wpabuf_overflow(buf, res);
 	buf->used += res;
 }
+
+
+/**
+ * wpabuf_parse_bin - Parse a null terminatred string of binary data to a wpabuf
+ *
+ * @buf: Buffer with null terminated string of binary data.
+ * Returns: wpabuf or %NULL on failure
+ *
+ * The string len must be a multiple of two and contain only hexadecimal digits.
+ */
+struct wpabuf *wpabuf_parse_bin(const char *buf)
+{
+	size_t len;
+	struct wpabuf *ret;
+
+	len = os_strlen(buf);
+	if (len & 0x01)
+		return NULL;
+	len /= 2;
+
+	ret = wpabuf_alloc(len);
+	if (ret == NULL)
+		return NULL;
+
+	if (hexstr2bin(buf, wpabuf_put(ret, len), len)) {
+		wpabuf_free(ret);
+		return NULL;
+	}
+
+	return ret;
+}
diff --git a/src/utils/wpabuf.h b/src/utils/wpabuf.h
index 9cd8a07..7265858 100644
--- a/src/utils/wpabuf.h
+++ b/src/utils/wpabuf.h
@@ -37,6 +37,7 @@ void * wpabuf_put(struct wpabuf *buf, size_t len);
 struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b);
 struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len);
 void wpabuf_printf(struct wpabuf *buf, char *fmt, ...) PRINTF_FORMAT(2, 3);
+struct wpabuf *wpabuf_parse_bin(const char *buf);
 
 
 /**
diff --git a/wpa_supplicant/config.c b/wpa_supplicant/config.c
index c167f09..cba7087 100644
--- a/wpa_supplicant/config.c
+++ b/wpa_supplicant/config.c
@@ -3778,21 +3778,11 @@ static int wpa_global_config_parse_bin(const struct global_parse_data *data,
 				       struct wpa_config *config, int line,
 				       const char *pos)
 {
-	size_t len;
 	struct wpabuf **dst, *tmp;
 
-	len = os_strlen(pos);
-	if (len & 0x01)
-		return -1;
-
-	tmp = wpabuf_alloc(len / 2);
-	if (tmp == NULL)
-		return -1;
-
-	if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
-		wpabuf_free(tmp);
+	tmp = wpabuf_parse_bin(pos);
+	if (!tmp)
 		return -1;
-	}
 
 	dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
 	wpabuf_free(*dst);
-- 
1.9.1




More information about the Hostap mailing list