Skip to content

Commit a177116

Browse files
Jake ChampionJakeChampion
authored andcommitted
make parse be const
1 parent 82c82c8 commit a177116

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

c-dependencies/js-compute-runtime/builtins/backend.cpp

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,25 @@ class OpenSSLCipherConfigurationParser {
252252
static constexpr auto COMPLEMENTOFDEFAULT = "COMPLEMENTOFDEFAULT";
253253
static constexpr auto ALL = "ALL";
254254

255-
void moveToEnd(const AliasMap &aliases, std::vector<Cipher> &ciphers, std::string_view cipher) {
255+
void moveToEnd(const AliasMap &aliases, std::vector<Cipher> &ciphers,
256+
std::string_view cipher) const {
256257
this->moveToEnd(ciphers, aliases.at(cipher));
257258
}
258259

259-
void moveToEnd(std::vector<Cipher> &ciphers, const std::vector<Cipher> &ciphersToMoveToEnd) {
260+
void moveToEnd(std::vector<Cipher> &ciphers,
261+
const std::vector<Cipher> &ciphersToMoveToEnd) const {
260262
std::stable_partition(ciphers.begin(), ciphers.end(), [ciphersToMoveToEnd](auto cipher) {
261263
return std::find(ciphersToMoveToEnd.begin(), ciphersToMoveToEnd.end(), cipher) ==
262264
ciphersToMoveToEnd.end();
263265
});
264266
}
265267

266-
void add(const AliasMap &aliases, std::vector<Cipher> &ciphers, std::string_view alias) {
268+
void add(const AliasMap &aliases, std::vector<Cipher> &ciphers, std::string_view alias) const {
267269
auto toAdd = aliases.at(alias);
268270
ciphers.insert(ciphers.end(), toAdd.begin(), toAdd.end());
269271
}
270272

271-
void remove(const AliasMap &aliases, std::vector<Cipher> &ciphers, std::string_view alias) {
273+
void remove(const AliasMap &aliases, std::vector<Cipher> &ciphers, std::string_view alias) const {
272274
auto &toRemove = aliases.at(alias);
273275
ciphers.erase(std::remove_if(ciphers.begin(), ciphers.end(),
274276
[&](auto x) {
@@ -278,7 +280,7 @@ class OpenSSLCipherConfigurationParser {
278280
ciphers.end());
279281
}
280282

281-
void strengthSort(std::vector<Cipher> &ciphers) {
283+
void strengthSort(std::vector<Cipher> &ciphers) const {
282284
/*
283285
* This routine sorts the ciphers with descending strength. The sorting
284286
* must keep the pre-sorted sequence.
@@ -291,13 +293,13 @@ class OpenSSLCipherConfigurationParser {
291293
* See
292294
* https://github.com/openssl/openssl/blob/709651c9022e7be7e69cf8a2f6edf2c8722a6a1e/ssl/ssl_ciph.c#L1455
293295
*/
294-
void defaultSort(std::vector<Cipher> &ciphers) {
296+
void defaultSort(std::vector<Cipher> &ciphers) const {
295297
auto byStrength = [](auto &l, auto &r) { return l.strength_bits > r.strength_bits; };
296298
// order all ciphers by strength first
297299
std::sort(ciphers.begin(), ciphers.end(), byStrength);
298300

299-
auto it =
300-
std::stable_partition(ciphers.begin(), ciphers.end(), this->byKeyExchange(KeyExchange::EECDH));
301+
auto it = std::stable_partition(ciphers.begin(), ciphers.end(),
302+
this->byKeyExchange(KeyExchange::EECDH));
301303

302304
/* AES is our preferred symmetric cipher */
303305
auto aes = {Encryption::AES128, Encryption::AES128GCM, Encryption::AES256,
@@ -307,40 +309,40 @@ class OpenSSLCipherConfigurationParser {
307309
it = std::stable_partition(it, ciphers.end(), this->byEncryption(aes));
308310

309311
/* Move ciphers without forward secrecy to the end */;
310-
std::stable_partition(it, ciphers.end(), [compare = this->byKeyExchange(KeyExchange::RSA)](auto &c) {
311-
return !compare(c);
312-
});
312+
std::stable_partition(
313+
it, ciphers.end(),
314+
[compare = this->byKeyExchange(KeyExchange::RSA)](auto &c) { return !compare(c); });
313315
}
314316

315-
std::function<bool(const Cipher &)> byProtocol(Protocol val) {
317+
std::function<bool(const Cipher &)> byProtocol(Protocol val) const {
316318
return [val](auto &c) { return c.protocol == val; };
317319
}
318320

319-
std::function<bool(const Cipher &)> byKeyExchange(KeyExchange val) {
321+
std::function<bool(const Cipher &)> byKeyExchange(KeyExchange val) const {
320322
return [val](auto &c) { return c.kx == val; };
321323
}
322324

323-
std::function<bool(const Cipher &)> byAuthentication(Authentication val) {
325+
std::function<bool(const Cipher &)> byAuthentication(Authentication val) const {
324326
return [val](auto &c) { return c.au == val; };
325327
}
326328

327-
std::function<bool(const Cipher &)> byEncryption(std::set<Encryption> vals) {
329+
std::function<bool(const Cipher &)> byEncryption(std::set<Encryption> vals) const {
328330
return [vals](auto &c) { return vals.find(c.enc) != vals.end(); };
329331
}
330332

331-
std::function<bool(const Cipher &)> byEncryption(Encryption val) {
333+
std::function<bool(const Cipher &)> byEncryption(Encryption val) const {
332334
return [val](auto &c) { return c.enc == val; };
333335
}
334336

335-
std::function<bool(const Cipher &)> byEncryptionLevel(EncryptionLevel val) {
337+
std::function<bool(const Cipher &)> byEncryptionLevel(EncryptionLevel val) const {
336338
return [val](auto &c) { return c.level == val; };
337339
}
338340

339-
std::function<bool(const Cipher &)> byMessageDigest(MessageDigest val) {
341+
std::function<bool(const Cipher &)> byMessageDigest(MessageDigest val) const {
340342
return [val](auto &c) { return c.mac == val; };
341343
}
342344

343-
std::vector<std::string_view> split(std::string_view s, std::string_view delimiter) {
345+
std::vector<std::string_view> split(std::string_view s, std::string_view delimiter) const {
344346
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
345347
std::string token;
346348
std::vector<std::string_view> res;
@@ -355,7 +357,7 @@ class OpenSSLCipherConfigurationParser {
355357
return res;
356358
}
357359

358-
std::pair<std::string_view, std::string_view> split_on(std::string_view str, char c) {
360+
std::pair<std::string_view, std::string_view> split_on(std::string_view str, char c) const {
359361
auto ix = str.find(c);
360362
if (ix == str.npos) {
361363
return {str, ""};
@@ -370,7 +372,7 @@ class OpenSSLCipherConfigurationParser {
370372
return {left, str.substr(ix)};
371373
}
372374

373-
std::vector<std::string_view> splitCipherSuiteString(std::string_view string) {
375+
std::vector<std::string_view> splitCipherSuiteString(std::string_view string) const {
374376
std::vector<std::string_view> result;
375377

376378
while (!string.empty()) {
@@ -531,7 +533,7 @@ class OpenSSLCipherConfigurationParser {
531533
aliases.insert({DEFAULT, this->parse("ALL:!COMPLEMENTOFDEFAULT:!eNULL")});
532534
}
533535

534-
std::vector<Cipher> parse(std::string_view expression) {
536+
std::vector<Cipher> parse(std::string_view expression) const {
535537
/**
536538
* All ciphers by their openssl alias name.
537539
*/
@@ -547,8 +549,10 @@ class OpenSSLCipherConfigurationParser {
547549
}
548550
} else if (element.rfind(EXCLUDE, 0) == 0) {
549551
auto alias = element.substr(1);
550-
if (aliases.find(alias) != aliases.end()) {
551-
auto toAdd = aliases[alias];
552+
auto found = aliases.find(alias);
553+
if (found != aliases.end()) {
554+
555+
auto toAdd = found.operator->()->second;
552556
removedCiphers.insert(removedCiphers.end(), toAdd.begin(), toAdd.end());
553557
}
554558
} else if (element.rfind(TO_END, 0) == 0) {
@@ -563,23 +567,26 @@ class OpenSSLCipherConfigurationParser {
563567
this->add(aliases, ciphers, element);
564568
} else if (element.find(AND) != std::string::npos) {
565569
auto intersections = this->split(element, "+\\");
566-
if (intersections.size() > 0 && aliases.find(intersections[0]) != aliases.end()) {
567-
auto result{aliases[intersections[0]]};
568-
for (int i = 1; i < intersections.size(); i++) {
569-
auto alias = aliases.find(intersections[i]);
570-
if (alias != aliases.end()) {
571-
// make `result` only contain the aliases from `alias`
572-
result.erase(std::remove_if(result.begin(), result.end(),
573-
[&](auto x) {
574-
return std::find(alias->second.begin(),
575-
alias->second.end(),
576-
x) != alias->second.end();
577-
}),
578-
result.end());
570+
if (intersections.size() > 0) {
571+
auto found = aliases.find(intersections[0]);
572+
if (found != aliases.end()) {
573+
auto result{found.operator->()->second};
574+
for (int i = 1; i < intersections.size(); i++) {
575+
auto alias = aliases.find(intersections[i]);
576+
if (alias != aliases.end()) {
577+
// make `result` only contain the aliases from `alias`
578+
result.erase(std::remove_if(result.begin(), result.end(),
579+
[&](auto x) {
580+
return std::find(alias->second.begin(),
581+
alias->second.end(),
582+
x) != alias->second.end();
583+
}),
584+
result.end());
585+
}
579586
}
587+
// Add all of `result` onto `ciphers`
588+
ciphers.insert(ciphers.end(), result.begin(), result.end());
580589
}
581-
// Add all of `result` onto `ciphers`
582-
ciphers.insert(ciphers.end(), result.begin(), result.end());
583590
}
584591
}
585592
}

0 commit comments

Comments
 (0)