LCOV - code coverage report
Current view: top level - src - base64.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 20 24 83.3 %
Date: 2024-11-15 09:04:49 Functions: 2 2 100.0 %

          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, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "base64.h"
      19             : #include "connectivity/sip_utils.h"
      20             : 
      21             : #include <pjlib.h>
      22             : #include <pjlib-util/base64.h>
      23             : 
      24             : namespace jami {
      25             : namespace base64 {
      26             : 
      27             : std::string
      28        5413 : encode(std::string_view dat)
      29             : {
      30        5413 :     if (dat.empty() || dat.size() > INT_MAX)
      31           0 :         return {};
      32             : 
      33        5413 :     int input_length = (int)dat.size();
      34        5413 :     int output_length = PJ_BASE256_TO_BASE64_LEN(input_length);
      35        5413 :     std::string out;
      36        5413 :     out.resize(output_length);
      37             : 
      38        5413 :     if (pj_base64_encode((const uint8_t*)dat.data(), input_length, &(*out.begin()), &output_length) != PJ_SUCCESS) {
      39           0 :         throw base64_exception();
      40             :     }
      41             : 
      42        5413 :     out.resize(output_length);
      43        5413 :     return out;
      44        5413 : }
      45             : 
      46             : std::vector<uint8_t>
      47       30587 : decode(std::string_view str)
      48             : {
      49       30587 :     if (str.empty())
      50           0 :         return {};
      51             : 
      52       30586 :     int output_length = PJ_BASE64_TO_BASE256_LEN(str.length());
      53       30587 :     auto input = sip_utils::CONST_PJ_STR(str);
      54             : 
      55       30584 :     std::vector<uint8_t> out;
      56       30586 :     out.resize(output_length);
      57             : 
      58       30584 :     if (pj_base64_decode(&input, &(*out.begin()), &output_length) != PJ_SUCCESS) {
      59           0 :         throw base64_exception();
      60             :     }
      61             : 
      62       30586 :     out.resize(output_length);
      63       30581 :     return out;
      64       30583 : }
      65             : 
      66             : } // namespace base64
      67             : } // namespace jami

Generated by: LCOV version 1.14