sdi_toolBox
base64.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2026 - SD-Innovation S.A.S. - FRANCE
3*/
4
5/*
6ver: 2.x.x - build: 2026-04-28
7*/
8
9/*
10The zlib License Copyright (c) 2026 SD-Innovation S.A.S. This software is provided ‘as-is’, without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
11*/
12
13#pragma once
14
15#include <optional>
16#include <span>
17#include <string>
18#include <vector>
19
21{
22//--------------------------------------------------------------
52class Base64
53{
54 public:
58 Base64() = delete;
59
62
69 [[nodiscard]] static std::string encode(std::span<const std::uint8_t> data);
70
77 [[nodiscard]] static std::string encode(std::string_view text);
78
82
91 [[nodiscard]] static std::optional<std::vector<std::uint8_t>> decode(std::string_view input);
92
101 [[nodiscard]] static std::optional<std::string> decode_to_string(std::string_view input);
102
104
105 private:
108
116 [[nodiscard]] static std::optional<std::uint8_t> decode_char(char c) noexcept;
117
121
123 static constexpr std::string_view ENCODE_LOOKUP = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
124
126 static constexpr char PADDING_CAR = '=';
127
129};
130//--------------------------------------------------------------
131
132//--------------------------------------------------------------
133/* Encode binary data to Base64 string */
134inline std::string Base64::encode(const std::span<const std::uint8_t> data)
135{
136 std::string result;
137 result.reserve(((data.size() + 2) / 3) * 4);
138
139 for (std::size_t i = 0; i < data.size(); i += 3)
140 {
141 const std::uint32_t b0 = data[i];
142 const std::uint32_t b1 = (i + 1 < data.size()) ? data[i + 1] : 0u;
143 const std::uint32_t b2 = (i + 2 < data.size()) ? data[i + 2] : 0u;
144
145 const std::uint32_t triple = (b0 << 16) | (b1 << 8) | b2;
146
147 result += ENCODE_LOOKUP[(triple >> 18) & 0x3F];
148 result += ENCODE_LOOKUP[(triple >> 12) & 0x3F];
149 result += (i + 1 < data.size()) ? ENCODE_LOOKUP[(triple >> 6) & 0x3F] : PADDING_CAR;
150 result += (i + 2 < data.size()) ? ENCODE_LOOKUP[(triple >> 0) & 0x3F] : PADDING_CAR;
151 }
152
153 return result;
154}
155//--------------------------------------------------------------
156/* Encode a text string to Base64 string */
157inline std::string Base64::encode(const std::string_view text)
158{
159 return encode(std::span(reinterpret_cast<const std::uint8_t *>(text.data()), text.size()));
160}
161//--------------------------------------------------------------
162/* Decode a Base64 string to binary data (returns std::nullopt if invalid) */
163inline std::optional<std::vector<std::uint8_t>> Base64::decode(const std::string_view input)
164{
165 if (input.size() % 4 != 0)
166 return std::nullopt;
167
168 if (input.empty())
169 return std::vector<std::uint8_t>{};
170
171 // Validate padding: '=' is only allowed in the last group, in position 2 or 3
172 // Valid forms: "xxx=" or "xx=="
173 for (std::size_t i = 0; i < input.size() - 4; ++i)
174 {
175 if (input[i] == PADDING_CAR)
176 return std::nullopt; // '=' found outside the last group
177 }
178
179 const std::string_view last = input.substr(input.size() - 4);
180 // "xx==" : positions 0,1 must be valid chars, positions 2,3 must be '='
181 // "xxx=" : positions 0,1,2 must be valid chars, position 3 must be '='
182 // "xxxx" : all positions must be valid chars
183 if (last[2] == PADDING_CAR && last[3] != PADDING_CAR)
184 return std::nullopt; // "xx=x" is invalid
185
186 std::vector<std::uint8_t> result;
187 result.reserve((input.size() / 4) * 3);
188
189 for (std::size_t i = 0; i < input.size(); i += 4)
190 {
191 const auto v0 = decode_char(input[i]);
192 const auto v1 = decode_char(input[i + 1]);
193 const auto v2 = input[i + 2] == PADDING_CAR ? std::optional<std::uint8_t>{ 0 } : decode_char(input[i + 2]);
194 const auto v3 = input[i + 3] == PADDING_CAR ? std::optional<std::uint8_t>{ 0 } : decode_char(input[i + 3]);
195
196 if (!v0 || !v1 || !v2 || !v3)
197 return std::nullopt;
198
199 const std::uint32_t triple =
200 (static_cast<std::uint32_t>(*v0) << 18) |
201 (static_cast<std::uint32_t>(*v1) << 12) |
202 (static_cast<std::uint32_t>(*v2) << 6) |
203 (static_cast<std::uint32_t>(*v3));
204
205 result.push_back(static_cast<std::uint8_t>((triple >> 16) & 0xFF));
206 if (input[i + 2] != PADDING_CAR)
207 result.push_back(static_cast<std::uint8_t>((triple >> 8) & 0xFF));
208 if (input[i + 3] != PADDING_CAR)
209 result.push_back(static_cast<std::uint8_t>((triple >> 0) & 0xFF));
210 }
211
212 return result;
213}
214//--------------------------------------------------------------
215/* Decode a Base64 string to a text string (returns std::nullopt if invalid) */
216inline std::optional<std::string> Base64::decode_to_string(const std::string_view input)
217{
218 const auto bytes = decode(input);
219 if (!bytes)
220 return std::nullopt;
221 return std::string(reinterpret_cast<const char *>(bytes->data()), bytes->size());
222}
223//--------------------------------------------------------------
224/* Helper to decode a single Base64 character to its 6-bit value (returns std::nullopt if invalid) */
225inline std::optional<std::uint8_t> Base64::decode_char(const char c) noexcept
226{
227 if (c >= 'A' && c <= 'Z')
228 return static_cast<std::uint8_t>(c - 'A');
229 if (c >= 'a' && c <= 'z')
230 return static_cast<std::uint8_t>(c - 'a' + 26);
231 if (c >= '0' && c <= '9')
232 return static_cast<std::uint8_t>(c - '0' + 52);
233 if (c == '+')
234 return 62;
235 if (c == '/')
236 return 63;
237 return std::nullopt;
238}
239//--------------------------------------------------------------
240} // namespace sdi_toolBox::desktop::utils
Utility class providing Base64 encoding and decoding functionality.
Definition base64.h:53
Base64()=delete
Deleted default constructor - this class is not meant to be instantiated.
static std::optional< std::vector< std::uint8_t > > decode(std::string_view input)
Decodes a Base64 string into raw binary data.
Definition base64.h:163
static std::optional< std::string > decode_to_string(std::string_view input)
Decodes a Base64 string into a text string.
Definition base64.h:216
static std::string encode(std::span< const std::uint8_t > data)
Encodes binary data into a Base64 string.
Definition base64.h:134
General-purpose utility functions and helpers for desktop applications.
Definition base64.h:21