Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 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, write to the Free Software 16 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 : */ 18 : 19 : #include "base64.h" 20 : #include "connectivity/sip_utils.h" 21 : 22 : #include <pjlib.h> 23 : #include <pjlib-util/base64.h> 24 : 25 : namespace jami { 26 : namespace base64 { 27 : 28 : std::string 29 5364 : encode(std::string_view dat) 30 : { 31 5364 : if (dat.empty() || dat.size() > INT_MAX) 32 0 : return {}; 33 : 34 5364 : int input_length = (int)dat.size(); 35 5364 : int output_length = PJ_BASE256_TO_BASE64_LEN(input_length); 36 5364 : std::string out; 37 5364 : out.resize(output_length); 38 : 39 5364 : if (pj_base64_encode((const uint8_t*)dat.data(), input_length, &(*out.begin()), &output_length) != PJ_SUCCESS) { 40 0 : throw base64_exception(); 41 : } 42 : 43 5364 : out.resize(output_length); 44 5364 : return out; 45 5364 : } 46 : 47 : std::vector<uint8_t> 48 45583 : decode(std::string_view str) 49 : { 50 45583 : if (str.empty()) 51 0 : return {}; 52 : 53 45577 : int output_length = PJ_BASE64_TO_BASE256_LEN(str.length()); 54 45577 : auto input = sip_utils::CONST_PJ_STR(str); 55 : 56 45584 : std::vector<uint8_t> out; 57 45584 : out.resize(output_length); 58 : 59 45578 : if (pj_base64_decode(&input, &(*out.begin()), &output_length) != PJ_SUCCESS) { 60 0 : throw base64_exception(); 61 : } 62 : 63 45578 : out.resize(output_length); 64 45573 : return out; 65 45576 : } 66 : 67 : } // namespace base64 68 : } // namespace jami