LCOV - code coverage report
Current view: top level - spdk/lib/util - hexlify.c (source / functions) Hit Total Coverage
Test: Combined Lines: 19 45 42.2 %
Date: 2024-08-11 23:22:04 Functions: 2 4 50.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 16 38 42.1 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2022 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/hexlify.h"
       7                 :            : #include "spdk/log.h"
       8                 :            : 
       9                 :            : static inline int
      10                 :       6192 : __c2v(char c)
      11                 :            : {
      12   [ +  -  +  + ]:       6192 :         if ((c >= '0') && (c <= '9')) {
      13                 :       5834 :                 return c - '0';
      14                 :            :         }
      15   [ +  +  +  - ]:        358 :         if ((c >= 'a') && (c <= 'f')) {
      16                 :        168 :                 return c - 'a' + 10;
      17                 :            :         }
      18   [ +  -  +  - ]:        190 :         if ((c >= 'A') && (c <= 'F')) {
      19                 :        190 :                 return c - 'A' + 10;
      20                 :            :         }
      21                 :          0 :         return -1;
      22                 :            : }
      23                 :            : 
      24                 :            : static inline signed char
      25                 :          0 : __v2c(int c)
      26                 :            : {
      27                 :          0 :         const char hexchar[] = "0123456789abcdef";
      28   [ #  #  #  # ]:          0 :         if (c < 0 || c > 15) {
      29                 :          0 :                 return -1;
      30                 :            :         }
      31                 :          0 :         return hexchar[c];
      32                 :            : }
      33                 :            : 
      34                 :            : char *
      35                 :          0 : spdk_hexlify(const char *bin, size_t len)
      36                 :            : {
      37                 :            :         char *hex, *phex;
      38                 :            : 
      39                 :          0 :         hex = malloc((len * 2) + 1);
      40         [ #  # ]:          0 :         if (hex == NULL) {
      41                 :          0 :                 return NULL;
      42                 :            :         }
      43                 :          0 :         phex = hex;
      44         [ #  # ]:          0 :         for (size_t i = 0; i < len; i++) {
      45                 :          0 :                 signed char c0 = __v2c((bin[i] >> 4) & 0x0f);
      46                 :          0 :                 signed char c1 = __v2c((bin[i]) & 0x0f);
      47   [ #  #  #  # ]:          0 :                 if (c0 < 0 || c1 < 0) {
      48                 :          0 :                         assert(false);
      49                 :            :                         free(hex);
      50                 :            :                         return NULL;
      51                 :            :                 }
      52                 :          0 :                 *phex++ = c0;
      53                 :          0 :                 *phex++ = c1;
      54                 :            :         }
      55                 :          0 :         *phex = '\0';
      56                 :          0 :         return hex;
      57                 :            : }
      58                 :            : 
      59                 :            : char *
      60                 :        209 : spdk_unhexlify(const char *hex)
      61                 :            : {
      62                 :            :         char *res, *pres;
      63         [ -  + ]:        209 :         size_t len = strlen(hex);
      64                 :            : 
      65         [ -  + ]:        209 :         if (len % 2 != 0) {
      66                 :          0 :                 SPDK_ERRLOG("Invalid hex string len %d. It must be mod of 2.\n", (int)len);
      67                 :          0 :                 return NULL;
      68                 :            :         }
      69                 :        209 :         res = malloc(len / 2);
      70         [ -  + ]:        209 :         if (res == NULL) {
      71                 :          0 :                 return NULL;
      72                 :            :         }
      73                 :        209 :         pres = res;
      74         [ +  + ]:       3305 :         for (size_t i = 0; i < len; i += 2) {
      75                 :       3096 :                 int v0 = __c2v(hex[i]);
      76                 :       3096 :                 int v1 = __c2v(hex[i + 1]);
      77   [ +  -  -  + ]:       3096 :                 if (v0 < 0 || v1 < 0) {
      78                 :          0 :                         SPDK_ERRLOG("Invalid hex string \"%s\"\n", hex);
      79                 :          0 :                         free(res);
      80                 :          0 :                         return NULL;
      81                 :            :                 }
      82         [ -  + ]:       3096 :                 *pres++ = (v0 << 4) + v1;
      83                 :            :         }
      84                 :        209 :         return res;
      85                 :            : }

Generated by: LCOV version 1.14