wire: use common/bigsize routines

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2019-07-30 14:55:12 +09:30
parent 2241e25cb4
commit cc70b9c4ec
20 changed files with 83 additions and 43 deletions

View File

@@ -163,37 +163,18 @@ bool fromwire_bool(const u8 **cursor, size_t *max)
return ret;
}
u64 fromwire_bigsize(const u8 **cursor, size_t *max)
bigsize_t fromwire_bigsize(const u8 **cursor, size_t *max)
{
u8 flag = fromwire_u8(cursor, max);
u64 ret;
bigsize_t v;
size_t len = bigsize_get(*cursor, *max, &v);
switch(flag) {
case 0xff:
ret = fromwire_u64(cursor, max);
if ((ret >> 32) == 0) {
SUPERVERBOSE("not minimal encoded");
fromwire_fail(cursor, max);
}
break;
case 0xfe:
ret = fromwire_u32(cursor, max);
if ((ret >> 16) == 0) {
SUPERVERBOSE("not minimal encoded");
fromwire_fail(cursor, max);
}
break;
case 0xfd:
ret = fromwire_u16(cursor, max);
if (ret < 0xfd) {
SUPERVERBOSE("not minimal encoded");
fromwire_fail(cursor, max);
}
break;
default:
ret = flag;
if (len == 0) {
fromwire_fail(cursor, max);
return 0;
}
return ret;
assert(len <= *max);
fromwire(cursor, max, NULL, len);
return v;
}
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)

View File

@@ -12,6 +12,12 @@
secp256k1_context *secp256k1_ctx;
/* 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(); }
/* AUTOGENERATED MOCKS END */
/* memsetting pubkeys doesn't work */

View File

@@ -1,9 +1,13 @@
#include <ccan/array_size/array_size.h>
#include <ccan/str/hex/hex.h>
#include <common/utils.h>
#include <stdio.h>
#include <wally_core.h>
#include <common/bigsize.c>
static const char *reason;
#undef SUPERVERBOSE
#define SUPERVERBOSE(r) do { reason = (r); } while(0)
#include <wire/tlvstream.c>

View File

@@ -87,20 +87,13 @@ void towire_bool(u8 **pptr, bool v)
towire(pptr, &val, sizeof(val));
}
void towire_bigsize(u8 **pptr, const u64 val)
void towire_bigsize(u8 **pptr, const bigsize_t val)
{
if (val < 0xfd) {
towire_u8(pptr, val);
} else if (val <= 0xffff) {
towire_u8(pptr, 0xfd);
towire_u16(pptr, val);
} else if (val <= 0xffffffff) {
towire_u8(pptr, 0xfe);
towire_u32(pptr, val);
} else {
towire_u8(pptr, 0xff);
towire_u64(pptr, val);
}
u8 buf[BIGSIZE_MAX_LEN];
size_t len;
len = bigsize_put(buf, val);
towire(pptr, buf, len);
}
void towire_pubkey(u8 **pptr, const struct pubkey *pubkey)

View File

@@ -79,7 +79,7 @@ void towire_tu64(u8 **pptr, u64 v);
void towire_double(u8 **pptr, const double *v);
void towire_pad(u8 **pptr, size_t num);
void towire_bool(u8 **pptr, bool v);
void towire_bigsize(u8 **pptr, const u64 val);
void towire_bigsize(u8 **pptr, const bigsize_t val);
void towire_u8_array(u8 **pptr, const u8 *arr, size_t num);
@@ -99,7 +99,7 @@ u32 fromwire_tu32(const u8 **cursor, size_t *max);
u64 fromwire_tu64(const u8 **cursor, size_t *max);
void fromwire_double(const u8 **cursor, size_t *max, double *v);
bool fromwire_bool(const u8 **cursor, size_t *max);
u64 fromwire_bigsize(const u8 **cursor, size_t *max);
bigsize_t fromwire_bigsize(const u8 **cursor, size_t *max);
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_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);