phrocker commented on a change in pull request #690: MINIFICPP-1026 - Added
base64 encoder-decoder to StringUtils
URL: https://github.com/apache/nifi-minifi-cpp/pull/690#discussion_r354289136
##########
File path: libminifi/include/utils/StringUtils.h
##########
@@ -330,6 +336,194 @@ class StringUtils {
return to_hex(reinterpret_cast<const uint8_t*>(str.data()), str.length(),
uppercase);
}
+ /**
+ * Hexencodes a vector of bytes
+ * @param data the vector of bytes to be hexencoded
+ * @param uppercase whether the hexencoded string should be upper case
+ * @return the hexencoded string
+ */
+ inline static std::string to_hex(const std::vector<uint8_t>& data, bool
uppercase = false) {
+ return to_hex(data.data(), data.size(), uppercase);
+ }
+
+ /**
+ * Decodes the Base64 encoded string into data
+ * @param data the output buffer where the decoded bytes will be written.
Must be at least (base64_length / 4 + 1) * 3 bytes long.
+ * @param data_length pointer to the length of data the data buffer. It will
be filled with the length of the decoded bytes.
+ * @param base64 the Base64 encoded string
+ * @param base64_length the length of base64
+ * @return true on success
+ */
+ inline static bool from_base64(uint8_t* data, size_t* data_length, const
char* base64, size_t base64_length) {
+ if (*data_length < (base64_length / 4 + 1) * 3) {
+ return false;
+ }
+
+ uint8_t digits[4];
+ size_t digit_counter = 0U;
+ size_t decoded_size = 0U;
+ size_t padding_counter = 0U;
+ size_t i;
+ for (i = 0U; i < base64_length; i++) {
+ const uint8_t byte = static_cast<uint8_t>(base64[i]);
+ if (byte > 127) {
+ return false;
+ }
+
+ const uint8_t decoded = base64_dec_lut[byte];
+ switch (decoded) {
+ case SKIP:
+ continue;
+ case ILGL:
+ return false;
+ case PDNG:
+ padding_counter++;
+ continue;
+ default:
+ if (padding_counter > 0U) {
+ return false;
+ }
+ digits[digit_counter++] = decoded;
+ if (digit_counter == 4U) {
+ base64_digits_to_bytes(digits, data + decoded_size);
+ decoded_size += 3U;
+ digit_counter = 0U;
+ }
+ }
+ }
+
+ if (padding_counter > 0U && padding_counter != 4U - digit_counter) {
+ return false;
+ }
+
+ switch (digit_counter) {
+ case 0:
+ break;
+ case 1:
+ return false;
+ case 2:
+ digits[2] = 0x00;
+ case 3: {
+ digits[3] = 0x00;
+
+ uint8_t bytes_temp[3];
+ base64_digits_to_bytes(digits, bytes_temp);
+ const size_t num_bytes = digit_counter == 2 ? 1 : 2;
+ memcpy(data + decoded_size, bytes_temp, num_bytes);
+ decoded_size += num_bytes;
+ break;
+ }
+ default:
+ return false;
+ }
+
+ *data_length = decoded_size;
+ return true;
+ }
+
+ /**
+ * Base64 decodes a string
+ * @param base64 the Base64 encoded string
+ * @param base64_length the length of base64
+ * @return the vector containing the decoded bytes
+ */
+ inline static std::vector<uint8_t> from_base64(const char* base64, size_t
base64_length) {
+ std::vector<uint8_t> decoded((base64_length / 4 + 1) * 3);
+ size_t data_length = decoded.size();
+ if (!from_base64(decoded.data(), &data_length, base64, base64_length)) {
+ throw std::runtime_error("Base64 encoded string is malformatted");
Review comment:
I always felt that runtime errors "are due to events beyond the scope of the
program and can not be easily predicted." so when those are used versus
internal or named exceptions they have different connotations. We catch them
but this error seems recoverable from a programmatic perspective. I don't have
a super strong opinion on this but it does have a bit of code smell worth
mentioning
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services