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// Functions for encoding and decoding data in Base64 as specified by RFC 3548
16// and RFC 4648. See https://tools.ietf.org/html/rfc4648
17#pragma once
18
19#include <stdbool.h>
20#include <stddef.h>
21
22// C-compatible versions of a subset of the pw_base64 module.
23#ifdef __cplusplus
24extern "C" {
25#endif // __cplusplus
26
27// Returns the size of the given number of bytes when encoded as Base64. Base64
28//
29// Equivalent to pw::base64::EncodedSize().
30#define PW_BASE64_ENCODED_SIZE(binary_size_bytes) \
31 (((size_t)binary_size_bytes + 2) / 3 * 4) // +2 to round up to a 3-byte group
32
33// Encodes the provided data in Base64 and writes the result to the buffer.
34// Exactly PW_BASE64_ENCODED_SIZE(binary_size_bytes) bytes will be written. The
35// output buffer *MUST* be large enough for the encoded output!
36//
37// Equivalent to pw::base64::Encode().
38void pw_Base64Encode(const void* binary_data,
39 const size_t binary_size_bytes,
40 char* output);
41
42// Evaluates to the maximum size of decoded Base64 data in bytes.
43//
44// Returns 0 if the size is not a multiple of 4.
45//
46// Equivalent to pw::base64::MaxDecodedSize().
47#define PW_BASE64_MAX_DECODED_SIZE(base64_size_bytes) \
48 (((base64_size_bytes % 4) == 0) ? ((size_t)base64_size_bytes) / 4 * 3 : 0)
49
50// Decodes the provided Base64 data into raw binary. The output buffer *MUST* be
51// at least PW_BASE64_MAX_DECODED_SIZE bytes large.
52//
53// Equivalent to pw::base64::Decode().
54size_t pw_Base64Decode(const char* base64,
55 size_t base64_size_bytes,
56 void* output);
57
58// Returns true if provided char is a valid non-padding Base64 character.
59bool pw_Base64IsValidChar(char base64_char);
60
61// Returns true if the provided string is valid Base64 encoded data. Accepts
62// either the standard (+/) or URL-safe (-_) alphabets.
63//
64// Equivalent to pw::base64::IsValid().
65bool pw_Base64IsValid(const char* base64_data, size_t base64_size);
66
67// C++ API, which uses the C functions internally.
68#ifdef __cplusplus
69} // extern "C"
70
71#include <string_view>
72#include <type_traits>
73
74#include "pw_span/span.h"
75#include "pw_string/string.h"
76
78namespace pw::base64 {
79
81
89constexpr size_t EncodedSize(size_t binary_size_bytes) {
90 return PW_BASE64_ENCODED_SIZE(binary_size_bytes);
91}
92
109inline void Encode(span<const std::byte> binary, char* output) {
110 pw_Base64Encode(binary.data(), binary.size_bytes(), output);
111}
112
125size_t Encode(span<const std::byte> binary, span<char> output_buffer);
126
135
139template <size_t kMaxBinaryDataSizeBytes>
140inline InlineString<EncodedSize(kMaxBinaryDataSizeBytes)> Encode(
141 span<const std::byte> binary) {
142 InlineString<EncodedSize(kMaxBinaryDataSizeBytes)> output;
143 Encode(binary, output);
144 return output;
145}
146
156constexpr size_t MaxDecodedSize(size_t base64_size_bytes) {
157 return PW_BASE64_MAX_DECODED_SIZE(base64_size_bytes);
158}
159
167constexpr size_t DecodedSize(std::string_view valid_base64) {
168 if ((valid_base64.size() % 4) != 0 || valid_base64.empty()) {
169 return 0;
170 }
171 const size_t max_bytes = valid_base64.size() / 4 * 3;
172 size_t padding = 0;
173 if (valid_base64[valid_base64.size() - 2] == '=') {
174 padding = 2;
175 } else if (valid_base64[valid_base64.size() - 1] == '=') {
176 padding = 1;
177 }
178 return max_bytes - padding;
179}
180
197inline size_t Decode(std::string_view base64, void* output) {
198 return pw_Base64Decode(base64.data(), base64.size(), output);
199}
200
206size_t Decode(std::string_view base64, span<std::byte> output_buffer);
207
209template <typename T>
211 static_assert(sizeof(T) == sizeof(char));
212 buffer.resize(Decode(buffer, buffer.data()));
213}
214
219inline bool IsValid(std::string_view base64) {
220 return pw_Base64IsValid(base64.data(), base64.size());
221}
222
227inline bool IsValidChar(char base64) { return pw_Base64IsValidChar(base64); }
228
229} // namespace pw::base64
230
231#endif // __cplusplus
pw::InlineBasicString is a fixed-capacity version of std::basic_string. In brief:
Definition: string.h:68
Definition: span_impl.h:235
size_t Decode(std::string_view base64, void *output)
Definition: base64.h:197
bool IsValid(std::string_view base64)
Definition: base64.h:219
constexpr size_t DecodedSize(std::string_view valid_base64)
Definition: base64.h:167
bool IsValidChar(char base64)
Definition: base64.h:227
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 MaxDecodedSize(size_t base64_size_bytes)
Definition: base64.h:156
void DecodeInPlace(InlineBasicString< T > &buffer)
Decodes a pw::InlineString<> in place.
Definition: base64.h:210
Base64 encoding, decoding, and validating library.
Definition: base64.h:78
pw::InlineBasicString and pw::InlineString are safer alternatives to std::basic_string and std::strin...