-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifiCredentials.cpp
More file actions
58 lines (50 loc) · 1.75 KB
/
Copy pathwifiCredentials.cpp
File metadata and controls
58 lines (50 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "wifiCredentials.h"
#include <cstring>
static bool isEmpty(const char *s) { return s == nullptr || s[0] == '\0'; }
// Copy up to sizeof(dst)-1 chars and always NUL-terminate.
static void copyField(char *dst, size_t dstSize, const char *src)
{
if (dstSize == 0) return;
if (src == nullptr) src = "";
size_t n = 0;
while (src[n] != '\0' && n < dstSize - 1)
{
dst[n] = src[n];
n++;
}
dst[n] = '\0';
}
int orderWifiCandidates(const char *savedSsid, const char *savedPass,
const char *builtinSsid, const char *builtinPass,
WifiCandidate *out, int outCap)
{
int count = 0;
// Portal-saved network first: the user typed it into the captive portal on
// purpose.
if (!isEmpty(savedSsid) && count < outCap)
{
copyField(out[count].ssid, sizeof(out[count].ssid), savedSsid);
copyField(out[count].pass, sizeof(out[count].pass), savedPass);
out[count].source = "saved";
count++;
}
// Compiled-in secrets.h pair, unless it is the exact same network already
// queued as "saved" (matching the field-truncated values, so an over-long
// input can't sneak past the dedupe).
if (!isEmpty(builtinSsid) && count < outCap)
{
WifiCandidate builtin;
copyField(builtin.ssid, sizeof(builtin.ssid), builtinSsid);
copyField(builtin.pass, sizeof(builtin.pass), builtinPass);
builtin.source = "built-in";
bool duplicate = (count > 0 &&
strcmp(out[0].ssid, builtin.ssid) == 0 &&
strcmp(out[0].pass, builtin.pass) == 0);
if (!duplicate)
{
out[count] = builtin;
count++;
}
}
return count;
}