diff --git a/bitcoin/varint.c b/bitcoin/varint.c index 15199b521..1e97cddd3 100644 --- a/bitcoin/varint.c +++ b/bitcoin/varint.c @@ -60,65 +60,3 @@ size_t varint_get(const u8 *p, size_t max, varint_t *val) return 1; } } - -size_t bigsize_put(u8 buf[VARINT_MAX_LEN], varint_t v) -{ - u8 *p = buf; - - if (v < 0xfd) { - *(p++) = v; - } else if (v <= 0xffff) { - (*p++) = 0xfd; - (*p++) = v >> 8; - (*p++) = v; - } else if (v <= 0xffffffff) { - (*p++) = 0xfe; - (*p++) = v >> 24; - (*p++) = v >> 16; - (*p++) = v >> 8; - (*p++) = v; - } else { - (*p++) = 0xff; - (*p++) = v >> 56; - (*p++) = v >> 48; - (*p++) = v >> 40; - (*p++) = v >> 32; - (*p++) = v >> 24; - (*p++) = v >> 16; - (*p++) = v >> 8; - (*p++) = v; - } - return p - buf; -} - -size_t bigsize_get(const u8 *p, size_t max, varint_t *val) -{ - if (max < 1) - return 0; - - switch (*p) { - case 0xfd: - if (max < 3) - return 0; - *val = ((u64)p[1] << 8) + p[2]; - return 3; - case 0xfe: - if (max < 5) - return 0; - *val = ((u64)p[1] << 24) + ((u64)p[2] << 16) - + ((u64)p[3] << 8) + p[4]; - return 5; - case 0xff: - if (max < 9) - return 0; - *val = ((u64)p[1] << 56) + ((u64)p[2] << 48) - + ((u64)p[3] << 40) + ((u64)p[4] << 32) - + ((u64)p[5] << 24) + ((u64)p[6] << 16) - + ((u64)p[7] << 8) + p[8]; - return 9; - default: - *val = *p; - return 1; - } - -} diff --git a/bitcoin/varint.h b/bitcoin/varint.h index 6d9003fd0..b0b21c106 100644 --- a/bitcoin/varint.h +++ b/bitcoin/varint.h @@ -15,12 +15,4 @@ size_t varint_put(u8 buf[VARINT_MAX_LEN], varint_t v); /* Returns bytes used: 0 if max_len too small. */ size_t varint_get(const u8 *p, size_t max_len, varint_t *val); - -/* Big-endian variant of varint_put, used in lightning */ -size_t bigsize_put(u8 buf[VARINT_MAX_LEN], varint_t v); - -/* Big-endian variant of varint_get, used in lightning */ -size_t bigsize_get(const u8 *p, size_t max, varint_t *val); - - #endif /* LIGHTNING_BITCOIN_VARINT_H */ diff --git a/channeld/Makefile b/channeld/Makefile index 57e516fa9..7e31a9d11 100644 --- a/channeld/Makefile +++ b/channeld/Makefile @@ -35,6 +35,7 @@ ALL_GEN_HEADERS += $(LIGHTNINGD_CHANNEL_HEADERS_GEN) CHANNELD_COMMON_OBJS := \ common/amount.o \ common/base32.o \ + common/bigsize.o \ common/bip32.o \ common/channel_config.o \ common/crypto_state.o \ diff --git a/common/Makefile b/common/Makefile index 232f92e77..4af7412b9 100644 --- a/common/Makefile +++ b/common/Makefile @@ -4,6 +4,7 @@ COMMON_SRC_NOGEN := \ common/base32.c \ common/bech32.c \ common/bech32_util.c \ + common/bigsize.c \ common/bip32.c \ common/bolt11.c \ common/channel_config.c \ diff --git a/common/bigsize.c b/common/bigsize.c new file mode 100644 index 000000000..31ac97ab2 --- /dev/null +++ b/common/bigsize.c @@ -0,0 +1,75 @@ +#include + +size_t bigsize_len(bigsize_t v) +{ + if (v < 0xfd) { + return 1; + } else if (v <= 0xffff) { + return 3; + } else if (v <= 0xffffffff) { + return 5; + } else { + return 9; + } +} + +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN], bigsize_t v) +{ + u8 *p = buf; + + if (v < 0xfd) { + *(p++) = v; + } else if (v <= 0xffff) { + (*p++) = 0xfd; + (*p++) = v >> 8; + (*p++) = v; + } else if (v <= 0xffffffff) { + (*p++) = 0xfe; + (*p++) = v >> 24; + (*p++) = v >> 16; + (*p++) = v >> 8; + (*p++) = v; + } else { + (*p++) = 0xff; + (*p++) = v >> 56; + (*p++) = v >> 48; + (*p++) = v >> 40; + (*p++) = v >> 32; + (*p++) = v >> 24; + (*p++) = v >> 16; + (*p++) = v >> 8; + (*p++) = v; + } + return p - buf; +} + +size_t bigsize_get(const u8 *p, size_t max, bigsize_t *val) +{ + if (max < 1) + return 0; + + switch (*p) { + case 0xfd: + if (max < 3) + return 0; + *val = ((u64)p[1] << 8) + p[2]; + return 3; + case 0xfe: + if (max < 5) + return 0; + *val = ((u64)p[1] << 24) + ((u64)p[2] << 16) + + ((u64)p[3] << 8) + p[4]; + return 5; + case 0xff: + if (max < 9) + return 0; + *val = ((u64)p[1] << 56) + ((u64)p[2] << 48) + + ((u64)p[3] << 40) + ((u64)p[4] << 32) + + ((u64)p[5] << 24) + ((u64)p[6] << 16) + + ((u64)p[7] << 8) + p[8]; + return 9; + default: + *val = *p; + return 1; + } +} diff --git a/common/bigsize.h b/common/bigsize.h new file mode 100644 index 000000000..1c582896f --- /dev/null +++ b/common/bigsize.h @@ -0,0 +1,21 @@ +#ifndef LIGHTNING_COMMON_BIGSIZE_H +#define LIGHTNING_COMMON_BIGSIZE_H +#include "config.h" +#include +#include + +/* typedef for clarity. */ +typedef u64 bigsize_t; + +#define BIGSIZE_MAX_LEN 9 + +/* Returns length of buf used. */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN], bigsize_t v); + +/* Returns 0 on failure, otherwise length (<= max) used. */ +size_t bigsize_get(const u8 *p, size_t max, bigsize_t *val); + +/* How many bytes does it take to encode v? */ +size_t bigsize_len(bigsize_t v); + +#endif /* LIGHTNING_COMMON_BIGSIZE_H */ diff --git a/common/sphinx.c b/common/sphinx.c index 913c0650e..af2f96a76 100644 --- a/common/sphinx.c +++ b/common/sphinx.c @@ -1,7 +1,5 @@ #include -#include - #include #include #include @@ -96,7 +94,7 @@ static size_t sphinx_hop_size(const struct sphinx_hop *hop) /* There is no point really in trying to serialize something that is * larger than the maximum length we can fit into the payload region - * anyway. 3 here is the maximum varint size that we allow. */ + * anyway. 3 here is the maximum bigsize size that we allow. */ assert(size < ROUTING_INFO_SIZE - 3 - HMAC_SIZE); /* Backwards compatibility: realm 0 is the legacy hop_data format and @@ -112,7 +110,7 @@ static size_t sphinx_hop_size(const struct sphinx_hop *hop) else vsize = 3; - /* The hop must accomodate the hop_payload, as well as the varint + /* The hop must accomodate the hop_payload, as well as the bigsize * describing the length and HMAC. */ return vsize + size + HMAC_SIZE; } @@ -471,7 +469,7 @@ static bool sphinx_write_frame(u8 *dest, const struct sphinx_hop *hop) static void sphinx_parse_payload(struct route_step *step, const u8 *src) { size_t hop_size, vsize; - varint_t raw_size; + bigsize_t raw_size; #if !EXPERIMENTAL_FEATURES if (src[0] != 0x00) { step->type = SPHINX_INVALID_PAYLOAD; @@ -588,7 +586,7 @@ struct route_step *process_onionpacket( u8 stream[NUM_STREAM_BYTES]; u8 paddedheader[2*ROUTING_INFO_SIZE]; size_t vsize; - varint_t shift_size; + bigsize_t shift_size; step->next = talz(step, struct onionpacket); step->next->version = msg->version; diff --git a/common/test/run-sphinx.c b/common/test/run-sphinx.c index 9d2256a20..4a4232d37 100644 --- a/common/test/run-sphinx.c +++ b/common/test/run-sphinx.c @@ -12,6 +12,12 @@ #include /* AUTOGENERATED MOCKS START */ +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } /* Generated stub for fromwire */ const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED) { fprintf(stderr, "fromwire called!\n"); abort(); } @@ -21,9 +27,6 @@ struct amount_msat fromwire_amount_msat(const u8 **cursor UNNEEDED, size_t *max /* Generated stub for fromwire_fail */ const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) { fprintf(stderr, "fromwire_fail called!\n"); abort(); } -/* Generated stub for fromwire_pad */ -void fromwire_pad(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED) -{ fprintf(stderr, "fromwire_pad called!\n"); abort(); } /* Generated stub for fromwire_short_channel_id */ void fromwire_short_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct short_channel_id *short_channel_id UNNEEDED) @@ -40,9 +43,6 @@ u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) /* Generated stub for towire */ void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED) { fprintf(stderr, "towire called!\n"); abort(); } -/* Generated stub for towire_amount_msat */ -void towire_amount_msat(u8 **pptr UNNEEDED, const struct amount_msat msat UNNEEDED) -{ fprintf(stderr, "towire_amount_msat called!\n"); abort(); } /* Generated stub for towire_pad */ void towire_pad(u8 **pptr UNNEEDED, size_t num UNNEEDED) { fprintf(stderr, "towire_pad called!\n"); abort(); } @@ -59,9 +59,6 @@ void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED) /* Generated stub for towire_u64 */ void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED) { fprintf(stderr, "towire_u64 called!\n"); abort(); } -/* Generated stub for towire_u8 */ -void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED) -{ fprintf(stderr, "towire_u8 called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ secp256k1_context *secp256k1_ctx; diff --git a/devtools/Makefile b/devtools/Makefile index 13c45c2aa..af0753c4d 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -11,6 +11,7 @@ DEVTOOLS_COMMON_OBJS := \ common/base32.o \ common/bech32.o \ common/bech32_util.o \ + common/bigsize.o \ common/bolt11.o \ common/crypto_state.o \ common/decode_short_channel_ids.o \ diff --git a/lightningd/Makefile b/lightningd/Makefile index ede14b7d6..25064c8de 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -19,6 +19,7 @@ LIGHTNINGD_COMMON_OBJS := \ common/base32.o \ common/bech32.o \ common/bech32_util.o \ + common/bigsize.o \ common/bip32.o \ common/bolt11.o \ common/channel_config.o \ diff --git a/wire/wire.h b/wire/wire.h index 8bc248c05..6a945869e 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -31,7 +32,7 @@ struct siphash_seed; /* Makes generate-wire.py work */ typedef char wirestring; -typedef u64 bigsize; +typedef bigsize_t bigsize; /* FIXME: Some versions of spec using 'varint' for bigsize' */ typedef bigsize varint;