MCPcopy Create free account
hub / github.com/ElementsProject/elements / TryParsePermissionFlags

Function TryParsePermissionFlags

src/net_permissions.cpp:24–67  ·  view source on GitHub ↗

Parse the following format: "perm1,perm2@xxxxxx"

Source from the content-addressed store, hash-verified

22
23// Parse the following format: "perm1,perm2@xxxxxx"
24bool TryParsePermissionFlags(const std::string& str, NetPermissionFlags& output, size_t& readen, bilingual_str& error)
25{
26 NetPermissionFlags flags = NetPermissionFlags::None;
27 const auto atSeparator = str.find('@');
28
29 // if '@' is not found (ie, "xxxxx"), the caller should apply implicit permissions
30 if (atSeparator == std::string::npos) {
31 NetPermissions::AddFlag(flags, NetPermissionFlags::Implicit);
32 readen = 0;
33 }
34 // else (ie, "perm1,perm2@xxxxx"), let's enumerate the permissions by splitting by ',' and calculate the flags
35 else {
36 readen = 0;
37 // permissions == perm1,perm2
38 const auto permissions = str.substr(0, atSeparator);
39 while (readen < permissions.length()) {
40 const auto commaSeparator = permissions.find(',', readen);
41 const auto len = commaSeparator == std::string::npos ? permissions.length() - readen : commaSeparator - readen;
42 // permission == perm1
43 const auto permission = permissions.substr(readen, len);
44 readen += len; // We read "perm1"
45 if (commaSeparator != std::string::npos) readen++; // We read ","
46
47 if (permission == "bloomfilter" || permission == "bloom") NetPermissions::AddFlag(flags, NetPermissionFlags::BloomFilter);
48 else if (permission == "noban") NetPermissions::AddFlag(flags, NetPermissionFlags::NoBan);
49 else if (permission == "forcerelay") NetPermissions::AddFlag(flags, NetPermissionFlags::ForceRelay);
50 else if (permission == "mempool") NetPermissions::AddFlag(flags, NetPermissionFlags::Mempool);
51 else if (permission == "download") NetPermissions::AddFlag(flags, NetPermissionFlags::Download);
52 else if (permission == "all") NetPermissions::AddFlag(flags, NetPermissionFlags::All);
53 else if (permission == "relay") NetPermissions::AddFlag(flags, NetPermissionFlags::Relay);
54 else if (permission == "addr") NetPermissions::AddFlag(flags, NetPermissionFlags::Addr);
55 else if (permission.length() == 0); // Allow empty entries
56 else {
57 error = strprintf(_("Invalid P2P permission: '%s'"), permission);
58 return false;
59 }
60 }
61 readen++;
62 }
63
64 output = flags;
65 error = Untranslated("");
66 return true;
67}
68
69}
70

Callers 1

TryParseMethod · 0.85

Calls 3

_Function · 0.85
UntranslatedFunction · 0.85
findMethod · 0.80

Tested by

no test coverage detected