LCOV - code coverage report
Current view: top level - ballet/pb - fd_pb_tokenize.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 52 54 96.3 %
Date: 2026-11-11 12:40:43 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include "fd_pb_tokenize.h"
       2             : #include "fd_pb_wire.h"
       3             : 
       4             : fd_pb_inbuf_t *
       5             : fd_pb_read_varint( fd_pb_inbuf_t * buf,
       6    17144167 :                    ulong *         out ) {
       7    17144167 :   ulong sz    = (ulong)( buf->end - buf->cur );
       8    17144167 :   ulong var   = 0UL;
       9    17144167 :   int   shift = 0;
      10    23414041 :   for( ulong i=0UL;; i++ ) {
      11    23414041 :     if( FD_UNLIKELY( i>=10 || i>=sz ) ) return NULL;
      12    23414041 :     if( FD_UNLIKELY( i==9UL && buf->cur[i]>1U ) ) return NULL;
      13    23414041 :     var |= (ulong)( buf->cur[i] & 0x7f ) << shift;
      14    23414041 :     shift += 7;
      15    23414041 :     if( !( buf->cur[i] & 0x80 ) ) {
      16    16478976 :       buf->cur += i+1;
      17    16478976 :       *out = var;
      18    16478976 :       return buf;
      19    16478976 :     }
      20    23414041 :   }
      21    17144167 : }
      22             : 
      23             : static fd_pb_tlv_t *
      24             : fd_pb_tlv_read_slow( fd_pb_inbuf_t * buf,
      25    10359292 :                      fd_pb_tlv_t *   tlv ) {
      26    10359292 :   fd_pb_inbuf_t buf2 = *buf;
      27    10359292 :   ulong tag;
      28    10359292 :   if( FD_UNLIKELY( !fd_pb_read_varint( &buf2, &tag ) ) ) return NULL;
      29    10359292 :   if( FD_UNLIKELY( tag>UINT_MAX ) ) return NULL;
      30    10359292 :   uint  wire_type = fd_pb_tag_wire_type( (uint)tag );
      31    10359292 :   uint  field_id  = fd_pb_tag_field_id ( (uint)tag );
      32    10359292 :   if( FD_UNLIKELY( !field_id ) ) return NULL;
      33    10359292 :   ulong val;
      34    10359292 :   switch( wire_type ) {
      35     4021448 :   case FD_PB_WIRE_TYPE_VARINT:
      36    11072752 :   case FD_PB_WIRE_TYPE_LEN:
      37    11072752 :     if( FD_UNLIKELY( !fd_pb_read_varint( &buf2, &val ) ) ) return NULL;
      38    11072752 :     break;
      39    11072752 :   case FD_PB_WIRE_TYPE_I64:
      40      540435 :     if( FD_UNLIKELY( (ulong)( buf2.end - buf2.cur )<8UL ) ) return NULL;
      41      540435 :     val       = FD_LOAD( ulong, buf2.cur );
      42      540435 :     buf2.cur += 8UL;
      43      540435 :     break;
      44          44 :   case FD_PB_WIRE_TYPE_I32:
      45          44 :     if( FD_UNLIKELY( (ulong)( buf2.end - buf2.cur )<4UL ) ) return NULL;
      46          44 :     val       = (ulong)FD_LOAD( uint, buf2.cur );
      47          44 :     buf2.cur += 4UL;
      48          44 :     break;
      49           0 :   default:
      50           0 :     return NULL;
      51    10359292 :   }
      52    10302670 :   *tlv = (fd_pb_tlv_t) {
      53    10302670 :     .wire_type = wire_type,
      54    10302670 :     .field_id  = field_id,
      55    10302670 :     .varint    = val
      56    10302670 :   };
      57    10302670 :   *buf = buf2;
      58    10302670 :   return tlv;
      59    10359292 : }
      60             : 
      61             : fd_pb_tlv_t *
      62             : fd_pb_tlv_read( fd_pb_inbuf_t * buf,
      63    10613017 :                 fd_pb_tlv_t *   tlv ) {
      64             :   /* FIXME plenty of optimization opportunities here */
      65    10613017 :   return fd_pb_tlv_read_slow( buf, tlv );
      66    10613017 : }

Generated by: LCOV version 1.14