Large updates to webserver
Do not merge yet
Change-Id: I38c56844c1b0e3e8e5493c2705e62e6db7ee2102
diff --git a/src/base64.cpp b/src/base64.cpp
index 0715a8e..54a6008 100644
--- a/src/base64.cpp
+++ b/src/base64.cpp
@@ -1,12 +1,18 @@
#include <base64.hpp>
-#include <cassert>
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+/";
- unsigned int input_length = input.size();
+ size_t input_length = input.size();
// allocate space for output string
output.clear();
@@ -17,32 +23,33 @@
// encoding_data lookup table.
// if input do not contains enough chars to complete 3-byte sequence,use pad
// char '='
- for (unsigned int i = 0; i < input_length; i++) {
+ for (size_t i = 0; i < input_length; i++) {
int base64code0 = 0;
int base64code1 = 0;
int base64code2 = 0;
int base64code3 = 0;
base64code0 = (input[i] >> 2) & 0x3f; // 1-byte 6 bits
- output += encoding_data[base64code0];
+
+ output += encoding_data[base64code0]; // NOLINT
base64code1 = (input[i] << 4) & 0x3f; // 1-byte 2 bits +
if (++i < input_length) {
base64code1 |= (input[i] >> 4) & 0x0f; // 2-byte 4 bits
- output += encoding_data[base64code1];
- base64code2 = (input[i] << 2) & 0x3f; // 2-byte 4 bits +
+ output += encoding_data[base64code1]; // NOLINT
+ base64code2 = (input[i] << 2) & 0x3f; // 2-byte 4 bits +
if (++i < input_length) {
base64code2 |= (input[i] >> 6) & 0x03; // 3-byte 2 bits
base64code3 = input[i] & 0x3f; // 3-byte 6 bits
- output += encoding_data[base64code2];
- output += encoding_data[base64code3];
+ output += encoding_data[base64code2]; // NOLINT
+ output += encoding_data[base64code3]; // NOLINT
} else {
- output += encoding_data[base64code2];
+ output += encoding_data[base64code2]; // NOLINT
output += '=';
}
} else {
- output += encoding_data[base64code1];
+ output += encoding_data[base64code1]; // NOLINT
output += '=';
output += '=';
}
@@ -53,6 +60,7 @@
bool base64_decode(const std::string &input, std::string &output) {
static const char nop = -1;
+ // See note on encoding_data[] in above function
static const char 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,
@@ -73,7 +81,7 @@
nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
nop};
- unsigned int input_length = input.size();
+ size_t input_length = input.size();
// allocate space for output string
output.clear();
@@ -81,53 +89,53 @@
// for each 4-bytes sequence from the input, extract 4 6-bits sequences by
// droping first two bits
- // and regenerate into 3 8-bits sequence
+ // and regenerate into 3 8-bits sequences
- for (unsigned int i = 0; i < input_length; i++) {
+ for (size_t i = 0; i < input_length; i++) {
char base64code0;
char base64code1;
char base64code2 = 0; // initialized to 0 to suppress warnings
char base64code3;
- base64code0 = decoding_data[static_cast<int>(input[i])];
- if (base64code0 == nop) // non base64 character
+ base64code0 = decoding_data[static_cast<int>(input[i])]; // NOLINT
+ if (base64code0 == nop) { // non base64 character
return false;
- if (!(++i < input_length)) // we need at least two input bytes for first
- // byte output
+ }
+ if (!(++i < input_length)) { // we need at least two input bytes for first
+ // byte output
return false;
- base64code1 = decoding_data[static_cast<int>(input[i])];
- if (base64code1 == nop) // non base64 character
+ }
+ base64code1 = decoding_data[static_cast<int>(input[i])]; // NOLINT
+ if (base64code1 == nop) { // non base64 character
return false;
-
- output += ((base64code0 << 2) | ((base64code1 >> 4) & 0x3));
+ }
+ output += static_cast<char>((base64code0 << 2) | ((base64code1 >> 4) & 0x3));
if (++i < input_length) {
char c = input[i];
if (c == '=') { // padding , end of input
- assert((base64code1 & 0x0f) == 0);
- return true;
+ return (base64code1 & 0x0f) == 0;
}
- base64code2 = decoding_data[static_cast<int>(input[i])];
- if (base64code2 == nop) // non base64 character
+ base64code2 = decoding_data[static_cast<int>(input[i])]; // NOLINT
+ if (base64code2 == nop) { // non base64 character
return false;
-
- output += ((base64code1 << 4) & 0xf0) | ((base64code2 >> 2) & 0x0f);
+ }
+ output += static_cast<char>(((base64code1 << 4) & 0xf0) | ((base64code2 >> 2) & 0x0f));
}
if (++i < input_length) {
char c = input[i];
if (c == '=') { // padding , end of input
- assert((base64code2 & 0x03) == 0);
- return true;
+ return (base64code2 & 0x03) == 0;
}
- base64code3 = decoding_data[static_cast<int>(input[i])];
- if (base64code3 == nop) // non base64 character
+ base64code3 = decoding_data[static_cast<int>(input[i])]; // NOLINT
+ if (base64code3 == nop) { // non base64 character
return false;
-
- output += (((base64code2 << 6) & 0xc0) | base64code3);
+ }
+ output += static_cast<char>((((base64code2 << 6) & 0xc0) | base64code3));
}
}
return true;
}
-}
\ No newline at end of file
+} // namespace base64