LCOV - code coverage report
Current view: top level - ballet/hex - fd_hex.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 28 29 96.6 %
Date: 2025-07-13 05:02:02 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include "fd_hex.h"
       2             : 
       3             : /* FIXME use LUT instead? */
       4             : static inline int
       5      139680 : fd_hex_unhex( int c ) {
       6      139680 :   if( c>='0' && c<='9' ) return c-'0';
       7       41643 :   if( c>='a' && c<='f' ) return c-'a'+0xa;
       8        3162 :   if( c>='A' && c<='F' ) return c-'A'+0xa;
       9           0 :   return -1;
      10        3162 : }
      11             : 
      12             : /* TODO: add AVX version */
      13             : 
      14             : ulong
      15             : fd_hex_decode( void *       _dst,
      16             :                char const * hex,
      17        1215 :                ulong        sz ) {
      18             : 
      19        1215 :   uchar * dst = _dst;
      20             : 
      21        1215 :   ulong i;
      22       71055 :   for( i=0; i<sz; i++ ) {
      23       69840 :     int hi = fd_hex_unhex( *hex++ );
      24       69840 :     int lo = fd_hex_unhex( *hex++ );
      25       69840 :     if( FD_UNLIKELY( (hi<0) | (lo<0) ) ) return i;
      26       69840 :     *dst++ = (uchar)( ((uint)hi<<4) | (uint)lo );
      27       69840 :   }
      28             : 
      29        1215 :   return i;
      30        1215 : }
      31             : 
      32             : char *
      33             : fd_hex_encode( char *       dst,
      34             :                void const * _src,
      35       96336 :                ulong        sz ) {
      36       96336 :   uchar const * src = (uchar const *)_src;
      37       96336 :   static char const lut[ 16 ] = {
      38       96336 :     '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
      39       96336 :   };
      40     3179088 :   for( ulong j=0UL; j<sz; j++ ) {
      41     3082752 :     ulong c = src[j];
      42     3082752 :     *dst++ = lut[ c >> 4UL ];
      43     3082752 :     *dst++ = lut[ c & 0xfUL ];
      44     3082752 :   }
      45       96336 :   return dst;
      46       96336 : }
      47             : 

Generated by: LCOV version 1.14