diff --git a/wpiutil/src/main/native/cpp/Base64.cpp b/wpiutil/src/main/native/cpp/Base64.cpp index 08c9fce9ce..8f1f81020b 100644 --- a/wpiutil/src/main/native/cpp/Base64.cpp +++ b/wpiutil/src/main/native/cpp/Base64.cpp @@ -132,6 +132,20 @@ std::string_view Base64Decode(std::string_view encoded, size_t* num_read, return os.str(); } +size_t Base64Decode(std::string_view encoded, std::vector* plain) { + plain->resize(0); + raw_uvector_ostream os(*plain); + return Base64Decode(os, encoded); +} + +span Base64Decode(std::string_view encoded, size_t* num_read, + SmallVectorImpl& buf) { + buf.clear(); + raw_usvector_ostream os(buf); + *num_read = Base64Decode(os, encoded); + return os.array(); +} + static const char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -179,4 +193,24 @@ std::string_view Base64Encode(std::string_view plain, return os.str(); } +void Base64Encode(raw_ostream& os, span plain) { + Base64Encode(os, std::string_view{reinterpret_cast(plain.data()), + plain.size()}); +} + +void Base64Encode(span plain, std::string* encoded) { + encoded->resize(0); + raw_string_ostream os(*encoded); + Base64Encode(os, plain); + os.flush(); +} + +std::string_view Base64Encode(span plain, + SmallVectorImpl& buf) { + buf.clear(); + raw_svector_ostream os(buf); + Base64Encode(os, plain); + return os.str(); +} + } // namespace wpi diff --git a/wpiutil/src/main/native/include/wpi/Base64.h b/wpiutil/src/main/native/include/wpi/Base64.h index bea6e18fb4..41b0062408 100644 --- a/wpiutil/src/main/native/include/wpi/Base64.h +++ b/wpiutil/src/main/native/include/wpi/Base64.h @@ -8,6 +8,9 @@ #include #include #include +#include + +#include "wpi/span.h" namespace wpi { template @@ -21,6 +24,11 @@ size_t Base64Decode(std::string_view encoded, std::string* plain); std::string_view Base64Decode(std::string_view encoded, size_t* num_read, SmallVectorImpl& buf); +size_t Base64Decode(std::string_view encoded, std::vector* plain); + +span Base64Decode(std::string_view encoded, size_t* num_read, + SmallVectorImpl& buf); + void Base64Encode(raw_ostream& os, std::string_view plain); void Base64Encode(std::string_view plain, std::string* encoded); @@ -28,6 +36,13 @@ void Base64Encode(std::string_view plain, std::string* encoded); std::string_view Base64Encode(std::string_view plain, SmallVectorImpl& buf); +void Base64Encode(raw_ostream& os, span plain); + +void Base64Encode(span plain, std::string* encoded); + +std::string_view Base64Encode(span plain, + SmallVectorImpl& buf); + } // namespace wpi #endif // WPIUTIL_WPI_BASE64_H_