Ring Daemon
Loading...
Searching...
No Matches
base64.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "base64.h"
20
21#include <simdutf.h>
22
23namespace jami {
24namespace base64 {
25
26std::string
27encode(std::string_view str)
28{
29 std::string buffer(simdutf::base64_length_from_binary(str.size()), '\0');
30 simdutf::binary_to_base64((const char*) str.data(), str.size(), buffer.data());
31 return buffer;
32}
33
34std::vector<unsigned char>
35decode(std::string_view str)
36{
37 std::vector<unsigned char> buffer(simdutf::maximal_binary_length_from_base64(str.data(), str.size()));
38 const simdutf::result r = simdutf::base64_to_binary(str.data(), str.size(), (char*) buffer.data());
39 if (r.error)
40 throw base64_exception();
41 buffer.resize(r.count); // resize the buffer according to actual number of bytes
42 return buffer;
43}
44
45} // namespace base64
46} // namespace jami
std::string encode(std::string_view str)
Definition base64.cpp:27
std::vector< unsigned char > decode(std::string_view str)
Definition base64.cpp:35
void emitSignal(Args... args)
Definition jami_signal.h:64