hostapd: Update vendor branch to 0.6.10
[dragonfly.git] / contrib / hostapd / src / utils / common.c
blob9a46ebe461da50203ad30624eea55b67dcbf2668
1 /*
2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
20 static int hex2num(char c)
22 if (c >= '0' && c <= '9')
23 return c - '0';
24 if (c >= 'a' && c <= 'f')
25 return c - 'a' + 10;
26 if (c >= 'A' && c <= 'F')
27 return c - 'A' + 10;
28 return -1;
32 static int hex2byte(const char *hex)
34 int a, b;
35 a = hex2num(*hex++);
36 if (a < 0)
37 return -1;
38 b = hex2num(*hex++);
39 if (b < 0)
40 return -1;
41 return (a << 4) | b;
45 /**
46 * hwaddr_aton - Convert ASCII string to MAC address
47 * @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
48 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
49 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
51 int hwaddr_aton(const char *txt, u8 *addr)
53 int i;
55 for (i = 0; i < 6; i++) {
56 int a, b;
58 a = hex2num(*txt++);
59 if (a < 0)
60 return -1;
61 b = hex2num(*txt++);
62 if (b < 0)
63 return -1;
64 *addr++ = (a << 4) | b;
65 if (i < 5 && *txt++ != ':')
66 return -1;
69 return 0;
73 /**
74 * hexstr2bin - Convert ASCII hex string into binary data
75 * @hex: ASCII hex string (e.g., "01ab")
76 * @buf: Buffer for the binary data
77 * @len: Length of the text to convert in bytes (of buf); hex will be double
78 * this size
79 * Returns: 0 on success, -1 on failure (invalid hex string)
81 int hexstr2bin(const char *hex, u8 *buf, size_t len)
83 size_t i;
84 int a;
85 const char *ipos = hex;
86 u8 *opos = buf;
88 for (i = 0; i < len; i++) {
89 a = hex2byte(ipos);
90 if (a < 0)
91 return -1;
92 *opos++ = a;
93 ipos += 2;
95 return 0;
99 /**
100 * inc_byte_array - Increment arbitrary length byte array by one
101 * @counter: Pointer to byte array
102 * @len: Length of the counter in bytes
104 * This function increments the last byte of the counter by one and continues
105 * rolling over to more significant bytes if the byte was incremented from
106 * 0xff to 0x00.
108 void inc_byte_array(u8 *counter, size_t len)
110 int pos = len - 1;
111 while (pos >= 0) {
112 counter[pos]++;
113 if (counter[pos] != 0)
114 break;
115 pos--;
120 void wpa_get_ntp_timestamp(u8 *buf)
122 struct os_time now;
123 u32 sec, usec;
124 be32 tmp;
126 /* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
127 os_get_time(&now);
128 sec = now.sec + 2208988800U; /* Epoch to 1900 */
129 /* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
130 usec = now.usec;
131 usec = 4295 * usec - (usec >> 5) - (usec >> 9);
132 tmp = host_to_be32(sec);
133 os_memcpy(buf, (u8 *) &tmp, 4);
134 tmp = host_to_be32(usec);
135 os_memcpy(buf + 4, (u8 *) &tmp, 4);
139 static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
140 size_t len, int uppercase)
142 size_t i;
143 char *pos = buf, *end = buf + buf_size;
144 int ret;
145 if (buf_size == 0)
146 return 0;
147 for (i = 0; i < len; i++) {
148 ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
149 data[i]);
150 if (ret < 0 || ret >= end - pos) {
151 end[-1] = '\0';
152 return pos - buf;
154 pos += ret;
156 end[-1] = '\0';
157 return pos - buf;
161 * wpa_snprintf_hex - Print data as a hex string into a buffer
162 * @buf: Memory area to use as the output buffer
163 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
164 * @data: Data to be printed
165 * @len: Length of data in bytes
166 * Returns: Number of bytes written
168 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
170 return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
175 * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf
176 * @buf: Memory area to use as the output buffer
177 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
178 * @data: Data to be printed
179 * @len: Length of data in bytes
180 * Returns: Number of bytes written
182 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
183 size_t len)
185 return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
189 #ifdef CONFIG_ANSI_C_EXTRA
191 #ifdef _WIN32_WCE
192 void perror(const char *s)
194 wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
195 s, (int) GetLastError());
197 #endif /* _WIN32_WCE */
200 int optind = 1;
201 int optopt;
202 char *optarg;
204 int getopt(int argc, char *const argv[], const char *optstring)
206 static int optchr = 1;
207 char *cp;
209 if (optchr == 1) {
210 if (optind >= argc) {
211 /* all arguments processed */
212 return EOF;
215 if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
216 /* no option characters */
217 return EOF;
221 if (os_strcmp(argv[optind], "--") == 0) {
222 /* no more options */
223 optind++;
224 return EOF;
227 optopt = argv[optind][optchr];
228 cp = os_strchr(optstring, optopt);
229 if (cp == NULL || optopt == ':') {
230 if (argv[optind][++optchr] == '\0') {
231 optchr = 1;
232 optind++;
234 return '?';
237 if (cp[1] == ':') {
238 /* Argument required */
239 optchr = 1;
240 if (argv[optind][optchr + 1]) {
241 /* No space between option and argument */
242 optarg = &argv[optind++][optchr + 1];
243 } else if (++optind >= argc) {
244 /* option requires an argument */
245 return '?';
246 } else {
247 /* Argument in the next argv */
248 optarg = argv[optind++];
250 } else {
251 /* No argument */
252 if (argv[optind][++optchr] == '\0') {
253 optchr = 1;
254 optind++;
256 optarg = NULL;
258 return *cp;
260 #endif /* CONFIG_ANSI_C_EXTRA */
263 #ifdef CONFIG_NATIVE_WINDOWS
265 * wpa_unicode2ascii_inplace - Convert unicode string into ASCII
266 * @str: Pointer to string to convert
268 * This function converts a unicode string to ASCII using the same
269 * buffer for output. If UNICODE is not set, the buffer is not
270 * modified.
272 void wpa_unicode2ascii_inplace(TCHAR *str)
274 #ifdef UNICODE
275 char *dst = (char *) str;
276 while (*str)
277 *dst++ = (char) *str++;
278 *dst = '\0';
279 #endif /* UNICODE */
283 TCHAR * wpa_strdup_tchar(const char *str)
285 #ifdef UNICODE
286 TCHAR *buf;
287 buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
288 if (buf == NULL)
289 return NULL;
290 wsprintf(buf, L"%S", str);
291 return buf;
292 #else /* UNICODE */
293 return os_strdup(str);
294 #endif /* UNICODE */
296 #endif /* CONFIG_NATIVE_WINDOWS */
300 * wpa_ssid_txt - Convert SSID to a printable string
301 * @ssid: SSID (32-octet string)
302 * @ssid_len: Length of ssid in octets
303 * Returns: Pointer to a printable string
305 * This function can be used to convert SSIDs into printable form. In most
306 * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
307 * does not limit the used character set, so anything could be used in an SSID.
309 * This function uses a static buffer, so only one call can be used at the
310 * time, i.e., this is not re-entrant and the returned buffer must be used
311 * before calling this again.
313 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
315 static char ssid_txt[33];
316 char *pos;
318 if (ssid_len > 32)
319 ssid_len = 32;
320 os_memcpy(ssid_txt, ssid, ssid_len);
321 ssid_txt[ssid_len] = '\0';
322 for (pos = ssid_txt; *pos != '\0'; pos++) {
323 if ((u8) *pos < 32 || (u8) *pos >= 127)
324 *pos = '_';
326 return ssid_txt;
330 void * __hide_aliasing_typecast(void *foo)
332 return foo;