aboutsummaryrefslogtreecommitdiff
path: root/pw_tokenizer/public/pw_tokenizer/base64.h
diff options
context:
space:
mode:
Diffstat (limited to 'pw_tokenizer/public/pw_tokenizer/base64.h')
-rw-r--r--pw_tokenizer/public/pw_tokenizer/base64.h74
1 files changed, 61 insertions, 13 deletions
diff --git a/pw_tokenizer/public/pw_tokenizer/base64.h b/pw_tokenizer/public/pw_tokenizer/base64.h
index 735196a2a..36acaf20d 100644
--- a/pw_tokenizer/public/pw_tokenizer/base64.h
+++ b/pw_tokenizer/public/pw_tokenizer/base64.h
@@ -30,6 +30,7 @@
#include <stddef.h>
#include "pw_preprocessor/util.h"
+#include "pw_tokenizer/config.h"
// This character is used to mark the start of a Base64-encoded tokenized
// message. For consistency, it is recommended to always use $ if possible.
@@ -64,10 +65,10 @@ PW_EXTERN_C_END
#ifdef __cplusplus
-#include <span>
#include <string_view>
#include "pw_base64/base64.h"
+#include "pw_span/span.h"
#include "pw_tokenizer/config.h"
#include "pw_tokenizer/tokenize.h"
@@ -75,12 +76,19 @@ namespace pw::tokenizer {
inline constexpr char kBase64Prefix = PW_TOKENIZER_BASE64_PREFIX;
+#undef PW_TOKENIZER_BASE64_PREFIX // In C++, use the variable, not the macro.
+
// Returns the size of a tokenized message (token + arguments) when encoded as
-// prefixed Base64. This can be used to size a buffer for encoding. Includes
-// room for the prefix character ($), encoded message, and a null terminator.
+// prefixed Base64. Includes room for the prefix character ($) and encoded
+// message. This value is the capacity needed to encode to a pw::InlineString.
+constexpr size_t Base64EncodedStringSize(size_t message_size) {
+ return sizeof(kBase64Prefix) + base64::EncodedSize(message_size);
+}
+
+// Same as Base64EncodedStringSize(), but for sizing char buffers. Includes room
+// for the prefix character ($), encoded message, and a null terminator.
constexpr size_t Base64EncodedBufferSize(size_t message_size) {
- return sizeof(kBase64Prefix) + base64::EncodedSize(message_size) +
- sizeof('\0');
+ return Base64EncodedStringSize(message_size) + sizeof('\0');
}
// The minimum buffer size that can hold a tokenized message that is
@@ -92,25 +100,56 @@ inline constexpr size_t kDefaultBase64EncodedBufferSize =
// Returns the encoded string length (excluding the null terminator). Returns 0
// if the buffer is too small. Always null terminates if the output buffer is
// not empty.
-inline size_t PrefixedBase64Encode(std::span<const std::byte> binary_message,
- std::span<char> output_buffer) {
+inline size_t PrefixedBase64Encode(span<const std::byte> binary_message,
+ span<char> output_buffer) {
return pw_tokenizer_PrefixedBase64Encode(binary_message.data(),
binary_message.size(),
output_buffer.data(),
output_buffer.size());
}
-// Also accept a std::span<const uint8_t> for the binary message.
-inline size_t PrefixedBase64Encode(std::span<const uint8_t> binary_message,
- std::span<char> output_buffer) {
- return PrefixedBase64Encode(std::as_bytes(binary_message), output_buffer);
+// Also accept a span<const uint8_t> for the binary message.
+inline size_t PrefixedBase64Encode(span<const uint8_t> binary_message,
+ span<char> output_buffer) {
+ return PrefixedBase64Encode(as_bytes(binary_message), output_buffer);
+}
+
+// Encodes a binary tokenized message as prefixed Base64 to a pw::InlineString,
+// appending to any existing contents. Asserts if the message does not fit in
+// the string.
+void PrefixedBase64Encode(span<const std::byte> binary_message,
+ InlineString<>& output);
+
+inline void PrefixedBase64Encode(span<const uint8_t> binary_message,
+ InlineString<>& output) {
+ return PrefixedBase64Encode(as_bytes(binary_message), output);
+}
+
+// Encodes a binary tokenized message as prefixed Base64 to a pw::InlineString.
+// The pw::InlineString is sized to fit messages up to
+// kMaxBinaryMessageSizeBytes long. Asserts if the message is larger.
+template <size_t kMaxBinaryMessageSizeBytes =
+ PW_TOKENIZER_CFG_ENCODING_BUFFER_SIZE_BYTES>
+auto PrefixedBase64Encode(span<const std::byte> binary_message) {
+ static_assert(kMaxBinaryMessageSizeBytes >= 1, "Messages cannot be empty");
+ InlineString<Base64EncodedStringSize(kMaxBinaryMessageSizeBytes)> string(
+ 1, kBase64Prefix);
+ base64::Encode(binary_message, string);
+ return string;
+}
+
+template <size_t kMaxBinaryMessageSizeBytes =
+ PW_TOKENIZER_CFG_ENCODING_BUFFER_SIZE_BYTES>
+auto PrefixedBase64Encode(span<const uint8_t> binary_message) {
+ return PrefixedBase64Encode<kMaxBinaryMessageSizeBytes>(
+ as_bytes(binary_message));
}
// Decodes a prefixed Base64 tokenized message to binary. Returns the size of
// the decoded binary data. The resulting data is ready to be passed to
// pw::tokenizer::Detokenizer::Detokenize.
inline size_t PrefixedBase64Decode(std::string_view base64_message,
- std::span<std::byte> output_buffer) {
+ span<std::byte> output_buffer) {
return pw_tokenizer_PrefixedBase64Decode(base64_message.data(),
base64_message.size(),
output_buffer.data(),
@@ -119,11 +158,20 @@ inline size_t PrefixedBase64Decode(std::string_view base64_message,
// Decodes a prefixed Base64 tokenized message to binary in place. Returns the
// size of the decoded binary data.
-inline size_t PrefixedBase64DecodeInPlace(std::span<std::byte> buffer) {
+inline size_t PrefixedBase64DecodeInPlace(span<std::byte> buffer) {
return pw_tokenizer_PrefixedBase64Decode(
buffer.data(), buffer.size(), buffer.data(), buffer.size());
}
+// Decodes a prefixed Base64 tokenized message to binary in place. Resizes the
+// string to fit the decoded binary data.
+template <typename CharT>
+inline void PrefixedBase64DecodeInPlace(InlineBasicString<CharT>& string) {
+ static_assert(sizeof(CharT) == sizeof(char));
+ string.resize(pw_tokenizer_PrefixedBase64Decode(
+ string.data(), string.size(), string.data(), string.size()));
+}
+
} // namespace pw::tokenizer
#endif // __cplusplus