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-12-21 08:56:24 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        5416 : encode(std::string_view dat)
      29             : {
      30        5416 :     if (dat.empty() || dat.size() > INT_MAX)
      31           0 :         return {};
      32             : 
      33        5416 :     int input_length = (int)dat.size();
      34        5416 :     int output_length = PJ_BASE256_TO_BASE64_LEN(input_length);
      35        5416 :     std::string out;
      36        5416 :     out.resize(output_length);
      37             : 
      38        5416 :     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        5416 :     out.resize(output_length);
      43        5416 :     return out;
      44        5416 : }
      45             : 
      46             : std::vector<uint8_t>
      47       29801 : decode(std::string_view str)
      48             : {
      49       29801 :     if (str.empty())
      50           0 :         return {};
      51             : 
      52       29802 :     int output_length = PJ_BASE64_TO_BASE256_LEN(str.length());
      53       29801 :     auto input = sip_utils::CONST_PJ_STR(str);
      54             : 
      55       29794 :     std::vector<uint8_t> out;
      56       29798 :     out.resize(output_length);
      57             : 
      58       29789 :     if (pj_base64_decode(&input, &(*out.begin()), &output_length) != PJ_SUCCESS) {
      59           0 :         throw base64_exception();
      60             :     }
      61             : 
      62       29799 :     out.resize(output_length);
      63       29796 :     return out;
      64       29797 : }
      65             : 
      66             : } // namespace base64
      67             : } // namespace jami

Generated by: LCOV version 1.14