1
8
9
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
80
81
82
83
84
85
86
87
94
95
96
97
98
99
100
101
102
103
104
105
106
107
117
118
119
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
159
163
167
171
175
179
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
211
215
219
223
227
231
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
262
263
264
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
298
299
300
301
302
303
304
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
342
343
344
345
346
347
348
349
350
351
353
354
355
356
357
360
361
362
363
364
365
366
367
368
369
373
374
375
376
377
378
379
380
390
391
392
402
403
404
405
406
407
408
409
410
411
412
413
422
423
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
470
471
472
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* ... */
#include "includes.h"
#include <limits.h>
#include <time.h>
#include "common/ieee802_11_defs.h"
#include "common.h"5 includes
static int hex2num(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}{ ... }
int hex2byte(const char *hex)
{
int a, b;
a = hex2num(*hex++);
if (a < 0)
return -1;
b = hex2num(*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}{ ... }
/* ... */
int hwaddr_aton2(const char *txt, u8 *addr)
{
int i;
const char *pos = txt;
for (i = 0; i < 6; i++) {
int a, b;
while (*pos == ':' || *pos == '.' || *pos == '-')
pos++;
a = hex2num(*pos++);
if (a < 0)
return -1;
b = hex2num(*pos++);
if (b < 0)
return -1;
*addr++ = (a << 4) | b;
}{...}
return pos - txt;
}{ ... }
/* ... */
int hexstr2bin(const char *hex, u8 *buf, size_t len)
{
size_t i;
int a;
const char *ipos = hex;
u8 *opos = buf;
for (i = 0; i < len; i++) {
a = hex2byte(ipos);
if (a < 0)
return -1;
*opos++ = a;
ipos += 2;
}{...}
return 0;
}{ ... }
/* ... */
void inc_byte_array(u8 *counter, size_t len)
{
int pos = len - 1;
while (pos >= 0) {
counter[pos]++;
if (counter[pos] != 0)
break;
pos--;
}{...}
}{ ... }
void buf_shift_right(u8 *buf, size_t len, size_t bits)
{
size_t i;
for (i = len - 1; i > 0; i--)
buf[i] = (buf[i - 1] << (8 - bits)) | (buf[i] >> bits);
buf[0] >>= bits;
}{ ... }
void wpa_get_ntp_timestamp(u8 *buf)
{
struct os_time now;
u32 sec, usec;
be32 tmp;
os_get_time(&now);
sec = now.sec + 2208988800U;
usec = now.usec;
usec = 4295 * usec - (usec >> 5) - (usec >> 9);
tmp = host_to_be32(sec);
os_memcpy(buf, (u8 *) &tmp, 4);
tmp = host_to_be32(usec);
os_memcpy(buf + 4, (u8 *) &tmp, 4);
}{ ... }
void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len)
{
char *end = txt + maxlen;
size_t i;
for (i = 0; i < len; i++) {
if (txt + 4 >= end)
break;
switch (data[i]) {
case '\"':
*txt++ = '\\';
*txt++ = '\"';
break;...
case '\\':
*txt++ = '\\';
*txt++ = '\\';
break;...
case '\033':
*txt++ = '\\';
*txt++ = 'e';
break;...
case '\n':
*txt++ = '\\';
*txt++ = 'n';
break;...
case '\r':
*txt++ = '\\';
*txt++ = 'r';
break;...
case '\t':
*txt++ = '\\';
*txt++ = 't';
break;...
default:
if (data[i] >= 32 && data[i] <= 126) {
*txt++ = data[i];
}{...} else {
txt += os_snprintf(txt, end - txt, "\\x%02x",
data[i]);
}{...}
break;...
}{...}
}{...}
*txt = '\0';
}{ ... }
size_t printf_decode(u8 *buf, size_t maxlen, const char *str)
{
const char *pos = str;
size_t len = 0;
int val;
while (*pos) {
if (len + 1 >= maxlen)
break;
switch (*pos) {
case '\\':
pos++;
switch (*pos) {
case '\\':
buf[len++] = '\\';
pos++;
break;...
case '"':
buf[len++] = '"';
pos++;
break;...
case 'n':
buf[len++] = '\n';
pos++;
break;...
case 'r':
buf[len++] = '\r';
pos++;
break;...
case 't':
buf[len++] = '\t';
pos++;
break;...
case 'e':
buf[len++] = '\033';
pos++;
break;...
case 'x':
pos++;
val = hex2byte(pos);
if (val < 0) {
val = hex2num(*pos);
if (val < 0)
break;
buf[len++] = val;
pos++;
}{...} else {
buf[len++] = val;
pos += 2;
}{...}
break;...
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
val = *pos++ - '0';
if (*pos >= '0' && *pos <= '7')
val = val * 8 + (*pos++ - '0');
if (*pos >= '0' && *pos <= '7')
val = val * 8 + (*pos++ - '0');
buf[len++] = val;
break;...
default:
break;...
}{...}
break;...
default:
buf[len++] = *pos++;
break;...
}{...}
}{...}
if (maxlen > len)
buf[len] = '\0';
return len;
}{ ... }
/* ... */
const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
{
static char ssid_txt[SSID_MAX_LEN * 4 + 1];
if (ssid == NULL) {
ssid_txt[0] = '\0';
return ssid_txt;
}{...}
printf_encode(ssid_txt, sizeof(ssid_txt), ssid, ssid_len);
return ssid_txt;
}{ ... }
void * __hide_aliasing_typecast(void *foo)
{
return foo;
}{ ... }
char * wpa_config_parse_string(const char *value, size_t *len)
{
if (*value == '"') {
const char *pos;
char *str;
value++;
pos = os_strrchr(value, '"');
if (pos == NULL || pos[1] != '\0')
return NULL;
*len = pos - value;
str = dup_binstr(value, *len);
if (str == NULL)
return NULL;
return str;
}{...} else if (*value == 'P' && value[1] == '"') {
const char *pos;
char *tstr, *str;
size_t tlen;
value += 2;
pos = os_strrchr(value, '"');
if (pos == NULL || pos[1] != '\0')
return NULL;
tlen = pos - value;
tstr = dup_binstr(value, tlen);
if (tstr == NULL)
return NULL;
str = os_malloc(tlen + 1);
if (str == NULL) {
os_free(tstr);
return NULL;
}{...}
*len = printf_decode((u8 *) str, tlen + 1, tstr);
os_free(tstr);
return str;
}{...} else {
u8 *str;
size_t tlen, hlen = os_strlen(value);
#ifndef ESP_SUPPLICANT
if (hlen & 1)
return NULL;/* ... */
#else
if (hlen == 5 || hlen == 13) {
*len = hlen;
str = (u8 *)os_malloc(*len + 1);
if (str == NULL) {
return NULL;
}{...}
memcpy(str, value, *len);
str[*len] = '\0';
return (char *) str;
}{...}
#endif/* ... */
tlen = hlen / 2;
str = os_malloc(tlen + 1);
if (str == NULL)
return NULL;
if (hexstr2bin(value, str, tlen)) {
os_free(str);
return NULL;
}{...}
str[tlen] = '\0';
*len = tlen;
return (char *) str;
}{...}
}{ ... }
int wpa_is_hex(const u8 *data, size_t len)
{
size_t i;
for (i = 0; i < len; i++) {
if (data[i] < 32 || data[i] >= 127)
return 1;
}{...}
return 0;
}{ ... }
int has_ctrl_char(const u8 *data, size_t len)
{
size_t i;
for (i = 0; i < len; i++) {
if (data[i] < 32 || data[i] == 127)
return 1;
}{...}
return 0;
}{ ... }
size_t wpa_merge_byte_arrays(u8 *res, size_t res_len,
const u8 *src1, size_t src1_len,
const u8 *src2, size_t src2_len)
{
size_t len = 0;
os_memset(res, 0, res_len);
if (src1) {
if (src1_len >= res_len) {
os_memcpy(res, src1, res_len);
return res_len;
}{...}
os_memcpy(res, src1, src1_len);
len += src1_len;
}{...}
if (src2) {
if (len + src2_len >= res_len) {
os_memcpy(res + len, src2, res_len - len);
return res_len;
}{...}
os_memcpy(res + len, src2, src2_len);
len += src2_len;
}{...}
return len;
}{ ... }
char * dup_binstr(const void *src, size_t len)
{
char *res;
if (src == NULL)
return NULL;
res = os_malloc(len + 1);
if (res == NULL)
return NULL;
os_memcpy(res, src, len);
res[len] = '\0';
return res;
}{ ... }
size_t int_array_len(const int *a)
{
size_t i;
for (i = 0; a && a[i]; i++)
;
return i;
}{ ... }
void str_clear_free(char *str)
{
if (str) {
size_t len = os_strlen(str);
forced_memzero(str, len);
os_free(str);
}{...}
}{ ... }
void bin_clear_free(void *bin, size_t len)
{
if (bin) {
forced_memzero(bin, len);
os_free(bin);
}{...}
}{ ... }
int os_gmtime(os_time_t t, struct os_tm *tm)
{
struct tm *tm2;
time_t t2 = t;
tm2 = gmtime(&t2);
if (tm2 == NULL)
return -1;
tm->sec = tm2->tm_sec;
tm->min = tm2->tm_min;
tm->hour = tm2->tm_hour;
tm->day = tm2->tm_mday;
tm->month = tm2->tm_mon + 1;
tm->year = tm2->tm_year + 1900;
return 0;
}{ ... }
int os_mktime(int year, int month, int day, int hour, int min, int sec,
os_time_t *t)
{
struct tm tm;
if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
sec > 60)
return -1;
os_memset(&tm, 0, sizeof(tm));
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
tm.tm_hour = hour;
tm.tm_min = min;
tm.tm_sec = sec;
*t = (os_time_t) mktime(&tm);
return 0;
}{ ... }
void * os_memdup(const void *src, size_t len)
{
void *r = os_malloc(len);
if (r && src)
os_memcpy(r, src, len);
return r;
}{ ... }
int os_reltime_expired(struct os_time *now,
struct os_time *ts,
os_time_t timeout_secs)
{
struct os_time age;
os_time_sub(now, ts, &age);
return (age.sec > timeout_secs) ||
(age.sec == timeout_secs && age.usec > 0);
}{ ... }
int os_reltime_initialized(struct os_reltime *t)
{
return t->sec != 0 || t->usec != 0;
}{ ... }
u8 rssi_to_rcpi(int rssi)
{
if (!rssi)
return 255;
if (rssi < -110)
return 0;
if (rssi > 0)
return 220;
return (rssi + 110) * 2;
}{ ... }
char * get_param(const char *cmd, const char *param)
{
const char *pos, *end;
char *val;
size_t len;
pos = os_strstr(cmd, param);
if (!pos)
return NULL;
pos += os_strlen(param);
end = os_strchr(pos, ' ');
if (end)
len = end - pos;
else
len = os_strlen(pos);
val = os_malloc(len + 1);
if (!val)
return NULL;
os_memcpy(val, pos, len);
val[len] = '\0';
return val;
}{ ... }