LCOV - code coverage report
Current view: top level - fwknop.git/lib - base64.c (source / functions) Hit Total Coverage
Test: lcov_coverage_final.info Lines: 32 32 100.0 %
Date: 2014-07-28 Functions: 3 3 100.0 %
Branches: 19 20 95.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *****************************************************************************
       3                 :            :  *
       4                 :            :  * File:    base64.c
       5                 :            :  *
       6                 :            :  * Purpose: Implementation of the Base64 encode/decode algorithim.
       7                 :            :  *
       8                 :            :  * This code was derived from the base64.c part of FFmpeg written
       9                 :            :  * by Ryan Martell. (rdm4@martellventures.com).
      10                 :            :  *
      11                 :            :  * Copyright (C) Ryan Martell. (rdm4@martellventures.com)
      12                 :            :  *
      13                 :            :  *  Fwknop is developed primarily by the people listed in the file 'AUTHORS'.
      14                 :            :  *  Copyright (C) 2009-2014 fwknop developers and contributors. For a full
      15                 :            :  *  list of contributors, see the file 'CREDITS'.
      16                 :            :  *
      17                 :            :  *  License (GNU General Public License):
      18                 :            :  *
      19                 :            :  *  This library is free software; you can redistribute it and/or
      20                 :            :  *  modify it under the terms of the GNU General Public License
      21                 :            :  *  as published by the Free Software Foundation; either version 2
      22                 :            :  *  of the License, or (at your option) any later version.
      23                 :            :  *
      24                 :            :  *  This program is distributed in the hope that it will be useful,
      25                 :            :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      26                 :            :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      27                 :            :  *  GNU General Public License for more details.
      28                 :            :  *
      29                 :            :  *  You should have received a copy of the GNU General Public License
      30                 :            :  *  along with this program; if not, write to the Free Software
      31                 :            :  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
      32                 :            :  *  USA
      33                 :            :  *
      34                 :            :  *****************************************************************************
      35                 :            : */
      36                 :            : #include "base64.h"
      37                 :            : #include "fko_common.h"
      38                 :            : 
      39                 :            : static unsigned char map2[] =
      40                 :            : {
      41                 :            :     0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36,
      42                 :            :     0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff,
      43                 :            :     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01,
      44                 :            :     0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
      45                 :            :     0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
      46                 :            :     0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
      47                 :            :     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b,
      48                 :            :     0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
      49                 :            :     0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
      50                 :            :     0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33
      51                 :            : };
      52                 :            : 
      53                 :            : int
      54                 :    8683500 : b64_decode(const char *in, unsigned char *out)
      55                 :            : {
      56                 :            :     int i, v;
      57                 :    8683500 :     unsigned char *dst = out;
      58                 :            : 
      59                 :    8683500 :     v = 0;
      60         [ +  + ]: 1177450427 :     for (i = 0; in[i] && in[i] != '='; i++) {
      61                 : 1168780509 :         unsigned int index= in[i]-43;
      62                 :            : 
      63 [ +  + ][ +  + ]: 1168780509 :         if (index>=(sizeof(map2)/sizeof(map2[0])) || map2[index] == 0xff)
      64                 :            :             return(-1);
      65                 :            : 
      66                 : 1168766927 :         v = (v << 6) + map2[index];
      67                 :            : 
      68         [ +  + ]: 1168766927 :         if (i & 3)
      69                 :  874001956 :             *dst++ = v >> (6 - 2 * (i & 3));
      70                 :            :     }
      71                 :            : 
      72                 :    8669918 :     *dst = '\0';
      73                 :            : 
      74                 :    8669918 :     return(dst - out);
      75                 :            : }
      76                 :            : 
      77                 :            : /*****************************************************************************
      78                 :            :  * b64_encode: Stolen from VLC's http.c
      79                 :            :  * Simplified by michael
      80                 :            :  * fixed edge cases and made it work from data (vs. strings) by ryan.
      81                 :            :  *****************************************************************************
      82                 :            : */
      83                 :            : int
      84                 :   13062910 : b64_encode(unsigned char *in, char *out, int in_len)
      85                 :            : {
      86                 :            :     static const char b64[] =
      87                 :            :         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      88                 :   13062910 :     unsigned i_bits = 0;
      89                 :   13062910 :     int i_shift = 0;
      90                 :   13062910 :     int bytes_remaining = in_len;
      91                 :            : 
      92                 :   13062910 :     char *dst = out;
      93                 :            : 
      94         [ +  - ]:   13062910 :     if (in_len > 0) { /* Special edge case, what should we really do here? */
      95         [ +  + ]:  829416657 :         while (bytes_remaining) {
      96                 :  816353747 :             i_bits = (i_bits << 8) + *in++;
      97                 :  816353747 :             bytes_remaining--;
      98                 : 1093362187 :             i_shift += 8;
      99                 :            : 
     100                 :            :             do {
     101                 : 1093362187 :                 *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f];
     102                 : 1093362187 :                 i_shift -= 6;
     103 [ +  + ][ +  + ]: 1106425097 :             } while (i_shift > 6 || (bytes_remaining == 0 && i_shift > 0));
     104                 :            :         }
     105         [ +  + ]:   27734483 :         while ((dst - out) & 3)
     106                 :   14671573 :             *dst++ = '=';
     107                 :            :     }
     108                 :            : 
     109                 :   13062910 :     *dst = '\0';
     110                 :            : 
     111                 :   13062910 :     return(dst - out);
     112                 :            : }
     113                 :            : 
     114                 :            : /* Strip trailing equals ("=") charcters from a base64-encoded
     115                 :            :  * message digest.
     116                 :            : */
     117                 :            : void
     118                 :   13062674 : strip_b64_eq(char *data)
     119                 :            : {
     120                 :            :     char *ndx;
     121                 :            : 
     122         [ +  + ]:   13062674 :     if((ndx = strchr(data, '=')) != NULL)
     123                 :   11145613 :         *ndx = '\0';
     124                 :   13062674 : }
     125                 :            : 
     126                 :            : /***EOF***/

Generated by: LCOV version 1.9