/src/hostap/src/utils/common.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * wpa_supplicant/hostapd / common helper functions, etc. |
3 | | * Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi> |
4 | | * |
5 | | * This software may be distributed under the terms of the BSD license. |
6 | | * See README for more details. |
7 | | */ |
8 | | |
9 | | #include "includes.h" |
10 | | #include <limits.h> |
11 | | |
12 | | #include "common/ieee802_11_defs.h" |
13 | | #include "common.h" |
14 | | |
15 | | |
16 | | int hex2num(char c) |
17 | 65.9k | { |
18 | 65.9k | if (c >= '0' && c <= '9') |
19 | 65.9k | return c - '0'; |
20 | 0 | if (c >= 'a' && c <= 'f') |
21 | 0 | return c - 'a' + 10; |
22 | 0 | if (c >= 'A' && c <= 'F') |
23 | 0 | return c - 'A' + 10; |
24 | 0 | return -1; |
25 | 0 | } |
26 | | |
27 | | |
28 | | int hex2byte(const char *hex) |
29 | 32.9k | { |
30 | 32.9k | int a, b; |
31 | 32.9k | a = hex2num(*hex++); |
32 | 32.9k | if (a < 0) |
33 | 0 | return -1; |
34 | 32.9k | b = hex2num(*hex++); |
35 | 32.9k | if (b < 0) |
36 | 0 | return -1; |
37 | 32.9k | return (a << 4) | b; |
38 | 32.9k | } |
39 | | |
40 | | |
41 | | static const char * hwaddr_parse(const char *txt, u8 *addr) |
42 | 5.49k | { |
43 | 5.49k | size_t i; |
44 | | |
45 | 38.4k | for (i = 0; i < ETH_ALEN; i++) { |
46 | 32.9k | int a; |
47 | | |
48 | 32.9k | a = hex2byte(txt); |
49 | 32.9k | if (a < 0) |
50 | 0 | return NULL; |
51 | 32.9k | txt += 2; |
52 | 32.9k | addr[i] = a; |
53 | 32.9k | if (i < ETH_ALEN - 1 && *txt++ != ':') |
54 | 0 | return NULL; |
55 | 32.9k | } |
56 | 5.49k | return txt; |
57 | 5.49k | } |
58 | | |
59 | | |
60 | | /** |
61 | | * hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format) |
62 | | * @txt: MAC address as a string (e.g., "00:11:22:33:44:55") |
63 | | * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes) |
64 | | * Returns: 0 on success, -1 on failure (e.g., string not a MAC address) |
65 | | */ |
66 | | int hwaddr_aton(const char *txt, u8 *addr) |
67 | 5.49k | { |
68 | 5.49k | return hwaddr_parse(txt, addr) ? 0 : -1; |
69 | 5.49k | } |
70 | | |
71 | | |
72 | | /** |
73 | | * hwaddr_masked_aton - Convert ASCII string with optional mask to MAC address (colon-delimited format) |
74 | | * @txt: MAC address with optional mask as a string (e.g., "00:11:22:33:44:55/ff:ff:ff:ff:00:00") |
75 | | * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes) |
76 | | * @mask: Buffer for the MAC address mask (ETH_ALEN = 6 bytes) |
77 | | * @maskable: Flag to indicate whether a mask is allowed |
78 | | * Returns: 0 on success, -1 on failure (e.g., string not a MAC address) |
79 | | */ |
80 | | int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable) |
81 | 0 | { |
82 | 0 | const char *r; |
83 | | |
84 | | /* parse address part */ |
85 | 0 | r = hwaddr_parse(txt, addr); |
86 | 0 | if (!r) |
87 | 0 | return -1; |
88 | | |
89 | | /* check for optional mask */ |
90 | 0 | if (*r == '\0' || isspace((unsigned char) *r)) { |
91 | | /* no mask specified, assume default */ |
92 | 0 | os_memset(mask, 0xff, ETH_ALEN); |
93 | 0 | } else if (maskable && *r == '/') { |
94 | | /* mask specified and allowed */ |
95 | 0 | r = hwaddr_parse(r + 1, mask); |
96 | | /* parser error? */ |
97 | 0 | if (!r) |
98 | 0 | return -1; |
99 | 0 | } else { |
100 | | /* mask specified but not allowed or trailing garbage */ |
101 | 0 | return -1; |
102 | 0 | } |
103 | | |
104 | 0 | return 0; |
105 | 0 | } |
106 | | |
107 | | |
108 | | /** |
109 | | * hwaddr_compact_aton - Convert ASCII string to MAC address (no colon delimitors format) |
110 | | * @txt: MAC address as a string (e.g., "001122334455") |
111 | | * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes) |
112 | | * Returns: 0 on success, -1 on failure (e.g., string not a MAC address) |
113 | | */ |
114 | | int hwaddr_compact_aton(const char *txt, u8 *addr) |
115 | 0 | { |
116 | 0 | int i; |
117 | |
|
118 | 0 | for (i = 0; i < 6; i++) { |
119 | 0 | int a, b; |
120 | |
|
121 | 0 | a = hex2num(*txt++); |
122 | 0 | if (a < 0) |
123 | 0 | return -1; |
124 | 0 | b = hex2num(*txt++); |
125 | 0 | if (b < 0) |
126 | 0 | return -1; |
127 | 0 | *addr++ = (a << 4) | b; |
128 | 0 | } |
129 | | |
130 | 0 | return 0; |
131 | 0 | } |
132 | | |
133 | | /** |
134 | | * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format) |
135 | | * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455) |
136 | | * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes) |
137 | | * Returns: Characters used (> 0) on success, -1 on failure |
138 | | */ |
139 | | int hwaddr_aton2(const char *txt, u8 *addr) |
140 | 0 | { |
141 | 0 | int i; |
142 | 0 | const char *pos = txt; |
143 | |
|
144 | 0 | for (i = 0; i < 6; i++) { |
145 | 0 | int a, b; |
146 | |
|
147 | 0 | while (*pos == ':' || *pos == '.' || *pos == '-') |
148 | 0 | pos++; |
149 | |
|
150 | 0 | a = hex2num(*pos++); |
151 | 0 | if (a < 0) |
152 | 0 | return -1; |
153 | 0 | b = hex2num(*pos++); |
154 | 0 | if (b < 0) |
155 | 0 | return -1; |
156 | 0 | *addr++ = (a << 4) | b; |
157 | 0 | } |
158 | | |
159 | 0 | return pos - txt; |
160 | 0 | } |
161 | | |
162 | | |
163 | | /** |
164 | | * hexstr2bin - Convert ASCII hex string into binary data |
165 | | * @hex: ASCII hex string (e.g., "01ab") |
166 | | * @buf: Buffer for the binary data |
167 | | * @len: Length of the text to convert in bytes (of buf); hex will be double |
168 | | * this size |
169 | | * Returns: 0 on success, -1 on failure (invalid hex string) |
170 | | */ |
171 | | int hexstr2bin(const char *hex, u8 *buf, size_t len) |
172 | 0 | { |
173 | 0 | size_t i; |
174 | 0 | int a; |
175 | 0 | const char *ipos = hex; |
176 | 0 | u8 *opos = buf; |
177 | |
|
178 | 0 | for (i = 0; i < len; i++) { |
179 | 0 | a = hex2byte(ipos); |
180 | 0 | if (a < 0) |
181 | 0 | return -1; |
182 | 0 | *opos++ = a; |
183 | 0 | ipos += 2; |
184 | 0 | } |
185 | 0 | return 0; |
186 | 0 | } |
187 | | |
188 | | |
189 | | int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask) |
190 | 0 | { |
191 | 0 | size_t i; |
192 | 0 | int print_mask = 0; |
193 | 0 | int res; |
194 | |
|
195 | 0 | for (i = 0; i < ETH_ALEN; i++) { |
196 | 0 | if (mask[i] != 0xff) { |
197 | 0 | print_mask = 1; |
198 | 0 | break; |
199 | 0 | } |
200 | 0 | } |
201 | |
|
202 | 0 | if (print_mask) |
203 | 0 | res = os_snprintf(buf, len, MACSTR "/" MACSTR, |
204 | 0 | MAC2STR(addr), MAC2STR(mask)); |
205 | 0 | else |
206 | 0 | res = os_snprintf(buf, len, MACSTR, MAC2STR(addr)); |
207 | 0 | if (os_snprintf_error(len, res)) |
208 | 0 | return -1; |
209 | 0 | return res; |
210 | 0 | } |
211 | | |
212 | | |
213 | | /** |
214 | | * inc_byte_array - Increment arbitrary length byte array by one |
215 | | * @counter: Pointer to byte array |
216 | | * @len: Length of the counter in bytes |
217 | | * |
218 | | * This function increments the last byte of the counter by one and continues |
219 | | * rolling over to more significant bytes if the byte was incremented from |
220 | | * 0xff to 0x00. |
221 | | */ |
222 | | void inc_byte_array(u8 *counter, size_t len) |
223 | 0 | { |
224 | 0 | int pos = len - 1; |
225 | 0 | while (pos >= 0) { |
226 | 0 | counter[pos]++; |
227 | 0 | if (counter[pos] != 0) |
228 | 0 | break; |
229 | 0 | pos--; |
230 | 0 | } |
231 | 0 | } |
232 | | |
233 | | |
234 | | void buf_shift_right(u8 *buf, size_t len, size_t bits) |
235 | 0 | { |
236 | 0 | size_t i; |
237 | |
|
238 | 0 | for (i = len - 1; i > 0; i--) |
239 | 0 | buf[i] = (buf[i - 1] << (8 - bits)) | (buf[i] >> bits); |
240 | 0 | buf[0] >>= bits; |
241 | 0 | } |
242 | | |
243 | | |
244 | | void wpa_get_ntp_timestamp(u8 *buf) |
245 | 0 | { |
246 | 0 | struct os_time now; |
247 | 0 | u32 sec, usec; |
248 | 0 | be32 tmp; |
249 | | |
250 | | /* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */ |
251 | 0 | os_get_time(&now); |
252 | 0 | sec = now.sec + 2208988800U; /* Epoch to 1900 */ |
253 | | /* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */ |
254 | 0 | usec = now.usec; |
255 | 0 | usec = 4295 * usec - (usec >> 5) - (usec >> 9); |
256 | 0 | tmp = host_to_be32(sec); |
257 | 0 | os_memcpy(buf, (u8 *) &tmp, 4); |
258 | 0 | tmp = host_to_be32(usec); |
259 | 0 | os_memcpy(buf + 4, (u8 *) &tmp, 4); |
260 | 0 | } |
261 | | |
262 | | /** |
263 | | * wpa_scnprintf - Simpler-to-use snprintf function |
264 | | * @buf: Output buffer |
265 | | * @size: Buffer size |
266 | | * @fmt: format |
267 | | * |
268 | | * Simpler snprintf version that doesn't require further error checks - the |
269 | | * return value only indicates how many bytes were actually written, excluding |
270 | | * the NULL byte (i.e., 0 on error, size-1 if buffer is not big enough). |
271 | | */ |
272 | | int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...) |
273 | 0 | { |
274 | 0 | va_list ap; |
275 | 0 | int ret; |
276 | |
|
277 | 0 | if (!size) |
278 | 0 | return 0; |
279 | | |
280 | 0 | va_start(ap, fmt); |
281 | 0 | ret = vsnprintf(buf, size, fmt, ap); |
282 | 0 | va_end(ap); |
283 | |
|
284 | 0 | if (ret < 0) |
285 | 0 | return 0; |
286 | 0 | if ((size_t) ret >= size) |
287 | 0 | return size - 1; |
288 | | |
289 | 0 | return ret; |
290 | 0 | } |
291 | | |
292 | | |
293 | | int wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len, |
294 | | char sep) |
295 | 0 | { |
296 | 0 | size_t i; |
297 | 0 | char *pos = buf, *end = buf + buf_size; |
298 | 0 | int ret; |
299 | |
|
300 | 0 | if (buf_size == 0) |
301 | 0 | return 0; |
302 | | |
303 | 0 | for (i = 0; i < len; i++) { |
304 | 0 | ret = os_snprintf(pos, end - pos, "%02x%c", |
305 | 0 | data[i], sep); |
306 | 0 | if (os_snprintf_error(end - pos, ret)) { |
307 | 0 | end[-1] = '\0'; |
308 | 0 | return pos - buf; |
309 | 0 | } |
310 | 0 | pos += ret; |
311 | 0 | } |
312 | 0 | pos[-1] = '\0'; |
313 | 0 | return pos - buf; |
314 | 0 | } |
315 | | |
316 | | |
317 | | static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, |
318 | | size_t len, int uppercase) |
319 | 0 | { |
320 | 0 | size_t i; |
321 | 0 | char *pos = buf, *end = buf + buf_size; |
322 | 0 | int ret; |
323 | 0 | if (buf_size == 0) |
324 | 0 | return 0; |
325 | 0 | for (i = 0; i < len; i++) { |
326 | 0 | ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x", |
327 | 0 | data[i]); |
328 | 0 | if (os_snprintf_error(end - pos, ret)) { |
329 | 0 | end[-1] = '\0'; |
330 | 0 | return pos - buf; |
331 | 0 | } |
332 | 0 | pos += ret; |
333 | 0 | } |
334 | 0 | end[-1] = '\0'; |
335 | 0 | return pos - buf; |
336 | 0 | } |
337 | | |
338 | | /** |
339 | | * wpa_snprintf_hex - Print data as a hex string into a buffer |
340 | | * @buf: Memory area to use as the output buffer |
341 | | * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1) |
342 | | * @data: Data to be printed |
343 | | * @len: Length of data in bytes |
344 | | * Returns: Number of bytes written |
345 | | */ |
346 | | int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len) |
347 | 0 | { |
348 | 0 | return _wpa_snprintf_hex(buf, buf_size, data, len, 0); |
349 | 0 | } |
350 | | |
351 | | |
352 | | /** |
353 | | * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf |
354 | | * @buf: Memory area to use as the output buffer |
355 | | * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1) |
356 | | * @data: Data to be printed |
357 | | * @len: Length of data in bytes |
358 | | * Returns: Number of bytes written |
359 | | */ |
360 | | int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data, |
361 | | size_t len) |
362 | 0 | { |
363 | 0 | return _wpa_snprintf_hex(buf, buf_size, data, len, 1); |
364 | 0 | } |
365 | | |
366 | | |
367 | | #ifdef CONFIG_ANSI_C_EXTRA |
368 | | |
369 | | #ifdef _WIN32_WCE |
370 | | void perror(const char *s) |
371 | | { |
372 | | wpa_printf(MSG_ERROR, "%s: GetLastError: %d", |
373 | | s, (int) GetLastError()); |
374 | | } |
375 | | #endif /* _WIN32_WCE */ |
376 | | |
377 | | |
378 | | int optind = 1; |
379 | | int optopt; |
380 | | char *optarg; |
381 | | |
382 | | int getopt(int argc, char *const argv[], const char *optstring) |
383 | | { |
384 | | static int optchr = 1; |
385 | | char *cp; |
386 | | |
387 | | if (optchr == 1) { |
388 | | if (optind >= argc) { |
389 | | /* all arguments processed */ |
390 | | return EOF; |
391 | | } |
392 | | |
393 | | if (argv[optind][0] != '-' || argv[optind][1] == '\0') { |
394 | | /* no option characters */ |
395 | | return EOF; |
396 | | } |
397 | | } |
398 | | |
399 | | if (os_strcmp(argv[optind], "--") == 0) { |
400 | | /* no more options */ |
401 | | optind++; |
402 | | return EOF; |
403 | | } |
404 | | |
405 | | optopt = argv[optind][optchr]; |
406 | | cp = os_strchr(optstring, optopt); |
407 | | if (cp == NULL || optopt == ':') { |
408 | | if (argv[optind][++optchr] == '\0') { |
409 | | optchr = 1; |
410 | | optind++; |
411 | | } |
412 | | return '?'; |
413 | | } |
414 | | |
415 | | if (cp[1] == ':') { |
416 | | /* Argument required */ |
417 | | optchr = 1; |
418 | | if (argv[optind][optchr + 1]) { |
419 | | /* No space between option and argument */ |
420 | | optarg = &argv[optind++][optchr + 1]; |
421 | | } else if (++optind >= argc) { |
422 | | /* option requires an argument */ |
423 | | return '?'; |
424 | | } else { |
425 | | /* Argument in the next argv */ |
426 | | optarg = argv[optind++]; |
427 | | } |
428 | | } else { |
429 | | /* No argument */ |
430 | | if (argv[optind][++optchr] == '\0') { |
431 | | optchr = 1; |
432 | | optind++; |
433 | | } |
434 | | optarg = NULL; |
435 | | } |
436 | | return *cp; |
437 | | } |
438 | | #endif /* CONFIG_ANSI_C_EXTRA */ |
439 | | |
440 | | |
441 | | #ifdef CONFIG_NATIVE_WINDOWS |
442 | | /** |
443 | | * wpa_unicode2ascii_inplace - Convert unicode string into ASCII |
444 | | * @str: Pointer to string to convert |
445 | | * |
446 | | * This function converts a unicode string to ASCII using the same |
447 | | * buffer for output. If UNICODE is not set, the buffer is not |
448 | | * modified. |
449 | | */ |
450 | | void wpa_unicode2ascii_inplace(TCHAR *str) |
451 | | { |
452 | | #ifdef UNICODE |
453 | | char *dst = (char *) str; |
454 | | while (*str) |
455 | | *dst++ = (char) *str++; |
456 | | *dst = '\0'; |
457 | | #endif /* UNICODE */ |
458 | | } |
459 | | |
460 | | |
461 | | TCHAR * wpa_strdup_tchar(const char *str) |
462 | | { |
463 | | #ifdef UNICODE |
464 | | TCHAR *buf; |
465 | | buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR)); |
466 | | if (buf == NULL) |
467 | | return NULL; |
468 | | wsprintf(buf, L"%S", str); |
469 | | return buf; |
470 | | #else /* UNICODE */ |
471 | | return os_strdup(str); |
472 | | #endif /* UNICODE */ |
473 | | } |
474 | | #endif /* CONFIG_NATIVE_WINDOWS */ |
475 | | |
476 | | |
477 | | void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len) |
478 | 0 | { |
479 | 0 | char *end = txt + maxlen; |
480 | 0 | size_t i; |
481 | |
|
482 | 0 | for (i = 0; i < len; i++) { |
483 | 0 | if (txt + 4 >= end) |
484 | 0 | break; |
485 | | |
486 | 0 | switch (data[i]) { |
487 | 0 | case '\"': |
488 | 0 | *txt++ = '\\'; |
489 | 0 | *txt++ = '\"'; |
490 | 0 | break; |
491 | 0 | case '\\': |
492 | 0 | *txt++ = '\\'; |
493 | 0 | *txt++ = '\\'; |
494 | 0 | break; |
495 | 0 | case '\033': |
496 | 0 | *txt++ = '\\'; |
497 | 0 | *txt++ = 'e'; |
498 | 0 | break; |
499 | 0 | case '\n': |
500 | 0 | *txt++ = '\\'; |
501 | 0 | *txt++ = 'n'; |
502 | 0 | break; |
503 | 0 | case '\r': |
504 | 0 | *txt++ = '\\'; |
505 | 0 | *txt++ = 'r'; |
506 | 0 | break; |
507 | 0 | case '\t': |
508 | 0 | *txt++ = '\\'; |
509 | 0 | *txt++ = 't'; |
510 | 0 | break; |
511 | 0 | default: |
512 | 0 | if (data[i] >= 32 && data[i] <= 126) { |
513 | 0 | *txt++ = data[i]; |
514 | 0 | } else { |
515 | 0 | txt += os_snprintf(txt, end - txt, "\\x%02x", |
516 | 0 | data[i]); |
517 | 0 | } |
518 | 0 | break; |
519 | 0 | } |
520 | 0 | } |
521 | | |
522 | 0 | *txt = '\0'; |
523 | 0 | } |
524 | | |
525 | | |
526 | | size_t printf_decode(u8 *buf, size_t maxlen, const char *str) |
527 | 0 | { |
528 | 0 | const char *pos = str; |
529 | 0 | size_t len = 0; |
530 | 0 | int val; |
531 | |
|
532 | 0 | while (*pos) { |
533 | 0 | if (len + 1 >= maxlen) |
534 | 0 | break; |
535 | 0 | switch (*pos) { |
536 | 0 | case '\\': |
537 | 0 | pos++; |
538 | 0 | switch (*pos) { |
539 | 0 | case '\\': |
540 | 0 | buf[len++] = '\\'; |
541 | 0 | pos++; |
542 | 0 | break; |
543 | 0 | case '"': |
544 | 0 | buf[len++] = '"'; |
545 | 0 | pos++; |
546 | 0 | break; |
547 | 0 | case 'n': |
548 | 0 | buf[len++] = '\n'; |
549 | 0 | pos++; |
550 | 0 | break; |
551 | 0 | case 'r': |
552 | 0 | buf[len++] = '\r'; |
553 | 0 | pos++; |
554 | 0 | break; |
555 | 0 | case 't': |
556 | 0 | buf[len++] = '\t'; |
557 | 0 | pos++; |
558 | 0 | break; |
559 | 0 | case 'e': |
560 | 0 | buf[len++] = '\033'; |
561 | 0 | pos++; |
562 | 0 | break; |
563 | 0 | case 'x': |
564 | 0 | pos++; |
565 | 0 | val = hex2byte(pos); |
566 | 0 | if (val < 0) { |
567 | 0 | val = hex2num(*pos); |
568 | 0 | if (val < 0) |
569 | 0 | break; |
570 | 0 | buf[len++] = val; |
571 | 0 | pos++; |
572 | 0 | } else { |
573 | 0 | buf[len++] = val; |
574 | 0 | pos += 2; |
575 | 0 | } |
576 | 0 | break; |
577 | 0 | case '0': |
578 | 0 | case '1': |
579 | 0 | case '2': |
580 | 0 | case '3': |
581 | 0 | case '4': |
582 | 0 | case '5': |
583 | 0 | case '6': |
584 | 0 | case '7': |
585 | 0 | val = *pos++ - '0'; |
586 | 0 | if (*pos >= '0' && *pos <= '7') |
587 | 0 | val = val * 8 + (*pos++ - '0'); |
588 | 0 | if (*pos >= '0' && *pos <= '7') |
589 | 0 | val = val * 8 + (*pos++ - '0'); |
590 | 0 | buf[len++] = val; |
591 | 0 | break; |
592 | 0 | default: |
593 | 0 | break; |
594 | 0 | } |
595 | 0 | break; |
596 | 0 | default: |
597 | 0 | buf[len++] = *pos++; |
598 | 0 | break; |
599 | 0 | } |
600 | 0 | } |
601 | 0 | if (maxlen > len) |
602 | 0 | buf[len] = '\0'; |
603 | |
|
604 | 0 | return len; |
605 | 0 | } |
606 | | |
607 | | |
608 | | /** |
609 | | * wpa_ssid_txt - Convert SSID to a printable string |
610 | | * @ssid: SSID (32-octet string) |
611 | | * @ssid_len: Length of ssid in octets |
612 | | * Returns: Pointer to a printable string |
613 | | * |
614 | | * This function can be used to convert SSIDs into printable form. In most |
615 | | * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard |
616 | | * does not limit the used character set, so anything could be used in an SSID. |
617 | | * |
618 | | * This function uses a static buffer, so only one call can be used at the |
619 | | * time, i.e., this is not re-entrant and the returned buffer must be used |
620 | | * before calling this again. |
621 | | */ |
622 | | const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len) |
623 | 0 | { |
624 | 0 | static char ssid_txt[SSID_MAX_LEN * 4 + 1]; |
625 | |
|
626 | 0 | if (ssid == NULL) { |
627 | 0 | ssid_txt[0] = '\0'; |
628 | 0 | return ssid_txt; |
629 | 0 | } |
630 | | |
631 | 0 | printf_encode(ssid_txt, sizeof(ssid_txt), ssid, ssid_len); |
632 | 0 | return ssid_txt; |
633 | 0 | } |
634 | | |
635 | | |
636 | | void * __hide_aliasing_typecast(void *foo) |
637 | 0 | { |
638 | 0 | return foo; |
639 | 0 | } |
640 | | |
641 | | |
642 | | char * wpa_config_parse_string(const char *value, size_t *len) |
643 | 0 | { |
644 | 0 | if (*value == '"') { |
645 | 0 | const char *pos; |
646 | 0 | char *str; |
647 | 0 | value++; |
648 | 0 | pos = os_strrchr(value, '"'); |
649 | 0 | if (pos == NULL || pos[1] != '\0') |
650 | 0 | return NULL; |
651 | 0 | *len = pos - value; |
652 | 0 | str = dup_binstr(value, *len); |
653 | 0 | if (str == NULL) |
654 | 0 | return NULL; |
655 | 0 | return str; |
656 | 0 | } else if (*value == 'P' && value[1] == '"') { |
657 | 0 | const char *pos; |
658 | 0 | char *tstr, *str; |
659 | 0 | size_t tlen; |
660 | 0 | value += 2; |
661 | 0 | pos = os_strrchr(value, '"'); |
662 | 0 | if (pos == NULL || pos[1] != '\0') |
663 | 0 | return NULL; |
664 | 0 | tlen = pos - value; |
665 | 0 | tstr = dup_binstr(value, tlen); |
666 | 0 | if (tstr == NULL) |
667 | 0 | return NULL; |
668 | | |
669 | 0 | str = os_malloc(tlen + 1); |
670 | 0 | if (str == NULL) { |
671 | 0 | os_free(tstr); |
672 | 0 | return NULL; |
673 | 0 | } |
674 | | |
675 | 0 | *len = printf_decode((u8 *) str, tlen + 1, tstr); |
676 | 0 | os_free(tstr); |
677 | |
|
678 | 0 | return str; |
679 | 0 | } else { |
680 | 0 | u8 *str; |
681 | 0 | size_t tlen, hlen = os_strlen(value); |
682 | 0 | if (hlen & 1) |
683 | 0 | return NULL; |
684 | 0 | tlen = hlen / 2; |
685 | 0 | str = os_malloc(tlen + 1); |
686 | 0 | if (str == NULL) |
687 | 0 | return NULL; |
688 | 0 | if (hexstr2bin(value, str, tlen)) { |
689 | 0 | os_free(str); |
690 | 0 | return NULL; |
691 | 0 | } |
692 | 0 | str[tlen] = '\0'; |
693 | 0 | *len = tlen; |
694 | 0 | return (char *) str; |
695 | 0 | } |
696 | 0 | } |
697 | | |
698 | | |
699 | | int is_hex(const u8 *data, size_t len) |
700 | 0 | { |
701 | 0 | size_t i; |
702 | |
|
703 | 0 | for (i = 0; i < len; i++) { |
704 | 0 | if (data[i] < 32 || data[i] >= 127) |
705 | 0 | return 1; |
706 | 0 | } |
707 | 0 | return 0; |
708 | 0 | } |
709 | | |
710 | | |
711 | | int has_ctrl_char(const u8 *data, size_t len) |
712 | 0 | { |
713 | 0 | size_t i; |
714 | |
|
715 | 0 | for (i = 0; i < len; i++) { |
716 | 0 | if (data[i] < 32 || data[i] == 127) |
717 | 0 | return 1; |
718 | 0 | } |
719 | 0 | return 0; |
720 | 0 | } |
721 | | |
722 | | |
723 | | int has_newline(const char *str) |
724 | 0 | { |
725 | 0 | while (*str) { |
726 | 0 | if (*str == '\n' || *str == '\r') |
727 | 0 | return 1; |
728 | 0 | str++; |
729 | 0 | } |
730 | 0 | return 0; |
731 | 0 | } |
732 | | |
733 | | |
734 | | size_t merge_byte_arrays(u8 *res, size_t res_len, |
735 | | const u8 *src1, size_t src1_len, |
736 | | const u8 *src2, size_t src2_len) |
737 | 0 | { |
738 | 0 | size_t len = 0; |
739 | |
|
740 | 0 | os_memset(res, 0, res_len); |
741 | |
|
742 | 0 | if (src1) { |
743 | 0 | if (src1_len >= res_len) { |
744 | 0 | os_memcpy(res, src1, res_len); |
745 | 0 | return res_len; |
746 | 0 | } |
747 | | |
748 | 0 | os_memcpy(res, src1, src1_len); |
749 | 0 | len += src1_len; |
750 | 0 | } |
751 | | |
752 | 0 | if (src2) { |
753 | 0 | if (len + src2_len >= res_len) { |
754 | 0 | os_memcpy(res + len, src2, res_len - len); |
755 | 0 | return res_len; |
756 | 0 | } |
757 | | |
758 | 0 | os_memcpy(res + len, src2, src2_len); |
759 | 0 | len += src2_len; |
760 | 0 | } |
761 | | |
762 | 0 | return len; |
763 | 0 | } |
764 | | |
765 | | |
766 | | char * dup_binstr(const void *src, size_t len) |
767 | 0 | { |
768 | 0 | char *res; |
769 | |
|
770 | 0 | if (src == NULL) |
771 | 0 | return NULL; |
772 | 0 | res = os_malloc(len + 1); |
773 | 0 | if (res == NULL) |
774 | 0 | return NULL; |
775 | 0 | os_memcpy(res, src, len); |
776 | 0 | res[len] = '\0'; |
777 | |
|
778 | 0 | return res; |
779 | 0 | } |
780 | | |
781 | | |
782 | | int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value) |
783 | 0 | { |
784 | 0 | struct wpa_freq_range *freq = NULL, *n; |
785 | 0 | unsigned int count = 0; |
786 | 0 | const char *pos, *pos2, *pos3; |
787 | | |
788 | | /* |
789 | | * Comma separated list of frequency ranges. |
790 | | * For example: 2412-2432,2462,5000-6000 |
791 | | */ |
792 | 0 | pos = value; |
793 | 0 | while (pos && pos[0]) { |
794 | 0 | if (count == UINT_MAX) { |
795 | 0 | os_free(freq); |
796 | 0 | return -1; |
797 | 0 | } |
798 | 0 | n = os_realloc_array(freq, count + 1, |
799 | 0 | sizeof(struct wpa_freq_range)); |
800 | 0 | if (n == NULL) { |
801 | 0 | os_free(freq); |
802 | 0 | return -1; |
803 | 0 | } |
804 | 0 | freq = n; |
805 | 0 | freq[count].min = atoi(pos); |
806 | 0 | pos2 = os_strchr(pos, '-'); |
807 | 0 | pos3 = os_strchr(pos, ','); |
808 | 0 | if (pos2 && (!pos3 || pos2 < pos3)) { |
809 | 0 | pos2++; |
810 | 0 | freq[count].max = atoi(pos2); |
811 | 0 | } else |
812 | 0 | freq[count].max = freq[count].min; |
813 | 0 | pos = pos3; |
814 | 0 | if (pos) |
815 | 0 | pos++; |
816 | 0 | count++; |
817 | 0 | } |
818 | | |
819 | 0 | os_free(res->range); |
820 | 0 | res->range = freq; |
821 | 0 | res->num = count; |
822 | |
|
823 | 0 | return 0; |
824 | 0 | } |
825 | | |
826 | | |
827 | | int freq_range_list_includes(const struct wpa_freq_range_list *list, |
828 | | unsigned int freq) |
829 | 0 | { |
830 | 0 | unsigned int i; |
831 | |
|
832 | 0 | if (list == NULL) |
833 | 0 | return 0; |
834 | | |
835 | 0 | for (i = 0; i < list->num; i++) { |
836 | 0 | if (freq >= list->range[i].min && freq <= list->range[i].max) |
837 | 0 | return 1; |
838 | 0 | } |
839 | | |
840 | 0 | return 0; |
841 | 0 | } |
842 | | |
843 | | |
844 | | char * freq_range_list_str(const struct wpa_freq_range_list *list) |
845 | 0 | { |
846 | 0 | char *buf, *pos, *end; |
847 | 0 | size_t maxlen; |
848 | 0 | unsigned int i; |
849 | 0 | int res; |
850 | |
|
851 | 0 | if (list->num == 0) |
852 | 0 | return NULL; |
853 | | |
854 | 0 | maxlen = list->num * 30; |
855 | 0 | buf = os_malloc(maxlen); |
856 | 0 | if (buf == NULL) |
857 | 0 | return NULL; |
858 | 0 | pos = buf; |
859 | 0 | end = buf + maxlen; |
860 | |
|
861 | 0 | for (i = 0; i < list->num; i++) { |
862 | 0 | struct wpa_freq_range *range = &list->range[i]; |
863 | |
|
864 | 0 | if (range->min == range->max) |
865 | 0 | res = os_snprintf(pos, end - pos, "%s%u", |
866 | 0 | i == 0 ? "" : ",", range->min); |
867 | 0 | else |
868 | 0 | res = os_snprintf(pos, end - pos, "%s%u-%u", |
869 | 0 | i == 0 ? "" : ",", |
870 | 0 | range->min, range->max); |
871 | 0 | if (os_snprintf_error(end - pos, res)) { |
872 | 0 | os_free(buf); |
873 | 0 | return NULL; |
874 | 0 | } |
875 | 0 | pos += res; |
876 | 0 | } |
877 | | |
878 | 0 | return buf; |
879 | 0 | } |
880 | | |
881 | | |
882 | | size_t int_array_len(const int *a) |
883 | 0 | { |
884 | 0 | size_t i; |
885 | |
|
886 | 0 | for (i = 0; a && a[i]; i++) |
887 | 0 | ; |
888 | 0 | return i; |
889 | 0 | } |
890 | | |
891 | | |
892 | | void int_array_concat(int **res, const int *a) |
893 | 0 | { |
894 | 0 | size_t reslen, alen, i, max_size; |
895 | 0 | int *n; |
896 | |
|
897 | 0 | reslen = int_array_len(*res); |
898 | 0 | alen = int_array_len(a); |
899 | 0 | max_size = (size_t) -1; |
900 | 0 | if (alen >= max_size - reslen) { |
901 | | /* This should not really happen, but if it did, something |
902 | | * would overflow. Do not try to merge the arrays; instead, make |
903 | | * this behave like memory allocation failure to avoid messing |
904 | | * up memory. */ |
905 | 0 | os_free(*res); |
906 | 0 | *res = NULL; |
907 | 0 | return; |
908 | 0 | } |
909 | 0 | n = os_realloc_array(*res, reslen + alen + 1, sizeof(int)); |
910 | 0 | if (n == NULL) { |
911 | 0 | os_free(*res); |
912 | 0 | *res = NULL; |
913 | 0 | return; |
914 | 0 | } |
915 | 0 | for (i = 0; i <= alen; i++) |
916 | 0 | n[reslen + i] = a[i]; |
917 | 0 | *res = n; |
918 | 0 | } |
919 | | |
920 | | |
921 | | static int freq_cmp(const void *a, const void *b) |
922 | 0 | { |
923 | 0 | int _a = *(int *) a; |
924 | 0 | int _b = *(int *) b; |
925 | |
|
926 | 0 | if (_a == 0) |
927 | 0 | return 1; |
928 | 0 | if (_b == 0) |
929 | 0 | return -1; |
930 | 0 | return _a - _b; |
931 | 0 | } |
932 | | |
933 | | |
934 | | void int_array_sort_unique(int *a) |
935 | 0 | { |
936 | 0 | size_t alen, i, j; |
937 | |
|
938 | 0 | if (a == NULL) |
939 | 0 | return; |
940 | | |
941 | 0 | alen = int_array_len(a); |
942 | 0 | qsort(a, alen, sizeof(int), freq_cmp); |
943 | |
|
944 | 0 | i = 0; |
945 | 0 | j = 1; |
946 | 0 | while (a[i] && a[j]) { |
947 | 0 | if (a[i] == a[j]) { |
948 | 0 | j++; |
949 | 0 | continue; |
950 | 0 | } |
951 | 0 | a[++i] = a[j++]; |
952 | 0 | } |
953 | 0 | if (a[i]) |
954 | 0 | i++; |
955 | 0 | a[i] = 0; |
956 | 0 | } |
957 | | |
958 | | |
959 | | void int_array_add_unique(int **res, int a) |
960 | 0 | { |
961 | 0 | size_t reslen, max_size; |
962 | 0 | int *n; |
963 | |
|
964 | 0 | for (reslen = 0; *res && (*res)[reslen]; reslen++) { |
965 | 0 | if ((*res)[reslen] == a) |
966 | 0 | return; /* already in the list */ |
967 | 0 | } |
968 | | |
969 | 0 | max_size = (size_t) -1; |
970 | 0 | if (reslen > max_size - 2) { |
971 | | /* This should not really happen in practice, but if it did, |
972 | | * something would overflow. Do not try to add the new value; |
973 | | * instead, make this behave like memory allocation failure to |
974 | | * avoid messing up memory. */ |
975 | 0 | os_free(*res); |
976 | 0 | *res = NULL; |
977 | 0 | return; |
978 | 0 | } |
979 | 0 | n = os_realloc_array(*res, reslen + 2, sizeof(int)); |
980 | 0 | if (n == NULL) { |
981 | 0 | os_free(*res); |
982 | 0 | *res = NULL; |
983 | 0 | return; |
984 | 0 | } |
985 | | |
986 | 0 | n[reslen] = a; |
987 | 0 | n[reslen + 1] = 0; |
988 | |
|
989 | 0 | *res = n; |
990 | 0 | } |
991 | | |
992 | | |
993 | | bool int_array_includes(const int *arr, int val) |
994 | 0 | { |
995 | 0 | int i; |
996 | |
|
997 | 0 | for (i = 0; arr && arr[i]; i++) { |
998 | 0 | if (val == arr[i]) |
999 | 0 | return true; |
1000 | 0 | } |
1001 | | |
1002 | 0 | return false; |
1003 | 0 | } |
1004 | | |
1005 | | |
1006 | | bool int_array_equal(const int *a, const int *b) |
1007 | 0 | { |
1008 | 0 | size_t alen, blen, i; |
1009 | |
|
1010 | 0 | if (!a || !b) |
1011 | 0 | return false; |
1012 | | |
1013 | 0 | alen = int_array_len(a); |
1014 | 0 | blen = int_array_len(b); |
1015 | |
|
1016 | 0 | if (alen != blen) |
1017 | 0 | return false; |
1018 | | |
1019 | 0 | for (i = 0; i <= alen; i++) { |
1020 | 0 | if (!int_array_includes(b, a[i])) |
1021 | 0 | return false; |
1022 | 0 | } |
1023 | | |
1024 | 0 | return true; |
1025 | 0 | } |
1026 | | |
1027 | | |
1028 | | void str_clear_free(char *str) |
1029 | 0 | { |
1030 | 0 | if (str) { |
1031 | 0 | size_t len = os_strlen(str); |
1032 | 0 | forced_memzero(str, len); |
1033 | 0 | os_free(str); |
1034 | 0 | } |
1035 | 0 | } |
1036 | | |
1037 | | |
1038 | | void bin_clear_free(void *bin, size_t len) |
1039 | 3.28k | { |
1040 | 3.28k | if (bin) { |
1041 | 3.28k | forced_memzero(bin, len); |
1042 | 3.28k | os_free(bin); |
1043 | 3.28k | } |
1044 | 3.28k | } |
1045 | | |
1046 | | |
1047 | | int random_mac_addr(u8 *addr) |
1048 | 0 | { |
1049 | 0 | if (os_get_random(addr, ETH_ALEN) < 0) |
1050 | 0 | return -1; |
1051 | 0 | addr[0] &= 0xfe; /* unicast */ |
1052 | 0 | addr[0] |= 0x02; /* locally administered */ |
1053 | 0 | return 0; |
1054 | 0 | } |
1055 | | |
1056 | | |
1057 | | int random_mac_addr_keep_oui(u8 *addr) |
1058 | 0 | { |
1059 | 0 | if (os_get_random(addr + 3, 3) < 0) |
1060 | 0 | return -1; |
1061 | 0 | addr[0] &= 0xfe; /* unicast */ |
1062 | 0 | addr[0] |= 0x02; /* locally administered */ |
1063 | 0 | return 0; |
1064 | 0 | } |
1065 | | |
1066 | | |
1067 | | /** |
1068 | | * cstr_token - Get next token from const char string |
1069 | | * @str: a constant string to tokenize |
1070 | | * @delim: a string of delimiters |
1071 | | * @last: a pointer to a character following the returned token |
1072 | | * It has to be set to NULL for the first call and passed for any |
1073 | | * further call. |
1074 | | * Returns: a pointer to token position in str or NULL |
1075 | | * |
1076 | | * This function is similar to str_token, but it can be used with both |
1077 | | * char and const char strings. Differences: |
1078 | | * - The str buffer remains unmodified |
1079 | | * - The returned token is not a NULL terminated string, but a token |
1080 | | * position in str buffer. If a return value is not NULL a size |
1081 | | * of the returned token could be calculated as (last - token). |
1082 | | */ |
1083 | | const char * cstr_token(const char *str, const char *delim, const char **last) |
1084 | 0 | { |
1085 | 0 | const char *end, *token = str; |
1086 | |
|
1087 | 0 | if (!str || !delim || !last) |
1088 | 0 | return NULL; |
1089 | | |
1090 | 0 | if (*last) |
1091 | 0 | token = *last; |
1092 | |
|
1093 | 0 | while (*token && os_strchr(delim, *token)) |
1094 | 0 | token++; |
1095 | |
|
1096 | 0 | if (!*token) |
1097 | 0 | return NULL; |
1098 | | |
1099 | 0 | end = token + 1; |
1100 | |
|
1101 | 0 | while (*end && !os_strchr(delim, *end)) |
1102 | 0 | end++; |
1103 | |
|
1104 | 0 | *last = end; |
1105 | 0 | return token; |
1106 | 0 | } |
1107 | | |
1108 | | |
1109 | | /** |
1110 | | * str_token - Get next token from a string |
1111 | | * @buf: String to tokenize. Note that the string might be modified. |
1112 | | * @delim: String of delimiters |
1113 | | * @context: Pointer to save our context. Should be initialized with |
1114 | | * NULL on the first call, and passed for any further call. |
1115 | | * Returns: The next token, NULL if there are no more valid tokens. |
1116 | | */ |
1117 | | char * str_token(char *str, const char *delim, char **context) |
1118 | 0 | { |
1119 | 0 | char *token = (char *) cstr_token(str, delim, (const char **) context); |
1120 | |
|
1121 | 0 | if (token && **context) |
1122 | 0 | *(*context)++ = '\0'; |
1123 | |
|
1124 | 0 | return token; |
1125 | 0 | } |
1126 | | |
1127 | | |
1128 | | size_t utf8_unescape(const char *inp, size_t in_size, |
1129 | | char *outp, size_t out_size) |
1130 | 0 | { |
1131 | 0 | size_t res_size = 0; |
1132 | |
|
1133 | 0 | if (!inp || !outp) |
1134 | 0 | return 0; |
1135 | | |
1136 | 0 | if (!in_size) |
1137 | 0 | in_size = os_strlen(inp); |
1138 | | |
1139 | | /* Advance past leading single quote */ |
1140 | 0 | if (*inp == '\'' && in_size) { |
1141 | 0 | inp++; |
1142 | 0 | in_size--; |
1143 | 0 | } |
1144 | |
|
1145 | 0 | while (in_size) { |
1146 | 0 | in_size--; |
1147 | 0 | if (res_size >= out_size) |
1148 | 0 | return 0; |
1149 | | |
1150 | 0 | switch (*inp) { |
1151 | 0 | case '\'': |
1152 | | /* Terminate on bare single quote */ |
1153 | 0 | *outp = '\0'; |
1154 | 0 | return res_size; |
1155 | | |
1156 | 0 | case '\\': |
1157 | 0 | if (!in_size) |
1158 | 0 | return 0; |
1159 | 0 | in_size--; |
1160 | 0 | inp++; |
1161 | | /* fall through */ |
1162 | |
|
1163 | 0 | default: |
1164 | 0 | *outp++ = *inp++; |
1165 | 0 | res_size++; |
1166 | 0 | } |
1167 | 0 | } |
1168 | | |
1169 | | /* NUL terminate if space allows */ |
1170 | 0 | if (res_size < out_size) |
1171 | 0 | *outp = '\0'; |
1172 | |
|
1173 | 0 | return res_size; |
1174 | 0 | } |
1175 | | |
1176 | | |
1177 | | size_t utf8_escape(const char *inp, size_t in_size, |
1178 | | char *outp, size_t out_size) |
1179 | 0 | { |
1180 | 0 | size_t res_size = 0; |
1181 | |
|
1182 | 0 | if (!inp || !outp) |
1183 | 0 | return 0; |
1184 | | |
1185 | | /* inp may or may not be NUL terminated, but must be if 0 size |
1186 | | * is specified */ |
1187 | 0 | if (!in_size) |
1188 | 0 | in_size = os_strlen(inp); |
1189 | |
|
1190 | 0 | while (in_size) { |
1191 | 0 | in_size--; |
1192 | 0 | if (res_size++ >= out_size) |
1193 | 0 | return 0; |
1194 | | |
1195 | 0 | switch (*inp) { |
1196 | 0 | case '\\': |
1197 | 0 | case '\'': |
1198 | 0 | if (res_size++ >= out_size) |
1199 | 0 | return 0; |
1200 | 0 | *outp++ = '\\'; |
1201 | | /* fall through */ |
1202 | |
|
1203 | 0 | default: |
1204 | 0 | *outp++ = *inp++; |
1205 | 0 | break; |
1206 | 0 | } |
1207 | 0 | } |
1208 | | |
1209 | | /* NUL terminate if space allows */ |
1210 | 0 | if (res_size < out_size) |
1211 | 0 | *outp = '\0'; |
1212 | |
|
1213 | 0 | return res_size; |
1214 | 0 | } |
1215 | | |
1216 | | |
1217 | | int is_ctrl_char(char c) |
1218 | 0 | { |
1219 | 0 | return c > 0 && c < 32; |
1220 | 0 | } |
1221 | | |
1222 | | |
1223 | | /** |
1224 | | * ssid_parse - Parse a string that contains SSID in hex or text format |
1225 | | * @buf: Input NULL terminated string that contains the SSID |
1226 | | * @ssid: Output SSID |
1227 | | * Returns: 0 on success, -1 otherwise |
1228 | | * |
1229 | | * The SSID has to be enclosed in double quotes for the text format or space |
1230 | | * or NULL terminated string of hex digits for the hex format. buf can include |
1231 | | * additional arguments after the SSID. |
1232 | | */ |
1233 | | int ssid_parse(const char *buf, struct wpa_ssid_value *ssid) |
1234 | 0 | { |
1235 | 0 | char *tmp, *res, *end; |
1236 | 0 | size_t len; |
1237 | |
|
1238 | 0 | ssid->ssid_len = 0; |
1239 | |
|
1240 | 0 | tmp = os_strdup(buf); |
1241 | 0 | if (!tmp) |
1242 | 0 | return -1; |
1243 | | |
1244 | 0 | if (*tmp != '"') { |
1245 | 0 | end = os_strchr(tmp, ' '); |
1246 | 0 | if (end) |
1247 | 0 | *end = '\0'; |
1248 | 0 | } else { |
1249 | 0 | end = os_strchr(tmp + 1, '"'); |
1250 | 0 | if (!end) { |
1251 | 0 | os_free(tmp); |
1252 | 0 | return -1; |
1253 | 0 | } |
1254 | | |
1255 | 0 | end[1] = '\0'; |
1256 | 0 | } |
1257 | | |
1258 | 0 | res = wpa_config_parse_string(tmp, &len); |
1259 | 0 | if (res && len <= SSID_MAX_LEN) { |
1260 | 0 | ssid->ssid_len = len; |
1261 | 0 | os_memcpy(ssid->ssid, res, len); |
1262 | 0 | } |
1263 | |
|
1264 | 0 | os_free(tmp); |
1265 | 0 | os_free(res); |
1266 | |
|
1267 | 0 | return ssid->ssid_len ? 0 : -1; |
1268 | 0 | } |
1269 | | |
1270 | | |
1271 | | int str_starts(const char *str, const char *start) |
1272 | 0 | { |
1273 | 0 | return os_strncmp(str, start, os_strlen(start)) == 0; |
1274 | 0 | } |
1275 | | |
1276 | | |
1277 | | /** |
1278 | | * rssi_to_rcpi - Convert RSSI to RCPI |
1279 | | * @rssi: RSSI to convert |
1280 | | * Returns: RCPI corresponding to the given RSSI value, or 255 if not available. |
1281 | | * |
1282 | | * It's possible to estimate RCPI based on RSSI in dBm. This calculation will |
1283 | | * not reflect the correct value for high rates, but it's good enough for Action |
1284 | | * frames which are transmitted with up to 24 Mbps rates. |
1285 | | */ |
1286 | | u8 rssi_to_rcpi(int rssi) |
1287 | 0 | { |
1288 | 0 | if (!rssi) |
1289 | 0 | return 255; /* not available */ |
1290 | 0 | if (rssi < -110) |
1291 | 0 | return 0; |
1292 | 0 | if (rssi > 0) |
1293 | 0 | return 220; |
1294 | 0 | return (rssi + 110) * 2; |
1295 | 0 | } |
1296 | | |
1297 | | |
1298 | | char * get_param(const char *cmd, const char *param) |
1299 | 0 | { |
1300 | 0 | const char *pos, *end; |
1301 | 0 | char *val; |
1302 | 0 | size_t len; |
1303 | |
|
1304 | 0 | pos = os_strstr(cmd, param); |
1305 | 0 | if (!pos) |
1306 | 0 | return NULL; |
1307 | | |
1308 | 0 | pos += os_strlen(param); |
1309 | 0 | end = os_strchr(pos, ' '); |
1310 | 0 | if (end) |
1311 | 0 | len = end - pos; |
1312 | 0 | else |
1313 | 0 | len = os_strlen(pos); |
1314 | 0 | val = os_malloc(len + 1); |
1315 | 0 | if (!val) |
1316 | 0 | return NULL; |
1317 | 0 | os_memcpy(val, pos, len); |
1318 | 0 | val[len] = '\0'; |
1319 | 0 | return val; |
1320 | 0 | } |
1321 | | |
1322 | | |
1323 | | /* Try to prevent most compilers from optimizing out clearing of memory that |
1324 | | * becomes unaccessible after this function is called. This is mostly the case |
1325 | | * for clearing local stack variables at the end of a function. This is not |
1326 | | * exactly perfect, i.e., someone could come up with a compiler that figures out |
1327 | | * the pointer is pointing to memset and then end up optimizing the call out, so |
1328 | | * try go a bit further by storing the first octet (now zero) to make this even |
1329 | | * a bit more difficult to optimize out. Once memset_s() is available, that |
1330 | | * could be used here instead. */ |
1331 | | static void * (* const volatile memset_func)(void *, int, size_t) = memset; |
1332 | | static u8 forced_memzero_val; |
1333 | | |
1334 | | void forced_memzero(void *ptr, size_t len) |
1335 | 3.28k | { |
1336 | 3.28k | memset_func(ptr, 0, len); |
1337 | 3.28k | if (len) |
1338 | 3.28k | forced_memzero_val = ((u8 *) ptr)[0]; |
1339 | 3.28k | } |