LCOV - code coverage report
Current view: top level - src/utils - base64.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 81 81 100.0 %
Date: 2015-09-27 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Base64 encoding/decoding (RFC1341)
       3             :  * Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : 
      11             : #include "os.h"
      12             : #include "base64.h"
      13             : 
      14             : static const unsigned char base64_table[65] =
      15             :         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      16             : 
      17             : /**
      18             :  * base64_encode - Base64 encode
      19             :  * @src: Data to be encoded
      20             :  * @len: Length of the data to be encoded
      21             :  * @out_len: Pointer to output length variable, or %NULL if not used
      22             :  * Returns: Allocated buffer of out_len bytes of encoded data,
      23             :  * or %NULL on failure
      24             :  *
      25             :  * Caller is responsible for freeing the returned buffer. Returned buffer is
      26             :  * nul terminated to make it easier to use as a C string. The nul terminator is
      27             :  * not included in out_len.
      28             :  */
      29        1083 : unsigned char * base64_encode(const unsigned char *src, size_t len,
      30             :                               size_t *out_len)
      31             : {
      32             :         unsigned char *out, *pos;
      33             :         const unsigned char *end, *in;
      34             :         size_t olen;
      35             :         int line_len;
      36             : 
      37        1083 :         olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
      38        1083 :         olen += olen / 72; /* line feeds */
      39        1083 :         olen++; /* nul termination */
      40        1083 :         if (olen < len)
      41           1 :                 return NULL; /* integer overflow */
      42        1082 :         out = os_malloc(olen);
      43        1082 :         if (out == NULL)
      44           4 :                 return NULL;
      45             : 
      46        1078 :         end = src + len;
      47        1078 :         in = src;
      48        1078 :         pos = out;
      49        1078 :         line_len = 0;
      50       53096 :         while (end - in >= 3) {
      51       50940 :                 *pos++ = base64_table[(in[0] >> 2) & 0x3f];
      52      152820 :                 *pos++ = base64_table[(((in[0] & 0x03) << 4) |
      53      101880 :                                        (in[1] >> 4)) & 0x3f];
      54      152820 :                 *pos++ = base64_table[(((in[1] & 0x0f) << 2) |
      55      101880 :                                        (in[2] >> 6)) & 0x3f];
      56       50940 :                 *pos++ = base64_table[in[2] & 0x3f];
      57       50940 :                 in += 3;
      58       50940 :                 line_len += 4;
      59       50940 :                 if (line_len >= 72) {
      60        2483 :                         *pos++ = '\n';
      61        2483 :                         line_len = 0;
      62             :                 }
      63             :         }
      64             : 
      65        1078 :         if (end - in) {
      66         127 :                 *pos++ = base64_table[(in[0] >> 2) & 0x3f];
      67         127 :                 if (end - in == 1) {
      68          44 :                         *pos++ = base64_table[((in[0] & 0x03) << 4) & 0x3f];
      69          44 :                         *pos++ = '=';
      70             :                 } else {
      71         249 :                         *pos++ = base64_table[(((in[0] & 0x03) << 4) |
      72         166 :                                                (in[1] >> 4)) & 0x3f];
      73          83 :                         *pos++ = base64_table[((in[1] & 0x0f) << 2) & 0x3f];
      74             :                 }
      75         127 :                 *pos++ = '=';
      76         127 :                 line_len += 4;
      77             :         }
      78             : 
      79        1078 :         if (line_len)
      80        1077 :                 *pos++ = '\n';
      81             : 
      82        1078 :         *pos = '\0';
      83        1078 :         if (out_len)
      84        1065 :                 *out_len = pos - out;
      85        1078 :         return out;
      86             : }
      87             : 
      88             : 
      89             : /**
      90             :  * base64_decode - Base64 decode
      91             :  * @src: Data to be decoded
      92             :  * @len: Length of the data to be decoded
      93             :  * @out_len: Pointer to output length variable
      94             :  * Returns: Allocated buffer of out_len bytes of decoded data,
      95             :  * or %NULL on failure
      96             :  *
      97             :  * Caller is responsible for freeing the returned buffer.
      98             :  */
      99         370 : unsigned char * base64_decode(const unsigned char *src, size_t len,
     100             :                               size_t *out_len)
     101             : {
     102             :         unsigned char dtable[256], *out, *pos, block[4], tmp;
     103             :         size_t i, count, olen;
     104         370 :         int pad = 0;
     105             : 
     106         370 :         os_memset(dtable, 0x80, 256);
     107       24050 :         for (i = 0; i < sizeof(base64_table) - 1; i++)
     108       23680 :                 dtable[base64_table[i]] = (unsigned char) i;
     109         370 :         dtable['='] = 0;
     110             : 
     111         370 :         count = 0;
     112       97197 :         for (i = 0; i < len; i++) {
     113       96827 :                 if (dtable[src[i]] != 0x80)
     114       95489 :                         count++;
     115             :         }
     116             : 
     117         370 :         if (count == 0 || count % 4)
     118           3 :                 return NULL;
     119             : 
     120         367 :         olen = count / 4 * 3;
     121         367 :         pos = out = os_malloc(olen);
     122         367 :         if (out == NULL)
     123           2 :                 return NULL;
     124             : 
     125         365 :         count = 0;
     126       96215 :         for (i = 0; i < len; i++) {
     127       95996 :                 tmp = dtable[src[i]];
     128       95996 :                 if (tmp == 0x80)
     129        1212 :                         continue;
     130             : 
     131       94784 :                 if (src[i] == '=')
     132         192 :                         pad++;
     133       94784 :                 block[count] = tmp;
     134       94784 :                 count++;
     135       94784 :                 if (count == 4) {
     136       23696 :                         *pos++ = (block[0] << 2) | (block[1] >> 4);
     137       23696 :                         *pos++ = (block[1] << 4) | (block[2] >> 2);
     138       23696 :                         *pos++ = (block[2] << 6) | block[3];
     139       23696 :                         count = 0;
     140       23696 :                         if (pad) {
     141         146 :                                 if (pad == 1)
     142         102 :                                         pos--;
     143          44 :                                 else if (pad == 2)
     144          43 :                                         pos -= 2;
     145             :                                 else {
     146             :                                         /* Invalid padding */
     147           1 :                                         os_free(out);
     148           1 :                                         return NULL;
     149             :                                 }
     150         145 :                                 break;
     151             :                         }
     152             :                 }
     153             :         }
     154             : 
     155         364 :         *out_len = pos - out;
     156         364 :         return out;
     157             : }

Generated by: LCOV version 1.10