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
12 * See README and COPYING for more details.
20 static int hex2num(char c)
22 if (c >= '0' && c <= '9')
24 if (c >= 'a' && c <= 'f')
26 if (c >= 'A' && c <= 'F')
32 static int hex2byte(const char *hex)
46 * hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format)
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)
55 for (i = 0; i < 6; i++) {
64 *addr++ = (a << 4) | b;
65 if (i < 5 && *txt++ != ':')
74 * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
75 * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
76 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
77 * Returns: Characters used (> 0) on success, -1 on failure
79 int hwaddr_aton2(const char *txt, u8 *addr)
82 const char *pos = txt;
84 for (i = 0; i < 6; i++) {
87 while (*pos == ':' || *pos == '.' || *pos == '-')
96 *addr++ = (a << 4) | b;
104 * hexstr2bin - Convert ASCII hex string into binary data
105 * @hex: ASCII hex string (e.g., "01ab")
106 * @buf: Buffer for the binary data
107 * @len: Length of the text to convert in bytes (of buf); hex will be double
109 * Returns: 0 on success, -1 on failure (invalid hex string)
111 int hexstr2bin(const char *hex, u8 *buf, size_t len)
115 const char *ipos = hex;
118 for (i = 0; i < len; i++) {
130 * inc_byte_array - Increment arbitrary length byte array by one
131 * @counter: Pointer to byte array
132 * @len: Length of the counter in bytes
134 * This function increments the last byte of the counter by one and continues
135 * rolling over to more significant bytes if the byte was incremented from
138 void inc_byte_array(u8 *counter, size_t len)
143 if (counter[pos] != 0)
150 void wpa_get_ntp_timestamp(u8 *buf)
156 /* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
158 sec = now.sec + 2208988800U; /* Epoch to 1900 */
159 /* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
161 usec = 4295 * usec - (usec >> 5) - (usec >> 9);
162 tmp = host_to_be32(sec);
163 os_memcpy(buf, (u8 *) &tmp, 4);
164 tmp = host_to_be32(usec);
165 os_memcpy(buf + 4, (u8 *) &tmp, 4);
169 static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
170 size_t len, int uppercase)
173 char *pos = buf, *end = buf + buf_size;
177 for (i = 0; i < len; i++) {
178 ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
180 if (ret < 0 || ret >= end - pos) {
191 * wpa_snprintf_hex - Print data as a hex string into a buffer
192 * @buf: Memory area to use as the output buffer
193 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
194 * @data: Data to be printed
195 * @len: Length of data in bytes
196 * Returns: Number of bytes written
198 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
200 return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
205 * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf
206 * @buf: Memory area to use as the output buffer
207 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
208 * @data: Data to be printed
209 * @len: Length of data in bytes
210 * Returns: Number of bytes written
212 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
215 return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
219 #ifdef CONFIG_ANSI_C_EXTRA
222 void perror(const char *s)
224 wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
225 s, (int) GetLastError());
227 #endif /* _WIN32_WCE */
234 int getopt(int argc, char *const argv[], const char *optstring)
236 static int optchr = 1;
240 if (optind >= argc) {
241 /* all arguments processed */
245 if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
246 /* no option characters */
251 if (os_strcmp(argv[optind], "--") == 0) {
252 /* no more options */
257 optopt = argv[optind][optchr];
258 cp = os_strchr(optstring, optopt);
259 if (cp == NULL || optopt == ':') {
260 if (argv[optind][++optchr] == '\0') {
268 /* Argument required */
270 if (argv[optind][optchr + 1]) {
271 /* No space between option and argument */
272 optarg = &argv[optind++][optchr + 1];
273 } else if (++optind >= argc) {
274 /* option requires an argument */
277 /* Argument in the next argv */
278 optarg = argv[optind++];
282 if (argv[optind][++optchr] == '\0') {
290 #endif /* CONFIG_ANSI_C_EXTRA */
293 #ifdef CONFIG_NATIVE_WINDOWS
295 * wpa_unicode2ascii_inplace - Convert unicode string into ASCII
296 * @str: Pointer to string to convert
298 * This function converts a unicode string to ASCII using the same
299 * buffer for output. If UNICODE is not set, the buffer is not
302 void wpa_unicode2ascii_inplace(TCHAR *str)
305 char *dst = (char *) str;
307 *dst++ = (char) *str++;
313 TCHAR * wpa_strdup_tchar(const char *str)
317 buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
320 wsprintf(buf, L"%S", str);
323 return os_strdup(str);
326 #endif /* CONFIG_NATIVE_WINDOWS */
330 * wpa_ssid_txt - Convert SSID to a printable string
331 * @ssid: SSID (32-octet string)
332 * @ssid_len: Length of ssid in octets
333 * Returns: Pointer to a printable string
335 * This function can be used to convert SSIDs into printable form. In most
336 * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
337 * does not limit the used character set, so anything could be used in an SSID.
339 * This function uses a static buffer, so only one call can be used at the
340 * time, i.e., this is not re-entrant and the returned buffer must be used
341 * before calling this again.
343 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
345 static char ssid_txt[33];
350 os_memcpy(ssid_txt, ssid, ssid_len);
351 ssid_txt[ssid_len] = '\0';
353 * Oct, 26th. 2011. TIZEN
354 * As UI requirements, AP's essid should indicate ascii characters
355 * whether it is alphabet or not
358 for (pos = ssid_txt; *pos != '\0'; pos++) {
359 if ((u8) *pos < 32 || (u8) *pos >= 127)
367 void * __hide_aliasing_typecast(void *foo)