pw_tokenizer: Make Base64 encoding easier to use

- Have PW_TOKENIZER_CFG_ENCODING_BUFFER_SIZE represent the whole encoded
  buffer, including the 4-byte token. This makes the value easier to use
  directly since the token is already accounted for.
- Update the Base64 encoding functions to always add a null terminator
  to the Base64 output.
- Provide a pw::tokenizer::PrefixedBase64Encode overload that allocates
  the buffer using pw::Vector.

Change-Id: Id78ef06a7d2111e7dfe5604ee091975be40ceed4
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/19720
Reviewed-by: Ewout van Bekkum <ewout@google.com>
Commit-Queue: Wyatt Hepler <hepler@google.com>
diff --git a/pw_tokenizer/base64.cc b/pw_tokenizer/base64.cc
index 3404916..7aab4af 100644
--- a/pw_tokenizer/base64.cc
+++ b/pw_tokenizer/base64.cc
@@ -14,10 +14,6 @@
 
 #include "pw_tokenizer/base64.h"
 
-#include <span>
-
-#include "pw_base64/base64.h"
-
 namespace pw::tokenizer {
 
 extern "C" size_t pw_tokenizer_PrefixedBase64Encode(
@@ -25,19 +21,22 @@
     size_t binary_size_bytes,
     void* output_buffer,
     size_t output_buffer_size_bytes) {
-  const size_t encoded_size = base64::EncodedSize(binary_size_bytes) + 1;
+  char* output = static_cast<char*>(output_buffer);
+  const size_t encoded_size = Base64EncodedSize(binary_size_bytes);
 
-  if (output_buffer_size_bytes < encoded_size) {
+  if (output_buffer_size_bytes < encoded_size + sizeof('\0')) {
+    if (output_buffer_size_bytes > 0u) {
+      output[0] = '\0';
+    }
+
     return 0;
   }
 
-  char* output = static_cast<char*>(output_buffer);
   output[0] = kBase64Prefix;
-
   base64::Encode(std::span(static_cast<const std::byte*>(binary_message),
                            binary_size_bytes),
                  &output[1]);
-
+  output[encoded_size] = '\0';
   return encoded_size;
 }