Ring Daemon 16.0.0
Loading...
Searching...
No Matches
base64.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 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 <pjlib.h>
22#include <pjlib-util/base64.h>
23
24namespace jami {
25namespace base64 {
26
27std::string
28encode(std::string_view dat)
29{
30 if (dat.empty() || dat.size() > INT_MAX)
31 return {};
32
33 int input_length = (int)dat.size();
35 std::string out;
36 out.resize(output_length);
37
38 if (pj_base64_encode((const uint8_t*)dat.data(), input_length, &(*out.begin()), &output_length) != PJ_SUCCESS) {
39 throw base64_exception();
40 }
41
42 out.resize(output_length);
43 return out;
44}
45
46std::vector<uint8_t>
47decode(std::string_view str)
48{
49 if (str.empty())
50 return {};
51
53 auto input = sip_utils::CONST_PJ_STR(str);
54
55 std::vector<uint8_t> out;
56 out.resize(output_length);
57
58 if (pj_base64_decode(&input, &(*out.begin()), &output_length) != PJ_SUCCESS) {
59 throw base64_exception();
60 }
61
62 out.resize(output_length);
63 return out;
64}
65
66} // namespace base64
67} // namespace jami
std::vector< uint8_t > decode(std::string_view str)
Definition base64.cpp:47
std::string encode(std::string_view dat)
Definition base64.cpp:28
constexpr const pj_str_t CONST_PJ_STR(T(&a)[N]) noexcept
Definition sip_utils.h:89
void emitSignal(Args... args)
Definition ring_signal.h:64