diff --git a/bitcoin/Makefile b/bitcoin/Makefile index 9b204bc73..a0536df0b 100644 --- a/bitcoin/Makefile +++ b/bitcoin/Makefile @@ -9,7 +9,6 @@ BITCOIN_SRC := \ bitcoin/privkey.c \ bitcoin/psbt.c \ bitcoin/pubkey.c \ - bitcoin/pullpush.c \ bitcoin/script.c \ bitcoin/shadouble.c \ bitcoin/short_channel_id.c \ @@ -30,7 +29,6 @@ BITCOIN_HEADERS := bitcoin/address.h \ bitcoin/privkey.h \ bitcoin/psbt.h \ bitcoin/pubkey.h \ - bitcoin/pullpush.h \ bitcoin/script.h \ bitcoin/shadouble.h \ bitcoin/short_channel_id.h \ diff --git a/bitcoin/block.c b/bitcoin/block.c index 1951b0b1e..53df3e13e 100644 --- a/bitcoin/block.c +++ b/bitcoin/block.c @@ -1,11 +1,58 @@ +#include #include #include -#include #include +#include #include #include #include +/* Sets *cursor to NULL and returns NULL when a pull fails. */ +static const u8 *pull(const u8 **cursor, size_t *max, void *copy, size_t n) +{ + const u8 *p = *cursor; + + if (*max < n) { + *cursor = NULL; + *max = 0; + /* Just make sure we don't leak uninitialized mem! */ + if (copy) + memset(copy, 0, n); + return NULL; + } + *cursor += n; + *max -= n; + assert(p); + if (copy) + memcpy(copy, p, n); + return memcheck(p, n); +} + +static u32 pull_le32(const u8 **cursor, size_t *max) +{ + le32 ret; + + if (!pull(cursor, max, &ret, sizeof(ret))) + return 0; + return le32_to_cpu(ret); +} + +static u64 pull_varint(const u8 **cursor, size_t *max) +{ + u64 ret; + size_t len; + + len = varint_get(*cursor, *max, &ret); + if (len == 0) { + *cursor = NULL; + *max = 0; + return 0; + } + pull(cursor, max, NULL, len); + return ret; +} + + static void sha256_varint(struct sha256_ctx *ctx, u64 val) { u8 vt[VARINT_MAX_LEN]; diff --git a/bitcoin/pullpush.c b/bitcoin/pullpush.c deleted file mode 100644 index baa94d501..000000000 --- a/bitcoin/pullpush.c +++ /dev/null @@ -1,104 +0,0 @@ -#include "pullpush.h" -#include "varint.h" -#include -#include -#include - -void push_varint(varint_t v, - void (*push)(const void *, size_t, void *), void *pushp) -{ - u8 buf[VARINT_MAX_LEN]; - - push(buf, varint_put(buf, v), pushp); -} - -void push_le32(u32 v, - void (*push)(const void *, size_t, void *), void *pushp) -{ - le32 l = cpu_to_le32(v); - push(&l, sizeof(l), pushp); -} - -void push_le64(u64 v, - void (*push)(const void *, size_t, void *), void *pushp) -{ - le64 l = cpu_to_le64(v); - push(&l, sizeof(l), pushp); -} - -void push_amount_sat(struct amount_sat v, - void (*push)(const void *, size_t, void *), void *pushp) -{ - push_le64(v.satoshis, push, pushp); /* Raw: low-level helper */ -} - -void push_varint_blob(const tal_t *blob, - void (*push)(const void *, size_t, void *), - void *pushp) -{ - push_varint(tal_bytelen(blob), push, pushp); - push(blob, tal_bytelen(blob), pushp); -} - -void push(const void *data, size_t len, void *pptr_) -{ - u8 **pptr = pptr_; - size_t oldsize = tal_count(*pptr); - - tal_resize(pptr, oldsize + len); - memcpy(*pptr + oldsize, memcheck(data, len), len); -} - -/* Sets *cursor to NULL and returns NULL when a pull fails. */ -const u8 *pull(const u8 **cursor, size_t *max, void *copy, size_t n) -{ - const u8 *p = *cursor; - - if (*max < n) { - *cursor = NULL; - *max = 0; - /* Just make sure we don't leak uninitialized mem! */ - if (copy) - memset(copy, 0, n); - return NULL; - } - *cursor += n; - *max -= n; - assert(p); - if (copy) - memcpy(copy, p, n); - return memcheck(p, n); -} - -u64 pull_varint(const u8 **cursor, size_t *max) -{ - u64 ret; - size_t len; - - len = varint_get(*cursor, *max, &ret); - if (len == 0) { - *cursor = NULL; - *max = 0; - return 0; - } - pull(cursor, max, NULL, len); - return ret; -} - -u32 pull_le32(const u8 **cursor, size_t *max) -{ - le32 ret; - - if (!pull(cursor, max, &ret, sizeof(ret))) - return 0; - return le32_to_cpu(ret); -} - -u64 pull_le64(const u8 **cursor, size_t *max) -{ - le64 ret; - - if (!pull(cursor, max, &ret, sizeof(ret))) - return 0; - return le64_to_cpu(ret); -} diff --git a/bitcoin/pullpush.h b/bitcoin/pullpush.h deleted file mode 100644 index 268125aba..000000000 --- a/bitcoin/pullpush.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef LIGHTNING_BITCOIN_PULLPUSH_H -#define LIGHTNING_BITCOIN_PULLPUSH_H -#include "config.h" -#include "bitcoin/varint.h" -#include -#include - -void push_varint(varint_t v, - void (*push)(const void *, size_t, void *), void *pushp); -void push_le32(u32 v, void (*push)(const void *, size_t, void *), void *pushp); -void push_le64(u64 v, void (*push)(const void *, size_t, void *), void *pushp); -void push_amount_sat(struct amount_sat v, - void (*push)(const void *, size_t, void *), void *pushp); -void push_varint_blob(const tal_t *blob, - void (*push)(const void *, size_t, void *), - void *pushp); - -u64 pull_varint(const u8 **cursor, size_t *max); -u32 pull_le32(const u8 **cursor, size_t *max); -u64 pull_le64(const u8 **cursor, size_t *max); - -/* This extends **pptr by tal_resize */ -void push(const void *data, size_t len, void *pptr_); -const u8 *pull(const u8 **cursor, size_t *max, void *copy, size_t n); - -#endif /* LIGHTNING_BITCOIN_PULLPUSH_H */ diff --git a/bitcoin/test/run-bitcoin_block_from_hex.c b/bitcoin/test/run-bitcoin_block_from_hex.c index 52d13fa32..6bbcc1274 100644 --- a/bitcoin/test/run-bitcoin_block_from_hex.c +++ b/bitcoin/test/run-bitcoin_block_from_hex.c @@ -1,6 +1,5 @@ #include "../block.c" #include "../psbt.c" -#include "../pullpush.c" #include "../shadouble.c" #include "../tx.c" #include "../varint.c" diff --git a/bitcoin/test/run-tx-encode.c b/bitcoin/test/run-tx-encode.c index 30238ef7e..03de237ef 100644 --- a/bitcoin/test/run-tx-encode.c +++ b/bitcoin/test/run-tx-encode.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 2056bc195..4027c0d9b 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include diff --git a/plugins/Makefile b/plugins/Makefile index 39ac8efca..a95065f9f 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -22,7 +22,6 @@ PLUGIN_COMMON_OBJS := \ bitcoin/privkey.o \ bitcoin/psbt.o \ bitcoin/pubkey.o \ - bitcoin/pullpush.o \ bitcoin/script.o \ bitcoin/shadouble.o \ bitcoin/short_channel_id.o \