LCOV - code coverage report
Current view: top level - spdk/lib/util - base64.c (source / functions) Hit Total Coverage
Test: Combined Lines: 75 78 96.2 %
Date: 2024-11-17 13:58:19 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 217 382 56.8 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2018 Intel Corporation. All rights reserved.
       3                 :            :  *   Copyright(c) ARM Limited. 2021 All rights reserved.
       4                 :            :  *   All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "spdk/stdinc.h"
       8                 :            : #include "spdk/endian.h"
       9                 :            : #include "spdk/base64.h"
      10                 :            : 
      11                 :            : #ifdef __aarch64__
      12                 :            : #ifdef __ARM_FEATURE_SVE
      13                 :            : #include "base64_sve.c"
      14                 :            : #else
      15                 :            : #include "base64_neon.c"
      16                 :            : #endif
      17                 :            : #endif
      18                 :            : 
      19                 :            : 
      20                 :            : #define BASE64_ENC_BITMASK 0x3FUL
      21                 :            : #define BASE64_PADDING_CHAR '='
      22                 :            : 
      23                 :            : static const char base64_enc_table[] =
      24                 :            :         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      25                 :            :         "abcdefghijklmnopqrstuvwxyz"
      26                 :            :         "0123456789+/";
      27                 :            : 
      28                 :            : static const char base64_urlsafe_enc_table[] =
      29                 :            :         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      30                 :            :         "abcdefghijklmnopqrstuvwxyz"
      31                 :            :         "0123456789-_";
      32                 :            : 
      33                 :            : static const uint8_t
      34                 :            : base64_dec_table[] = {
      35                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      36                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      37                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  62, 255, 255, 255,  63,
      38                 :            :         52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255, 255, 255, 255, 255,
      39                 :            :         255,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
      40                 :            :         15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255,
      41                 :            :         255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
      42                 :            :         41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51, 255, 255, 255, 255, 255,
      43                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      44                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      45                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      46                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      47                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      48                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      49                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      50                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      51                 :            : };
      52                 :            : 
      53                 :            : static const uint8_t
      54                 :            : base64_urlsafe_dec_table[] = {
      55                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      56                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      57                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  62, 255, 255,
      58                 :            :         52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255, 255, 255, 255, 255,
      59                 :            :         255,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
      60                 :            :         15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255,  63,
      61                 :            :         255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
      62                 :            :         41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51, 255, 255, 255, 255, 255,
      63                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      64                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      65                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      66                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      67                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      68                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      69                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      70                 :            :         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
      71                 :            : };
      72                 :            : 
      73                 :            : static int
      74                 :     156706 : base64_encode(char *dst, const char *enc_table, const void *src, size_t src_len)
      75                 :            : {
      76                 :         95 :         uint32_t raw_u32;
      77                 :            : 
      78   [ +  +  +  +  :     156706 :         if (!dst || !src || src_len <= 0) {
                   +  + ]
      79                 :         36 :                 return -EINVAL;
      80                 :            :         }
      81                 :            : 
      82                 :            : #ifdef __aarch64__
      83                 :            : #ifdef __ARM_FEATURE_SVE
      84                 :            :         base64_encode_sve(&dst, enc_table, &src, &src_len);
      85                 :            : #else
      86                 :            :         base64_encode_neon64(&dst, enc_table, &src, &src_len);
      87                 :            : #endif
      88                 :            : #endif
      89                 :            : 
      90                 :            : 
      91         [ +  + ]:     271845 :         while (src_len >= 4) {
      92                 :     115175 :                 raw_u32 = from_be32(src);
      93                 :            : 
      94   [ +  -  +  -  :     115175 :                 *dst++ = enc_table[(raw_u32 >> 26) & BASE64_ENC_BITMASK];
          +  -  +  -  +  
                      - ]
      95   [ +  -  +  -  :     115175 :                 *dst++ = enc_table[(raw_u32 >> 20) & BASE64_ENC_BITMASK];
          +  -  +  -  +  
                      - ]
      96   [ +  -  +  -  :     115175 :                 *dst++ = enc_table[(raw_u32 >> 14) & BASE64_ENC_BITMASK];
          +  -  +  -  +  
                      - ]
      97   [ +  -  +  -  :     115175 :                 *dst++ = enc_table[(raw_u32 >> 8) & BASE64_ENC_BITMASK];
          +  -  +  -  +  
                      - ]
      98                 :            : 
      99                 :     115175 :                 src_len -= 3;
     100         [ +  - ]:     115175 :                 src = (uint8_t *)src + 3;
     101                 :            :         }
     102                 :            : 
     103         [ +  + ]:     156670 :         if (src_len == 0) {
     104                 :          0 :                 goto out;
     105                 :            :         }
     106                 :            : 
     107                 :     156670 :         raw_u32 = 0;
     108   [ +  +  +  + ]:     156670 :         memcpy(&raw_u32, src, src_len);
     109                 :     156670 :         raw_u32 = from_be32(&raw_u32);
     110                 :            : 
     111   [ +  -  +  -  :     156670 :         *dst++ = enc_table[(raw_u32 >> 26) & BASE64_ENC_BITMASK];
          +  -  +  -  +  
                      - ]
     112   [ +  -  +  -  :     156670 :         *dst++ = enc_table[(raw_u32 >> 20) & BASE64_ENC_BITMASK];
          +  -  +  -  +  
                      - ]
     113   [ +  +  +  -  :     156670 :         *dst++ = (src_len >= 2) ? enc_table[(raw_u32 >> 14) & BASE64_ENC_BITMASK] : BASE64_PADDING_CHAR;
          +  -  +  -  +  
                -  +  - ]
     114   [ +  +  +  -  :     156670 :         *dst++ = (src_len == 3) ? enc_table[(raw_u32 >> 8) & BASE64_ENC_BITMASK] : BASE64_PADDING_CHAR;
          +  -  +  -  +  
                -  +  - ]
     115                 :            : 
     116                 :     156658 : out:
     117         [ +  - ]:     156670 :         *dst = '\0';
     118                 :            : 
     119                 :     156670 :         return 0;
     120                 :         18 : }
     121                 :            : 
     122                 :            : int
     123                 :     156646 : spdk_base64_encode(char *dst, const void *src, size_t src_len)
     124                 :            : {
     125                 :     156646 :         return base64_encode(dst, base64_enc_table, src, src_len);
     126                 :            : }
     127                 :            : 
     128                 :            : int
     129                 :         60 : spdk_base64_urlsafe_encode(char *dst, const void *src, size_t src_len)
     130                 :            : {
     131                 :         60 :         return base64_encode(dst, base64_urlsafe_enc_table, src, src_len);
     132                 :            : }
     133                 :            : 
     134                 :            : #if defined(__aarch64__) && !defined(__ARM_FEATURE_SVE)
     135                 :            : static int
     136                 :            : base64_decode(void *dst, size_t *_dst_len, const uint8_t *dec_table,
     137                 :            :               const uint8_t *dec_table_opt, const char *src)
     138                 :            : #else
     139                 :            : static int
     140                 :        330 : base64_decode(void *dst, size_t *_dst_len, const uint8_t *dec_table, const char *src)
     141                 :            : #endif
     142                 :            : {
     143                 :         35 :         size_t src_strlen;
     144                 :        330 :         size_t tail_len = 0;
     145                 :         35 :         const uint8_t *src_in;
     146                 :        178 :         uint32_t tmp[4];
     147                 :         35 :         int i;
     148                 :            : 
     149         [ +  + ]:        330 :         if (!src) {
     150                 :         12 :                 return -EINVAL;
     151                 :            :         }
     152                 :            : 
     153         [ +  + ]:        318 :         src_strlen = strlen(src);
     154                 :            : 
     155                 :            :         /* strlen of src should be 4n */
     156   [ +  -  +  +  :        318 :         if (src_strlen == 0 || src_strlen % 4 != 0) {
                   +  + ]
     157                 :         36 :                 return -EINVAL;
     158                 :            :         }
     159                 :            : 
     160                 :            :         /* Consider Base64 padding, it at most has 2 padding characters. */
     161   [ +  +  +  - ]:        606 :         for (i = 0; i < 2; i++) {
     162   [ +  +  +  -  :        458 :                 if (src[src_strlen - 1] != BASE64_PADDING_CHAR) {
                   +  + ]
     163                 :        134 :                         break;
     164                 :            :                 }
     165                 :        324 :                 src_strlen--;
     166                 :         24 :         }
     167                 :            : 
     168                 :            :         /* strlen of src without padding shouldn't be 4n+1 */
     169   [ +  -  +  +  :        282 :         if (src_strlen == 0 || src_strlen % 4 == 1) {
                   -  + ]
     170                 :          0 :                 return -EINVAL;
     171                 :            :         }
     172                 :            : 
     173         [ +  + ]:        282 :         if (_dst_len) {
     174         [ +  - ]:        282 :                 *_dst_len = spdk_base64_get_decoded_len(src_strlen);
     175                 :         27 :         }
     176                 :            : 
     177                 :            :         /* If dst is NULL, the client is only concerned w/ _dst_len, return */
     178         [ +  + ]:        282 :         if (!dst) {
     179                 :         72 :                 return 0;
     180                 :            :         }
     181                 :            : 
     182                 :        210 :         src_in = (const uint8_t *) src;
     183                 :            : 
     184                 :            : #ifdef __aarch64__
     185                 :            : #ifdef __ARM_FEATURE_SVE
     186                 :            :         base64_decode_sve(&dst, dec_table, &src_in, &src_strlen);
     187                 :            : #else
     188                 :            :         base64_decode_neon64(&dst, dec_table_opt, &src_in, &src_strlen);
     189                 :            : #endif
     190                 :            : 
     191                 :            :         if (src_strlen == 0) {
     192                 :            :                 return 0;
     193                 :            :         }
     194                 :            : #endif
     195                 :            : 
     196                 :            : 
     197                 :            :         /* space of dst can be used by to_be32 */
     198         [ +  + ]:       1992 :         while (src_strlen > 4) {
     199   [ +  -  +  -  :       1782 :                 tmp[0] = dec_table[*src_in++];
          +  -  +  -  +  
                -  +  - ]
     200   [ +  -  +  -  :       1782 :                 tmp[1] = dec_table[*src_in++];
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     201   [ +  -  +  -  :       1782 :                 tmp[2] = dec_table[*src_in++];
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     202   [ +  -  +  -  :       1782 :                 tmp[3] = dec_table[*src_in++];
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     203                 :            : 
     204   [ +  -  +  -  :       1782 :                 if (tmp[0] == 255 || tmp[1] == 255 || tmp[2] == 255 || tmp[3] == 255) {
          +  -  +  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     205                 :          0 :                         return -EINVAL;
     206                 :            :                 }
     207                 :            : 
     208   [ +  -  +  -  :       1782 :                 to_be32(dst, tmp[3] << 8 | tmp[2] << 14 | tmp[1] << 20 | tmp[0] << 26);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     209                 :            : 
     210         [ +  - ]:       1782 :                 dst = (uint8_t *)dst + 3;
     211                 :       1782 :                 src_strlen -= 4;
     212                 :            :         }
     213                 :            : 
     214                 :            :         /* space of dst is not enough to be used by to_be32 */
     215   [ +  -  +  -  :        210 :         tmp[0] = dec_table[src_in[0]];
          +  -  +  -  +  
                -  +  - ]
     216   [ +  -  +  -  :        210 :         tmp[1] = dec_table[src_in[1]];
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     217   [ +  +  +  -  :        210 :         tmp[2] = (src_strlen >= 3) ? dec_table[src_in[2]] : 0;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     218   [ +  +  +  -  :        210 :         tmp[3] = (src_strlen == 4) ? dec_table[src_in[3]] : 0;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     219                 :        210 :         tail_len = src_strlen - 1;
     220                 :            : 
     221   [ +  -  +  +  :        210 :         if (tmp[0] == 255 || tmp[1] == 255 || tmp[2] == 255 || tmp[3] == 255) {
          +  -  +  +  +  
          -  +  -  +  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     222                 :         12 :                 return -EINVAL;
     223                 :            :         }
     224                 :            : 
     225   [ -  +  -  +  :        198 :         to_be32(&tmp[3], tmp[3] << 8 | tmp[2] << 14 | tmp[1] << 20 | tmp[0] << 26);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  -  
                      + ]
     226   [ -  +  -  +  :        198 :         memcpy(dst, (uint8_t *)&tmp[3], tail_len);
             -  +  -  + ]
     227                 :            : 
     228                 :        198 :         return 0;
     229                 :         35 : }
     230                 :            : 
     231                 :            : int
     232                 :        222 : spdk_base64_decode(void *dst, size_t *dst_len, const char *src)
     233                 :            : {
     234                 :            : #if defined(__aarch64__) && !defined(__ARM_FEATURE_SVE)
     235                 :            :         return base64_decode(dst, dst_len, base64_dec_table, base64_dec_table_neon64, src);
     236                 :            : #else
     237                 :        222 :         return base64_decode(dst, dst_len, base64_dec_table, src);
     238                 :            : #endif
     239                 :            : }
     240                 :            : 
     241                 :            : int
     242                 :        108 : spdk_base64_urlsafe_decode(void *dst, size_t *dst_len, const char *src)
     243                 :            : {
     244                 :            : #if defined(__aarch64__) && !defined(__ARM_FEATURE_SVE)
     245                 :            :         return base64_decode(dst, dst_len, base64_urlsafe_dec_table, base64_urlsafe_dec_table_neon64,
     246                 :            :                              src);
     247                 :            : #else
     248                 :        108 :         return base64_decode(dst, dst_len, base64_urlsafe_dec_table, src);
     249                 :            : #endif
     250                 :            : }

Generated by: LCOV version 1.14