mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 15:44:21 +01:00
wire: rename var_int to bigsize, and insist on minimal.
The new TLV spec uses BigSize, like Bitcoin's CompactInt but *little-endian*. So change our name for clarity, and insist that decoding be minimal as the spec requires. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -1057,7 +1057,7 @@ tlv__type_impl_fromwire_template = """static struct {tlv_name} *fromwire__{tlv_n
|
|||||||
|
|
||||||
\twhile (*plen) {{
|
\twhile (*plen) {{
|
||||||
\t\tmsg_type = fromwire_u8(p, plen);
|
\t\tmsg_type = fromwire_u8(p, plen);
|
||||||
\t\tmsg_len = fromwire_var_int(p, plen);
|
\t\tmsg_len = fromwire_bigsize(p, plen);
|
||||||
\t\tif (*plen < msg_len) {{
|
\t\tif (*plen < msg_len) {{
|
||||||
\t\t\tfromwire_fail(p, plen);
|
\t\t\tfromwire_fail(p, plen);
|
||||||
\t\t\tbreak;
|
\t\t\tbreak;
|
||||||
@@ -1103,7 +1103,7 @@ print_tlv_template = """static void printwire_{tlv_name}(const char *fieldname,
|
|||||||
|
|
||||||
\twhile (cursor) {{
|
\twhile (cursor) {{
|
||||||
\t\tmsg_type = fromwire_u8(&cursor, &plen);
|
\t\tmsg_type = fromwire_u8(&cursor, &plen);
|
||||||
\t\tmsg_size = fromwire_var_int(&cursor, &plen);
|
\t\tmsg_size = fromwire_bigsize(&cursor, &plen);
|
||||||
\t\tif (!cursor)
|
\t\tif (!cursor)
|
||||||
\t\t\tbreak;
|
\t\t\tbreak;
|
||||||
\t\tswitch ((enum {tlv_name}_type)msg_type) {{
|
\t\tswitch ((enum {tlv_name}_type)msg_type) {{
|
||||||
|
|||||||
@@ -102,20 +102,31 @@ bool fromwire_bool(const u8 **cursor, size_t *max)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 fromwire_var_int(const u8 **cursor, size_t *max)
|
u64 fromwire_bigsize(const u8 **cursor, size_t *max)
|
||||||
{
|
{
|
||||||
u8 flag = fromwire_u8(cursor, max);
|
u8 flag = fromwire_u8(cursor, max);
|
||||||
|
u64 ret;
|
||||||
|
|
||||||
switch(flag) {
|
switch(flag) {
|
||||||
case 0xff:
|
case 0xff:
|
||||||
return fromwire_u64(cursor, max);
|
ret = fromwire_u64(cursor, max);
|
||||||
|
if ((ret >> 32) == 0)
|
||||||
|
fromwire_fail(cursor, max);
|
||||||
|
break;
|
||||||
case 0xfe:
|
case 0xfe:
|
||||||
return (u64)fromwire_u32(cursor, max);
|
ret = fromwire_u32(cursor, max);
|
||||||
|
if ((ret >> 16) == 0)
|
||||||
|
fromwire_fail(cursor, max);
|
||||||
|
break;
|
||||||
case 0xfd:
|
case 0xfd:
|
||||||
return (u64)fromwire_u16(cursor, max);
|
ret = fromwire_u16(cursor, max);
|
||||||
|
if (ret < 0xfd)
|
||||||
|
fromwire_fail(cursor, max);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return (u64)flag;
|
ret = flag;
|
||||||
}
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)
|
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)
|
||||||
|
|||||||
@@ -55,16 +55,16 @@ void towire_bool(u8 **pptr, bool v)
|
|||||||
towire(pptr, &val, sizeof(val));
|
towire(pptr, &val, sizeof(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
void towire_var_int(u8 **pptr, const u64 val)
|
void towire_bigsize(u8 **pptr, const u64 val)
|
||||||
{
|
{
|
||||||
if (val < 0xfd) {
|
if (val < 0xfd) {
|
||||||
towire_u8(pptr, (u8)val);
|
towire_u8(pptr, val);
|
||||||
} else if (val <= 0xffff) {
|
} else if (val <= 0xffff) {
|
||||||
towire_u8(pptr, 0xfd);
|
towire_u8(pptr, 0xfd);
|
||||||
towire_u16(pptr, (u16)val);
|
towire_u16(pptr, val);
|
||||||
} else if (val <= 0xffffffff) {
|
} else if (val <= 0xffffffff) {
|
||||||
towire_u8(pptr, 0xfe);
|
towire_u8(pptr, 0xfe);
|
||||||
towire_u32(pptr, (u32)val);
|
towire_u32(pptr, val);
|
||||||
} else {
|
} else {
|
||||||
towire_u8(pptr, 0xff);
|
towire_u8(pptr, 0xff);
|
||||||
towire_u64(pptr, val);
|
towire_u64(pptr, val);
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ void towire_u64(u8 **pptr, u64 v);
|
|||||||
void towire_double(u8 **pptr, const double *v);
|
void towire_double(u8 **pptr, const double *v);
|
||||||
void towire_pad(u8 **pptr, size_t num);
|
void towire_pad(u8 **pptr, size_t num);
|
||||||
void towire_bool(u8 **pptr, bool v);
|
void towire_bool(u8 **pptr, bool v);
|
||||||
void towire_var_int(u8 **pptr, const u64 val);
|
void towire_bigsize(u8 **pptr, const u64 val);
|
||||||
|
|
||||||
void towire_u8_array(u8 **pptr, const u8 *arr, size_t num);
|
void towire_u8_array(u8 **pptr, const u8 *arr, size_t num);
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ u32 fromwire_u32(const u8 **cursor, size_t *max);
|
|||||||
u64 fromwire_u64(const u8 **cursor, size_t *max);
|
u64 fromwire_u64(const u8 **cursor, size_t *max);
|
||||||
void fromwire_double(const u8 **cursor, size_t *max, double *v);
|
void fromwire_double(const u8 **cursor, size_t *max, double *v);
|
||||||
bool fromwire_bool(const u8 **cursor, size_t *max);
|
bool fromwire_bool(const u8 **cursor, size_t *max);
|
||||||
u64 fromwire_var_int(const u8 **cursor, size_t *max);
|
u64 fromwire_bigsize(const u8 **cursor, size_t *max);
|
||||||
void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret);
|
void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret);
|
||||||
void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey);
|
void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey);
|
||||||
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);
|
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);
|
||||||
|
|||||||
Reference in New Issue
Block a user