Line data Source code
1 : #ifndef HEADER_fd_src_ballet_base58_fd_base58_h 2 : #define HEADER_fd_src_ballet_base58_fd_base58_h 3 : 4 : /* fd_base58.h provides methods for converting between binary and 5 : base58. */ 6 : 7 : #include "../fd_ballet_base.h" 8 : 9 : /* FD_BASE58_ENCODED_{32,64}_{LEN,SZ} give the maximum string length 10 : (LEN) and size (SZ, which includes the '\0') of the base58 cstrs that 11 : result from converting 32 or 64 bytes to base58. */ 12 : 13 17118 : #define FD_BASE58_ENCODED_32_LEN (44UL) /* Computed as ceil(log_58(256^32 - 1)) */ 14 0 : #define FD_BASE58_ENCODED_64_LEN (88UL) /* Computed as ceil(log_58(256^64 - 1)) */ 15 17118 : #define FD_BASE58_ENCODED_32_SZ (FD_BASE58_ENCODED_32_LEN+1UL) /* Including the nul terminator */ 16 0 : #define FD_BASE58_ENCODED_64_SZ (FD_BASE58_ENCODED_64_LEN+1UL) /* Including the nul terminator */ 17 : 18 : FD_PROTOTYPES_BEGIN 19 : 20 : /* Macros for initializing a correctly sized out char 21 : array to encode into. */ 22 : #define FD_BASE58_ENCODE_32_BYTES( bytes, out ) \ 23 162 : char out[ FD_BASE58_ENCODED_32_SZ ]; \ 24 162 : ulong out##_len; \ 25 162 : fd_base58_encode_32( bytes, &out##_len, out ); 26 : 27 : #define FD_BASE58_ENCODE_64_BYTES( bytes, out ) \ 28 : char out[ FD_BASE58_ENCODED_64_SZ ]; \ 29 : ulong out##_len; \ 30 : fd_base58_encode_64( bytes, &out##_len, out ); 31 : 32 : /* fd_base58_encode_{32, 64}: Interprets the supplied 32 or 64 bytes 33 : (respectively) as a large big-endian integer, and converts it to a 34 : nul-terminated base58 string of: 35 : 36 : 32 to 44 characters, inclusive (not counting nul) for 32 B 37 : 64 to 88 characters, inclusive (not counting nul) for 64 B 38 : 39 : Stores the output in the buffer pointed to by out. If opt_len is 40 : non-NULL, *opt_len == strlen( out ) on return. Returns out. out is 41 : guaranteed to be nul terminated on return. 42 : 43 : Out must have enough space for FD_BASE58_ENCODED_{32,64}_SZ 44 : characters, including the nul terminator. 45 : 46 : The 32 byte conversion is suitable for printing Solana account 47 : addresses, and the 64 byte conversion is suitable for printing Solana 48 : transaction signatures. This is high performance (~100ns for 32B and 49 : ~200ns for 64B without AVX, and roughly twice as fast with AVX), but 50 : base58 is an inherently slow format and should not be used in any 51 : performance critical places except where absolutely necessary. */ 52 : 53 : char * fd_base58_encode_32( uchar const * bytes, ulong * opt_len, char * out ); 54 : char * fd_base58_encode_64( uchar const * bytes, ulong * opt_len, char * out ); 55 : 56 : /* fd_base58_decode_{32, 64}: Converts the base58 encoded number stored 57 : in the cstr `encoded` to a 32 or 64 byte number, which is written to 58 : out in big endian. out must have room for 32 and 64 bytes respective 59 : on entry. Returns out on success and NULL if the input string is 60 : invalid in some way: illegal base58 character or decodes to something 61 : other than 32 or 64 bytes (respectively). The contents of out are 62 : undefined on failure (i.e. out may be clobbered). 63 : 64 : A similar note to the above applies: these are high performance 65 : (~120ns for 32 byte and ~300ns for 64 byte), but base58 is an 66 : inherently slow format and should not be used in any performance 67 : critical places except where absolutely necessary. */ 68 : 69 : uchar * fd_base58_decode_32( char const * encoded, uchar * out ); 70 : uchar * fd_base58_decode_64( char const * encoded, uchar * out ); 71 : 72 : FD_PROTOTYPES_END 73 : 74 : #endif /* HEADER_fd_src_ballet_base58_fd_base58_h */