diff --git a/Makefile b/Makefile index 87132fda4..3e77ae70a 100644 --- a/Makefile +++ b/Makefile @@ -94,6 +94,7 @@ CCAN_HEADERS := \ $(CCANDIR)/ccan/check_type/check_type.h \ $(CCANDIR)/ccan/compiler/compiler.h \ $(CCANDIR)/ccan/container_of/container_of.h \ + $(CCANDIR)/ccan/cppmagic/cppmagic.h \ $(CCANDIR)/ccan/crypto/ripemd160/ripemd160.h \ $(CCANDIR)/ccan/crypto/sha256/sha256.h \ $(CCANDIR)/ccan/crypto/shachain/shachain.h \ diff --git a/ccan/README b/ccan/README index 980e933a8..f93123766 100644 --- a/ccan/README +++ b/ccan/README @@ -1,3 +1,3 @@ CCAN imported from http://ccodearchive.net. -CCAN version: init-2207-ge0b86f0 +CCAN version: init-2247-g5e37a0f diff --git a/ccan/ccan/cast/cast.h b/ccan/ccan/cast/cast.h index 1f3a7aac1..cd384c463 100644 --- a/ccan/ccan/cast/cast.h +++ b/ccan/ccan/cast/cast.h @@ -38,8 +38,8 @@ * { * size_t i; * for (i = 0; i < strlen(haystack); i++) - * if (memcmp("needle", haystack+i, strlen("needle")) == 0) - * return cast_const(char *, haystack+i); + * if (memcmp("needle", haystack+i, strlen("needle")) == 0) + * return cast_const(char *, haystack+i); * return NULL; * } */ diff --git a/ccan/ccan/cppmagic/LICENSE b/ccan/ccan/cppmagic/LICENSE new file mode 120000 index 000000000..2354d1294 --- /dev/null +++ b/ccan/ccan/cppmagic/LICENSE @@ -0,0 +1 @@ +../../licenses/BSD-MIT \ No newline at end of file diff --git a/ccan/ccan/cppmagic/_info b/ccan/ccan/cppmagic/_info new file mode 100644 index 000000000..aad394b9b --- /dev/null +++ b/ccan/ccan/cppmagic/_info @@ -0,0 +1,30 @@ +#include "config.h" +#include +#include + +/** + * cppmagic - Abuse of the C preprocessor + * + * This contains a bunch of fancy macro techniques such as + * preprocessor-time evaluated conditionals and (quasi) recursion and + * iteration. + * + * It's based on these articles: + * - http://jhnet.co.uk/articles/cpp_magic + * - https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms + * and code from the Boost C++ library. + * + * License: BSD-MIT + */ +int main(int argc, char *argv[]) +{ + /* Expect exactly one argument */ + if (argc != 2) + return 1; + + if (strcmp(argv[1], "depends") == 0) { + return 0; + } + + return 1; +} diff --git a/ccan/ccan/cppmagic/cppmagic.h b/ccan/ccan/cppmagic/cppmagic.h new file mode 100644 index 000000000..f1f6868e5 --- /dev/null +++ b/ccan/ccan/cppmagic/cppmagic.h @@ -0,0 +1,191 @@ +/* MIT (BSD) license - see LICENSE file for details */ +#ifndef CCAN_CPPMAGIC_H +#define CCAN_CPPMAGIC_H + +/** + * CPPMAGIC_NOTHING - expands to nothing + */ +#define CPPMAGIC_NOTHING() + +/** + * CPPMAGIC_STRINGIFY - convert arguments to a string literal + */ +#define _CPPMAGIC_STRINGIFY(...) #__VA_ARGS__ +#define CPPMAGIC_STRINGIFY(...) _CPPMAGIC_STRINGIFY(__VA_ARGS__) + +/** + * CPPMAGIC_GLUE2 - glue arguments together + * + * CPPMAGIC_GLUE2(@a_, @b_) + * expands to the expansion of @a_ followed immediately + * (combining tokens) by the expansion of @b_ + */ +#define _CPPMAGIC_GLUE2(a_, b_) a_##b_ +#define CPPMAGIC_GLUE2(a_, b_) _CPPMAGIC_GLUE2(a_, b_) + +/** + * CPPMAGIC_1ST - return 1st argument + * + * CPPMAGIC_1ST(@a_, ...) + * expands to the expansion of @a_ + */ +#define CPPMAGIC_1ST(a_, ...) a_ + +/** + * CPPMAGIC_2ND - return 2nd argument + * + * CPPMAGIC_2ST(@a_, @b_, ...) + * expands to the expansion of @b_ + */ +#define CPPMAGIC_2ND(a_, b_, ...) b_ + +/** + * CPPMAGIC_ISZERO - is argument '0' + * + * CPPMAGIC_ISZERO(@a) + * expands to '1' if @a is '0', otherwise expands to '0'. + */ +#define _CPPMAGIC_ISPROBE(...) CPPMAGIC_2ND(__VA_ARGS__, 0) +#define _CPPMAGIC_PROBE() $, 1 +#define _CPPMAGIC_ISZERO_0 _CPPMAGIC_PROBE() +#define CPPMAGIC_ISZERO(a_) \ + _CPPMAGIC_ISPROBE(CPPMAGIC_GLUE2(_CPPMAGIC_ISZERO_, a_)) + +/** + * CPPMAGIC_NONZERO - is argument not '0' + * + * CPPMAGIC_NONZERO(@a) + * expands to '0' if @a is '0', otherwise expands to '1'. + */ +#define CPPMAGIC_NONZERO(a_) CPPMAGIC_ISZERO(CPPMAGIC_ISZERO(a_)) + +/** + * CPPMAGIC_NONEMPTY - does the macro have any arguments? + * + * CPPMAGIC_NONEMPTY() + * expands to '0' + * CPPMAGIC_NONEMPTY(@a) + * CPPMAGIC_NONEMPTY(@a, ...) + * expand to '1' + */ +#define _CPPMAGIC_EOA() 0 +#define CPPMAGIC_NONEMPTY(...) \ + CPPMAGIC_NONZERO(CPPMAGIC_1ST(_CPPMAGIC_EOA __VA_ARGS__)()) + +/** + * CPPMAGIC_ISEMPTY - does the macro have no arguments? + * + * CPPMAGIC_ISEMPTY() + * expands to '1' + * CPPMAGIC_ISEMPTY(@a) + * CPPMAGIC_ISEMPTY(@a, ...) + * expand to '0' + */ +#define CPPMAGIC_ISEMPTY(...) \ + CPPMAGIC_ISZERO(CPPMAGIC_NONEMPTY(__VA_ARGS__)) + +/* + * CPPMAGIC_IFELSE - preprocessor conditional + * + * CPPMAGIC_IFELSE(@cond)(@if)(@else) + * expands to @else if @cond is '0', otherwise expands to @if + */ +#define _CPPMAGIC_IF_0(...) _CPPMAGIC_IF_0_ELSE +#define _CPPMAGIC_IF_1(...) __VA_ARGS__ _CPPMAGIC_IF_1_ELSE +#define _CPPMAGIC_IF_0_ELSE(...) __VA_ARGS__ +#define _CPPMAGIC_IF_1_ELSE(...) +#define _CPPMAGIC_IFELSE(cond_) CPPMAGIC_GLUE2(_CPPMAGIC_IF_, cond_) +#define CPPMAGIC_IFELSE(cond_) \ + _CPPMAGIC_IFELSE(CPPMAGIC_NONZERO(cond_)) + +/** + * CPPMAGIC_EVAL - force multiple expansion passes + * + * Forces macros in the arguments to be expanded repeatedly (up to + * 1024 times) even when CPP would usually stop expanding. + */ +#define CPPMAGIC_EVAL1(...) __VA_ARGS__ +#define CPPMAGIC_EVAL2(...) \ + CPPMAGIC_EVAL1(CPPMAGIC_EVAL1(__VA_ARGS__)) +#define CPPMAGIC_EVAL4(...) \ + CPPMAGIC_EVAL2(CPPMAGIC_EVAL2(__VA_ARGS__)) +#define CPPMAGIC_EVAL8(...) \ + CPPMAGIC_EVAL4(CPPMAGIC_EVAL4(__VA_ARGS__)) +#define CPPMAGIC_EVAL16(...) \ + CPPMAGIC_EVAL8(CPPMAGIC_EVAL8(__VA_ARGS__)) +#define CPPMAGIC_EVAL32(...) \ + CPPMAGIC_EVAL16(CPPMAGIC_EVAL16(__VA_ARGS__)) +#define CPPMAGIC_EVAL64(...) \ + CPPMAGIC_EVAL32(CPPMAGIC_EVAL32(__VA_ARGS__)) +#define CPPMAGIC_EVAL128(...) \ + CPPMAGIC_EVAL64(CPPMAGIC_EVAL64(__VA_ARGS__)) +#define CPPMAGIC_EVAL256(...) \ + CPPMAGIC_EVAL128(CPPMAGIC_EVAL128(__VA_ARGS__)) +#define CPPMAGIC_EVAL512(...) \ + CPPMAGIC_EVAL256(CPPMAGIC_EVAL256(__VA_ARGS__)) +#define CPPMAGIC_EVAL1024(...) \ + CPPMAGIC_EVAL512(CPPMAGIC_EVAL512(__VA_ARGS__)) +#define CPPMAGIC_EVAL(...) CPPMAGIC_EVAL1024(__VA_ARGS__) + +/** + * CPPMAGIC_DEFER1, CPPMAGIC_DEFER2 - defer expansion + */ +#define CPPMAGIC_DEFER1(a_) a_ CPPMAGIC_NOTHING() +#define CPPMAGIC_DEFER2(a_) a_ CPPMAGIC_NOTHING CPPMAGIC_NOTHING()() + +/** + * CPPMAGIC_MAP - iterate another macro across arguments + * @m: name of a one argument macro + * + * CPPMAGIC_MAP(@m, @a1, @a2, ... @an) + * expands to the expansion of @m(@a1) , @m(@a2) , ... , @m(@an) + */ +#define _CPPMAGIC_MAP_() _CPPMAGIC_MAP +#define _CPPMAGIC_MAP(m_, a_, ...) \ + m_(a_) \ + CPPMAGIC_IFELSE(CPPMAGIC_NONEMPTY(__VA_ARGS__)) \ + (, CPPMAGIC_DEFER2(_CPPMAGIC_MAP_)()(m_, __VA_ARGS__)) \ + () +#define CPPMAGIC_MAP(m_, ...) \ + CPPMAGIC_IFELSE(CPPMAGIC_NONEMPTY(__VA_ARGS__)) \ + (CPPMAGIC_EVAL(_CPPMAGIC_MAP(m_, __VA_ARGS__))) \ + () + +/** + * CPPMAGIC_2MAP - iterate another macro across pairs of arguments + * @m: name of a two argument macro + * + * CPPMAGIC_2MAP(@m, @a1, @b1, @a2, @b2, ..., @an, @bn) + * expands to the expansion of + * @m(@a1, @b1) , @m(@a2, @b2) , ... , @m(@an, @bn) + */ +#define _CPPMAGIC_2MAP_() _CPPMAGIC_2MAP +#define _CPPMAGIC_2MAP(m_, a_, b_, ...) \ + m_(a_, b_) \ + CPPMAGIC_IFELSE(CPPMAGIC_NONEMPTY(__VA_ARGS__)) \ + (, CPPMAGIC_DEFER2(_CPPMAGIC_2MAP_)()(m_, __VA_ARGS__)) \ + () +#define CPPMAGIC_2MAP(m_, ...) \ + CPPMAGIC_IFELSE(CPPMAGIC_NONEMPTY(__VA_ARGS__)) \ + (CPPMAGIC_EVAL(_CPPMAGIC_2MAP(m_, __VA_ARGS__))) \ + () + +/** + * CPPMAGIC_JOIN - separate arguments with given delimiter + * @d: delimiter + * + * CPPMAGIC_JOIN(@d, @a1, @a2, ..., @an) + * expands to the expansion of @a1 @d @a2 @d ... @d @an + */ +#define _CPPMAGIC_JOIN_() _CPPMAGIC_JOIN +#define _CPPMAGIC_JOIN(d_, a_, ...) \ + a_ \ + CPPMAGIC_IFELSE(CPPMAGIC_NONEMPTY(__VA_ARGS__)) \ + (d_ CPPMAGIC_DEFER2(_CPPMAGIC_JOIN_)()(d_, __VA_ARGS__)) \ + () +#define CPPMAGIC_JOIN(d_, ...) \ + CPPMAGIC_IFELSE(CPPMAGIC_NONEMPTY(__VA_ARGS__)) \ + (CPPMAGIC_EVAL(_CPPMAGIC_JOIN(d_, __VA_ARGS__))) \ + () + +#endif /* CCAN_CPPMAGIC_H */ diff --git a/ccan/ccan/cppmagic/test/run.c b/ccan/ccan/cppmagic/test/run.c new file mode 100644 index 000000000..ee02c6c8d --- /dev/null +++ b/ccan/ccan/cppmagic/test/run.c @@ -0,0 +1,92 @@ +#include "config.h" + +#include + +#include +#include + +static inline void check1(const char *orig, const char *expand, + const char *match) +{ + ok(strcmp(expand, match) == 0, + "%s => %s : %s", orig, expand, match); +} + +#define CHECK1(orig, match) \ + check1(#orig, CPPMAGIC_STRINGIFY(orig), match) + +#define TESTRECURSE() R CPPMAGIC_DEFER1(_TESTRECURSE)()() +#define _TESTRECURSE() TESTRECURSE + +#define TESTMAP1(x) <> + +#define TESTMAP2(x) [[ x +#define TESTMAP3(x) x ]] + +#define TEST2MAP(x, y) x ** y + +int main(void) +{ + plan_tests(42); + + CHECK1(CPPMAGIC_NOTHING(), ""); + CHECK1(CPPMAGIC_GLUE2(a, b), "ab"); + + CHECK1(CPPMAGIC_1ST(a), "a"); + CHECK1(CPPMAGIC_1ST(a, b), "a"); + CHECK1(CPPMAGIC_1ST(a, b, c), "a"); + + CHECK1(CPPMAGIC_2ND(a, b), "b"); + CHECK1(CPPMAGIC_2ND(a, b, c), "b"); + + CHECK1(CPPMAGIC_ISZERO(0), "1"); + CHECK1(CPPMAGIC_ISZERO(1), "0"); + CHECK1(CPPMAGIC_ISZERO(123), "0"); + CHECK1(CPPMAGIC_ISZERO(abc), "0"); + + CHECK1(CPPMAGIC_NONZERO(0), "0"); + CHECK1(CPPMAGIC_NONZERO(1), "1"); + CHECK1(CPPMAGIC_NONZERO(123), "1"); + CHECK1(CPPMAGIC_NONZERO(abc), "1"); + + CHECK1(CPPMAGIC_NONEMPTY(), "0"); + CHECK1(CPPMAGIC_NONEMPTY(0), "1"); + CHECK1(CPPMAGIC_NONEMPTY(a, b, c), "1"); + + CHECK1(CPPMAGIC_ISEMPTY(), "1"); + CHECK1(CPPMAGIC_ISEMPTY(0), "0"); + CHECK1(CPPMAGIC_ISEMPTY(a, b, c), "0"); + + CHECK1(CPPMAGIC_IFELSE(0)(abc)(def), "def"); + CHECK1(CPPMAGIC_IFELSE(1)(abc)(def), "abc"); + CHECK1(CPPMAGIC_IFELSE(not zero)(abc)(def), "abc"); + + CHECK1(TESTRECURSE(), "R R _TESTRECURSE ()()"); + CHECK1(CPPMAGIC_EVAL1(TESTRECURSE()), "R R R _TESTRECURSE ()()"); + CHECK1(CPPMAGIC_EVAL2(TESTRECURSE()), "R R R R R _TESTRECURSE ()()"); + + CHECK1(CPPMAGIC_MAP(TESTMAP1), ""); + CHECK1(CPPMAGIC_MAP(TESTMAP1, a), "<>"); + CHECK1(CPPMAGIC_MAP(TESTMAP1, a, b), "<> , <>"); + CHECK1(CPPMAGIC_MAP(TESTMAP1, a, b, c), "<> , <> , <>"); + + CHECK1(CPPMAGIC_2MAP(TEST2MAP), ""); + CHECK1(CPPMAGIC_2MAP(TEST2MAP, a, 1), "a ** 1"); + CHECK1(CPPMAGIC_2MAP(TEST2MAP, a, 1, b, 2), "a ** 1 , b ** 2"); + + CHECK1(CPPMAGIC_JOIN(;), ""); + CHECK1(CPPMAGIC_JOIN(;, a), "a"); + CHECK1(CPPMAGIC_JOIN(;, a, b), "a ; b"); + CHECK1(CPPMAGIC_JOIN(;, a, b, c), "a ; b ; c"); + + /* Check chaining of MAPs */ + CHECK1(CPPMAGIC_MAP(TESTMAP2, CPPMAGIC_MAP(TESTMAP3)), ""); + CHECK1(CPPMAGIC_MAP(TESTMAP2, CPPMAGIC_MAP(TESTMAP3, a)), "[[ a ]]"); + CHECK1(CPPMAGIC_MAP(TESTMAP2, CPPMAGIC_MAP(TESTMAP3, a, b)), + "[[ a ]] , [[ b ]]"); + CHECK1(CPPMAGIC_MAP(TESTMAP2, CPPMAGIC_MAP(TESTMAP3, a, b, c)), + "[[ a ]] , [[ b ]] , [[ c ]]"); + + /* This exits depending on whether all tests passed */ + return exit_status(); +} diff --git a/ccan/ccan/crypto/ripemd160/_info b/ccan/ccan/crypto/ripemd160/_info index 4df7d4d28..db37b681b 100644 --- a/ccan/ccan/crypto/ripemd160/_info +++ b/ccan/ccan/crypto/ripemd160/_info @@ -40,6 +40,7 @@ int main(int argc, char *argv[]) return 1; if (strcmp(argv[1], "depends") == 0) { + printf("ccan/compiler\n"); printf("ccan/endian\n"); return 0; } diff --git a/ccan/ccan/crypto/ripemd160/ripemd160.c b/ccan/ccan/crypto/ripemd160/ripemd160.c index 46a6c950e..cdd0266d7 100644 --- a/ccan/ccan/crypto/ripemd160/ripemd160.c +++ b/ccan/ccan/crypto/ripemd160/ripemd160.c @@ -8,6 +8,7 @@ */ #include #include +#include #include #include #include @@ -21,7 +22,7 @@ static void invalidate_ripemd160(struct ripemd160_ctx *ctx) #endif } -static void check_ripemd160(struct ripemd160_ctx *ctx) +static void check_ripemd160(struct ripemd160_ctx *ctx UNUSED) { #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL assert(ctx->c.num != -1U); @@ -54,19 +55,9 @@ static uint32_t inline f3(uint32_t x, uint32_t y, uint32_t z) { return (x | ~y) static uint32_t inline f4(uint32_t x, uint32_t y, uint32_t z) { return (x & z) | (y & ~z); } static uint32_t inline f5(uint32_t x, uint32_t y, uint32_t z) { return x ^ (y | ~z); } -/** Initialize RIPEMD-160 state. */ -static void inline Initialize(uint32_t* s) -{ - s[0] = 0x67452301ul; - s[1] = 0xEFCDAB89ul; - s[2] = 0x98BADCFEul; - s[3] = 0x10325476ul; - s[4] = 0xC3D2E1F0ul; -} - static uint32_t inline rol(uint32_t x, int i) { return (x << i) | (x >> (32 - i)); } -static void inline Round(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r) +static void inline Round(uint32_t *a, uint32_t b UNUSED, uint32_t *c, uint32_t d UNUSED, uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r) { *a = rol(*a + f + x + k, r) + e; *c = rol(*c, 10); @@ -93,6 +84,7 @@ static void Transform(uint32_t *s, const uint32_t *chunk) uint32_t w4 = le32_to_cpu(chunk[4]), w5 = le32_to_cpu(chunk[5]), w6 = le32_to_cpu(chunk[6]), w7 = le32_to_cpu(chunk[7]); uint32_t w8 = le32_to_cpu(chunk[8]), w9 = le32_to_cpu(chunk[9]), w10 = le32_to_cpu(chunk[10]), w11 = le32_to_cpu(chunk[11]); uint32_t w12 = le32_to_cpu(chunk[12]), w13 = le32_to_cpu(chunk[13]), w14 = le32_to_cpu(chunk[14]), w15 = le32_to_cpu(chunk[15]); + uint32_t t; R11(&a1, b1, &c1, d1, e1, w0, 11); R12(&a2, b2, &c2, d2, e2, w5, 8); @@ -259,7 +251,7 @@ static void Transform(uint32_t *s, const uint32_t *chunk) R51(&b1, c1, &d1, e1, a1, w13, 6); R52(&b2, c2, &d2, e2, a2, w11, 11); - uint32_t t = s[0]; + t = s[0]; s[0] = s[1] + c1 + d2; s[1] = s[2] + d1 + e2; s[2] = s[3] + e1 + a2; @@ -267,7 +259,7 @@ static void Transform(uint32_t *s, const uint32_t *chunk) s[4] = t + b1 + c2; } -static bool alignment_ok(const void *p, size_t n) +static bool alignment_ok(const void *p UNUSED, size_t n UNUSED) { #if HAVE_UNALIGNED_ACCESS return true; @@ -282,7 +274,7 @@ static void add(struct ripemd160_ctx *ctx, const void *p, size_t len) size_t bufsize = ctx->bytes % 64; if (bufsize + len >= 64) { - // Fill the buffer, and process it. + /* Fill the buffer, and process it. */ memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize); ctx->bytes += 64 - bufsize; data += 64 - bufsize; @@ -292,7 +284,7 @@ static void add(struct ripemd160_ctx *ctx, const void *p, size_t len) } while (len >= 64) { - // Process full chunks directly from the source. + /* Process full chunks directly from the source. */ if (alignment_ok(data, sizeof(uint32_t))) Transform(ctx->s, (const uint32_t *)data); else { @@ -305,7 +297,7 @@ static void add(struct ripemd160_ctx *ctx, const void *p, size_t len) } if (len) { - // Fill the buffer with what remains. + /* Fill the buffer with what remains. */ memcpy(ctx->buf.u8 + bufsize, data, len); ctx->bytes += len; } @@ -340,13 +332,13 @@ void ripemd160_done(struct ripemd160_ctx *ctx, struct ripemd160 *res) } #endif -void ripemd160(struct ripemd160 *sha, const void *p, size_t size) +void ripemd160(struct ripemd160 *ripemd, const void *p, size_t size) { struct ripemd160_ctx ctx; ripemd160_init(&ctx); ripemd160_update(&ctx, p, size); - ripemd160_done(&ctx, sha); + ripemd160_done(&ctx, ripemd); } void ripemd160_u8(struct ripemd160_ctx *ctx, uint8_t v) diff --git a/ccan/ccan/crypto/ripemd160/ripemd160.h b/ccan/ccan/crypto/ripemd160/ripemd160.h index e29e42bb0..377a07df4 100644 --- a/ccan/ccan/crypto/ripemd160/ripemd160.h +++ b/ccan/ccan/crypto/ripemd160/ripemd160.h @@ -6,7 +6,7 @@ #include /* Uncomment this to use openssl's RIPEMD160 routines (and link with -lcrypto) */ -//#define CCAN_CRYPTO_RIPEMD160_USE_OPENSSL 1 +/*#define CCAN_CRYPTO_RIPEMD160_USE_OPENSSL 1*/ #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL #include @@ -37,7 +37,7 @@ struct ripemd160 { * The bytes pointed to by @p is RIPEMD160 hashed into @ripemd160. This is * equivalent to ripemd160_init(), ripemd160_update() then ripemd160_done(). */ -void ripemd160(struct ripemd160 *sha, const void *p, size_t size); +void ripemd160(struct ripemd160 *ripemd, const void *p, size_t size); /** * struct ripemd160_ctx - structure to store running context for ripemd160 diff --git a/ccan/ccan/crypto/sha256/_info b/ccan/ccan/crypto/sha256/_info index fe1f5b8c8..4419508dd 100644 --- a/ccan/ccan/crypto/sha256/_info +++ b/ccan/ccan/crypto/sha256/_info @@ -40,10 +40,16 @@ int main(int argc, char *argv[]) return 1; if (strcmp(argv[1], "depends") == 0) { + printf("ccan/compiler\n"); printf("ccan/endian\n"); return 0; } + if (strcmp(argv[1], "testdepends") == 0) { + printf("ccan/str/hex\n"); + return 0; + } + if (strcmp(argv[1], "libs") == 0) { #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL printf("crypto\n"); diff --git a/ccan/ccan/crypto/sha256/benchmarks/double-sha-bench.c b/ccan/ccan/crypto/sha256/benchmarks/double-sha-bench.c index b66531b85..09a9900e4 100644 --- a/ccan/ccan/crypto/sha256/benchmarks/double-sha-bench.c +++ b/ccan/ccan/crypto/sha256/benchmarks/double-sha-bench.c @@ -37,7 +37,7 @@ int main(int argc, char *argv[]) memset(&block, 0, sizeof(block)); sha256(&block.h, &n, sizeof(n)); block.u8[sizeof(block.h)] = 0x80; - // Size is 256 bits + /* Size is 256 bits */ block.u8[sizeof(block)-2] = 1; start = time_now(); diff --git a/ccan/ccan/crypto/sha256/sha256.c b/ccan/ccan/crypto/sha256/sha256.c index 40da0b4bb..d42b47b0a 100644 --- a/ccan/ccan/crypto/sha256/sha256.c +++ b/ccan/ccan/crypto/sha256/sha256.c @@ -8,6 +8,7 @@ */ #include #include +#include #include #include #include @@ -17,16 +18,16 @@ static void invalidate_sha256(struct sha256_ctx *ctx) #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL ctx->c.md_len = 0; #else - ctx->bytes = -1ULL; + ctx->bytes = (size_t)-1; #endif } -static void check_sha256(struct sha256_ctx *ctx) +static void check_sha256(struct sha256_ctx *ctx UNUSED) { #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL assert(ctx->c.md_len != 0); #else - assert(ctx->bytes != -1ULL); + assert(ctx->bytes != (size_t)-1); #endif } @@ -166,7 +167,7 @@ static void Transform(uint32_t *s, const uint32_t *chunk) s[7] += h; } -static bool alignment_ok(const void *p, size_t n) +static bool alignment_ok(const void *p UNUSED, size_t n UNUSED) { #if HAVE_UNALIGNED_ACCESS return true; @@ -181,7 +182,7 @@ static void add(struct sha256_ctx *ctx, const void *p, size_t len) size_t bufsize = ctx->bytes % 64; if (bufsize + len >= 64) { - // Fill the buffer, and process it. + /* Fill the buffer, and process it. */ memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize); ctx->bytes += 64 - bufsize; data += 64 - bufsize; @@ -191,7 +192,7 @@ static void add(struct sha256_ctx *ctx, const void *p, size_t len) } while (len >= 64) { - // Process full chunks directly from the source. + /* Process full chunks directly from the source. */ if (alignment_ok(data, sizeof(uint32_t))) Transform(ctx->s, (const uint32_t *)data); else { @@ -204,7 +205,7 @@ static void add(struct sha256_ctx *ctx, const void *p, size_t len) } if (len) { - // Fill the buffer with what remains. + /* Fill the buffer with what remains. */ memcpy(ctx->buf.u8 + bufsize, data, len); ctx->bytes += len; } @@ -228,9 +229,9 @@ void sha256_done(struct sha256_ctx *ctx, struct sha256 *res) uint64_t sizedesc; size_t i; - sizedesc = cpu_to_be64(ctx->bytes << 3); + sizedesc = cpu_to_be64((uint64_t)ctx->bytes << 3); /* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */ - add(ctx, pad, 1 + ((119 - (ctx->bytes % 64)) % 64)); + add(ctx, pad, 1 + ((128 - 8 - (ctx->bytes % 64) - 1) % 64)); /* Add number of bits of data (big endian) */ add(ctx, &sizedesc, 8); for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++) diff --git a/ccan/ccan/crypto/sha256/sha256.h b/ccan/ccan/crypto/sha256/sha256.h index eedaa94ee..9a310b956 100644 --- a/ccan/ccan/crypto/sha256/sha256.h +++ b/ccan/ccan/crypto/sha256/sha256.h @@ -6,7 +6,7 @@ #include /* Uncomment this to use openssl's SHA256 routines (and link with -lcrypto) */ -//#define CCAN_CRYPTO_SHA256_USE_OPENSSL 1 +/*#define CCAN_CRYPTO_SHA256_USE_OPENSSL 1*/ #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL #include @@ -21,10 +21,8 @@ */ struct sha256 { union { - /* Array of chars */ - unsigned char u8[32]; - /* Array of uint32_t */ uint32_t u32[8]; + unsigned char u8[32]; } u; }; @@ -47,11 +45,11 @@ struct sha256_ctx { SHA256_CTX c; #else uint32_t s[8]; - uint64_t bytes; union { - uint32_t u32[8]; + uint32_t u32[16]; unsigned char u8[64]; } buf; + size_t bytes; #endif }; @@ -82,7 +80,7 @@ void sha256_init(struct sha256_ctx *ctx); /** * SHA256_INIT - initializer for an SHA256 context. * - * This can be used to staticly initialize an SHA256 context (instead + * This can be used to statically initialize an SHA256 context (instead * of sha256_init()). * * Example: @@ -106,7 +104,8 @@ void sha256_init(struct sha256_ctx *ctx); #else #define SHA256_INIT \ { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \ - 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, 0 } + 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \ + { { 0 } }, 0 } #endif /** diff --git a/ccan/ccan/crypto/sha256/test/run-33-bit-test.c b/ccan/ccan/crypto/sha256/test/run-33-bit-test.c index bde91e1c8..ea574b4f6 100644 --- a/ccan/ccan/crypto/sha256/test/run-33-bit-test.c +++ b/ccan/ccan/crypto/sha256/test/run-33-bit-test.c @@ -30,9 +30,9 @@ static const struct sha256_ctx after_16M_by_64 = { LE32_TO_CPU(0xd407a8fc), LE32_TO_CPU(0x1fad409b), LE32_TO_CPU(0x51fa46cc), LE32_TO_CPU(0xea528ae5), LE32_TO_CPU(0x5fa58ebb), LE32_TO_CPU(0x8be97931) }, - 1073741824, { .u32 = { 0x64636261, 0x68676665, 0x65646362, 0x69686766, - 0x66656463, 0x6a696867, 0x67666564, 0x6b6a6968 } } + 0x66656463, 0x6a696867, 0x67666564, 0x6b6a6968 } }, + 1073741824 #endif }; diff --git a/ccan/ccan/crypto/sha256/test/run-test-vectors.c b/ccan/ccan/crypto/sha256/test/run-test-vectors.c index a64c35e46..ba504b0c4 100644 --- a/ccan/ccan/crypto/sha256/test/run-test-vectors.c +++ b/ccan/ccan/crypto/sha256/test/run-test-vectors.c @@ -1,4 +1,5 @@ #include +#include /* Include the C files directly. */ #include #include @@ -7,53 +8,34 @@ struct test { const char *test; size_t repetitions; - beint32_t result[8]; + const char *result; }; static struct test tests[] = { { "", 1, - { CPU_TO_BE32(0xe3b0c442), CPU_TO_BE32(0x98fc1c14), - CPU_TO_BE32(0x9afbf4c8), CPU_TO_BE32(0x996fb924), - CPU_TO_BE32(0x27ae41e4), CPU_TO_BE32(0x649b934c), - CPU_TO_BE32(0xa495991b), CPU_TO_BE32(0x7852b855) } }, + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" }, { "abc", 1, - { CPU_TO_BE32(0xba7816bf), CPU_TO_BE32(0x8f01cfea), - CPU_TO_BE32(0x414140de), CPU_TO_BE32(0x5dae2223), - CPU_TO_BE32(0xb00361a3), CPU_TO_BE32(0x96177a9c), - CPU_TO_BE32(0xb410ff61), CPU_TO_BE32(0xf20015ad) } }, + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" }, { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1, - { CPU_TO_BE32(0x248d6a61), CPU_TO_BE32(0xd20638b8), - CPU_TO_BE32(0xe5c02693), CPU_TO_BE32(0x0c3e6039), - CPU_TO_BE32(0xa33ce459), CPU_TO_BE32(0x64ff2167), - CPU_TO_BE32(0xf6ecedd4), CPU_TO_BE32(0x19db06c1) } }, + "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" }, { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1, - { CPU_TO_BE32(0xcf5b16a7), CPU_TO_BE32(0x78af8380), - CPU_TO_BE32(0x036ce59e), CPU_TO_BE32(0x7b049237), - CPU_TO_BE32(0x0b249b11), CPU_TO_BE32(0xe8f07a51), - CPU_TO_BE32(0xafac4503), CPU_TO_BE32(0x7afee9d1) } }, + "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1" }, { "a", 1000000, - { CPU_TO_BE32(0xcdc76e5c), CPU_TO_BE32(0x9914fb92), - CPU_TO_BE32(0x81a1c7e2), CPU_TO_BE32(0x84d73e67), - CPU_TO_BE32(0xf1809a48), CPU_TO_BE32(0xa497200e), - CPU_TO_BE32(0x046d39cc), CPU_TO_BE32(0xc7112cd0) } } + "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" } #if 0 /* Good test, but takes ages! */ - , { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno", 16777216, - { CPU_TO_BE32(0x50e72a0e), CPU_TO_BE32(0x26442fe2), - CPU_TO_BE32(0x552dc393), CPU_TO_BE32(0x8ac58658), - CPU_TO_BE32(0x228c0cbf), CPU_TO_BE32(0xb1d2ca87), - CPU_TO_BE32(0x2ae43526), CPU_TO_BE32(0x6fcd055e) } } + , { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno", + 16777216, + "50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e" }, #endif }; -static bool do_test(const struct test *t, bool single) +static bool do_test(const struct test *t) { - struct sha256 h; + struct sha256 h, expected; - if (single) { - if (t->repetitions != 1) - return true; + if (t->repetitions == 1) sha256(&h, t->test, strlen(t->test)); - } else { + else { struct sha256_ctx ctx = SHA256_INIT; size_t i; @@ -62,21 +44,20 @@ static bool do_test(const struct test *t, bool single) sha256_done(&ctx, &h); } - return memcmp(&h.u, t->result, sizeof(t->result)) == 0; + hex_decode(t->result, strlen(t->result), &expected, sizeof(expected)); + return memcmp(&h, &expected, sizeof(h)) == 0; } int main(void) { + const size_t num_tests = sizeof(tests) / sizeof(tests[0]); size_t i; /* This is how many tests you plan to run */ - plan_tests(sizeof(tests) / sizeof(struct test) * 2); + plan_tests(num_tests); - for (i = 0; i < sizeof(tests) / sizeof(struct test); i++) - ok1(do_test(&tests[i], false)); - - for (i = 0; i < sizeof(tests) / sizeof(struct test); i++) - ok1(do_test(&tests[i], true)); + for (i = 0; i < num_tests; i++) + ok1(do_test(&tests[i])); /* This exits depending on whether all tests passed */ return exit_status(); diff --git a/ccan/ccan/crypto/shachain/shachain.c b/ccan/ccan/crypto/shachain/shachain.c index 6cfb7244f..c6bd37e8f 100644 --- a/ccan/ccan/crypto/shachain/shachain.c +++ b/ccan/ccan/crypto/shachain/shachain.c @@ -12,12 +12,12 @@ static void change_bit(unsigned char *arr, size_t index) arr[index / CHAR_BIT] ^= (1 << (index % CHAR_BIT)); } -static int count_trailing_zeroes(shachain_index_t index) +static unsigned int count_trailing_zeroes(shachain_index_t index) { #if HAVE_BUILTIN_CTZLL - return index ? __builtin_ctzll(index) : INDEX_BITS; + return index ? (unsigned int)__builtin_ctzll(index) : INDEX_BITS; #else - int i; + unsigned int i; for (i = 0; i < INDEX_BITS; i++) { if (index & (1ULL << i)) @@ -77,7 +77,7 @@ void shachain_init(struct shachain *chain) bool shachain_add_hash(struct shachain *chain, shachain_index_t index, const struct sha256 *hash) { - int i, pos; + unsigned int i, pos; /* You have to insert them in order! */ assert(index == chain->min_index - 1 || @@ -107,7 +107,7 @@ bool shachain_add_hash(struct shachain *chain, bool shachain_get_hash(const struct shachain *chain, shachain_index_t index, struct sha256 *hash) { - int i; + unsigned int i; for (i = 0; i < chain->num_valid; i++) { /* If we can get from key to index only by resetting bits, diff --git a/ccan/ccan/str/hex/hex.c b/ccan/ccan/str/hex/hex.c index fd4074f3d..6e031779f 100644 --- a/ccan/ccan/str/hex/hex.c +++ b/ccan/ccan/str/hex/hex.c @@ -50,21 +50,17 @@ static char hexchar(unsigned int val) bool hex_encode(const void *buf, size_t bufsize, char *dest, size_t destsize) { - size_t used = 0; + size_t i; - if (destsize < 1) + if (destsize < hex_str_size(bufsize)) return false; - while (used < bufsize) { - unsigned int c = ((const unsigned char *)buf)[used]; - if (destsize < 3) - return false; + for (i = 0; i < bufsize; i++) { + unsigned int c = ((const unsigned char *)buf)[i]; *(dest++) = hexchar(c >> 4); *(dest++) = hexchar(c & 0xF); - used++; - destsize -= 2; } *dest = '\0'; - return used + 1; + return true; } diff --git a/ccan/ccan/strmap/LICENSE b/ccan/ccan/strmap/LICENSE new file mode 120000 index 000000000..b7951dabd --- /dev/null +++ b/ccan/ccan/strmap/LICENSE @@ -0,0 +1 @@ +../../licenses/CC0 \ No newline at end of file diff --git a/ccan/ccan/tal/tal.h b/ccan/ccan/tal/tal.h index 0cfea9844..f360a9611 100644 --- a/ccan/ccan/tal/tal.h +++ b/ccan/ccan/tal/tal.h @@ -256,7 +256,7 @@ const char *tal_name(const tal_t *ptr); * tal_count - get the count of objects in a tal_arr. * @ptr: The tal allocated object array. * - * Returns 0 if @ptr has no length property, but we aware that that is + * Returns 0 if @ptr has no length property, but be aware that that is * also a valid size! */ size_t tal_count(const tal_t *ptr); diff --git a/ccan/tools/configurator/configurator.c b/ccan/tools/configurator/configurator.c index 5d959cf33..67dadc61e 100644 --- a/ccan/tools/configurator/configurator.c +++ b/ccan/tools/configurator/configurator.c @@ -330,7 +330,7 @@ static struct test tests[] = { " setcontext(&b);\n" " x |= 4;\n" "}\n" - "int main(int argc, char *argv[]) {\n" + "int main(void) {\n" " x |= 1;\n" " getcontext(&a);\n" " a.uc_stack.ss_sp = stack;\n" @@ -354,7 +354,7 @@ static struct test tests[] = { " worked = 1;\n" " setcontext(&b);\n" "}\n" - "int main(int argc, char *argv[]) {\n" + "int main(void) {\n" " void *ap = &worked;\n" " void *aq = (void *)(~((ptrdiff_t)ap));\n" " getcontext(&a);\n"