LCOV - code coverage report
Current view: top level - src - base64.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 37 46 80.4 %
Date: 2022-12-03 01:09:05 Functions: 3 5 60.0 %

          Line data    Source code
       1             : /*
       2             :    Bacula® - The Network Backup Solution
       3             : 
       4             :    Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
       5             : 
       6             :    The main author of Bacula is Kern Sibbald, with contributions from
       7             :    many others, a complete list can be found in the file AUTHORS.
       8             :    This program is Free Software; you can redistribute it and/or
       9             :    modify it under the terms of version three of the GNU Affero General Public
      10             :    License as published by the Free Software Foundation and included
      11             :    in the file LICENSE.
      12             : 
      13             :    This program is distributed in the hope that it will be useful, but
      14             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      16             :    General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU Affero General Public License
      19             :    along with this program; if not, write to the Free Software
      20             :    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
      21             :    02110-1301, USA.
      22             : 
      23             :    Bacula® is a registered trademark of Kern Sibbald.
      24             :    The licensor of Bacula is the Free Software Foundation Europe
      25             :    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
      26             :    Switzerland, email:ftf@fsfeurope.org.
      27             : */
      28             : /*
      29             :  *   Generic base 64 input and output routines
      30             :  *
      31             :  *    Written by Kern E. Sibbald, March MM.
      32             :  */
      33             : /*
      34             :  * Originally from bacula-5.0.3:src/lib/base64.c, with minor formatting
      35             :  * changes.
      36             :  *    Graham Keeling, 2014.
      37             :  */
      38             : 
      39             : #include "burp.h"
      40             : #include "base64.h"
      41             : 
      42             : static uint8_t const base64_digits[64]=
      43             : {
      44             :         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
      45             :         'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
      46             :         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
      47             :         'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
      48             :         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
      49             : };
      50             : 
      51             : static uint8_t base64_map[128];
      52             : 
      53             : /* Initialize the Base 64 conversion routines */
      54         145 : void base64_init(void)
      55             : {
      56             :         int i;
      57         145 :         memset(base64_map, 0, sizeof(base64_map));
      58        9425 :         for(i=0; i<64; i++)
      59        9280 :                 base64_map[(uint8_t)base64_digits[i]]=i;
      60         145 : }
      61             : 
      62             : /*
      63             :  * Convert a value to base64 characters. The result is stored in where, which
      64             :  * must be at least 13 bytes long.
      65             :  *
      66             :  * Returns the number of characters stored (not including the EOS).
      67             :  */
      68      417165 : int to_base64(int64_t value, char *where)
      69             : {
      70             :         uint64_t val;
      71      417165 :         int i=0;
      72             :         int n;
      73             : 
      74             :         /* Handle negative values */
      75      417165 :         if(value<0)
      76             :         {
      77       72071 :                 where[i++]='-';
      78       72071 :                 value=-value;
      79             :         }
      80             : 
      81             :         /* Determine output size */
      82      417165 :         val=value;
      83             :         do
      84             :         {
      85     1980779 :                 val>>=6;
      86     1980779 :                 i++;
      87     1980779 :         } while(val);
      88      417165 :         n=i;
      89             : 
      90             :         /* Output characters */
      91      417165 :         val=value;
      92      417165 :         where[i]=0;
      93             :         do
      94             :         {
      95     1980779 :                 where[--i]=base64_digits[val & (uint64_t)0x3F];
      96     1980779 :                 val>>=6;
      97     1980779 :         } while(val);
      98      417165 :         return n;
      99             : }
     100             : 
     101             : /*
     102             :  * Convert the Base 64 characters in where to a value.
     103             :  *
     104             :  * Returns the number of characters converted.
     105             :  */
     106      449899 : int from_base64(int64_t *value, const char *where)
     107             : {
     108      449899 :         uint64_t val=0;
     109      449899 :         int i=0;
     110      449899 :         int neg=0;
     111             : 
     112      449899 :         if(where[i]==' ')
     113      426178 :                 i++;
     114             : 
     115             :         /* Check if it is negative */
     116      449899 :         if(where[i]=='-')
     117             :         {
     118       73804 :                 i++;
     119       73804 :                 neg=1;
     120             :         }
     121             :         /* Construct value */
     122     2505925 :         for(char c=where[i]; c && c!=' '; c=where[++i])
     123             :         {
     124     2056026 :                 if(!isalnum((unsigned char)c) && c!='+' && c!='/')
     125           6 :                         continue;
     126     2056020 :                 val<<=6;
     127     2056020 :                 val+=base64_map[(uint8_t)c];
     128             :         }
     129             : 
     130      449899 :         *value=neg?-(int64_t)val:(int64_t)val;
     131      449899 :         return i;
     132             : }
     133             : 
     134           0 : uint64_t base64_to_uint64(const char *buf)
     135             : {
     136           0 :         int64_t val=0;
     137           0 :         from_base64(&val, buf);
     138           0 :         return (uint64_t)val;
     139             : }
     140             : 
     141           0 : void base64_from_uint64(uint64_t src, char *buf)
     142             : {
     143           0 :         char *p=buf;
     144           0 :         p+=to_base64(src, p);
     145           0 :         *p=0;
     146           0 : }

Generated by: LCOV version 1.13