summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEd Tanous <ed.tanous@intel.com>2019-10-23 21:46:55 +0300
committerJames Feist <james.feist@linux.intel.com>2020-03-06 20:14:06 +0300
commitf7929c31c792f96afd1d4e7469f5bf599a553487 (patch)
tree46e0d8ddb5dfee4f0158a08e6b774c4c9ac8a8be /src
parentf365910caf848c4cbc77f2177e66d1f2498c457d (diff)
downloadbmcweb-f7929c31c792f96afd1d4e7469f5bf599a553487.tar.xz
Implement a TODO(ed)
Aparently the static analysers have gotten smarter, as has my understanding of operator[] on std::array. Fix the character array to not use c style arrays. Tested: Should have no impact. Will test using webui to verify that sessions are still generated properly. Signed-off-by: Ed Tanous <ed.tanous@intel.com> Change-Id: Iaa6cbac7594dfb0c83383ff62fc591dd1d786547
Diffstat (limited to 'src')
-rw-r--r--src/base64.cpp32
1 files changed, 17 insertions, 15 deletions
diff --git a/src/base64.cpp b/src/base64.cpp
index 851ef72a42..2e64bfa2f6 100644
--- a/src/base64.cpp
+++ b/src/base64.cpp
@@ -4,15 +4,15 @@ namespace base64
{
bool base64_encode(const std::string &input, std::string &output)
{
- // This is left as a raw array (and not a range checked std::array) under
- // the suspicion that the optimizer is not smart enough to remove the range
- // checks that would be done below if at were called. As is, this array is
- // 64 bytes long, which should be greater than the max of 0b00111111 when
- // indexed NOLINT calls below are to silence clang-tidy about this
- // TODO(ed) this requires further investigation if a more safe method could
- // be used without performance impact.
- static const char encoding_data[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ // As is, this array is 64 bytes long, which should be greater than the max
+ // of 0b00111111 when indexed NOLINT calls below are to silence clang-tidy
+ // warnings about this
+ constexpr std::array<char, 64> encoding_data = {
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+ 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+ 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
size_t input_length = input.size();
@@ -69,9 +69,9 @@ bool base64_encode(const std::string &input, std::string &output)
bool base64_decode(const std::string &input, std::string &output)
{
- static const char nop = -1;
+ constexpr char nop = -1;
// See note on encoding_data[] in above function
- static const char decoding_data[] = {
+ constexpr std::array<char, 256> decoding_data = {
nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
@@ -109,7 +109,7 @@ bool base64_decode(const std::string &input, std::string &output)
char base64code2 = 0; // initialized to 0 to suppress warnings
char base64code3;
- base64code0 = decoding_data[static_cast<int>(input[i])]; // NOLINT
+ base64code0 = decoding_data[static_cast<size_t>(input[i])]; // NOLINT
if (base64code0 == nop)
{ // non base64 character
return false;
@@ -119,7 +119,7 @@ bool base64_decode(const std::string &input, std::string &output)
// byte output
return false;
}
- base64code1 = decoding_data[static_cast<int>(input[i])]; // NOLINT
+ base64code1 = decoding_data[static_cast<size_t>(input[i])]; // NOLINT
if (base64code1 == nop)
{ // non base64 character
return false;
@@ -134,7 +134,8 @@ bool base64_decode(const std::string &input, std::string &output)
{ // padding , end of input
return (base64code1 & 0x0f) == 0;
}
- base64code2 = decoding_data[static_cast<int>(input[i])]; // NOLINT
+ base64code2 =
+ decoding_data[static_cast<size_t>(input[i])]; // NOLINT
if (base64code2 == nop)
{ // non base64 character
return false;
@@ -150,7 +151,8 @@ bool base64_decode(const std::string &input, std::string &output)
{ // padding , end of input
return (base64code2 & 0x03) == 0;
}
- base64code3 = decoding_data[static_cast<int>(input[i])]; // NOLINT
+ base64code3 =
+ decoding_data[static_cast<size_t>(input[i])]; // NOLINT
if (base64code3 == nop)
{ // non base64 character
return false;