wire: add var_int parsing functions

so we can put and pull bitcoin 'var_int' length types from the
wire.

for more info on variable integers, see http://learnmeabitcoin.com/glossary/varint
This commit is contained in:
lisa neigut
2019-04-02 17:18:06 -07:00
committed by Rusty Russell
parent 1213f44071
commit 74ae9f09ac
3 changed files with 39 additions and 0 deletions

View File

@@ -101,6 +101,27 @@ bool fromwire_bool(const u8 **cursor, size_t *max)
return ret;
}
u64 fromwire_var_int(const u8 **cursor, size_t *max)
{
if (*max < 1) {
fromwire_fail(cursor, max);
return 0;
}
u8 flag = fromwire_u8(cursor, max);
switch(flag) {
case 0xff:
return fromwire_u64(cursor, max);
case 0xfe:
return (u64)fromwire_u32(cursor, max);
case 0xfd:
return (u64)fromwire_u16(cursor, max);
default:
return (u64)flag;
}
}
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)
{
u8 der[PUBKEY_DER_LEN];