[wpiutil] Base64: Add unsigned span/vector variants (#3702)

This commit is contained in:
Peter Johnson
2021-11-01 07:48:26 -07:00
committed by GitHub
parent 52f2d580eb
commit e473a00f97
2 changed files with 49 additions and 0 deletions

View File

@@ -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<uint8_t>* plain) {
plain->resize(0);
raw_uvector_ostream os(*plain);
return Base64Decode(os, encoded);
}
span<uint8_t> Base64Decode(std::string_view encoded, size_t* num_read,
SmallVectorImpl<uint8_t>& 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<const uint8_t> plain) {
Base64Encode(os, std::string_view{reinterpret_cast<const char*>(plain.data()),
plain.size()});
}
void Base64Encode(span<const uint8_t> plain, std::string* encoded) {
encoded->resize(0);
raw_string_ostream os(*encoded);
Base64Encode(os, plain);
os.flush();
}
std::string_view Base64Encode(span<const uint8_t> plain,
SmallVectorImpl<char>& buf) {
buf.clear();
raw_svector_ostream os(buf);
Base64Encode(os, plain);
return os.str();
}
} // namespace wpi