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
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
* 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
79
* Returns: 0 on success, -1 on failure (invalid hex string)
81
int hexstr2bin(const char *hex, u8 *buf, size_t len)
85
const char *ipos = hex;
88
for (i = 0; i < len; i++) {
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
108
void inc_byte_array(u8 *counter, size_t len)
113
if (counter[pos] != 0)
120
void wpa_get_ntp_timestamp(u8 *buf)
126
/* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
128
sec = now.sec + 2208988800U; /* Epoch to 1900 */
129
/* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
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)
143
char *pos = buf, *end = buf + buf_size;
147
for (i = 0; i < len; i++) {
148
ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
150
if (ret < 0 || ret >= end - pos) {
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,
185
return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
189
#ifdef CONFIG_ANSI_C_EXTRA
192
void perror(const char *s)
194
wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
195
s, (int) GetLastError());
197
#endif /* _WIN32_WCE */
204
int getopt(int argc, char *const argv[], const char *optstring)
206
static int optchr = 1;
210
if (optind >= argc) {
211
/* all arguments processed */
215
if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
216
/* no option characters */
221
if (os_strcmp(argv[optind], "--") == 0) {
222
/* no more options */
227
optopt = argv[optind][optchr];
228
cp = os_strchr(optstring, optopt);
229
if (cp == NULL || optopt == ':') {
230
if (argv[optind][++optchr] == '\0') {
238
/* Argument required */
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 */
247
/* Argument in the next argv */
248
optarg = argv[optind++];
252
if (argv[optind][++optchr] == '\0') {
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
272
void wpa_unicode2ascii_inplace(TCHAR *str)
275
char *dst = (char *) str;
277
*dst++ = (char) *str++;
283
TCHAR * wpa_strdup_tchar(const char *str)
287
buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
290
wsprintf(buf, L"%S", str);
293
return os_strdup(str);
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(u8 *ssid, size_t ssid_len)
315
static char ssid_txt[33];
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)