C/C++ API Reference
Loading...
Searching...
No Matches
base64.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15// This file provides functions for working with the prefixed Base64 format for
16// tokenized messages. This format is useful for transmitting tokenized messages
17// as plain text.
18//
19// The format uses a prefix character ($), followed by the Base64 version of the
20// tokenized message. For example, consider a tokenized message with token
21// 0xfeb35a42 and encoded argument 0x13. This message would be encoded as
22// follows:
23//
24// Binary: 42 5a b3 fe 13 [5 bytes]
25//
26// Prefixed Base64: $Qlqz/hM= [9 bytes]
27//
28#pragma once
29
30#include <stddef.h>
31
32#include "pw_preprocessor/util.h"
33#include "pw_tokenizer/config.h"
34#include "pw_tokenizer/nested_tokenization.h"
35
36PW_EXTERN_C_START
37
39
46size_t pw_tokenizer_PrefixedBase64Encode(const void* binary_message,
47 size_t binary_size_bytes,
48 void* output_buffer,
49 size_t output_buffer_size_bytes);
50
58size_t pw_tokenizer_PrefixedBase64Decode(const void* base64_message,
59 size_t base64_size_bytes,
60 void* output_buffer,
61 size_t output_buffer_size);
62
64
65PW_EXTERN_C_END
66
67#ifdef __cplusplus
68
69#include <string_view>
70
71#include "pw_base64/base64.h"
72#include "pw_span/span.h"
73#include "pw_tokenizer/config.h"
74#include "pw_tokenizer/tokenize.h"
75
76namespace pw::tokenizer {
77
79
83constexpr size_t Base64EncodedStringSize(size_t message_size) {
84 return sizeof(PW_TOKENIZER_NESTED_PREFIX) + base64::EncodedSize(message_size);
85}
86
89constexpr size_t Base64EncodedBufferSize(size_t message_size) {
90 return Base64EncodedStringSize(message_size) + sizeof('\0');
91}
92
97inline size_t PrefixedBase64Encode(span<const std::byte> binary_message,
98 span<char> output_buffer) {
99 return pw_tokenizer_PrefixedBase64Encode(binary_message.data(),
100 binary_message.size(),
101 output_buffer.data(),
102 output_buffer.size());
103}
104
106inline size_t PrefixedBase64Encode(span<const uint8_t> binary_message,
107 span<char> output_buffer) {
108 return PrefixedBase64Encode(as_bytes(binary_message), output_buffer);
109}
110
115 InlineString<>& output);
116
117inline void PrefixedBase64Encode(span<const uint8_t> binary_message,
118 InlineString<>& output) {
119 return PrefixedBase64Encode(as_bytes(binary_message), output);
120}
121
125template <size_t kMaxBinaryMessageSizeBytes>
127 static_assert(kMaxBinaryMessageSizeBytes >= 1, "Messages cannot be empty");
128 InlineString<Base64EncodedStringSize(kMaxBinaryMessageSizeBytes)> string(
129 1, PW_TOKENIZER_NESTED_PREFIX);
130 base64::Encode(binary_message, string);
131 return string;
132}
133
134template <size_t kMaxBinaryMessageSizeBytes>
135auto PrefixedBase64Encode(span<const uint8_t> binary_message) {
136 return PrefixedBase64Encode<kMaxBinaryMessageSizeBytes>(
137 as_bytes(binary_message));
138}
139
143inline size_t PrefixedBase64Decode(std::string_view base64_message,
144 span<std::byte> output_buffer) {
145 return pw_tokenizer_PrefixedBase64Decode(base64_message.data(),
146 base64_message.size(),
147 output_buffer.data(),
148 output_buffer.size());
149}
150
155 buffer.data(), buffer.size(), buffer.data(), buffer.size());
156}
157
158inline size_t PrefixedBase64DecodeInPlace(span<char> buffer) {
159 return PrefixedBase64DecodeInPlace(as_writable_bytes(buffer));
160}
161
164template <typename CharT>
166 static_assert(sizeof(CharT) == sizeof(char));
168 string.data(), string.size(), string.data(), string.size()));
169}
170
172
173} // namespace pw::tokenizer
174
175#endif // __cplusplus
pw::InlineBasicString is a fixed-capacity version of std::basic_string. In brief:
Definition: string.h:68
Definition: span_impl.h:235
constexpr size_t EncodedSize(size_t binary_size_bytes)
Definition: base64.h:89
void Encode(span< const std::byte > binary, char *output)
Definition: base64.h:109
constexpr size_t Base64EncodedStringSize(size_t message_size)
Definition: base64.h:83
constexpr size_t Base64EncodedBufferSize(size_t message_size)
Definition: base64.h:89
size_t pw_tokenizer_PrefixedBase64Encode(const void *binary_message, size_t binary_size_bytes, void *output_buffer, size_t output_buffer_size_bytes)
size_t PrefixedBase64Decode(std::string_view base64_message, span< std::byte > output_buffer)
Definition: base64.h:143
size_t pw_tokenizer_PrefixedBase64Decode(const void *base64_message, size_t base64_size_bytes, void *output_buffer, size_t output_buffer_size)
size_t PrefixedBase64DecodeInPlace(span< std::byte > buffer)
Definition: base64.h:153
size_t PrefixedBase64Encode(span< const std::byte > binary_message, span< char > output_buffer)
Definition: base64.h:97