Line data Source code
1 : #ifndef HEADER_fd_src_ballet_pb_fd_pb_tokenize_h 2 : #define HEADER_fd_src_ballet_pb_fd_pb_tokenize_h 3 : 4 : /* fd_pb_tokenize.h provides an API to iterate over tokens in Protobuf 5 : wire format. It can be used to deserialize Protobuf. */ 6 : 7 : #include "fd_pb_wire.h" 8 : 9 : struct fd_pb_inbuf { 10 : uchar const * cur; 11 : uchar const * end; 12 : }; 13 : 14 : typedef struct fd_pb_inbuf fd_pb_inbuf_t; 15 : 16 : FD_PROTOTYPES_BEGIN 17 : 18 : static inline fd_pb_inbuf_t * 19 : fd_pb_inbuf_init( fd_pb_inbuf_t * buf, 20 : void const * data, 21 0 : ulong data_sz ) { 22 0 : buf->cur = data; 23 0 : buf->end = buf->cur + data_sz; 24 0 : return buf; 25 0 : } 26 : 27 : static inline ulong 28 0 : fd_pb_inbuf_sz( fd_pb_inbuf_t * buf ) { 29 0 : return (ulong)( buf->end - buf->cur ); 30 0 : } 31 : 32 : static inline void 33 : fd_pb_inbuf_skip( fd_pb_inbuf_t * buf, 34 0 : ulong sz ) { 35 0 : FD_CRIT( sz<=fd_pb_inbuf_sz( buf ), "Attempt to skip past end of buffer" ); 36 0 : buf->cur += sz; 37 0 : } 38 : 39 : FD_PROTOTYPES_END 40 : 41 : struct fd_pb_tlv { 42 : uint wire_type; /* FD_PB_WIRE_TYPE_* */ 43 : uint field_id; 44 : 45 : union { 46 : ulong varint; 47 : ulong i64; 48 : ulong len; 49 : uint i32; 50 : }; 51 : }; 52 : 53 : typedef struct fd_pb_tlv fd_pb_tlv_t; 54 : 55 : FD_PROTOTYPES_BEGIN 56 : 57 : /* fd_pb_read_tlv reads a Protobuf TLV. This includes a wire type, a 58 : field ID, and data. Data is either a scalar value field, or a length 59 : prefix. Populates *tlv, advances buf, and returns tlv on success. 60 : On failure, silently returns NULL without advancing buf. Reasons for 61 : failure include: parse failure, unexpected EOF. */ 62 : 63 : fd_pb_tlv_t * 64 : fd_pb_read_tlv( fd_pb_inbuf_t * buf, 65 : fd_pb_tlv_t * tlv ); 66 : 67 : FD_PROTOTYPES_END 68 : 69 : #endif /* HEADER_fd_src_ballet_pb_fd_pb_tokenize_h */