From f50af430e1128dbd2aa77e931aaa325e3694cb51 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 4 Jan 2017 14:09:21 +1030 Subject: [PATCH] wire: add bool routines. Signed-off-by: Rusty Russell --- tools/generate-wire.py | 4 +++- wire/fromwire.c | 11 +++++++++++ wire/towire.c | 6 ++++++ wire/wire.h | 2 ++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tools/generate-wire.py b/tools/generate-wire.py index 36f74554c..4692815c1 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -14,7 +14,7 @@ class FieldType(object): self.tsize = FieldType._typesize(name) def is_assignable(self): - return self.name == 'u8' or self.name == 'u16' or self.name == 'u32' or self.name == 'u64' + return self.name == 'u8' or self.name == 'u16' or self.name == 'u32' or self.name == 'u64' or self.name == 'bool' # Returns typename and base size @staticmethod @@ -39,6 +39,8 @@ class FieldType(object): return 2 elif typename == 'u8': return 1 + elif typename == 'bool': + return 1 else: raise ValueError('Unknown typename {}'.format(typename)) diff --git a/wire/fromwire.c b/wire/fromwire.c index 267828e6b..863f96c1c 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -76,6 +76,17 @@ u64 fromwire_u64(const u8 **cursor, size_t *max) return be64_to_cpu(ret); } +bool fromwire_bool(const u8 **cursor, size_t *max) +{ + u8 ret; + + if (!fromwire(cursor, max, &ret, sizeof(ret))) + return false; + if (ret != 0 && ret != 1) + fail_pull(cursor, max); + return ret; +} + void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey) { u8 der[PUBKEY_DER_LEN]; diff --git a/wire/towire.c b/wire/towire.c index 51b490642..4330e5da3 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -35,6 +35,12 @@ void towire_u64(u8 **pptr, u64 v) towire(pptr, &l, sizeof(l)); } +void towire_bool(u8 **pptr, bool v) +{ + u8 val = !!v; + towire(pptr, &val, sizeof(val)); +} + void towire_pubkey(u8 **pptr, const struct pubkey *pubkey) { u8 output[PUBKEY_DER_LEN]; diff --git a/wire/wire.h b/wire/wire.h index 65105f110..9e3c3bb21 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -32,6 +32,7 @@ void towire_u16(u8 **pptr, u16 v); void towire_u32(u8 **pptr, u32 v); void towire_u64(u8 **pptr, u64 v); void towire_pad(u8 **pptr, size_t num); +void towire_bool(u8 **pptr, bool v); void towire_u8_array(u8 **pptr, const u8 *arr, size_t num); void towire_signature_array(u8 **pptr, const struct signature *arr, size_t num); @@ -42,6 +43,7 @@ u8 fromwire_u8(const u8 **cursor, size_t *max); u16 fromwire_u16(const u8 **cursor, size_t *max); u32 fromwire_u32(const u8 **cursor, size_t *max); u64 fromwire_u64(const u8 **cursor, size_t *max); +bool fromwire_bool(const u8 **cursor, size_t *max); void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey); void fromwire_signature(const u8 **cursor, size_t *max, struct signature *signature);