1189251Ssam/*
2189251Ssam * wpa_supplicant/hostapd / common helper functions, etc.
3189251Ssam * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4189251Ssam *
5252726Srpaulo * This software may be distributed under the terms of the BSD license.
6252726Srpaulo * See README for more details.
7189251Ssam */
8189251Ssam
9189251Ssam#ifndef COMMON_H
10189251Ssam#define COMMON_H
11189251Ssam
12189251Ssam#include "os.h"
13189251Ssam
14214734Srpaulo#if defined(__linux__) || defined(__GLIBC__)
15189251Ssam#include <endian.h>
16189251Ssam#include <byteswap.h>
17189251Ssam#endif /* __linux__ */
18189251Ssam
19209158Srpaulo#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
20209158Srpaulo    defined(__OpenBSD__)
21189251Ssam#include <sys/types.h>
22189251Ssam#include <sys/endian.h>
23189251Ssam#define __BYTE_ORDER	_BYTE_ORDER
24189251Ssam#define	__LITTLE_ENDIAN	_LITTLE_ENDIAN
25189251Ssam#define	__BIG_ENDIAN	_BIG_ENDIAN
26209158Srpaulo#ifdef __OpenBSD__
27209158Srpaulo#define bswap_16 swap16
28209158Srpaulo#define bswap_32 swap32
29209158Srpaulo#define bswap_64 swap64
30209158Srpaulo#else /* __OpenBSD__ */
31189251Ssam#define bswap_16 bswap16
32189251Ssam#define bswap_32 bswap32
33189251Ssam#define bswap_64 bswap64
34209158Srpaulo#endif /* __OpenBSD__ */
35189251Ssam#endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
36209158Srpaulo	* defined(__DragonFly__) || defined(__OpenBSD__) */
37189251Ssam
38189251Ssam#ifdef __APPLE__
39189251Ssam#include <sys/types.h>
40189251Ssam#include <machine/endian.h>
41189251Ssam#define __BYTE_ORDER	_BYTE_ORDER
42189251Ssam#define __LITTLE_ENDIAN	_LITTLE_ENDIAN
43189251Ssam#define __BIG_ENDIAN	_BIG_ENDIAN
44189251Ssamstatic inline unsigned short bswap_16(unsigned short v)
45189251Ssam{
46189251Ssam	return ((v & 0xff) << 8) | (v >> 8);
47189251Ssam}
48189251Ssam
49189251Ssamstatic inline unsigned int bswap_32(unsigned int v)
50189251Ssam{
51189251Ssam	return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
52189251Ssam		((v & 0xff0000) >> 8) | (v >> 24);
53189251Ssam}
54189251Ssam#endif /* __APPLE__ */
55189251Ssam
56189251Ssam#ifdef CONFIG_NATIVE_WINDOWS
57189251Ssam#include <winsock.h>
58189251Ssam
59189251Ssamtypedef int socklen_t;
60189251Ssam
61189251Ssam#ifndef MSG_DONTWAIT
62189251Ssam#define MSG_DONTWAIT 0 /* not supported */
63189251Ssam#endif
64189251Ssam
65189251Ssam#endif /* CONFIG_NATIVE_WINDOWS */
66189251Ssam
67189251Ssam#ifdef _MSC_VER
68189251Ssam#define inline __inline
69189251Ssam
70189251Ssam#undef vsnprintf
71189251Ssam#define vsnprintf _vsnprintf
72189251Ssam#undef close
73189251Ssam#define close closesocket
74189251Ssam#endif /* _MSC_VER */
75189251Ssam
76189251Ssam
77189251Ssam/* Define platform specific integer types */
78189251Ssam
79189251Ssam#ifdef _MSC_VER
80189251Ssamtypedef UINT64 u64;
81189251Ssamtypedef UINT32 u32;
82189251Ssamtypedef UINT16 u16;
83189251Ssamtypedef UINT8 u8;
84189251Ssamtypedef INT64 s64;
85189251Ssamtypedef INT32 s32;
86189251Ssamtypedef INT16 s16;
87189251Ssamtypedef INT8 s8;
88189251Ssam#define WPA_TYPES_DEFINED
89189251Ssam#endif /* _MSC_VER */
90189251Ssam
91189251Ssam#ifdef __vxworks
92189251Ssamtypedef unsigned long long u64;
93189251Ssamtypedef UINT32 u32;
94189251Ssamtypedef UINT16 u16;
95189251Ssamtypedef UINT8 u8;
96189251Ssamtypedef long long s64;
97189251Ssamtypedef INT32 s32;
98189251Ssamtypedef INT16 s16;
99189251Ssamtypedef INT8 s8;
100189251Ssam#define WPA_TYPES_DEFINED
101189251Ssam#endif /* __vxworks */
102189251Ssam
103189251Ssam#ifndef WPA_TYPES_DEFINED
104189251Ssam#ifdef CONFIG_USE_INTTYPES_H
105189251Ssam#include <inttypes.h>
106189251Ssam#else
107189251Ssam#include <stdint.h>
108189251Ssam#endif
109189251Ssamtypedef uint64_t u64;
110189251Ssamtypedef uint32_t u32;
111189251Ssamtypedef uint16_t u16;
112189251Ssamtypedef uint8_t u8;
113189251Ssamtypedef int64_t s64;
114189251Ssamtypedef int32_t s32;
115189251Ssamtypedef int16_t s16;
116189251Ssamtypedef int8_t s8;
117189251Ssam#define WPA_TYPES_DEFINED
118189251Ssam#endif /* !WPA_TYPES_DEFINED */
119189251Ssam
120189251Ssam
121189251Ssam/* Define platform specific byte swapping macros */
122189251Ssam
123189251Ssam#if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
124189251Ssam
125189251Ssamstatic inline unsigned short wpa_swap_16(unsigned short v)
126189251Ssam{
127189251Ssam	return ((v & 0xff) << 8) | (v >> 8);
128189251Ssam}
129189251Ssam
130189251Ssamstatic inline unsigned int wpa_swap_32(unsigned int v)
131189251Ssam{
132189251Ssam	return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
133189251Ssam		((v & 0xff0000) >> 8) | (v >> 24);
134189251Ssam}
135189251Ssam
136189251Ssam#define le_to_host16(n) (n)
137189251Ssam#define host_to_le16(n) (n)
138189251Ssam#define be_to_host16(n) wpa_swap_16(n)
139189251Ssam#define host_to_be16(n) wpa_swap_16(n)
140189251Ssam#define le_to_host32(n) (n)
141281806Srpaulo#define host_to_le32(n) (n)
142189251Ssam#define be_to_host32(n) wpa_swap_32(n)
143189251Ssam#define host_to_be32(n) wpa_swap_32(n)
144189251Ssam
145189251Ssam#define WPA_BYTE_SWAP_DEFINED
146189251Ssam
147189251Ssam#endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
148189251Ssam
149189251Ssam
150189251Ssam#ifndef WPA_BYTE_SWAP_DEFINED
151189251Ssam
152189251Ssam#ifndef __BYTE_ORDER
153189251Ssam#ifndef __LITTLE_ENDIAN
154189251Ssam#ifndef __BIG_ENDIAN
155189251Ssam#define __LITTLE_ENDIAN 1234
156189251Ssam#define __BIG_ENDIAN 4321
157189251Ssam#if defined(sparc)
158189251Ssam#define __BYTE_ORDER __BIG_ENDIAN
159189251Ssam#endif
160189251Ssam#endif /* __BIG_ENDIAN */
161189251Ssam#endif /* __LITTLE_ENDIAN */
162189251Ssam#endif /* __BYTE_ORDER */
163189251Ssam
164189251Ssam#if __BYTE_ORDER == __LITTLE_ENDIAN
165189251Ssam#define le_to_host16(n) ((__force u16) (le16) (n))
166189251Ssam#define host_to_le16(n) ((__force le16) (u16) (n))
167189251Ssam#define be_to_host16(n) bswap_16((__force u16) (be16) (n))
168189251Ssam#define host_to_be16(n) ((__force be16) bswap_16((n)))
169189251Ssam#define le_to_host32(n) ((__force u32) (le32) (n))
170189251Ssam#define host_to_le32(n) ((__force le32) (u32) (n))
171189251Ssam#define be_to_host32(n) bswap_32((__force u32) (be32) (n))
172189251Ssam#define host_to_be32(n) ((__force be32) bswap_32((n)))
173189251Ssam#define le_to_host64(n) ((__force u64) (le64) (n))
174189251Ssam#define host_to_le64(n) ((__force le64) (u64) (n))
175189251Ssam#define be_to_host64(n) bswap_64((__force u64) (be64) (n))
176189251Ssam#define host_to_be64(n) ((__force be64) bswap_64((n)))
177189251Ssam#elif __BYTE_ORDER == __BIG_ENDIAN
178189251Ssam#define le_to_host16(n) bswap_16(n)
179189251Ssam#define host_to_le16(n) bswap_16(n)
180189251Ssam#define be_to_host16(n) (n)
181189251Ssam#define host_to_be16(n) (n)
182189251Ssam#define le_to_host32(n) bswap_32(n)
183281806Srpaulo#define host_to_le32(n) bswap_32(n)
184189251Ssam#define be_to_host32(n) (n)
185189251Ssam#define host_to_be32(n) (n)
186189251Ssam#define le_to_host64(n) bswap_64(n)
187189251Ssam#define host_to_le64(n) bswap_64(n)
188189251Ssam#define be_to_host64(n) (n)
189189251Ssam#define host_to_be64(n) (n)
190189251Ssam#ifndef WORDS_BIGENDIAN
191189251Ssam#define WORDS_BIGENDIAN
192189251Ssam#endif
193189251Ssam#else
194189251Ssam#error Could not determine CPU byte order
195189251Ssam#endif
196189251Ssam
197189251Ssam#define WPA_BYTE_SWAP_DEFINED
198189251Ssam#endif /* !WPA_BYTE_SWAP_DEFINED */
199189251Ssam
200189251Ssam
201189251Ssam/* Macros for handling unaligned memory accesses */
202189251Ssam
203281806Srpaulostatic inline u16 WPA_GET_BE16(const u8 *a)
204281806Srpaulo{
205281806Srpaulo	return (a[0] << 8) | a[1];
206281806Srpaulo}
207189251Ssam
208281806Srpaulostatic inline void WPA_PUT_BE16(u8 *a, u16 val)
209281806Srpaulo{
210281806Srpaulo	a[0] = val >> 8;
211281806Srpaulo	a[1] = val & 0xff;
212281806Srpaulo}
213189251Ssam
214281806Srpaulostatic inline u16 WPA_GET_LE16(const u8 *a)
215281806Srpaulo{
216281806Srpaulo	return (a[1] << 8) | a[0];
217281806Srpaulo}
218189251Ssam
219281806Srpaulostatic inline void WPA_PUT_LE16(u8 *a, u16 val)
220281806Srpaulo{
221281806Srpaulo	a[1] = val >> 8;
222281806Srpaulo	a[0] = val & 0xff;
223281806Srpaulo}
224189251Ssam
225281806Srpaulostatic inline u32 WPA_GET_BE24(const u8 *a)
226281806Srpaulo{
227281806Srpaulo	return (a[0] << 16) | (a[1] << 8) | a[2];
228281806Srpaulo}
229189251Ssam
230281806Srpaulostatic inline void WPA_PUT_BE24(u8 *a, u32 val)
231281806Srpaulo{
232281806Srpaulo	a[0] = (val >> 16) & 0xff;
233281806Srpaulo	a[1] = (val >> 8) & 0xff;
234281806Srpaulo	a[2] = val & 0xff;
235281806Srpaulo}
236189251Ssam
237281806Srpaulostatic inline u32 WPA_GET_BE32(const u8 *a)
238281806Srpaulo{
239289549Srpaulo	return ((u32) a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
240281806Srpaulo}
241189251Ssam
242281806Srpaulostatic inline void WPA_PUT_BE32(u8 *a, u32 val)
243281806Srpaulo{
244281806Srpaulo	a[0] = (val >> 24) & 0xff;
245281806Srpaulo	a[1] = (val >> 16) & 0xff;
246281806Srpaulo	a[2] = (val >> 8) & 0xff;
247281806Srpaulo	a[3] = val & 0xff;
248281806Srpaulo}
249189251Ssam
250281806Srpaulostatic inline u32 WPA_GET_LE32(const u8 *a)
251281806Srpaulo{
252289549Srpaulo	return ((u32) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
253281806Srpaulo}
254281806Srpaulo
255281806Srpaulostatic inline void WPA_PUT_LE32(u8 *a, u32 val)
256281806Srpaulo{
257281806Srpaulo	a[3] = (val >> 24) & 0xff;
258281806Srpaulo	a[2] = (val >> 16) & 0xff;
259281806Srpaulo	a[1] = (val >> 8) & 0xff;
260281806Srpaulo	a[0] = val & 0xff;
261281806Srpaulo}
262281806Srpaulo
263281806Srpaulostatic inline u64 WPA_GET_BE64(const u8 *a)
264281806Srpaulo{
265281806Srpaulo	return (((u64) a[0]) << 56) | (((u64) a[1]) << 48) |
266281806Srpaulo		(((u64) a[2]) << 40) | (((u64) a[3]) << 32) |
267281806Srpaulo		(((u64) a[4]) << 24) | (((u64) a[5]) << 16) |
268281806Srpaulo		(((u64) a[6]) << 8) | ((u64) a[7]);
269281806Srpaulo}
270281806Srpaulo
271281806Srpaulostatic inline void WPA_PUT_BE64(u8 *a, u64 val)
272281806Srpaulo{
273281806Srpaulo	a[0] = val >> 56;
274281806Srpaulo	a[1] = val >> 48;
275281806Srpaulo	a[2] = val >> 40;
276281806Srpaulo	a[3] = val >> 32;
277281806Srpaulo	a[4] = val >> 24;
278281806Srpaulo	a[5] = val >> 16;
279281806Srpaulo	a[6] = val >> 8;
280281806Srpaulo	a[7] = val & 0xff;
281281806Srpaulo}
282281806Srpaulo
283281806Srpaulostatic inline u64 WPA_GET_LE64(const u8 *a)
284281806Srpaulo{
285281806Srpaulo	return (((u64) a[7]) << 56) | (((u64) a[6]) << 48) |
286281806Srpaulo		(((u64) a[5]) << 40) | (((u64) a[4]) << 32) |
287281806Srpaulo		(((u64) a[3]) << 24) | (((u64) a[2]) << 16) |
288281806Srpaulo		(((u64) a[1]) << 8) | ((u64) a[0]);
289281806Srpaulo}
290281806Srpaulo
291281806Srpaulostatic inline void WPA_PUT_LE64(u8 *a, u64 val)
292281806Srpaulo{
293281806Srpaulo	a[7] = val >> 56;
294281806Srpaulo	a[6] = val >> 48;
295281806Srpaulo	a[5] = val >> 40;
296281806Srpaulo	a[4] = val >> 32;
297281806Srpaulo	a[3] = val >> 24;
298281806Srpaulo	a[2] = val >> 16;
299281806Srpaulo	a[1] = val >> 8;
300281806Srpaulo	a[0] = val & 0xff;
301281806Srpaulo}
302281806Srpaulo
303281806Srpaulo
304189251Ssam#ifndef ETH_ALEN
305189251Ssam#define ETH_ALEN 6
306189251Ssam#endif
307281806Srpaulo#ifndef ETH_HLEN
308281806Srpaulo#define ETH_HLEN 14
309281806Srpaulo#endif
310214734Srpaulo#ifndef IFNAMSIZ
311214734Srpaulo#define IFNAMSIZ 16
312214734Srpaulo#endif
313214734Srpaulo#ifndef ETH_P_ALL
314214734Srpaulo#define ETH_P_ALL 0x0003
315214734Srpaulo#endif
316252726Srpaulo#ifndef ETH_P_80211_ENCAP
317252726Srpaulo#define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
318252726Srpaulo#endif
319214734Srpaulo#ifndef ETH_P_PAE
320214734Srpaulo#define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
321214734Srpaulo#endif /* ETH_P_PAE */
322214734Srpaulo#ifndef ETH_P_EAPOL
323214734Srpaulo#define ETH_P_EAPOL ETH_P_PAE
324214734Srpaulo#endif /* ETH_P_EAPOL */
325214734Srpaulo#ifndef ETH_P_RSN_PREAUTH
326214734Srpaulo#define ETH_P_RSN_PREAUTH 0x88c7
327214734Srpaulo#endif /* ETH_P_RSN_PREAUTH */
328214734Srpaulo#ifndef ETH_P_RRB
329214734Srpaulo#define ETH_P_RRB 0x890D
330214734Srpaulo#endif /* ETH_P_RRB */
331189251Ssam
332189251Ssam
333189251Ssam#ifdef __GNUC__
334189251Ssam#define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
335189251Ssam#define STRUCT_PACKED __attribute__ ((packed))
336189251Ssam#else
337189251Ssam#define PRINTF_FORMAT(a,b)
338189251Ssam#define STRUCT_PACKED
339189251Ssam#endif
340189251Ssam
341189251Ssam
342189251Ssam#ifdef CONFIG_ANSI_C_EXTRA
343189251Ssam
344189251Ssam#if !defined(_MSC_VER) || _MSC_VER < 1400
345189251Ssam/* snprintf - used in number of places; sprintf() is _not_ a good replacement
346189251Ssam * due to possible buffer overflow; see, e.g.,
347189251Ssam * http://www.ijs.si/software/snprintf/ for portable implementation of
348189251Ssam * snprintf. */
349189251Ssamint snprintf(char *str, size_t size, const char *format, ...);
350189251Ssam
351189251Ssam/* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
352189251Ssamint vsnprintf(char *str, size_t size, const char *format, va_list ap);
353189251Ssam#endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
354189251Ssam
355189251Ssam/* getopt - only used in main.c */
356189251Ssamint getopt(int argc, char *const argv[], const char *optstring);
357189251Ssamextern char *optarg;
358189251Ssamextern int optind;
359189251Ssam
360189251Ssam#ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
361189251Ssam#ifndef __socklen_t_defined
362189251Ssamtypedef int socklen_t;
363189251Ssam#endif
364189251Ssam#endif
365189251Ssam
366189251Ssam/* inline - define as __inline or just define it to be empty, if needed */
367189251Ssam#ifdef CONFIG_NO_INLINE
368189251Ssam#define inline
369189251Ssam#else
370189251Ssam#define inline __inline
371189251Ssam#endif
372189251Ssam
373189251Ssam#ifndef __func__
374189251Ssam#define __func__ "__func__ not defined"
375189251Ssam#endif
376189251Ssam
377189251Ssam#ifndef bswap_16
378189251Ssam#define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
379189251Ssam#endif
380189251Ssam
381189251Ssam#ifndef bswap_32
382189251Ssam#define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
383189251Ssam		     (((u32) (a) << 8) & 0xff0000) | \
384189251Ssam     		     (((u32) (a) >> 8) & 0xff00) | \
385189251Ssam     		     (((u32) (a) >> 24) & 0xff))
386189251Ssam#endif
387189251Ssam
388189251Ssam#ifndef MSG_DONTWAIT
389189251Ssam#define MSG_DONTWAIT 0
390189251Ssam#endif
391189251Ssam
392189251Ssam#ifdef _WIN32_WCE
393189251Ssamvoid perror(const char *s);
394189251Ssam#endif /* _WIN32_WCE */
395189251Ssam
396189251Ssam#endif /* CONFIG_ANSI_C_EXTRA */
397189251Ssam
398189251Ssam#ifndef MAC2STR
399189251Ssam#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
400189251Ssam#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
401252726Srpaulo
402252726Srpaulo/*
403252726Srpaulo * Compact form for string representation of MAC address
404252726Srpaulo * To be used, e.g., for constructing dbus paths for P2P Devices
405252726Srpaulo */
406252726Srpaulo#define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x"
407189251Ssam#endif
408189251Ssam
409189251Ssam#ifndef BIT
410289549Srpaulo#define BIT(x) (1U << (x))
411189251Ssam#endif
412189251Ssam
413189251Ssam/*
414189251Ssam * Definitions for sparse validation
415189251Ssam * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
416189251Ssam */
417189251Ssam#ifdef __CHECKER__
418189251Ssam#define __force __attribute__((force))
419189251Ssam#define __bitwise __attribute__((bitwise))
420189251Ssam#else
421189251Ssam#define __force
422189251Ssam#define __bitwise
423189251Ssam#endif
424189251Ssam
425189251Ssamtypedef u16 __bitwise be16;
426189251Ssamtypedef u16 __bitwise le16;
427189251Ssamtypedef u32 __bitwise be32;
428189251Ssamtypedef u32 __bitwise le32;
429189251Ssamtypedef u64 __bitwise be64;
430189251Ssamtypedef u64 __bitwise le64;
431189251Ssam
432189251Ssam#ifndef __must_check
433189251Ssam#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
434189251Ssam#define __must_check __attribute__((__warn_unused_result__))
435189251Ssam#else
436189251Ssam#define __must_check
437189251Ssam#endif /* __GNUC__ */
438189251Ssam#endif /* __must_check */
439189251Ssam
440281806Srpaulo#ifndef __maybe_unused
441281806Srpaulo#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
442281806Srpaulo#define __maybe_unused __attribute__((unused))
443281806Srpaulo#else
444281806Srpaulo#define __maybe_unused
445281806Srpaulo#endif /* __GNUC__ */
446281806Srpaulo#endif /* __must_check */
447281806Srpaulo
448189251Ssamint hwaddr_aton(const char *txt, u8 *addr);
449281806Srpauloint hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable);
450252726Srpauloint hwaddr_compact_aton(const char *txt, u8 *addr);
451214734Srpauloint hwaddr_aton2(const char *txt, u8 *addr);
452252726Srpauloint hex2byte(const char *hex);
453189251Ssamint hexstr2bin(const char *hex, u8 *buf, size_t len);
454189251Ssamvoid inc_byte_array(u8 *counter, size_t len);
455189251Ssamvoid wpa_get_ntp_timestamp(u8 *buf);
456281806Srpauloint wpa_scnprintf(char *buf, size_t size, const char *fmt, ...);
457289549Srpauloint wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len,
458289549Srpaulo			 char sep);
459189251Ssamint wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
460189251Ssamint wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
461189251Ssam			       size_t len);
462189251Ssam
463281806Srpauloint hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask);
464281806Srpaulo
465189251Ssam#ifdef CONFIG_NATIVE_WINDOWS
466189251Ssamvoid wpa_unicode2ascii_inplace(TCHAR *str);
467189251SsamTCHAR * wpa_strdup_tchar(const char *str);
468189251Ssam#else /* CONFIG_NATIVE_WINDOWS */
469189251Ssam#define wpa_unicode2ascii_inplace(s) do { } while (0)
470189251Ssam#define wpa_strdup_tchar(s) strdup((s))
471189251Ssam#endif /* CONFIG_NATIVE_WINDOWS */
472189251Ssam
473252726Srpaulovoid printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len);
474252726Srpaulosize_t printf_decode(u8 *buf, size_t maxlen, const char *str);
475252726Srpaulo
476189251Ssamconst char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
477189251Ssam
478252726Srpaulochar * wpa_config_parse_string(const char *value, size_t *len);
479252726Srpauloint is_hex(const u8 *data, size_t len);
480252726Srpaulosize_t merge_byte_arrays(u8 *res, size_t res_len,
481252726Srpaulo			 const u8 *src1, size_t src1_len,
482252726Srpaulo			 const u8 *src2, size_t src2_len);
483281806Srpaulochar * dup_binstr(const void *src, size_t len);
484252726Srpaulo
485189251Ssamstatic inline int is_zero_ether_addr(const u8 *a)
486189251Ssam{
487189251Ssam	return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
488189251Ssam}
489189251Ssam
490252726Srpaulostatic inline int is_broadcast_ether_addr(const u8 *a)
491252726Srpaulo{
492252726Srpaulo	return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
493252726Srpaulo}
494252726Srpaulo
495289549Srpaulostatic inline int is_multicast_ether_addr(const u8 *a)
496289549Srpaulo{
497289549Srpaulo	return a[0] & 0x01;
498289549Srpaulo}
499289549Srpaulo
500252726Srpaulo#define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
501252726Srpaulo
502189251Ssam#include "wpa_debug.h"
503189251Ssam
504209158Srpaulo
505281806Srpaulostruct wpa_freq_range_list {
506281806Srpaulo	struct wpa_freq_range {
507281806Srpaulo		unsigned int min;
508281806Srpaulo		unsigned int max;
509281806Srpaulo	} *range;
510281806Srpaulo	unsigned int num;
511281806Srpaulo};
512281806Srpaulo
513281806Srpauloint freq_range_list_parse(struct wpa_freq_range_list *res, const char *value);
514281806Srpauloint freq_range_list_includes(const struct wpa_freq_range_list *list,
515281806Srpaulo			     unsigned int freq);
516281806Srpaulochar * freq_range_list_str(const struct wpa_freq_range_list *list);
517281806Srpaulo
518281806Srpauloint int_array_len(const int *a);
519281806Srpaulovoid int_array_concat(int **res, const int *a);
520281806Srpaulovoid int_array_sort_unique(int *a);
521281806Srpaulovoid int_array_add_unique(int **res, int a);
522281806Srpaulo
523281806Srpaulo#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
524281806Srpaulo
525281806Srpaulovoid str_clear_free(char *str);
526281806Srpaulovoid bin_clear_free(void *bin, size_t len);
527281806Srpaulo
528281806Srpauloint random_mac_addr(u8 *addr);
529281806Srpauloint random_mac_addr_keep_oui(u8 *addr);
530281806Srpaulo
531289549Srpauloconst char * cstr_token(const char *str, const char *delim, const char **last);
532281806Srpaulochar * str_token(char *str, const char *delim, char **context);
533281806Srpaulosize_t utf8_escape(const char *inp, size_t in_size,
534281806Srpaulo		   char *outp, size_t out_size);
535281806Srpaulosize_t utf8_unescape(const char *inp, size_t in_size,
536281806Srpaulo		     char *outp, size_t out_size);
537289549Srpauloint is_ctrl_char(char c);
538281806Srpaulo
539281806Srpaulo
540209158Srpaulo/*
541209158Srpaulo * gcc 4.4 ends up generating strict-aliasing warnings about some very common
542209158Srpaulo * networking socket uses that do not really result in a real problem and
543209158Srpaulo * cannot be easily avoided with union-based type-punning due to struct
544209158Srpaulo * definitions including another struct in system header files. To avoid having
545209158Srpaulo * to fully disable strict-aliasing warnings, provide a mechanism to hide the
546209158Srpaulo * typecast from aliasing for now. A cleaner solution will hopefully be found
547209158Srpaulo * in the future to handle these cases.
548209158Srpaulo */
549209158Srpaulovoid * __hide_aliasing_typecast(void *foo);
550209158Srpaulo#define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
551209158Srpaulo
552252726Srpaulo#ifdef CONFIG_VALGRIND
553252726Srpaulo#include <valgrind/memcheck.h>
554252726Srpaulo#define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
555252726Srpaulo#else /* CONFIG_VALGRIND */
556252726Srpaulo#define WPA_MEM_DEFINED(ptr, len) do { } while (0)
557252726Srpaulo#endif /* CONFIG_VALGRIND */
558252726Srpaulo
559189251Ssam#endif /* COMMON_H */
560