Line data Source code
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"
19 : #include "connectivity/sip_utils.h"
20 :
21 : #include <simdutf.h>
22 :
23 : namespace jami {
24 : namespace base64 {
25 :
26 : std::string
27 5391 : encode(std::string_view str)
28 : {
29 5391 : std::string buffer(simdutf::base64_length_from_binary(str.size()), '\0');
30 5391 : simdutf::binary_to_base64((const char*) str.data(), str.size(), buffer.data());
31 5391 : return buffer;
32 : }
33 :
34 : std::vector<unsigned char>
35 15429 : decode(std::string_view str)
36 : {
37 15429 : std::vector<unsigned char> buffer(simdutf::maximal_binary_length_from_base64(str.data(), str.size()));
38 15424 : const simdutf::result r = simdutf::base64_to_binary(str.data(), str.size(), (char*) buffer.data());
39 15429 : if (r.error)
40 1 : throw base64_exception();
41 15428 : buffer.resize(r.count); // resize the buffer according to actual number of bytes
42 30852 : return buffer;
43 1 : }
44 :
45 : } // namespace base64
46 : } // namespace jami
|