LCOV - code coverage report
Current view: top level - util/crypto/nss - nss_base64.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 25 27 92.6 %
Date: 2012-11-29 Functions: 2 2 100.0 %
Branches: 10 16 62.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :     Authors:
       3                 :            :         Jakub Hrozek <jhrozek@redhat.com>
       4                 :            : 
       5                 :            :     Copyright (C) 2012 Red Hat
       6                 :            : 
       7                 :            :     This program is free software; you can redistribute it and/or modify
       8                 :            :     it under the terms of the GNU General Public License as published by
       9                 :            :     the Free Software Foundation; either version 3 of the License, or
      10                 :            :     (at your option) any later version.
      11                 :            : 
      12                 :            :     This program is distributed in the hope that it will be useful,
      13                 :            :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :     GNU General Public License for more details.
      16                 :            : 
      17                 :            :     You should have received a copy of the GNU General Public License
      18                 :            :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19                 :            : */
      20                 :            : 
      21                 :            : #include <talloc.h>
      22                 :            : 
      23                 :            : #include "util/util.h"
      24                 :            : #include "util/crypto/nss/nss_util.h"
      25                 :            : 
      26                 :            : #include <base64.h>
      27                 :            : 
      28                 :            : /* NSS wraps b64 encoded buffers with CRLF automatically after 64 chars. This
      29                 :            :  * function strips the CRLF double-chars. The buffer can be decoded with plain
      30                 :            :  * NSS calls */
      31                 :          4 : char *sss_base64_encode(TALLOC_CTX *mem_ctx,
      32                 :            :                         const unsigned char *inbuf,
      33                 :            :                         size_t inbufsize)
      34                 :            : {
      35                 :            :     int ret;
      36                 :          4 :     char *b64encoded = NULL;
      37                 :            :     int i, j, b64size;
      38                 :            :     char *outbuf;
      39                 :            : 
      40                 :            :     /* initialize NSS if needed */
      41                 :          4 :     ret = nspr_nss_init();
      42         [ +  - ]:          4 :     if (ret != EOK) {
      43                 :            :         return NULL;
      44                 :            :     }
      45                 :            : 
      46                 :          4 :     b64encoded = BTOA_DataToAscii(inbuf, inbufsize);
      47         [ +  - ]:          4 :     if (!b64encoded) return NULL;
      48                 :            : 
      49                 :          4 :     b64size = strlen(b64encoded) + 1;
      50                 :          4 :     outbuf = talloc_array(mem_ctx, char, b64size);
      51         [ +  - ]:          4 :     if (outbuf == NULL) {
      52                 :          0 :         PORT_Free(b64encoded);
      53                 :          0 :         return NULL;
      54                 :            :     }
      55                 :            : 
      56         [ +  + ]:        342 :     for (i=0, j=0; i < b64size; i++) {
      57         [ +  + ]:        338 :         if (b64encoded[i] == '\n' || b64encoded[i] == '\r') {
      58                 :          6 :             continue;
      59                 :            :         }
      60                 :        332 :         outbuf[j++] = b64encoded[i]; /* will also copy the trailing \0 char */
      61                 :            :     }
      62                 :            : 
      63                 :          4 :     PORT_Free(b64encoded);
      64                 :          4 :     return outbuf;
      65                 :            : }
      66                 :            : 
      67                 :          4 : unsigned char *sss_base64_decode(TALLOC_CTX *mem_ctx,
      68                 :            :                                  const char *inbuf,
      69                 :            :                                  size_t *outbufsize)
      70                 :            : {
      71                 :            :     int ret;
      72                 :          4 :     unsigned char *b64decoded = NULL;
      73                 :            :     unsigned int size;
      74                 :            :     unsigned char *outbuf;
      75                 :            : 
      76                 :            :     /* initialize NSS if needed */
      77                 :          4 :     ret = nspr_nss_init();
      78         [ +  - ]:          4 :     if (ret != EOK) {
      79                 :            :         return NULL;
      80                 :            :     }
      81                 :            : 
      82                 :          4 :     b64decoded = ATOB_AsciiToData(inbuf, &size);
      83         [ +  - ]:          4 :     if (!b64decoded) return NULL;
      84                 :            : 
      85                 :          4 :     outbuf = talloc_memdup(mem_ctx, b64decoded, size);
      86                 :          4 :     PORT_Free(b64decoded);
      87         [ +  - ]:          4 :     if (!outbuf) return NULL;
      88                 :            : 
      89                 :          4 :     *outbufsize = size;
      90                 :            :     return outbuf;
      91                 :            : }

Generated by: LCOV version 1.9