LCOV - code coverage report
Current view: top level - alma/ASDM - Base64.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 0 147 0.0 %
Date: 2024-10-12 00:35:29 Functions: 0 8 0.0 %

          Line data    Source code
       1             : /** \file Base64.cpp
       2             :  **     \date  2004-02-13
       3             :  **     \author grymse@alhem.net
       4             : **/
       5             : /*
       6             : Copyright (C) 2004,2005  Anders Hedstrom
       7             : 
       8             : This library is made available under the terms of the GNU GPL.
       9             : 
      10             : If you would like to use this library in a closed-source application,
      11             : a separate license agreement is available. For information about 
      12             : the closed-source license agreement for the C++ sockets library,
      13             : please visit http://www.alhem.net/Sockets/license.html and/or
      14             : email license@alhem.net.
      15             : 
      16             : This program is free software; you can redistribute it and/or
      17             : modify it under the terms of the GNU General Public License
      18             : as published by the Free Software Foundation; either version 2
      19             : of the License, or (at your option) any later version.
      20             : 
      21             : This program is distributed in the hope that it will be useful,
      22             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      23             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      24             : GNU General Public License for more details.
      25             : 
      26             : You should have received a copy of the GNU General Public License
      27             : along with this program; if not, write to the Free Software
      28             : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
      29             : */
      30             : #include <alma/ASDM/Base64.h>
      31             : 
      32             : #ifdef SOCKETS_NAMESPACE
      33             : namespace SOCKETS_NAMESPACE {
      34             : #endif
      35             : 
      36             : 
      37             : const char *Base64::bstr =
      38             :         "ABCDEFGHIJKLMNOPQ"
      39             :         "RSTUVWXYZabcdefgh"
      40             :         "ijklmnopqrstuvwxy"
      41             :         "z0123456789+/";
      42             : 
      43             : const char Base64::rstr[] = {
      44             :           0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
      45             :           0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
      46             :           0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  62,   0,   0,   0,  63, 
      47             :          52,  53,  54,  55,  56,  57,  58,  59,  60,  61,   0,   0,   0,   0,   0,   0, 
      48             :           0,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14, 
      49             :          15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,   0,   0,   0,   0,   0, 
      50             :           0,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40, 
      51             :          41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,   0,   0,   0,   0,   0};
      52             : 
      53             : 
      54           0 : Base64::Base64()
      55             : {
      56             : //      for (int i = 0; i < 64; i++)
      57             : //              rstr[(int)bstr[i]] = i;
      58           0 : }
      59             : 
      60             : 
      61           0 : void Base64::encode(FILE *fil, std::string& output, bool add_crlf)
      62             : {
      63             :         size_t remain;
      64           0 :         size_t i = 0;
      65           0 :         size_t o = 0;
      66             :         char input[4];
      67             : 
      68           0 :         output = "";
      69           0 :         remain = fread(input,1,3,fil);
      70           0 :         while (remain > 0)
      71             :         {
      72           0 :                 if (add_crlf && o && o % 76 == 0)
      73           0 :                         output += "\n";
      74           0 :                 switch (remain)
      75             :                 {
      76           0 :                 case 1:
      77           0 :                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
      78           0 :                         output += bstr[ ((input[i] << 4) & 0x30) ];
      79           0 :                         output += "==";
      80           0 :                         break;
      81           0 :                 case 2:
      82           0 :                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
      83           0 :                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
      84           0 :                         output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
      85           0 :                         output += "=";
      86           0 :                         break;
      87           0 :                 default:
      88           0 :                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
      89           0 :                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
      90           0 :                         output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
      91           0 :                         output += bstr[ (input[i + 2] & 0x3f) ];
      92             :                 }
      93           0 :                 o += 4;
      94             :                 //
      95           0 :                 remain = fread(input,1,3,fil);
      96             :         }
      97           0 : }
      98             : 
      99             : 
     100           0 : void Base64::encode(const std::string& str_in, std::string& str_out, bool add_crlf)
     101             : {
     102           0 :         encode(str_in.c_str(), str_in.size(), str_out, add_crlf);
     103           0 : }
     104             : 
     105             : 
     106           0 : void Base64::encode(const char* input,size_t l,std::string& output, bool add_crlf)
     107             : {
     108           0 :         size_t i = 0;
     109           0 :         size_t o = 0;
     110             :         
     111           0 :         output = "";
     112           0 :         while (i < l)
     113             :         {
     114           0 :                 size_t remain = l - i;
     115           0 :                 if (add_crlf && o && o % 76 == 0)
     116           0 :                         output += "\n";
     117           0 :                 switch (remain)
     118             :                 {
     119           0 :                 case 1:
     120           0 :                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
     121           0 :                         output += bstr[ ((input[i] << 4) & 0x30) ];
     122           0 :                         output += "==";
     123           0 :                         break;
     124           0 :                 case 2:
     125           0 :                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
     126           0 :                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
     127           0 :                         output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
     128           0 :                         output += "=";
     129           0 :                         break;
     130           0 :                 default:
     131           0 :                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
     132           0 :                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
     133           0 :                         output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
     134           0 :                         output += bstr[ (input[i + 2] & 0x3f) ];
     135             :                 }
     136           0 :                 o += 4;
     137           0 :                 i += 3;
     138             :         }
     139           0 : }
     140             : 
     141             : 
     142           0 : void Base64::encode(unsigned char* input,size_t l,std::string& output,bool add_crlf)
     143             : {
     144           0 :         size_t i = 0;
     145           0 :         size_t o = 0;
     146             :         
     147           0 :         output = "";
     148           0 :         while (i < l)
     149             :         {
     150           0 :                 size_t remain = l - i;
     151           0 :                 if (add_crlf && o && o % 76 == 0)
     152           0 :                         output += "\n";
     153           0 :                 switch (remain)
     154             :                 {
     155           0 :                 case 1:
     156           0 :                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
     157           0 :                         output += bstr[ ((input[i] << 4) & 0x30) ];
     158           0 :                         output += "==";
     159           0 :                         break;
     160           0 :                 case 2:
     161           0 :                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
     162           0 :                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
     163           0 :                         output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
     164           0 :                         output += "=";
     165           0 :                         break;
     166           0 :                 default:
     167           0 :                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
     168           0 :                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
     169           0 :                         output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
     170           0 :                         output += bstr[ (input[i + 2] & 0x3f) ];
     171             :                 }
     172           0 :                 o += 4;
     173           0 :                 i += 3;
     174             :         }
     175           0 : }
     176             : 
     177             : 
     178           0 : void Base64::decode(const std::string& input,std::string& output)
     179             : {
     180           0 :         size_t i = 0;
     181           0 :         size_t l = input.size();
     182             :         
     183           0 :         output = "";
     184           0 :         while (i < l)
     185             :         {
     186           0 :                 while (i < l && (input[i] == 13 || input[i] == 10))
     187           0 :                         i++;
     188           0 :                 if (i < l)
     189             :                 {
     190           0 :                         char b1 = (char)((rstr[(int)input[i]] << 2 & 0xfc) +
     191           0 :                                         (rstr[(int)input[i + 1]] >> 4 & 0x03));
     192           0 :                         output += b1;
     193           0 :                         if (input[i + 2] != '=')
     194             :                         {
     195           0 :                                 char b2 = (char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
     196           0 :                                                 (rstr[(int)input[i + 2]] >> 2 & 0x0f));
     197           0 :                                 output += b2;
     198             :                         }
     199           0 :                         if (input[i + 3] != '=')
     200             :                         {
     201           0 :                                 char b3 = (char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
     202           0 :                                                 rstr[(int)input[i + 3]]);
     203           0 :                                 output += b3;
     204             :                         }
     205           0 :                         i += 4;
     206             :                 }
     207             :         }
     208           0 : }
     209             : 
     210             : 
     211           0 : void Base64::decode(const std::string& input, unsigned char *output, size_t& sz)
     212             : {
     213           0 :         size_t i = 0;
     214           0 :         size_t l = input.size();
     215           0 :         size_t j = 0;
     216             :         
     217           0 :         while (i < l)
     218             :         {
     219           0 :                 while (i < l && (input[i] == 13 || input[i] == 10))
     220           0 :                         i++;
     221           0 :                 if (i < l)
     222             :                 {
     223           0 :                         unsigned char b1 = (unsigned char)((rstr[(int)input[i]] << 2 & 0xfc) +
     224           0 :                                         (rstr[(int)input[i + 1]] >> 4 & 0x03));
     225           0 :                         if (output)
     226             :                         {
     227           0 :                                 output[j] = b1;
     228             :                         }
     229           0 :                         j++;
     230           0 :                         if (input[i + 2] != '=')
     231             :                         {
     232           0 :                                 unsigned char b2 = (unsigned char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
     233           0 :                                                 (rstr[(int)input[i + 2]] >> 2 & 0x0f));
     234           0 :                                 if (output)
     235             :                                 {
     236           0 :                                         output[j] = b2;
     237             :                                 }
     238           0 :                                 j++;
     239             :                         }
     240           0 :                         if (input[i + 3] != '=')
     241             :                         {
     242           0 :                                 unsigned char b3 = (unsigned char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
     243           0 :                                                 rstr[(int)input[i + 3]]);
     244           0 :                                 if (output)
     245             :                                 {
     246           0 :                                         output[j] = b3;
     247             :                                 }
     248           0 :                                 j++;
     249             :                         }
     250           0 :                         i += 4;
     251             :                 }
     252             :         }
     253           0 :         sz = j;
     254           0 : }
     255             : 
     256             : 
     257           0 : size_t Base64::decode_length(const std::string& str64)
     258             : {
     259           0 :         if (!str64.size() || str64.size() % 4)
     260           0 :                 return 0;
     261           0 :         size_t l = 3 * (str64.size() / 4 - 1) + 1;
     262           0 :         if (str64[str64.size() - 2] != '=')
     263           0 :                 l++;
     264           0 :         if (str64[str64.size() - 1] != '=')
     265           0 :                 l++;
     266           0 :         return l;
     267             : }
     268             : 
     269             : 
     270             : #ifdef SOCKETS_NAMESPACE
     271             : }
     272             : #endif
     273             : 

Generated by: LCOV version 1.16