LCOV - code coverage report
Current view: top level - lib/util - base64.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 55 65 84.6 %
Date: 2021-09-23 10:06:22 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Samba utility functions
       4             : 
       5             :    Copyright (C) Andrew Tridgell 1992-2001
       6             :    Copyright (C) Simo Sorce      2001-2002
       7             :    Copyright (C) Martin Pool     2003
       8             :    Copyright (C) James Peach     2006
       9             :    Copyright (C) Jeremy Allison  1992-2007
      10             : 
      11             :    This program is free software; you can redistribute it and/or modify
      12             :    it under the terms of the GNU General Public License as published by
      13             :    the Free Software Foundation; either version 3 of the License, or
      14             :    (at your option) any later version.
      15             : 
      16             :    This program is distributed in the hope that it will be useful,
      17             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :    GNU General Public License for more details.
      20             : 
      21             :    You should have received a copy of the GNU General Public License
      22             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      23             : */
      24             : 
      25             : #include "replace.h"
      26             : #include "lib/util/base64.h"
      27             : 
      28             : static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      29             : 
      30             : /**
      31             :  * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
      32             :  **/
      33       24185 : _PUBLIC_ DATA_BLOB base64_decode_data_blob_talloc(TALLOC_CTX *mem_ctx, const char *s)
      34             : {
      35             :         int bit_offset, byte_offset, idx, i, n;
      36       24185 :         DATA_BLOB decoded = data_blob_talloc(mem_ctx, s, strlen(s)+1);
      37       24185 :         unsigned char *d = decoded.data;
      38             :         char *p;
      39             : 
      40       24185 :         n=i=0;
      41             : 
      42    39444419 :         while (*s && (p=strchr(b64,*s))) {
      43    39400300 :                 idx = (int)(p - b64);
      44    39400300 :                 byte_offset = (i*6)/8;
      45    39400300 :                 bit_offset = (i*6)%8;
      46    39400300 :                 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
      47    39400300 :                 if (bit_offset < 3) {
      48    19694994 :                         d[byte_offset] |= (idx << (2-bit_offset));
      49    19694994 :                         n = byte_offset+1;
      50             :                 } else {
      51    19705306 :                         d[byte_offset] |= (idx >> (bit_offset-2));
      52    19705306 :                         d[byte_offset+1] = (idx << (8-(bit_offset-2))) & 0xFF;
      53    19705306 :                         n = byte_offset+2;
      54             :                 }
      55    39400300 :                 s++; i++;
      56             :         }
      57             : 
      58       24185 :         if ((n > 0) && (*s == '=')) {
      59       20594 :                 n -= 1;
      60             :         }
      61             : 
      62             :         /* fix up length */
      63       24185 :         decoded.length = n;
      64       24185 :         decoded.data = talloc_realloc(mem_ctx, decoded.data, uint8_t, n);
      65       24185 :         return decoded;
      66             : }
      67             : 
      68             : /**
      69             :  * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
      70             :  **/
      71        2474 : _PUBLIC_ DATA_BLOB base64_decode_data_blob(const char *s)
      72             : {
      73        2474 :         return base64_decode_data_blob_talloc(NULL, s);
      74             : }
      75             : 
      76             : /**
      77             :  * Decode a base64 string in-place - wrapper for the above
      78             :  **/
      79           0 : _PUBLIC_ void base64_decode_inplace(char *s)
      80             : {
      81           0 :         DATA_BLOB decoded = base64_decode_data_blob(s);
      82             : 
      83           0 :         if ( decoded.length != 0 ) {
      84           0 :                 memcpy(s, decoded.data, decoded.length);
      85             : 
      86             :                 /* null terminate */
      87           0 :                 s[decoded.length] = '\0';
      88             :         } else {
      89           0 :                 *s = '\0';
      90             :         }
      91             : 
      92           0 :         data_blob_free(&decoded);
      93           0 : }
      94             : 
      95             : /**
      96             :  * Encode a base64 string into a talloc()ed string caller to free.
      97             :  *
      98             :  * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c
      99             :  * with adjustments
     100             :  **/
     101             : 
     102        2641 : _PUBLIC_ char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
     103             : {
     104        2641 :         int bits = 0;
     105        2641 :         int char_count = 0;
     106             :         size_t out_cnt, len, output_len;
     107             :         char *result;
     108             : 
     109        2641 :         if (!data.length || !data.data)
     110           0 :                 return NULL;
     111             : 
     112        2641 :         out_cnt = 0;
     113        2641 :         len = data.length;
     114        2641 :         output_len = data.length * 2 + 4; /* Account for closing bytes. 4 is
     115             :                                            * random but should be enough for
     116             :                                            * the = and \0 */
     117        2641 :         result = talloc_array(mem_ctx, char, output_len); /* get us plenty of space */
     118        2641 :         if (result == NULL) {
     119           0 :                 return NULL;
     120             :         }
     121             : 
     122     2135172 :         while (len--) {
     123     2132050 :                 int c = (unsigned char) *(data.data++);
     124     2132050 :                 bits += c;
     125     2132050 :                 char_count++;
     126     2132050 :                 if (char_count == 3) {
     127      709803 :                         result[out_cnt++] = b64[bits >> 18];
     128      709803 :                         result[out_cnt++] = b64[(bits >> 12) & 0x3f];
     129      709803 :                         result[out_cnt++] = b64[(bits >> 6) & 0x3f];
     130      709803 :                         result[out_cnt++] = b64[bits & 0x3f];
     131      709803 :                         bits = 0;
     132      709803 :                         char_count = 0;
     133             :                 } else {
     134     1422247 :                         bits <<= 8;
     135             :                 }
     136             :         }
     137        2641 :         if (char_count != 0) {
     138        1792 :                 bits <<= 16 - (8 * char_count);
     139        1792 :                 result[out_cnt++] = b64[bits >> 18];
     140        1792 :                 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
     141        1792 :                 if (char_count == 1) {
     142         943 :                         result[out_cnt++] = '=';
     143         943 :                         result[out_cnt++] = '=';
     144             :                 } else {
     145         849 :                         result[out_cnt++] = b64[(bits >> 6) & 0x3f];
     146         849 :                         result[out_cnt++] = '=';
     147             :                 }
     148             :         }
     149        2641 :         result[out_cnt] = '\0'; /* terminate */
     150        2641 :         return result;
     151             : }
     152             : 

Generated by: LCOV version 1.13