xref: /dflybsd-src/contrib/wpa_supplicant/src/utils/common.c (revision 3a84a4273475ed07d0ab1c2dfeffdfedef35d9cd)
16d49e1aeSJan Lentfer /*
26d49e1aeSJan Lentfer  * wpa_supplicant/hostapd / common helper functions, etc.
3*a1157835SDaniel Fojt  * Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi>
46d49e1aeSJan Lentfer  *
53ff40c12SJohn Marino  * This software may be distributed under the terms of the BSD license.
63ff40c12SJohn Marino  * See README for more details.
76d49e1aeSJan Lentfer  */
86d49e1aeSJan Lentfer 
96d49e1aeSJan Lentfer #include "includes.h"
106d49e1aeSJan Lentfer 
11*a1157835SDaniel Fojt #include "common/ieee802_11_defs.h"
126d49e1aeSJan Lentfer #include "common.h"
136d49e1aeSJan Lentfer 
146d49e1aeSJan Lentfer 
hex2num(char c)156d49e1aeSJan Lentfer static int hex2num(char c)
166d49e1aeSJan Lentfer {
176d49e1aeSJan Lentfer 	if (c >= '0' && c <= '9')
186d49e1aeSJan Lentfer 		return c - '0';
196d49e1aeSJan Lentfer 	if (c >= 'a' && c <= 'f')
206d49e1aeSJan Lentfer 		return c - 'a' + 10;
216d49e1aeSJan Lentfer 	if (c >= 'A' && c <= 'F')
226d49e1aeSJan Lentfer 		return c - 'A' + 10;
236d49e1aeSJan Lentfer 	return -1;
246d49e1aeSJan Lentfer }
256d49e1aeSJan Lentfer 
266d49e1aeSJan Lentfer 
hex2byte(const char * hex)273ff40c12SJohn Marino int hex2byte(const char *hex)
286d49e1aeSJan Lentfer {
296d49e1aeSJan Lentfer 	int a, b;
306d49e1aeSJan Lentfer 	a = hex2num(*hex++);
316d49e1aeSJan Lentfer 	if (a < 0)
326d49e1aeSJan Lentfer 		return -1;
336d49e1aeSJan Lentfer 	b = hex2num(*hex++);
346d49e1aeSJan Lentfer 	if (b < 0)
356d49e1aeSJan Lentfer 		return -1;
366d49e1aeSJan Lentfer 	return (a << 4) | b;
376d49e1aeSJan Lentfer }
386d49e1aeSJan Lentfer 
396d49e1aeSJan Lentfer 
hwaddr_parse(const char * txt,u8 * addr)40*a1157835SDaniel Fojt static const char * hwaddr_parse(const char *txt, u8 *addr)
41*a1157835SDaniel Fojt {
42*a1157835SDaniel Fojt 	size_t i;
43*a1157835SDaniel Fojt 
44*a1157835SDaniel Fojt 	for (i = 0; i < ETH_ALEN; i++) {
45*a1157835SDaniel Fojt 		int a;
46*a1157835SDaniel Fojt 
47*a1157835SDaniel Fojt 		a = hex2byte(txt);
48*a1157835SDaniel Fojt 		if (a < 0)
49*a1157835SDaniel Fojt 			return NULL;
50*a1157835SDaniel Fojt 		txt += 2;
51*a1157835SDaniel Fojt 		addr[i] = a;
52*a1157835SDaniel Fojt 		if (i < ETH_ALEN - 1 && *txt++ != ':')
53*a1157835SDaniel Fojt 			return NULL;
54*a1157835SDaniel Fojt 	}
55*a1157835SDaniel Fojt 	return txt;
56*a1157835SDaniel Fojt }
57*a1157835SDaniel Fojt 
58*a1157835SDaniel Fojt 
596d49e1aeSJan Lentfer /**
603ff40c12SJohn Marino  * hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format)
616d49e1aeSJan Lentfer  * @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
626d49e1aeSJan Lentfer  * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
636d49e1aeSJan Lentfer  * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
646d49e1aeSJan Lentfer  */
hwaddr_aton(const char * txt,u8 * addr)656d49e1aeSJan Lentfer int hwaddr_aton(const char *txt, u8 *addr)
666d49e1aeSJan Lentfer {
67*a1157835SDaniel Fojt 	return hwaddr_parse(txt, addr) ? 0 : -1;
68*a1157835SDaniel Fojt }
696d49e1aeSJan Lentfer 
706d49e1aeSJan Lentfer 
71*a1157835SDaniel Fojt /**
72*a1157835SDaniel Fojt  * hwaddr_masked_aton - Convert ASCII string with optional mask to MAC address (colon-delimited format)
73*a1157835SDaniel Fojt  * @txt: MAC address with optional mask as a string (e.g., "00:11:22:33:44:55/ff:ff:ff:ff:00:00")
74*a1157835SDaniel Fojt  * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
75*a1157835SDaniel Fojt  * @mask: Buffer for the MAC address mask (ETH_ALEN = 6 bytes)
76*a1157835SDaniel Fojt  * @maskable: Flag to indicate whether a mask is allowed
77*a1157835SDaniel Fojt  * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
78*a1157835SDaniel Fojt  */
hwaddr_masked_aton(const char * txt,u8 * addr,u8 * mask,u8 maskable)79*a1157835SDaniel Fojt int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable)
80*a1157835SDaniel Fojt {
81*a1157835SDaniel Fojt 	const char *r;
82*a1157835SDaniel Fojt 
83*a1157835SDaniel Fojt 	/* parse address part */
84*a1157835SDaniel Fojt 	r = hwaddr_parse(txt, addr);
85*a1157835SDaniel Fojt 	if (!r)
866d49e1aeSJan Lentfer 		return -1;
87*a1157835SDaniel Fojt 
88*a1157835SDaniel Fojt 	/* check for optional mask */
89*a1157835SDaniel Fojt 	if (*r == '\0' || isspace((unsigned char) *r)) {
90*a1157835SDaniel Fojt 		/* no mask specified, assume default */
91*a1157835SDaniel Fojt 		os_memset(mask, 0xff, ETH_ALEN);
92*a1157835SDaniel Fojt 	} else if (maskable && *r == '/') {
93*a1157835SDaniel Fojt 		/* mask specified and allowed */
94*a1157835SDaniel Fojt 		r = hwaddr_parse(r + 1, mask);
95*a1157835SDaniel Fojt 		/* parser error? */
96*a1157835SDaniel Fojt 		if (!r)
976d49e1aeSJan Lentfer 			return -1;
98*a1157835SDaniel Fojt 	} else {
99*a1157835SDaniel Fojt 		/* mask specified but not allowed or trailing garbage */
1006d49e1aeSJan Lentfer 		return -1;
1016d49e1aeSJan Lentfer 	}
1026d49e1aeSJan Lentfer 
1036d49e1aeSJan Lentfer 	return 0;
1046d49e1aeSJan Lentfer }
1056d49e1aeSJan Lentfer 
106*a1157835SDaniel Fojt 
1073ff40c12SJohn Marino /**
1083ff40c12SJohn Marino  * hwaddr_compact_aton - Convert ASCII string to MAC address (no colon delimitors format)
1093ff40c12SJohn Marino  * @txt: MAC address as a string (e.g., "001122334455")
1103ff40c12SJohn Marino  * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
1113ff40c12SJohn Marino  * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
1123ff40c12SJohn Marino  */
hwaddr_compact_aton(const char * txt,u8 * addr)1133ff40c12SJohn Marino int hwaddr_compact_aton(const char *txt, u8 *addr)
1143ff40c12SJohn Marino {
1153ff40c12SJohn Marino 	int i;
1163ff40c12SJohn Marino 
1173ff40c12SJohn Marino 	for (i = 0; i < 6; i++) {
1183ff40c12SJohn Marino 		int a, b;
1193ff40c12SJohn Marino 
1203ff40c12SJohn Marino 		a = hex2num(*txt++);
1213ff40c12SJohn Marino 		if (a < 0)
1223ff40c12SJohn Marino 			return -1;
1233ff40c12SJohn Marino 		b = hex2num(*txt++);
1243ff40c12SJohn Marino 		if (b < 0)
1253ff40c12SJohn Marino 			return -1;
1263ff40c12SJohn Marino 		*addr++ = (a << 4) | b;
1273ff40c12SJohn Marino 	}
1283ff40c12SJohn Marino 
1293ff40c12SJohn Marino 	return 0;
1303ff40c12SJohn Marino }
1313ff40c12SJohn Marino 
1323ff40c12SJohn Marino /**
1333ff40c12SJohn Marino  * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
1343ff40c12SJohn Marino  * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
1353ff40c12SJohn Marino  * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
1363ff40c12SJohn Marino  * Returns: Characters used (> 0) on success, -1 on failure
1373ff40c12SJohn Marino  */
hwaddr_aton2(const char * txt,u8 * addr)1383ff40c12SJohn Marino int hwaddr_aton2(const char *txt, u8 *addr)
1393ff40c12SJohn Marino {
1403ff40c12SJohn Marino 	int i;
1413ff40c12SJohn Marino 	const char *pos = txt;
1423ff40c12SJohn Marino 
1433ff40c12SJohn Marino 	for (i = 0; i < 6; i++) {
1443ff40c12SJohn Marino 		int a, b;
1453ff40c12SJohn Marino 
1463ff40c12SJohn Marino 		while (*pos == ':' || *pos == '.' || *pos == '-')
1473ff40c12SJohn Marino 			pos++;
1483ff40c12SJohn Marino 
1493ff40c12SJohn Marino 		a = hex2num(*pos++);
1503ff40c12SJohn Marino 		if (a < 0)
1513ff40c12SJohn Marino 			return -1;
1523ff40c12SJohn Marino 		b = hex2num(*pos++);
1533ff40c12SJohn Marino 		if (b < 0)
1543ff40c12SJohn Marino 			return -1;
1553ff40c12SJohn Marino 		*addr++ = (a << 4) | b;
1563ff40c12SJohn Marino 	}
1573ff40c12SJohn Marino 
1583ff40c12SJohn Marino 	return pos - txt;
1593ff40c12SJohn Marino }
1603ff40c12SJohn Marino 
1616d49e1aeSJan Lentfer 
1626d49e1aeSJan Lentfer /**
1636d49e1aeSJan Lentfer  * hexstr2bin - Convert ASCII hex string into binary data
1646d49e1aeSJan Lentfer  * @hex: ASCII hex string (e.g., "01ab")
1656d49e1aeSJan Lentfer  * @buf: Buffer for the binary data
1666d49e1aeSJan Lentfer  * @len: Length of the text to convert in bytes (of buf); hex will be double
1676d49e1aeSJan Lentfer  * this size
1686d49e1aeSJan Lentfer  * Returns: 0 on success, -1 on failure (invalid hex string)
1696d49e1aeSJan Lentfer  */
hexstr2bin(const char * hex,u8 * buf,size_t len)1706d49e1aeSJan Lentfer int hexstr2bin(const char *hex, u8 *buf, size_t len)
1716d49e1aeSJan Lentfer {
1726d49e1aeSJan Lentfer 	size_t i;
1736d49e1aeSJan Lentfer 	int a;
1746d49e1aeSJan Lentfer 	const char *ipos = hex;
1756d49e1aeSJan Lentfer 	u8 *opos = buf;
1766d49e1aeSJan Lentfer 
1776d49e1aeSJan Lentfer 	for (i = 0; i < len; i++) {
1786d49e1aeSJan Lentfer 		a = hex2byte(ipos);
1796d49e1aeSJan Lentfer 		if (a < 0)
1806d49e1aeSJan Lentfer 			return -1;
1816d49e1aeSJan Lentfer 		*opos++ = a;
1826d49e1aeSJan Lentfer 		ipos += 2;
1836d49e1aeSJan Lentfer 	}
1846d49e1aeSJan Lentfer 	return 0;
1856d49e1aeSJan Lentfer }
1866d49e1aeSJan Lentfer 
1876d49e1aeSJan Lentfer 
hwaddr_mask_txt(char * buf,size_t len,const u8 * addr,const u8 * mask)188*a1157835SDaniel Fojt int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask)
189*a1157835SDaniel Fojt {
190*a1157835SDaniel Fojt 	size_t i;
191*a1157835SDaniel Fojt 	int print_mask = 0;
192*a1157835SDaniel Fojt 	int res;
193*a1157835SDaniel Fojt 
194*a1157835SDaniel Fojt 	for (i = 0; i < ETH_ALEN; i++) {
195*a1157835SDaniel Fojt 		if (mask[i] != 0xff) {
196*a1157835SDaniel Fojt 			print_mask = 1;
197*a1157835SDaniel Fojt 			break;
198*a1157835SDaniel Fojt 		}
199*a1157835SDaniel Fojt 	}
200*a1157835SDaniel Fojt 
201*a1157835SDaniel Fojt 	if (print_mask)
202*a1157835SDaniel Fojt 		res = os_snprintf(buf, len, MACSTR "/" MACSTR,
203*a1157835SDaniel Fojt 				  MAC2STR(addr), MAC2STR(mask));
204*a1157835SDaniel Fojt 	else
205*a1157835SDaniel Fojt 		res = os_snprintf(buf, len, MACSTR, MAC2STR(addr));
206*a1157835SDaniel Fojt 	if (os_snprintf_error(len, res))
207*a1157835SDaniel Fojt 		return -1;
208*a1157835SDaniel Fojt 	return res;
209*a1157835SDaniel Fojt }
210*a1157835SDaniel Fojt 
211*a1157835SDaniel Fojt 
2126d49e1aeSJan Lentfer /**
2136d49e1aeSJan Lentfer  * inc_byte_array - Increment arbitrary length byte array by one
2146d49e1aeSJan Lentfer  * @counter: Pointer to byte array
2156d49e1aeSJan Lentfer  * @len: Length of the counter in bytes
2166d49e1aeSJan Lentfer  *
2176d49e1aeSJan Lentfer  * This function increments the last byte of the counter by one and continues
2186d49e1aeSJan Lentfer  * rolling over to more significant bytes if the byte was incremented from
2196d49e1aeSJan Lentfer  * 0xff to 0x00.
2206d49e1aeSJan Lentfer  */
inc_byte_array(u8 * counter,size_t len)2216d49e1aeSJan Lentfer void inc_byte_array(u8 *counter, size_t len)
2226d49e1aeSJan Lentfer {
2236d49e1aeSJan Lentfer 	int pos = len - 1;
2246d49e1aeSJan Lentfer 	while (pos >= 0) {
2256d49e1aeSJan Lentfer 		counter[pos]++;
2266d49e1aeSJan Lentfer 		if (counter[pos] != 0)
2276d49e1aeSJan Lentfer 			break;
2286d49e1aeSJan Lentfer 		pos--;
2296d49e1aeSJan Lentfer 	}
2306d49e1aeSJan Lentfer }
2316d49e1aeSJan Lentfer 
2326d49e1aeSJan Lentfer 
buf_shift_right(u8 * buf,size_t len,size_t bits)233*a1157835SDaniel Fojt void buf_shift_right(u8 *buf, size_t len, size_t bits)
234*a1157835SDaniel Fojt {
235*a1157835SDaniel Fojt 	size_t i;
236*a1157835SDaniel Fojt 
237*a1157835SDaniel Fojt 	for (i = len - 1; i > 0; i--)
238*a1157835SDaniel Fojt 		buf[i] = (buf[i - 1] << (8 - bits)) | (buf[i] >> bits);
239*a1157835SDaniel Fojt 	buf[0] >>= bits;
240*a1157835SDaniel Fojt }
241*a1157835SDaniel Fojt 
242*a1157835SDaniel Fojt 
wpa_get_ntp_timestamp(u8 * buf)2436d49e1aeSJan Lentfer void wpa_get_ntp_timestamp(u8 *buf)
2446d49e1aeSJan Lentfer {
2456d49e1aeSJan Lentfer 	struct os_time now;
2466d49e1aeSJan Lentfer 	u32 sec, usec;
2476d49e1aeSJan Lentfer 	be32 tmp;
2486d49e1aeSJan Lentfer 
2496d49e1aeSJan Lentfer 	/* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
2506d49e1aeSJan Lentfer 	os_get_time(&now);
2516d49e1aeSJan Lentfer 	sec = now.sec + 2208988800U; /* Epoch to 1900 */
2526d49e1aeSJan Lentfer 	/* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
2536d49e1aeSJan Lentfer 	usec = now.usec;
2546d49e1aeSJan Lentfer 	usec = 4295 * usec - (usec >> 5) - (usec >> 9);
2556d49e1aeSJan Lentfer 	tmp = host_to_be32(sec);
2566d49e1aeSJan Lentfer 	os_memcpy(buf, (u8 *) &tmp, 4);
2576d49e1aeSJan Lentfer 	tmp = host_to_be32(usec);
2586d49e1aeSJan Lentfer 	os_memcpy(buf + 4, (u8 *) &tmp, 4);
2596d49e1aeSJan Lentfer }
2606d49e1aeSJan Lentfer 
261*a1157835SDaniel Fojt /**
262*a1157835SDaniel Fojt  * wpa_scnprintf - Simpler-to-use snprintf function
263*a1157835SDaniel Fojt  * @buf: Output buffer
264*a1157835SDaniel Fojt  * @size: Buffer size
265*a1157835SDaniel Fojt  * @fmt: format
266*a1157835SDaniel Fojt  *
267*a1157835SDaniel Fojt  * Simpler snprintf version that doesn't require further error checks - the
268*a1157835SDaniel Fojt  * return value only indicates how many bytes were actually written, excluding
269*a1157835SDaniel Fojt  * the NULL byte (i.e., 0 on error, size-1 if buffer is not big enough).
270*a1157835SDaniel Fojt  */
wpa_scnprintf(char * buf,size_t size,const char * fmt,...)271*a1157835SDaniel Fojt int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...)
272*a1157835SDaniel Fojt {
273*a1157835SDaniel Fojt 	va_list ap;
274*a1157835SDaniel Fojt 	int ret;
275*a1157835SDaniel Fojt 
276*a1157835SDaniel Fojt 	if (!size)
277*a1157835SDaniel Fojt 		return 0;
278*a1157835SDaniel Fojt 
279*a1157835SDaniel Fojt 	va_start(ap, fmt);
280*a1157835SDaniel Fojt 	ret = vsnprintf(buf, size, fmt, ap);
281*a1157835SDaniel Fojt 	va_end(ap);
282*a1157835SDaniel Fojt 
283*a1157835SDaniel Fojt 	if (ret < 0)
284*a1157835SDaniel Fojt 		return 0;
285*a1157835SDaniel Fojt 	if ((size_t) ret >= size)
286*a1157835SDaniel Fojt 		return size - 1;
287*a1157835SDaniel Fojt 
288*a1157835SDaniel Fojt 	return ret;
289*a1157835SDaniel Fojt }
290*a1157835SDaniel Fojt 
291*a1157835SDaniel Fojt 
wpa_snprintf_hex_sep(char * buf,size_t buf_size,const u8 * data,size_t len,char sep)292*a1157835SDaniel Fojt int wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len,
293*a1157835SDaniel Fojt 			 char sep)
294*a1157835SDaniel Fojt {
295*a1157835SDaniel Fojt 	size_t i;
296*a1157835SDaniel Fojt 	char *pos = buf, *end = buf + buf_size;
297*a1157835SDaniel Fojt 	int ret;
298*a1157835SDaniel Fojt 
299*a1157835SDaniel Fojt 	if (buf_size == 0)
300*a1157835SDaniel Fojt 		return 0;
301*a1157835SDaniel Fojt 
302*a1157835SDaniel Fojt 	for (i = 0; i < len; i++) {
303*a1157835SDaniel Fojt 		ret = os_snprintf(pos, end - pos, "%02x%c",
304*a1157835SDaniel Fojt 				  data[i], sep);
305*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret)) {
306*a1157835SDaniel Fojt 			end[-1] = '\0';
307*a1157835SDaniel Fojt 			return pos - buf;
308*a1157835SDaniel Fojt 		}
309*a1157835SDaniel Fojt 		pos += ret;
310*a1157835SDaniel Fojt 	}
311*a1157835SDaniel Fojt 	pos[-1] = '\0';
312*a1157835SDaniel Fojt 	return pos - buf;
313*a1157835SDaniel Fojt }
314*a1157835SDaniel Fojt 
3156d49e1aeSJan Lentfer 
_wpa_snprintf_hex(char * buf,size_t buf_size,const u8 * data,size_t len,int uppercase)3166d49e1aeSJan Lentfer static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
3176d49e1aeSJan Lentfer 				    size_t len, int uppercase)
3186d49e1aeSJan Lentfer {
3196d49e1aeSJan Lentfer 	size_t i;
3206d49e1aeSJan Lentfer 	char *pos = buf, *end = buf + buf_size;
3216d49e1aeSJan Lentfer 	int ret;
3226d49e1aeSJan Lentfer 	if (buf_size == 0)
3236d49e1aeSJan Lentfer 		return 0;
3246d49e1aeSJan Lentfer 	for (i = 0; i < len; i++) {
3256d49e1aeSJan Lentfer 		ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
3266d49e1aeSJan Lentfer 				  data[i]);
327*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret)) {
3286d49e1aeSJan Lentfer 			end[-1] = '\0';
3296d49e1aeSJan Lentfer 			return pos - buf;
3306d49e1aeSJan Lentfer 		}
3316d49e1aeSJan Lentfer 		pos += ret;
3326d49e1aeSJan Lentfer 	}
3336d49e1aeSJan Lentfer 	end[-1] = '\0';
3346d49e1aeSJan Lentfer 	return pos - buf;
3356d49e1aeSJan Lentfer }
3366d49e1aeSJan Lentfer 
3376d49e1aeSJan Lentfer /**
3386d49e1aeSJan Lentfer  * wpa_snprintf_hex - Print data as a hex string into a buffer
3396d49e1aeSJan Lentfer  * @buf: Memory area to use as the output buffer
3406d49e1aeSJan Lentfer  * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
3416d49e1aeSJan Lentfer  * @data: Data to be printed
3426d49e1aeSJan Lentfer  * @len: Length of data in bytes
3436d49e1aeSJan Lentfer  * Returns: Number of bytes written
3446d49e1aeSJan Lentfer  */
wpa_snprintf_hex(char * buf,size_t buf_size,const u8 * data,size_t len)3456d49e1aeSJan Lentfer int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
3466d49e1aeSJan Lentfer {
3476d49e1aeSJan Lentfer 	return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
3486d49e1aeSJan Lentfer }
3496d49e1aeSJan Lentfer 
3506d49e1aeSJan Lentfer 
3516d49e1aeSJan Lentfer /**
3526d49e1aeSJan Lentfer  * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf
3536d49e1aeSJan Lentfer  * @buf: Memory area to use as the output buffer
3546d49e1aeSJan Lentfer  * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
3556d49e1aeSJan Lentfer  * @data: Data to be printed
3566d49e1aeSJan Lentfer  * @len: Length of data in bytes
3576d49e1aeSJan Lentfer  * Returns: Number of bytes written
3586d49e1aeSJan Lentfer  */
wpa_snprintf_hex_uppercase(char * buf,size_t buf_size,const u8 * data,size_t len)3596d49e1aeSJan Lentfer int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
3606d49e1aeSJan Lentfer 			       size_t len)
3616d49e1aeSJan Lentfer {
3626d49e1aeSJan Lentfer 	return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
3636d49e1aeSJan Lentfer }
3646d49e1aeSJan Lentfer 
3656d49e1aeSJan Lentfer 
3666d49e1aeSJan Lentfer #ifdef CONFIG_ANSI_C_EXTRA
3676d49e1aeSJan Lentfer 
3686d49e1aeSJan Lentfer #ifdef _WIN32_WCE
perror(const char * s)3696d49e1aeSJan Lentfer void perror(const char *s)
3706d49e1aeSJan Lentfer {
3716d49e1aeSJan Lentfer 	wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
3726d49e1aeSJan Lentfer 		   s, (int) GetLastError());
3736d49e1aeSJan Lentfer }
3746d49e1aeSJan Lentfer #endif /* _WIN32_WCE */
3756d49e1aeSJan Lentfer 
3766d49e1aeSJan Lentfer 
3776d49e1aeSJan Lentfer int optind = 1;
3786d49e1aeSJan Lentfer int optopt;
3796d49e1aeSJan Lentfer char *optarg;
3806d49e1aeSJan Lentfer 
getopt(int argc,char * const argv[],const char * optstring)3816d49e1aeSJan Lentfer int getopt(int argc, char *const argv[], const char *optstring)
3826d49e1aeSJan Lentfer {
3836d49e1aeSJan Lentfer 	static int optchr = 1;
3846d49e1aeSJan Lentfer 	char *cp;
3856d49e1aeSJan Lentfer 
3866d49e1aeSJan Lentfer 	if (optchr == 1) {
3876d49e1aeSJan Lentfer 		if (optind >= argc) {
3886d49e1aeSJan Lentfer 			/* all arguments processed */
3896d49e1aeSJan Lentfer 			return EOF;
3906d49e1aeSJan Lentfer 		}
3916d49e1aeSJan Lentfer 
3926d49e1aeSJan Lentfer 		if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
3936d49e1aeSJan Lentfer 			/* no option characters */
3946d49e1aeSJan Lentfer 			return EOF;
3956d49e1aeSJan Lentfer 		}
3966d49e1aeSJan Lentfer 	}
3976d49e1aeSJan Lentfer 
3986d49e1aeSJan Lentfer 	if (os_strcmp(argv[optind], "--") == 0) {
3996d49e1aeSJan Lentfer 		/* no more options */
4006d49e1aeSJan Lentfer 		optind++;
4016d49e1aeSJan Lentfer 		return EOF;
4026d49e1aeSJan Lentfer 	}
4036d49e1aeSJan Lentfer 
4046d49e1aeSJan Lentfer 	optopt = argv[optind][optchr];
4056d49e1aeSJan Lentfer 	cp = os_strchr(optstring, optopt);
4066d49e1aeSJan Lentfer 	if (cp == NULL || optopt == ':') {
4076d49e1aeSJan Lentfer 		if (argv[optind][++optchr] == '\0') {
4086d49e1aeSJan Lentfer 			optchr = 1;
4096d49e1aeSJan Lentfer 			optind++;
4106d49e1aeSJan Lentfer 		}
4116d49e1aeSJan Lentfer 		return '?';
4126d49e1aeSJan Lentfer 	}
4136d49e1aeSJan Lentfer 
4146d49e1aeSJan Lentfer 	if (cp[1] == ':') {
4156d49e1aeSJan Lentfer 		/* Argument required */
4166d49e1aeSJan Lentfer 		optchr = 1;
4176d49e1aeSJan Lentfer 		if (argv[optind][optchr + 1]) {
4186d49e1aeSJan Lentfer 			/* No space between option and argument */
4196d49e1aeSJan Lentfer 			optarg = &argv[optind++][optchr + 1];
4206d49e1aeSJan Lentfer 		} else if (++optind >= argc) {
4216d49e1aeSJan Lentfer 			/* option requires an argument */
4226d49e1aeSJan Lentfer 			return '?';
4236d49e1aeSJan Lentfer 		} else {
4246d49e1aeSJan Lentfer 			/* Argument in the next argv */
4256d49e1aeSJan Lentfer 			optarg = argv[optind++];
4266d49e1aeSJan Lentfer 		}
4276d49e1aeSJan Lentfer 	} else {
4286d49e1aeSJan Lentfer 		/* No argument */
4296d49e1aeSJan Lentfer 		if (argv[optind][++optchr] == '\0') {
4306d49e1aeSJan Lentfer 			optchr = 1;
4316d49e1aeSJan Lentfer 			optind++;
4326d49e1aeSJan Lentfer 		}
4336d49e1aeSJan Lentfer 		optarg = NULL;
4346d49e1aeSJan Lentfer 	}
4356d49e1aeSJan Lentfer 	return *cp;
4366d49e1aeSJan Lentfer }
4376d49e1aeSJan Lentfer #endif /* CONFIG_ANSI_C_EXTRA */
4386d49e1aeSJan Lentfer 
4396d49e1aeSJan Lentfer 
4406d49e1aeSJan Lentfer #ifdef CONFIG_NATIVE_WINDOWS
4416d49e1aeSJan Lentfer /**
4426d49e1aeSJan Lentfer  * wpa_unicode2ascii_inplace - Convert unicode string into ASCII
4436d49e1aeSJan Lentfer  * @str: Pointer to string to convert
4446d49e1aeSJan Lentfer  *
4456d49e1aeSJan Lentfer  * This function converts a unicode string to ASCII using the same
4466d49e1aeSJan Lentfer  * buffer for output. If UNICODE is not set, the buffer is not
4476d49e1aeSJan Lentfer  * modified.
4486d49e1aeSJan Lentfer  */
wpa_unicode2ascii_inplace(TCHAR * str)4496d49e1aeSJan Lentfer void wpa_unicode2ascii_inplace(TCHAR *str)
4506d49e1aeSJan Lentfer {
4516d49e1aeSJan Lentfer #ifdef UNICODE
4526d49e1aeSJan Lentfer 	char *dst = (char *) str;
4536d49e1aeSJan Lentfer 	while (*str)
4546d49e1aeSJan Lentfer 		*dst++ = (char) *str++;
4556d49e1aeSJan Lentfer 	*dst = '\0';
4566d49e1aeSJan Lentfer #endif /* UNICODE */
4576d49e1aeSJan Lentfer }
4586d49e1aeSJan Lentfer 
4596d49e1aeSJan Lentfer 
wpa_strdup_tchar(const char * str)4606d49e1aeSJan Lentfer TCHAR * wpa_strdup_tchar(const char *str)
4616d49e1aeSJan Lentfer {
4626d49e1aeSJan Lentfer #ifdef UNICODE
4636d49e1aeSJan Lentfer 	TCHAR *buf;
4646d49e1aeSJan Lentfer 	buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
4656d49e1aeSJan Lentfer 	if (buf == NULL)
4666d49e1aeSJan Lentfer 		return NULL;
4676d49e1aeSJan Lentfer 	wsprintf(buf, L"%S", str);
4686d49e1aeSJan Lentfer 	return buf;
4696d49e1aeSJan Lentfer #else /* UNICODE */
4706d49e1aeSJan Lentfer 	return os_strdup(str);
4716d49e1aeSJan Lentfer #endif /* UNICODE */
4726d49e1aeSJan Lentfer }
4736d49e1aeSJan Lentfer #endif /* CONFIG_NATIVE_WINDOWS */
4746d49e1aeSJan Lentfer 
4756d49e1aeSJan Lentfer 
printf_encode(char * txt,size_t maxlen,const u8 * data,size_t len)4763ff40c12SJohn Marino void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len)
4773ff40c12SJohn Marino {
4783ff40c12SJohn Marino 	char *end = txt + maxlen;
4793ff40c12SJohn Marino 	size_t i;
4803ff40c12SJohn Marino 
4813ff40c12SJohn Marino 	for (i = 0; i < len; i++) {
482*a1157835SDaniel Fojt 		if (txt + 4 >= end)
4833ff40c12SJohn Marino 			break;
4843ff40c12SJohn Marino 
4853ff40c12SJohn Marino 		switch (data[i]) {
4863ff40c12SJohn Marino 		case '\"':
4873ff40c12SJohn Marino 			*txt++ = '\\';
4883ff40c12SJohn Marino 			*txt++ = '\"';
4893ff40c12SJohn Marino 			break;
4903ff40c12SJohn Marino 		case '\\':
4913ff40c12SJohn Marino 			*txt++ = '\\';
4923ff40c12SJohn Marino 			*txt++ = '\\';
4933ff40c12SJohn Marino 			break;
494*a1157835SDaniel Fojt 		case '\033':
4953ff40c12SJohn Marino 			*txt++ = '\\';
4963ff40c12SJohn Marino 			*txt++ = 'e';
4973ff40c12SJohn Marino 			break;
4983ff40c12SJohn Marino 		case '\n':
4993ff40c12SJohn Marino 			*txt++ = '\\';
5003ff40c12SJohn Marino 			*txt++ = 'n';
5013ff40c12SJohn Marino 			break;
5023ff40c12SJohn Marino 		case '\r':
5033ff40c12SJohn Marino 			*txt++ = '\\';
5043ff40c12SJohn Marino 			*txt++ = 'r';
5053ff40c12SJohn Marino 			break;
5063ff40c12SJohn Marino 		case '\t':
5073ff40c12SJohn Marino 			*txt++ = '\\';
5083ff40c12SJohn Marino 			*txt++ = 't';
5093ff40c12SJohn Marino 			break;
5103ff40c12SJohn Marino 		default:
511*a1157835SDaniel Fojt 			if (data[i] >= 32 && data[i] <= 126) {
5123ff40c12SJohn Marino 				*txt++ = data[i];
5133ff40c12SJohn Marino 			} else {
5143ff40c12SJohn Marino 				txt += os_snprintf(txt, end - txt, "\\x%02x",
5153ff40c12SJohn Marino 						   data[i]);
5163ff40c12SJohn Marino 			}
5173ff40c12SJohn Marino 			break;
5183ff40c12SJohn Marino 		}
5193ff40c12SJohn Marino 	}
5203ff40c12SJohn Marino 
5213ff40c12SJohn Marino 	*txt = '\0';
5223ff40c12SJohn Marino }
5233ff40c12SJohn Marino 
5243ff40c12SJohn Marino 
printf_decode(u8 * buf,size_t maxlen,const char * str)5253ff40c12SJohn Marino size_t printf_decode(u8 *buf, size_t maxlen, const char *str)
5263ff40c12SJohn Marino {
5273ff40c12SJohn Marino 	const char *pos = str;
5283ff40c12SJohn Marino 	size_t len = 0;
5293ff40c12SJohn Marino 	int val;
5303ff40c12SJohn Marino 
5313ff40c12SJohn Marino 	while (*pos) {
5323ff40c12SJohn Marino 		if (len + 1 >= maxlen)
5333ff40c12SJohn Marino 			break;
5343ff40c12SJohn Marino 		switch (*pos) {
5353ff40c12SJohn Marino 		case '\\':
5363ff40c12SJohn Marino 			pos++;
5373ff40c12SJohn Marino 			switch (*pos) {
5383ff40c12SJohn Marino 			case '\\':
5393ff40c12SJohn Marino 				buf[len++] = '\\';
5403ff40c12SJohn Marino 				pos++;
5413ff40c12SJohn Marino 				break;
5423ff40c12SJohn Marino 			case '"':
5433ff40c12SJohn Marino 				buf[len++] = '"';
5443ff40c12SJohn Marino 				pos++;
5453ff40c12SJohn Marino 				break;
5463ff40c12SJohn Marino 			case 'n':
5473ff40c12SJohn Marino 				buf[len++] = '\n';
5483ff40c12SJohn Marino 				pos++;
5493ff40c12SJohn Marino 				break;
5503ff40c12SJohn Marino 			case 'r':
5513ff40c12SJohn Marino 				buf[len++] = '\r';
5523ff40c12SJohn Marino 				pos++;
5533ff40c12SJohn Marino 				break;
5543ff40c12SJohn Marino 			case 't':
5553ff40c12SJohn Marino 				buf[len++] = '\t';
5563ff40c12SJohn Marino 				pos++;
5573ff40c12SJohn Marino 				break;
5583ff40c12SJohn Marino 			case 'e':
559*a1157835SDaniel Fojt 				buf[len++] = '\033';
5603ff40c12SJohn Marino 				pos++;
5613ff40c12SJohn Marino 				break;
5623ff40c12SJohn Marino 			case 'x':
5633ff40c12SJohn Marino 				pos++;
5643ff40c12SJohn Marino 				val = hex2byte(pos);
5653ff40c12SJohn Marino 				if (val < 0) {
5663ff40c12SJohn Marino 					val = hex2num(*pos);
5673ff40c12SJohn Marino 					if (val < 0)
5683ff40c12SJohn Marino 						break;
5693ff40c12SJohn Marino 					buf[len++] = val;
5703ff40c12SJohn Marino 					pos++;
5713ff40c12SJohn Marino 				} else {
5723ff40c12SJohn Marino 					buf[len++] = val;
5733ff40c12SJohn Marino 					pos += 2;
5743ff40c12SJohn Marino 				}
5753ff40c12SJohn Marino 				break;
5763ff40c12SJohn Marino 			case '0':
5773ff40c12SJohn Marino 			case '1':
5783ff40c12SJohn Marino 			case '2':
5793ff40c12SJohn Marino 			case '3':
5803ff40c12SJohn Marino 			case '4':
5813ff40c12SJohn Marino 			case '5':
5823ff40c12SJohn Marino 			case '6':
5833ff40c12SJohn Marino 			case '7':
5843ff40c12SJohn Marino 				val = *pos++ - '0';
5853ff40c12SJohn Marino 				if (*pos >= '0' && *pos <= '7')
5863ff40c12SJohn Marino 					val = val * 8 + (*pos++ - '0');
5873ff40c12SJohn Marino 				if (*pos >= '0' && *pos <= '7')
5883ff40c12SJohn Marino 					val = val * 8 + (*pos++ - '0');
5893ff40c12SJohn Marino 				buf[len++] = val;
5903ff40c12SJohn Marino 				break;
5913ff40c12SJohn Marino 			default:
5923ff40c12SJohn Marino 				break;
5933ff40c12SJohn Marino 			}
5943ff40c12SJohn Marino 			break;
5953ff40c12SJohn Marino 		default:
5963ff40c12SJohn Marino 			buf[len++] = *pos++;
5973ff40c12SJohn Marino 			break;
5983ff40c12SJohn Marino 		}
5993ff40c12SJohn Marino 	}
6003ff40c12SJohn Marino 	if (maxlen > len)
6013ff40c12SJohn Marino 		buf[len] = '\0';
6023ff40c12SJohn Marino 
6033ff40c12SJohn Marino 	return len;
6043ff40c12SJohn Marino }
6053ff40c12SJohn Marino 
6063ff40c12SJohn Marino 
6076d49e1aeSJan Lentfer /**
6086d49e1aeSJan Lentfer  * wpa_ssid_txt - Convert SSID to a printable string
6096d49e1aeSJan Lentfer  * @ssid: SSID (32-octet string)
6106d49e1aeSJan Lentfer  * @ssid_len: Length of ssid in octets
6116d49e1aeSJan Lentfer  * Returns: Pointer to a printable string
6126d49e1aeSJan Lentfer  *
6136d49e1aeSJan Lentfer  * This function can be used to convert SSIDs into printable form. In most
6146d49e1aeSJan Lentfer  * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
6156d49e1aeSJan Lentfer  * does not limit the used character set, so anything could be used in an SSID.
6166d49e1aeSJan Lentfer  *
6176d49e1aeSJan Lentfer  * This function uses a static buffer, so only one call can be used at the
6186d49e1aeSJan Lentfer  * time, i.e., this is not re-entrant and the returned buffer must be used
6196d49e1aeSJan Lentfer  * before calling this again.
6206d49e1aeSJan Lentfer  */
wpa_ssid_txt(const u8 * ssid,size_t ssid_len)6216d49e1aeSJan Lentfer const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
6226d49e1aeSJan Lentfer {
623*a1157835SDaniel Fojt 	static char ssid_txt[SSID_MAX_LEN * 4 + 1];
6246d49e1aeSJan Lentfer 
6253ff40c12SJohn Marino 	if (ssid == NULL) {
6263ff40c12SJohn Marino 		ssid_txt[0] = '\0';
6273ff40c12SJohn Marino 		return ssid_txt;
6286d49e1aeSJan Lentfer 	}
6293ff40c12SJohn Marino 
6303ff40c12SJohn Marino 	printf_encode(ssid_txt, sizeof(ssid_txt), ssid, ssid_len);
6316d49e1aeSJan Lentfer 	return ssid_txt;
6326d49e1aeSJan Lentfer }
6336d49e1aeSJan Lentfer 
6346d49e1aeSJan Lentfer 
__hide_aliasing_typecast(void * foo)6356d49e1aeSJan Lentfer void * __hide_aliasing_typecast(void *foo)
6366d49e1aeSJan Lentfer {
6376d49e1aeSJan Lentfer 	return foo;
6386d49e1aeSJan Lentfer }
6393ff40c12SJohn Marino 
6403ff40c12SJohn Marino 
wpa_config_parse_string(const char * value,size_t * len)6413ff40c12SJohn Marino char * wpa_config_parse_string(const char *value, size_t *len)
6423ff40c12SJohn Marino {
6433ff40c12SJohn Marino 	if (*value == '"') {
6443ff40c12SJohn Marino 		const char *pos;
6453ff40c12SJohn Marino 		char *str;
6463ff40c12SJohn Marino 		value++;
6473ff40c12SJohn Marino 		pos = os_strrchr(value, '"');
6483ff40c12SJohn Marino 		if (pos == NULL || pos[1] != '\0')
6493ff40c12SJohn Marino 			return NULL;
6503ff40c12SJohn Marino 		*len = pos - value;
6513ff40c12SJohn Marino 		str = dup_binstr(value, *len);
6523ff40c12SJohn Marino 		if (str == NULL)
6533ff40c12SJohn Marino 			return NULL;
6543ff40c12SJohn Marino 		return str;
6553ff40c12SJohn Marino 	} else if (*value == 'P' && value[1] == '"') {
6563ff40c12SJohn Marino 		const char *pos;
6573ff40c12SJohn Marino 		char *tstr, *str;
6583ff40c12SJohn Marino 		size_t tlen;
6593ff40c12SJohn Marino 		value += 2;
6603ff40c12SJohn Marino 		pos = os_strrchr(value, '"');
6613ff40c12SJohn Marino 		if (pos == NULL || pos[1] != '\0')
6623ff40c12SJohn Marino 			return NULL;
6633ff40c12SJohn Marino 		tlen = pos - value;
6643ff40c12SJohn Marino 		tstr = dup_binstr(value, tlen);
6653ff40c12SJohn Marino 		if (tstr == NULL)
6663ff40c12SJohn Marino 			return NULL;
6673ff40c12SJohn Marino 
6683ff40c12SJohn Marino 		str = os_malloc(tlen + 1);
6693ff40c12SJohn Marino 		if (str == NULL) {
6703ff40c12SJohn Marino 			os_free(tstr);
6713ff40c12SJohn Marino 			return NULL;
6723ff40c12SJohn Marino 		}
6733ff40c12SJohn Marino 
6743ff40c12SJohn Marino 		*len = printf_decode((u8 *) str, tlen + 1, tstr);
6753ff40c12SJohn Marino 		os_free(tstr);
6763ff40c12SJohn Marino 
6773ff40c12SJohn Marino 		return str;
6783ff40c12SJohn Marino 	} else {
6793ff40c12SJohn Marino 		u8 *str;
6803ff40c12SJohn Marino 		size_t tlen, hlen = os_strlen(value);
6813ff40c12SJohn Marino 		if (hlen & 1)
6823ff40c12SJohn Marino 			return NULL;
6833ff40c12SJohn Marino 		tlen = hlen / 2;
6843ff40c12SJohn Marino 		str = os_malloc(tlen + 1);
6853ff40c12SJohn Marino 		if (str == NULL)
6863ff40c12SJohn Marino 			return NULL;
6873ff40c12SJohn Marino 		if (hexstr2bin(value, str, tlen)) {
6883ff40c12SJohn Marino 			os_free(str);
6893ff40c12SJohn Marino 			return NULL;
6903ff40c12SJohn Marino 		}
6913ff40c12SJohn Marino 		str[tlen] = '\0';
6923ff40c12SJohn Marino 		*len = tlen;
6933ff40c12SJohn Marino 		return (char *) str;
6943ff40c12SJohn Marino 	}
6953ff40c12SJohn Marino }
6963ff40c12SJohn Marino 
6973ff40c12SJohn Marino 
is_hex(const u8 * data,size_t len)6983ff40c12SJohn Marino int is_hex(const u8 *data, size_t len)
6993ff40c12SJohn Marino {
7003ff40c12SJohn Marino 	size_t i;
7013ff40c12SJohn Marino 
7023ff40c12SJohn Marino 	for (i = 0; i < len; i++) {
7033ff40c12SJohn Marino 		if (data[i] < 32 || data[i] >= 127)
7043ff40c12SJohn Marino 			return 1;
7053ff40c12SJohn Marino 	}
7063ff40c12SJohn Marino 	return 0;
7073ff40c12SJohn Marino }
7083ff40c12SJohn Marino 
7093ff40c12SJohn Marino 
has_ctrl_char(const u8 * data,size_t len)710*a1157835SDaniel Fojt int has_ctrl_char(const u8 *data, size_t len)
7113ff40c12SJohn Marino {
712*a1157835SDaniel Fojt 	size_t i;
7133ff40c12SJohn Marino 
714*a1157835SDaniel Fojt 	for (i = 0; i < len; i++) {
715*a1157835SDaniel Fojt 		if (data[i] < 32 || data[i] == 127)
716*a1157835SDaniel Fojt 			return 1;
717*a1157835SDaniel Fojt 	}
718*a1157835SDaniel Fojt 	return 0;
7193ff40c12SJohn Marino }
7203ff40c12SJohn Marino 
721*a1157835SDaniel Fojt 
has_newline(const char * str)722*a1157835SDaniel Fojt int has_newline(const char *str)
723*a1157835SDaniel Fojt {
724*a1157835SDaniel Fojt 	while (*str) {
725*a1157835SDaniel Fojt 		if (*str == '\n' || *str == '\r')
726*a1157835SDaniel Fojt 			return 1;
727*a1157835SDaniel Fojt 		str++;
728*a1157835SDaniel Fojt 	}
729*a1157835SDaniel Fojt 	return 0;
7303ff40c12SJohn Marino }
7313ff40c12SJohn Marino 
7323ff40c12SJohn Marino 
merge_byte_arrays(u8 * res,size_t res_len,const u8 * src1,size_t src1_len,const u8 * src2,size_t src2_len)7333ff40c12SJohn Marino size_t merge_byte_arrays(u8 *res, size_t res_len,
7343ff40c12SJohn Marino 			 const u8 *src1, size_t src1_len,
7353ff40c12SJohn Marino 			 const u8 *src2, size_t src2_len)
7363ff40c12SJohn Marino {
7373ff40c12SJohn Marino 	size_t len = 0;
7383ff40c12SJohn Marino 
7393ff40c12SJohn Marino 	os_memset(res, 0, res_len);
7403ff40c12SJohn Marino 
7413ff40c12SJohn Marino 	if (src1) {
7423ff40c12SJohn Marino 		if (src1_len >= res_len) {
7433ff40c12SJohn Marino 			os_memcpy(res, src1, res_len);
7443ff40c12SJohn Marino 			return res_len;
7453ff40c12SJohn Marino 		}
7463ff40c12SJohn Marino 
7473ff40c12SJohn Marino 		os_memcpy(res, src1, src1_len);
7483ff40c12SJohn Marino 		len += src1_len;
7493ff40c12SJohn Marino 	}
7503ff40c12SJohn Marino 
7513ff40c12SJohn Marino 	if (src2) {
7523ff40c12SJohn Marino 		if (len + src2_len >= res_len) {
7533ff40c12SJohn Marino 			os_memcpy(res + len, src2, res_len - len);
7543ff40c12SJohn Marino 			return res_len;
7553ff40c12SJohn Marino 		}
7563ff40c12SJohn Marino 
7573ff40c12SJohn Marino 		os_memcpy(res + len, src2, src2_len);
7583ff40c12SJohn Marino 		len += src2_len;
7593ff40c12SJohn Marino 	}
7603ff40c12SJohn Marino 
7613ff40c12SJohn Marino 	return len;
7623ff40c12SJohn Marino }
7633ff40c12SJohn Marino 
7643ff40c12SJohn Marino 
dup_binstr(const void * src,size_t len)7653ff40c12SJohn Marino char * dup_binstr(const void *src, size_t len)
7663ff40c12SJohn Marino {
7673ff40c12SJohn Marino 	char *res;
7683ff40c12SJohn Marino 
7693ff40c12SJohn Marino 	if (src == NULL)
7703ff40c12SJohn Marino 		return NULL;
7713ff40c12SJohn Marino 	res = os_malloc(len + 1);
7723ff40c12SJohn Marino 	if (res == NULL)
7733ff40c12SJohn Marino 		return NULL;
7743ff40c12SJohn Marino 	os_memcpy(res, src, len);
7753ff40c12SJohn Marino 	res[len] = '\0';
7763ff40c12SJohn Marino 
7773ff40c12SJohn Marino 	return res;
7783ff40c12SJohn Marino }
7793ff40c12SJohn Marino 
7803ff40c12SJohn Marino 
freq_range_list_parse(struct wpa_freq_range_list * res,const char * value)7813ff40c12SJohn Marino int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value)
7823ff40c12SJohn Marino {
7833ff40c12SJohn Marino 	struct wpa_freq_range *freq = NULL, *n;
7843ff40c12SJohn Marino 	unsigned int count = 0;
7853ff40c12SJohn Marino 	const char *pos, *pos2, *pos3;
7863ff40c12SJohn Marino 
7873ff40c12SJohn Marino 	/*
7883ff40c12SJohn Marino 	 * Comma separated list of frequency ranges.
7893ff40c12SJohn Marino 	 * For example: 2412-2432,2462,5000-6000
7903ff40c12SJohn Marino 	 */
7913ff40c12SJohn Marino 	pos = value;
7923ff40c12SJohn Marino 	while (pos && pos[0]) {
7933ff40c12SJohn Marino 		n = os_realloc_array(freq, count + 1,
7943ff40c12SJohn Marino 				     sizeof(struct wpa_freq_range));
7953ff40c12SJohn Marino 		if (n == NULL) {
7963ff40c12SJohn Marino 			os_free(freq);
7973ff40c12SJohn Marino 			return -1;
7983ff40c12SJohn Marino 		}
7993ff40c12SJohn Marino 		freq = n;
8003ff40c12SJohn Marino 		freq[count].min = atoi(pos);
8013ff40c12SJohn Marino 		pos2 = os_strchr(pos, '-');
8023ff40c12SJohn Marino 		pos3 = os_strchr(pos, ',');
8033ff40c12SJohn Marino 		if (pos2 && (!pos3 || pos2 < pos3)) {
8043ff40c12SJohn Marino 			pos2++;
8053ff40c12SJohn Marino 			freq[count].max = atoi(pos2);
8063ff40c12SJohn Marino 		} else
8073ff40c12SJohn Marino 			freq[count].max = freq[count].min;
8083ff40c12SJohn Marino 		pos = pos3;
8093ff40c12SJohn Marino 		if (pos)
8103ff40c12SJohn Marino 			pos++;
8113ff40c12SJohn Marino 		count++;
8123ff40c12SJohn Marino 	}
8133ff40c12SJohn Marino 
8143ff40c12SJohn Marino 	os_free(res->range);
8153ff40c12SJohn Marino 	res->range = freq;
8163ff40c12SJohn Marino 	res->num = count;
8173ff40c12SJohn Marino 
8183ff40c12SJohn Marino 	return 0;
8193ff40c12SJohn Marino }
8203ff40c12SJohn Marino 
8213ff40c12SJohn Marino 
freq_range_list_includes(const struct wpa_freq_range_list * list,unsigned int freq)8223ff40c12SJohn Marino int freq_range_list_includes(const struct wpa_freq_range_list *list,
8233ff40c12SJohn Marino 			     unsigned int freq)
8243ff40c12SJohn Marino {
8253ff40c12SJohn Marino 	unsigned int i;
8263ff40c12SJohn Marino 
8273ff40c12SJohn Marino 	if (list == NULL)
8283ff40c12SJohn Marino 		return 0;
8293ff40c12SJohn Marino 
8303ff40c12SJohn Marino 	for (i = 0; i < list->num; i++) {
8313ff40c12SJohn Marino 		if (freq >= list->range[i].min && freq <= list->range[i].max)
8323ff40c12SJohn Marino 			return 1;
8333ff40c12SJohn Marino 	}
8343ff40c12SJohn Marino 
8353ff40c12SJohn Marino 	return 0;
8363ff40c12SJohn Marino }
8373ff40c12SJohn Marino 
8383ff40c12SJohn Marino 
freq_range_list_str(const struct wpa_freq_range_list * list)8393ff40c12SJohn Marino char * freq_range_list_str(const struct wpa_freq_range_list *list)
8403ff40c12SJohn Marino {
8413ff40c12SJohn Marino 	char *buf, *pos, *end;
8423ff40c12SJohn Marino 	size_t maxlen;
8433ff40c12SJohn Marino 	unsigned int i;
8443ff40c12SJohn Marino 	int res;
8453ff40c12SJohn Marino 
8463ff40c12SJohn Marino 	if (list->num == 0)
8473ff40c12SJohn Marino 		return NULL;
8483ff40c12SJohn Marino 
8493ff40c12SJohn Marino 	maxlen = list->num * 30;
8503ff40c12SJohn Marino 	buf = os_malloc(maxlen);
8513ff40c12SJohn Marino 	if (buf == NULL)
8523ff40c12SJohn Marino 		return NULL;
8533ff40c12SJohn Marino 	pos = buf;
8543ff40c12SJohn Marino 	end = buf + maxlen;
8553ff40c12SJohn Marino 
8563ff40c12SJohn Marino 	for (i = 0; i < list->num; i++) {
8573ff40c12SJohn Marino 		struct wpa_freq_range *range = &list->range[i];
8583ff40c12SJohn Marino 
8593ff40c12SJohn Marino 		if (range->min == range->max)
8603ff40c12SJohn Marino 			res = os_snprintf(pos, end - pos, "%s%u",
8613ff40c12SJohn Marino 					  i == 0 ? "" : ",", range->min);
8623ff40c12SJohn Marino 		else
8633ff40c12SJohn Marino 			res = os_snprintf(pos, end - pos, "%s%u-%u",
8643ff40c12SJohn Marino 					  i == 0 ? "" : ",",
8653ff40c12SJohn Marino 					  range->min, range->max);
866*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, res)) {
8673ff40c12SJohn Marino 			os_free(buf);
8683ff40c12SJohn Marino 			return NULL;
8693ff40c12SJohn Marino 		}
8703ff40c12SJohn Marino 		pos += res;
8713ff40c12SJohn Marino 	}
8723ff40c12SJohn Marino 
8733ff40c12SJohn Marino 	return buf;
8743ff40c12SJohn Marino }
8753ff40c12SJohn Marino 
8763ff40c12SJohn Marino 
int_array_len(const int * a)8773ff40c12SJohn Marino int int_array_len(const int *a)
8783ff40c12SJohn Marino {
8793ff40c12SJohn Marino 	int i;
8803ff40c12SJohn Marino 	for (i = 0; a && a[i]; i++)
8813ff40c12SJohn Marino 		;
8823ff40c12SJohn Marino 	return i;
8833ff40c12SJohn Marino }
8843ff40c12SJohn Marino 
8853ff40c12SJohn Marino 
int_array_concat(int ** res,const int * a)8863ff40c12SJohn Marino void int_array_concat(int **res, const int *a)
8873ff40c12SJohn Marino {
8883ff40c12SJohn Marino 	int reslen, alen, i;
8893ff40c12SJohn Marino 	int *n;
8903ff40c12SJohn Marino 
8913ff40c12SJohn Marino 	reslen = int_array_len(*res);
8923ff40c12SJohn Marino 	alen = int_array_len(a);
8933ff40c12SJohn Marino 
8943ff40c12SJohn Marino 	n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
8953ff40c12SJohn Marino 	if (n == NULL) {
8963ff40c12SJohn Marino 		os_free(*res);
8973ff40c12SJohn Marino 		*res = NULL;
8983ff40c12SJohn Marino 		return;
8993ff40c12SJohn Marino 	}
9003ff40c12SJohn Marino 	for (i = 0; i <= alen; i++)
9013ff40c12SJohn Marino 		n[reslen + i] = a[i];
9023ff40c12SJohn Marino 	*res = n;
9033ff40c12SJohn Marino }
9043ff40c12SJohn Marino 
9053ff40c12SJohn Marino 
freq_cmp(const void * a,const void * b)9063ff40c12SJohn Marino static int freq_cmp(const void *a, const void *b)
9073ff40c12SJohn Marino {
9083ff40c12SJohn Marino 	int _a = *(int *) a;
9093ff40c12SJohn Marino 	int _b = *(int *) b;
9103ff40c12SJohn Marino 
9113ff40c12SJohn Marino 	if (_a == 0)
9123ff40c12SJohn Marino 		return 1;
9133ff40c12SJohn Marino 	if (_b == 0)
9143ff40c12SJohn Marino 		return -1;
9153ff40c12SJohn Marino 	return _a - _b;
9163ff40c12SJohn Marino }
9173ff40c12SJohn Marino 
9183ff40c12SJohn Marino 
int_array_sort_unique(int * a)9193ff40c12SJohn Marino void int_array_sort_unique(int *a)
9203ff40c12SJohn Marino {
9213ff40c12SJohn Marino 	int alen;
9223ff40c12SJohn Marino 	int i, j;
9233ff40c12SJohn Marino 
9243ff40c12SJohn Marino 	if (a == NULL)
9253ff40c12SJohn Marino 		return;
9263ff40c12SJohn Marino 
9273ff40c12SJohn Marino 	alen = int_array_len(a);
9283ff40c12SJohn Marino 	qsort(a, alen, sizeof(int), freq_cmp);
9293ff40c12SJohn Marino 
9303ff40c12SJohn Marino 	i = 0;
9313ff40c12SJohn Marino 	j = 1;
9323ff40c12SJohn Marino 	while (a[i] && a[j]) {
9333ff40c12SJohn Marino 		if (a[i] == a[j]) {
9343ff40c12SJohn Marino 			j++;
9353ff40c12SJohn Marino 			continue;
9363ff40c12SJohn Marino 		}
9373ff40c12SJohn Marino 		a[++i] = a[j++];
9383ff40c12SJohn Marino 	}
9393ff40c12SJohn Marino 	if (a[i])
9403ff40c12SJohn Marino 		i++;
9413ff40c12SJohn Marino 	a[i] = 0;
9423ff40c12SJohn Marino }
9433ff40c12SJohn Marino 
9443ff40c12SJohn Marino 
int_array_add_unique(int ** res,int a)9453ff40c12SJohn Marino void int_array_add_unique(int **res, int a)
9463ff40c12SJohn Marino {
9473ff40c12SJohn Marino 	int reslen;
9483ff40c12SJohn Marino 	int *n;
9493ff40c12SJohn Marino 
9503ff40c12SJohn Marino 	for (reslen = 0; *res && (*res)[reslen]; reslen++) {
9513ff40c12SJohn Marino 		if ((*res)[reslen] == a)
9523ff40c12SJohn Marino 			return; /* already in the list */
9533ff40c12SJohn Marino 	}
9543ff40c12SJohn Marino 
9553ff40c12SJohn Marino 	n = os_realloc_array(*res, reslen + 2, sizeof(int));
9563ff40c12SJohn Marino 	if (n == NULL) {
9573ff40c12SJohn Marino 		os_free(*res);
9583ff40c12SJohn Marino 		*res = NULL;
9593ff40c12SJohn Marino 		return;
9603ff40c12SJohn Marino 	}
9613ff40c12SJohn Marino 
9623ff40c12SJohn Marino 	n[reslen] = a;
9633ff40c12SJohn Marino 	n[reslen + 1] = 0;
9643ff40c12SJohn Marino 
9653ff40c12SJohn Marino 	*res = n;
9663ff40c12SJohn Marino }
967*a1157835SDaniel Fojt 
968*a1157835SDaniel Fojt 
str_clear_free(char * str)969*a1157835SDaniel Fojt void str_clear_free(char *str)
970*a1157835SDaniel Fojt {
971*a1157835SDaniel Fojt 	if (str) {
972*a1157835SDaniel Fojt 		size_t len = os_strlen(str);
973*a1157835SDaniel Fojt 		forced_memzero(str, len);
974*a1157835SDaniel Fojt 		os_free(str);
975*a1157835SDaniel Fojt 	}
976*a1157835SDaniel Fojt }
977*a1157835SDaniel Fojt 
978*a1157835SDaniel Fojt 
bin_clear_free(void * bin,size_t len)979*a1157835SDaniel Fojt void bin_clear_free(void *bin, size_t len)
980*a1157835SDaniel Fojt {
981*a1157835SDaniel Fojt 	if (bin) {
982*a1157835SDaniel Fojt 		forced_memzero(bin, len);
983*a1157835SDaniel Fojt 		os_free(bin);
984*a1157835SDaniel Fojt 	}
985*a1157835SDaniel Fojt }
986*a1157835SDaniel Fojt 
987*a1157835SDaniel Fojt 
random_mac_addr(u8 * addr)988*a1157835SDaniel Fojt int random_mac_addr(u8 *addr)
989*a1157835SDaniel Fojt {
990*a1157835SDaniel Fojt 	if (os_get_random(addr, ETH_ALEN) < 0)
991*a1157835SDaniel Fojt 		return -1;
992*a1157835SDaniel Fojt 	addr[0] &= 0xfe; /* unicast */
993*a1157835SDaniel Fojt 	addr[0] |= 0x02; /* locally administered */
994*a1157835SDaniel Fojt 	return 0;
995*a1157835SDaniel Fojt }
996*a1157835SDaniel Fojt 
997*a1157835SDaniel Fojt 
random_mac_addr_keep_oui(u8 * addr)998*a1157835SDaniel Fojt int random_mac_addr_keep_oui(u8 *addr)
999*a1157835SDaniel Fojt {
1000*a1157835SDaniel Fojt 	if (os_get_random(addr + 3, 3) < 0)
1001*a1157835SDaniel Fojt 		return -1;
1002*a1157835SDaniel Fojt 	addr[0] &= 0xfe; /* unicast */
1003*a1157835SDaniel Fojt 	addr[0] |= 0x02; /* locally administered */
1004*a1157835SDaniel Fojt 	return 0;
1005*a1157835SDaniel Fojt }
1006*a1157835SDaniel Fojt 
1007*a1157835SDaniel Fojt 
1008*a1157835SDaniel Fojt /**
1009*a1157835SDaniel Fojt  * cstr_token - Get next token from const char string
1010*a1157835SDaniel Fojt  * @str: a constant string to tokenize
1011*a1157835SDaniel Fojt  * @delim: a string of delimiters
1012*a1157835SDaniel Fojt  * @last: a pointer to a character following the returned token
1013*a1157835SDaniel Fojt  *      It has to be set to NULL for the first call and passed for any
1014*a1157835SDaniel Fojt  *      further call.
1015*a1157835SDaniel Fojt  * Returns: a pointer to token position in str or NULL
1016*a1157835SDaniel Fojt  *
1017*a1157835SDaniel Fojt  * This function is similar to str_token, but it can be used with both
1018*a1157835SDaniel Fojt  * char and const char strings. Differences:
1019*a1157835SDaniel Fojt  * - The str buffer remains unmodified
1020*a1157835SDaniel Fojt  * - The returned token is not a NULL terminated string, but a token
1021*a1157835SDaniel Fojt  *   position in str buffer. If a return value is not NULL a size
1022*a1157835SDaniel Fojt  *   of the returned token could be calculated as (last - token).
1023*a1157835SDaniel Fojt  */
cstr_token(const char * str,const char * delim,const char ** last)1024*a1157835SDaniel Fojt const char * cstr_token(const char *str, const char *delim, const char **last)
1025*a1157835SDaniel Fojt {
1026*a1157835SDaniel Fojt 	const char *end, *token = str;
1027*a1157835SDaniel Fojt 
1028*a1157835SDaniel Fojt 	if (!str || !delim || !last)
1029*a1157835SDaniel Fojt 		return NULL;
1030*a1157835SDaniel Fojt 
1031*a1157835SDaniel Fojt 	if (*last)
1032*a1157835SDaniel Fojt 		token = *last;
1033*a1157835SDaniel Fojt 
1034*a1157835SDaniel Fojt 	while (*token && os_strchr(delim, *token))
1035*a1157835SDaniel Fojt 		token++;
1036*a1157835SDaniel Fojt 
1037*a1157835SDaniel Fojt 	if (!*token)
1038*a1157835SDaniel Fojt 		return NULL;
1039*a1157835SDaniel Fojt 
1040*a1157835SDaniel Fojt 	end = token + 1;
1041*a1157835SDaniel Fojt 
1042*a1157835SDaniel Fojt 	while (*end && !os_strchr(delim, *end))
1043*a1157835SDaniel Fojt 		end++;
1044*a1157835SDaniel Fojt 
1045*a1157835SDaniel Fojt 	*last = end;
1046*a1157835SDaniel Fojt 	return token;
1047*a1157835SDaniel Fojt }
1048*a1157835SDaniel Fojt 
1049*a1157835SDaniel Fojt 
1050*a1157835SDaniel Fojt /**
1051*a1157835SDaniel Fojt  * str_token - Get next token from a string
1052*a1157835SDaniel Fojt  * @buf: String to tokenize. Note that the string might be modified.
1053*a1157835SDaniel Fojt  * @delim: String of delimiters
1054*a1157835SDaniel Fojt  * @context: Pointer to save our context. Should be initialized with
1055*a1157835SDaniel Fojt  *	NULL on the first call, and passed for any further call.
1056*a1157835SDaniel Fojt  * Returns: The next token, NULL if there are no more valid tokens.
1057*a1157835SDaniel Fojt  */
str_token(char * str,const char * delim,char ** context)1058*a1157835SDaniel Fojt char * str_token(char *str, const char *delim, char **context)
1059*a1157835SDaniel Fojt {
1060*a1157835SDaniel Fojt 	char *token = (char *) cstr_token(str, delim, (const char **) context);
1061*a1157835SDaniel Fojt 
1062*a1157835SDaniel Fojt 	if (token && **context)
1063*a1157835SDaniel Fojt 		*(*context)++ = '\0';
1064*a1157835SDaniel Fojt 
1065*a1157835SDaniel Fojt 	return token;
1066*a1157835SDaniel Fojt }
1067*a1157835SDaniel Fojt 
1068*a1157835SDaniel Fojt 
utf8_unescape(const char * inp,size_t in_size,char * outp,size_t out_size)1069*a1157835SDaniel Fojt size_t utf8_unescape(const char *inp, size_t in_size,
1070*a1157835SDaniel Fojt 		     char *outp, size_t out_size)
1071*a1157835SDaniel Fojt {
1072*a1157835SDaniel Fojt 	size_t res_size = 0;
1073*a1157835SDaniel Fojt 
1074*a1157835SDaniel Fojt 	if (!inp || !outp)
1075*a1157835SDaniel Fojt 		return 0;
1076*a1157835SDaniel Fojt 
1077*a1157835SDaniel Fojt 	if (!in_size)
1078*a1157835SDaniel Fojt 		in_size = os_strlen(inp);
1079*a1157835SDaniel Fojt 
1080*a1157835SDaniel Fojt 	/* Advance past leading single quote */
1081*a1157835SDaniel Fojt 	if (*inp == '\'' && in_size) {
1082*a1157835SDaniel Fojt 		inp++;
1083*a1157835SDaniel Fojt 		in_size--;
1084*a1157835SDaniel Fojt 	}
1085*a1157835SDaniel Fojt 
1086*a1157835SDaniel Fojt 	while (in_size) {
1087*a1157835SDaniel Fojt 		in_size--;
1088*a1157835SDaniel Fojt 		if (res_size >= out_size)
1089*a1157835SDaniel Fojt 			return 0;
1090*a1157835SDaniel Fojt 
1091*a1157835SDaniel Fojt 		switch (*inp) {
1092*a1157835SDaniel Fojt 		case '\'':
1093*a1157835SDaniel Fojt 			/* Terminate on bare single quote */
1094*a1157835SDaniel Fojt 			*outp = '\0';
1095*a1157835SDaniel Fojt 			return res_size;
1096*a1157835SDaniel Fojt 
1097*a1157835SDaniel Fojt 		case '\\':
1098*a1157835SDaniel Fojt 			if (!in_size)
1099*a1157835SDaniel Fojt 				return 0;
1100*a1157835SDaniel Fojt 			in_size--;
1101*a1157835SDaniel Fojt 			inp++;
1102*a1157835SDaniel Fojt 			/* fall through */
1103*a1157835SDaniel Fojt 
1104*a1157835SDaniel Fojt 		default:
1105*a1157835SDaniel Fojt 			*outp++ = *inp++;
1106*a1157835SDaniel Fojt 			res_size++;
1107*a1157835SDaniel Fojt 		}
1108*a1157835SDaniel Fojt 	}
1109*a1157835SDaniel Fojt 
1110*a1157835SDaniel Fojt 	/* NUL terminate if space allows */
1111*a1157835SDaniel Fojt 	if (res_size < out_size)
1112*a1157835SDaniel Fojt 		*outp = '\0';
1113*a1157835SDaniel Fojt 
1114*a1157835SDaniel Fojt 	return res_size;
1115*a1157835SDaniel Fojt }
1116*a1157835SDaniel Fojt 
1117*a1157835SDaniel Fojt 
utf8_escape(const char * inp,size_t in_size,char * outp,size_t out_size)1118*a1157835SDaniel Fojt size_t utf8_escape(const char *inp, size_t in_size,
1119*a1157835SDaniel Fojt 		   char *outp, size_t out_size)
1120*a1157835SDaniel Fojt {
1121*a1157835SDaniel Fojt 	size_t res_size = 0;
1122*a1157835SDaniel Fojt 
1123*a1157835SDaniel Fojt 	if (!inp || !outp)
1124*a1157835SDaniel Fojt 		return 0;
1125*a1157835SDaniel Fojt 
1126*a1157835SDaniel Fojt 	/* inp may or may not be NUL terminated, but must be if 0 size
1127*a1157835SDaniel Fojt 	 * is specified */
1128*a1157835SDaniel Fojt 	if (!in_size)
1129*a1157835SDaniel Fojt 		in_size = os_strlen(inp);
1130*a1157835SDaniel Fojt 
1131*a1157835SDaniel Fojt 	while (in_size) {
1132*a1157835SDaniel Fojt 		in_size--;
1133*a1157835SDaniel Fojt 		if (res_size++ >= out_size)
1134*a1157835SDaniel Fojt 			return 0;
1135*a1157835SDaniel Fojt 
1136*a1157835SDaniel Fojt 		switch (*inp) {
1137*a1157835SDaniel Fojt 		case '\\':
1138*a1157835SDaniel Fojt 		case '\'':
1139*a1157835SDaniel Fojt 			if (res_size++ >= out_size)
1140*a1157835SDaniel Fojt 				return 0;
1141*a1157835SDaniel Fojt 			*outp++ = '\\';
1142*a1157835SDaniel Fojt 			/* fall through */
1143*a1157835SDaniel Fojt 
1144*a1157835SDaniel Fojt 		default:
1145*a1157835SDaniel Fojt 			*outp++ = *inp++;
1146*a1157835SDaniel Fojt 			break;
1147*a1157835SDaniel Fojt 		}
1148*a1157835SDaniel Fojt 	}
1149*a1157835SDaniel Fojt 
1150*a1157835SDaniel Fojt 	/* NUL terminate if space allows */
1151*a1157835SDaniel Fojt 	if (res_size < out_size)
1152*a1157835SDaniel Fojt 		*outp = '\0';
1153*a1157835SDaniel Fojt 
1154*a1157835SDaniel Fojt 	return res_size;
1155*a1157835SDaniel Fojt }
1156*a1157835SDaniel Fojt 
1157*a1157835SDaniel Fojt 
is_ctrl_char(char c)1158*a1157835SDaniel Fojt int is_ctrl_char(char c)
1159*a1157835SDaniel Fojt {
1160*a1157835SDaniel Fojt 	return c > 0 && c < 32;
1161*a1157835SDaniel Fojt }
1162*a1157835SDaniel Fojt 
1163*a1157835SDaniel Fojt 
1164*a1157835SDaniel Fojt /**
1165*a1157835SDaniel Fojt  * ssid_parse - Parse a string that contains SSID in hex or text format
1166*a1157835SDaniel Fojt  * @buf: Input NULL terminated string that contains the SSID
1167*a1157835SDaniel Fojt  * @ssid: Output SSID
1168*a1157835SDaniel Fojt  * Returns: 0 on success, -1 otherwise
1169*a1157835SDaniel Fojt  *
1170*a1157835SDaniel Fojt  * The SSID has to be enclosed in double quotes for the text format or space
1171*a1157835SDaniel Fojt  * or NULL terminated string of hex digits for the hex format. buf can include
1172*a1157835SDaniel Fojt  * additional arguments after the SSID.
1173*a1157835SDaniel Fojt  */
ssid_parse(const char * buf,struct wpa_ssid_value * ssid)1174*a1157835SDaniel Fojt int ssid_parse(const char *buf, struct wpa_ssid_value *ssid)
1175*a1157835SDaniel Fojt {
1176*a1157835SDaniel Fojt 	char *tmp, *res, *end;
1177*a1157835SDaniel Fojt 	size_t len;
1178*a1157835SDaniel Fojt 
1179*a1157835SDaniel Fojt 	ssid->ssid_len = 0;
1180*a1157835SDaniel Fojt 
1181*a1157835SDaniel Fojt 	tmp = os_strdup(buf);
1182*a1157835SDaniel Fojt 	if (!tmp)
1183*a1157835SDaniel Fojt 		return -1;
1184*a1157835SDaniel Fojt 
1185*a1157835SDaniel Fojt 	if (*tmp != '"') {
1186*a1157835SDaniel Fojt 		end = os_strchr(tmp, ' ');
1187*a1157835SDaniel Fojt 		if (end)
1188*a1157835SDaniel Fojt 			*end = '\0';
1189*a1157835SDaniel Fojt 	} else {
1190*a1157835SDaniel Fojt 		end = os_strchr(tmp + 1, '"');
1191*a1157835SDaniel Fojt 		if (!end) {
1192*a1157835SDaniel Fojt 			os_free(tmp);
1193*a1157835SDaniel Fojt 			return -1;
1194*a1157835SDaniel Fojt 		}
1195*a1157835SDaniel Fojt 
1196*a1157835SDaniel Fojt 		end[1] = '\0';
1197*a1157835SDaniel Fojt 	}
1198*a1157835SDaniel Fojt 
1199*a1157835SDaniel Fojt 	res = wpa_config_parse_string(tmp, &len);
1200*a1157835SDaniel Fojt 	if (res && len <= SSID_MAX_LEN) {
1201*a1157835SDaniel Fojt 		ssid->ssid_len = len;
1202*a1157835SDaniel Fojt 		os_memcpy(ssid->ssid, res, len);
1203*a1157835SDaniel Fojt 	}
1204*a1157835SDaniel Fojt 
1205*a1157835SDaniel Fojt 	os_free(tmp);
1206*a1157835SDaniel Fojt 	os_free(res);
1207*a1157835SDaniel Fojt 
1208*a1157835SDaniel Fojt 	return ssid->ssid_len ? 0 : -1;
1209*a1157835SDaniel Fojt }
1210*a1157835SDaniel Fojt 
1211*a1157835SDaniel Fojt 
str_starts(const char * str,const char * start)1212*a1157835SDaniel Fojt int str_starts(const char *str, const char *start)
1213*a1157835SDaniel Fojt {
1214*a1157835SDaniel Fojt 	return os_strncmp(str, start, os_strlen(start)) == 0;
1215*a1157835SDaniel Fojt }
1216*a1157835SDaniel Fojt 
1217*a1157835SDaniel Fojt 
1218*a1157835SDaniel Fojt /**
1219*a1157835SDaniel Fojt  * rssi_to_rcpi - Convert RSSI to RCPI
1220*a1157835SDaniel Fojt  * @rssi: RSSI to convert
1221*a1157835SDaniel Fojt  * Returns: RCPI corresponding to the given RSSI value, or 255 if not available.
1222*a1157835SDaniel Fojt  *
1223*a1157835SDaniel Fojt  * It's possible to estimate RCPI based on RSSI in dBm. This calculation will
1224*a1157835SDaniel Fojt  * not reflect the correct value for high rates, but it's good enough for Action
1225*a1157835SDaniel Fojt  * frames which are transmitted with up to 24 Mbps rates.
1226*a1157835SDaniel Fojt  */
rssi_to_rcpi(int rssi)1227*a1157835SDaniel Fojt u8 rssi_to_rcpi(int rssi)
1228*a1157835SDaniel Fojt {
1229*a1157835SDaniel Fojt 	if (!rssi)
1230*a1157835SDaniel Fojt 		return 255; /* not available */
1231*a1157835SDaniel Fojt 	if (rssi < -110)
1232*a1157835SDaniel Fojt 		return 0;
1233*a1157835SDaniel Fojt 	if (rssi > 0)
1234*a1157835SDaniel Fojt 		return 220;
1235*a1157835SDaniel Fojt 	return (rssi + 110) * 2;
1236*a1157835SDaniel Fojt }
1237*a1157835SDaniel Fojt 
1238*a1157835SDaniel Fojt 
get_param(const char * cmd,const char * param)1239*a1157835SDaniel Fojt char * get_param(const char *cmd, const char *param)
1240*a1157835SDaniel Fojt {
1241*a1157835SDaniel Fojt 	const char *pos, *end;
1242*a1157835SDaniel Fojt 	char *val;
1243*a1157835SDaniel Fojt 	size_t len;
1244*a1157835SDaniel Fojt 
1245*a1157835SDaniel Fojt 	pos = os_strstr(cmd, param);
1246*a1157835SDaniel Fojt 	if (!pos)
1247*a1157835SDaniel Fojt 		return NULL;
1248*a1157835SDaniel Fojt 
1249*a1157835SDaniel Fojt 	pos += os_strlen(param);
1250*a1157835SDaniel Fojt 	end = os_strchr(pos, ' ');
1251*a1157835SDaniel Fojt 	if (end)
1252*a1157835SDaniel Fojt 		len = end - pos;
1253*a1157835SDaniel Fojt 	else
1254*a1157835SDaniel Fojt 		len = os_strlen(pos);
1255*a1157835SDaniel Fojt 	val = os_malloc(len + 1);
1256*a1157835SDaniel Fojt 	if (!val)
1257*a1157835SDaniel Fojt 		return NULL;
1258*a1157835SDaniel Fojt 	os_memcpy(val, pos, len);
1259*a1157835SDaniel Fojt 	val[len] = '\0';
1260*a1157835SDaniel Fojt 	return val;
1261*a1157835SDaniel Fojt }
1262*a1157835SDaniel Fojt 
1263*a1157835SDaniel Fojt 
1264*a1157835SDaniel Fojt /* Try to prevent most compilers from optimizing out clearing of memory that
1265*a1157835SDaniel Fojt  * becomes unaccessible after this function is called. This is mostly the case
1266*a1157835SDaniel Fojt  * for clearing local stack variables at the end of a function. This is not
1267*a1157835SDaniel Fojt  * exactly perfect, i.e., someone could come up with a compiler that figures out
1268*a1157835SDaniel Fojt  * the pointer is pointing to memset and then end up optimizing the call out, so
1269*a1157835SDaniel Fojt  * try go a bit further by storing the first octet (now zero) to make this even
1270*a1157835SDaniel Fojt  * a bit more difficult to optimize out. Once memset_s() is available, that
1271*a1157835SDaniel Fojt  * could be used here instead. */
1272*a1157835SDaniel Fojt static void * (* const volatile memset_func)(void *, int, size_t) = memset;
1273*a1157835SDaniel Fojt static u8 forced_memzero_val;
1274*a1157835SDaniel Fojt 
forced_memzero(void * ptr,size_t len)1275*a1157835SDaniel Fojt void forced_memzero(void *ptr, size_t len)
1276*a1157835SDaniel Fojt {
1277*a1157835SDaniel Fojt 	memset_func(ptr, 0, len);
1278*a1157835SDaniel Fojt 	if (len)
1279*a1157835SDaniel Fojt 		forced_memzero_val = ((u8 *) ptr)[0];
1280*a1157835SDaniel Fojt }
1281