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

@@ -54,6 +54,22 @@ void towire_bool(u8 **pptr, bool v)
towire(pptr, &val, sizeof(val));
}
void towire_var_int(u8 **pptr, const u64 val)
{
if (val < 0xfd) {
towire_u8(pptr, (u8)val);
} else if (val <= 0xffff) {
towire_u8(pptr, 0xfd);
towire_u16(pptr, (u16)val);
} else if (val <= 0xffffffff) {
towire_u8(pptr, 0xfe);
towire_u32(pptr, (u32)val);
} else {
towire_u8(pptr, 0xff);
towire_u64(pptr, val);
}
}
void towire_pubkey(u8 **pptr, const struct pubkey *pubkey)
{
u8 output[PUBKEY_DER_LEN];