Skip to content

Commit

Permalink
flamenco: add type utils
Browse files Browse the repository at this point in the history
  • Loading branch information
riptl authored and ripatel-fd committed Apr 15, 2024
1 parent e52a5df commit b0b667d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/flamenco/fd_flamenco_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ fd_acct_addr_cstr( char cstr[ static FD_BASE58_ENCODED_32_SZ ],
return fd_base58_encode_32( addr, NULL, cstr );
}

/* fd_pod utils */

FD_FN_UNUSED static fd_pubkey_t *
fd_pod_query_pubkey( uchar const * pod,
char const * path,
fd_pubkey_t * val ) {

ulong bufsz = 0UL;
void const * buf = fd_pod_query_buf( pod, path, &bufsz );

if( FD_UNLIKELY( (!buf) | (bufsz!=sizeof(fd_pubkey_t)) ) )
return NULL;

memcpy( val->uc, buf, sizeof(fd_pubkey_t) );
return val;
}

static inline ulong
fd_pod_insert_pubkey( uchar * pod,
char const * path,
fd_pubkey_t const * val ) {
return fd_pod_insert_buf( pod, path, val->uc, sizeof(fd_pubkey_t) );
}

FD_PROTOTYPES_END

#endif /* HEADER_fd_src_flamenco_fd_flamenco_base_h */
19 changes: 17 additions & 2 deletions src/flamenco/types/fd_bincode.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef HEADER_fd_src_util_encoders_fd_bincode_h
#define HEADER_fd_src_util_encoders_fd_bincode_h

#include "../../util/fd_util.h"
#include "../../util/valloc/fd_valloc.h"

typedef void
(* fd_types_walk_fn_t)( void * self,
Expand All @@ -20,6 +20,21 @@ struct fd_bincode_encode_ctx {
};
typedef struct fd_bincode_encode_ctx fd_bincode_encode_ctx_t;

FD_PROTOTYPES_BEGIN

static inline fd_bincode_encode_ctx_t
fd_bincode_encode_ctx( void * buf,
ulong sz ) {
return (fd_bincode_encode_ctx_t){ buf, (uchar *)buf + sz };
}

FD_FN_PURE static inline ulong
fd_bincode_encode_sz( fd_bincode_encode_ctx_t const * ctx ) {
return (ulong)( (uchar *)ctx->data - (uchar *)ctx->dataend );
}

FD_PROTOTYPES_END

/* Context argument used for decoding */
struct fd_bincode_decode_ctx {
/* Current position in data buffer */
Expand Down Expand Up @@ -76,7 +91,7 @@ typedef struct fd_bincode_destroy_ctx fd_bincode_destroy_ctx_t;
uchar * ptr = (uchar *)ctx->data; \
if ( FD_UNLIKELY((void *)(ptr + sizeof(type)) > ctx->dataend ) ) \
return FD_BINCODE_ERR_OVERFLOW; \
FD_STORE( type, ptr, self ); /* unaligned */ \
memcpy( ptr, &self, sizeof(type) ); /* unaligned */ \
ctx->data = ptr + sizeof(type); \
return FD_BINCODE_SUCCESS; \
}
Expand Down
1 change: 1 addition & 0 deletions src/flamenco/types/test_types_meta.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "fd_types_meta.h"
#include "../../util/fd_util.h"

int
main( int argc,
Expand Down
6 changes: 3 additions & 3 deletions src/util/pod/fd_pod.h
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ fd_pod_resize( uchar * pod,
is done such that the pod_max is reduced to be equal to pod_used and
the pod header is accordingly compacted (otherwise, the pod_max will
be unchanged on return).
Regardless of full, all subpods will be recursively fully compacted
and all cstrs in the pod will have had their padding removed (they
will be still be '\0' terminated if originally correctly '\0'
Expand Down Expand Up @@ -508,7 +508,7 @@ fd_pod_val_type_to_cstr( int val_type,
bytes), 0 on failure. Failure reasons include NULL pod, NULL path,
one of the path prefixes resolved to a non-subpod, path is already in
the pod, invalid val_type or no room in pod for val_sz.
If subpods along the path do not exist, they will be created in the
process.
Expand Down Expand Up @@ -655,7 +655,7 @@ fd_pod_insert_subpod( uchar * FD_RESTRICT pod,
static inline ulong
fd_pod_insert_buf( uchar * FD_RESTRICT pod,
char const * FD_RESTRICT path,
void * FD_RESTRICT val,
void const * FD_RESTRICT val,
ulong val_sz ) {
return fd_pod_insert( pod, path, FD_POD_VAL_TYPE_BUF, val_sz, val );
}
Expand Down

0 comments on commit b0b667d

Please sign in to comment.