From b102e12253078e8c0ebdeeb5e1893ea6a025a700 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 18 Oct 2010 11:39:47 +0200 Subject: *: use SWAP_BE64 instead of open-coding it Signed-off-by: Denys Vlasenko --- include/libbb.h | 2 +- libbb/hash_md5.c | 8 +++----- libbb/hash_sha.c | 10 +++++----- networking/udhcp/dumpleases.c | 2 +- networking/udhcp/files.c | 2 +- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index dec4e3af5..b16157d46 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1540,7 +1540,7 @@ typedef struct md5_ctx_t { uint32_t C; uint32_t D; uint64_t total64; - char wbuffer[64]; + char wbuffer[64]; /* NB: always correctly aligned for uint64_t */ } md5_ctx_t; #else /* libbb/md5prime.c uses a bit different one: */ diff --git a/libbb/hash_md5.c b/libbb/hash_md5.c index 051c8ede4..9de27f1d9 100644 --- a/libbb/hash_md5.c +++ b/libbb/hash_md5.c @@ -417,11 +417,9 @@ void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) if (remaining >= 8) { /* Store the 64-bit counter of bits in the buffer in BE format */ uint64_t t = ctx->total64 << 3; - unsigned i; - for (i = 0; i < 8; i++) { - ctx->wbuffer[56 + i] = t; - t >>= 8; - } + t = SWAP_BE64(t); + /* wbuffer is suitably aligned for this */ + *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t; } md5_process_block64(ctx); if (remaining >= 8) diff --git a/libbb/hash_sha.c b/libbb/hash_sha.c index d79291148..e7199d317 100644 --- a/libbb/hash_sha.c +++ b/libbb/hash_sha.c @@ -52,10 +52,10 @@ static ALWAYS_INLINE uint64_t rotr64(uint64_t x, unsigned n) return (x >> n) | (x << (64 - n)); } #if BB_LITTLE_ENDIAN -/* ALWAYS_INLINE below would hurt code size, using plain inline: */ +/* ALWAYS_INLINE below sometimes hurts code size, using plain inline: */ static inline uint64_t hton64(uint64_t v) { - return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32); + return SWAP_BE64(v); } #else #define hton64(v) (v) @@ -76,7 +76,7 @@ static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx) const uint32_t *words = (uint32_t*) ctx->wbuffer; for (t = 0; t < 16; ++t) - W[t] = ntohl(words[t]); + W[t] = SWAP_BE32(words[t]); for (/*t = 16*/; t < 80; ++t) { uint32_t T = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]; W[t] = rotl32(T, 1); @@ -198,7 +198,7 @@ static void FAST_FUNC sha256_process_block64(sha256_ctx_t *ctx) /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */ for (t = 0; t < 16; ++t) - W[t] = ntohl(words[t]); + W[t] = SWAP_BE32(words[t]); for (/*t = 16*/; t < 64; ++t) W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16]; @@ -490,7 +490,7 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) if (BB_LITTLE_ENDIAN) { unsigned i; for (i = 0; i < bufpos; ++i) - ctx->hash[i] = htonl(ctx->hash[i]); + ctx->hash[i] = SWAP_BE32(ctx->hash[i]); } memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * bufpos); } diff --git a/networking/udhcp/dumpleases.c b/networking/udhcp/dumpleases.c index 2eaadb6eb..341815a69 100644 --- a/networking/udhcp/dumpleases.c +++ b/networking/udhcp/dumpleases.c @@ -9,7 +9,7 @@ #if BB_LITTLE_ENDIAN static inline uint64_t hton64(uint64_t v) { - return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32); + return SWAP_BE64(v); } #else #define hton64(v) (v) diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index f5348f658..68b2085a9 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c @@ -14,7 +14,7 @@ #if BB_LITTLE_ENDIAN static inline uint64_t hton64(uint64_t v) { - return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32); + return SWAP_BE64(v); } #else #define hton64(v) (v) -- cgit v1.2.3-55-g6feb From 9ff50b869780aba131dc9b542ccd0f1a3959e920 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 18 Oct 2010 11:40:26 +0200 Subject: *: deinline SWAP_xE64 on 32-bit CPUs. Wins !90 bytes both on 32 and 64 bits Signed-off-by: Denys Vlasenko --- include/libbb.h | 7 +++++-- include/platform.h | 9 +++++++-- libbb/Kbuild.src | 1 + libbb/bb_bswap_64.c | 16 ++++++++++++++++ libbb/hash_sha.c | 20 +++++--------------- networking/udhcp/dumpleases.c | 13 +------------ networking/udhcp/files.c | 14 ++------------ 7 files changed, 37 insertions(+), 43 deletions(-) create mode 100644 libbb/bb_bswap_64.c diff --git a/include/libbb.h b/include/libbb.h index b16157d46..c161ed7e2 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -37,8 +37,6 @@ #include #include #include -/* Try to pull in PATH_MAX */ -#include #include #ifdef HAVE_MNTENT_H # include @@ -254,6 +252,11 @@ extern int *const bb_errno; #define errno (*bb_errno) #endif +#if !(ULONG_MAX > 0xffffffff) +/* Only 32-bit CPUs need this, 64-bit ones use inlined version */ +uint64_t bb_bswap_64(uint64_t x) FAST_FUNC; +#endif + unsigned long long monotonic_ns(void) FAST_FUNC; unsigned long long monotonic_us(void) FAST_FUNC; unsigned long long monotonic_ms(void) FAST_FUNC; diff --git a/include/platform.h b/include/platform.h index c255a17ce..b5c668517 100644 --- a/include/platform.h +++ b/include/platform.h @@ -150,6 +150,7 @@ /* ---- Endian Detection ------------------------------------ */ +#include #if defined(__digital__) && defined(__unix__) # include #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ @@ -189,6 +190,10 @@ # error "Can't determine endianness" #endif +#if ULONG_MAX > 0xffffffff +# define bb_bswap_64(x) bswap_64(x) +#endif + /* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */ #if BB_BIG_ENDIAN # define SWAP_BE16(x) (x) @@ -196,13 +201,13 @@ # define SWAP_BE64(x) (x) # define SWAP_LE16(x) bswap_16(x) # define SWAP_LE32(x) bswap_32(x) -# define SWAP_LE64(x) bswap_64(x) +# define SWAP_LE64(x) bb_bswap_64(x) # define IF_BIG_ENDIAN(...) __VA_ARGS__ # define IF_LITTLE_ENDIAN(...) #else # define SWAP_BE16(x) bswap_16(x) # define SWAP_BE32(x) bswap_32(x) -# define SWAP_BE64(x) bswap_64(x) +# define SWAP_BE64(x) bb_bswap_64(x) # define SWAP_LE16(x) (x) # define SWAP_LE32(x) (x) # define SWAP_LE64(x) (x) diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src index 0fd730247..0955d73e1 100644 --- a/libbb/Kbuild.src +++ b/libbb/Kbuild.src @@ -14,6 +14,7 @@ lib-y += appletlib.o lib-y += ask_confirmation.o lib-y += bb_askpass.o lib-y += bb_basename.o +lib-y += bb_bswap_64.o lib-y += bb_do_delay.o lib-y += bb_pwd.o lib-y += bb_qsort.o diff --git a/libbb/bb_bswap_64.c b/libbb/bb_bswap_64.c new file mode 100644 index 000000000..ce9d53b7f --- /dev/null +++ b/libbb/bb_bswap_64.c @@ -0,0 +1,16 @@ +/* + * Utility routines. + * + * Copyright (C) 2010 Denys Vlasenko + * + * Licensed under GPLv2, see file LICENSE in this source tree. + */ + +#include "libbb.h" + +#if !(ULONG_MAX > 0xffffffff) +uint64_t FAST_FUNC bb_bswap_64(uint64_t x) +{ + return bswap_64(x); +} +#endif diff --git a/libbb/hash_sha.c b/libbb/hash_sha.c index e7199d317..72d50928b 100644 --- a/libbb/hash_sha.c +++ b/libbb/hash_sha.c @@ -51,16 +51,6 @@ static ALWAYS_INLINE uint64_t rotr64(uint64_t x, unsigned n) { return (x >> n) | (x << (64 - n)); } -#if BB_LITTLE_ENDIAN -/* ALWAYS_INLINE below sometimes hurts code size, using plain inline: */ -static inline uint64_t hton64(uint64_t v) -{ - return SWAP_BE64(v); -} -#else -#define hton64(v) (v) -#endif -#define ntoh64(v) hton64(v) /* Some arch headers have conflicting defines */ @@ -274,7 +264,7 @@ static void FAST_FUNC sha512_process_block128(sha512_ctx_t *ctx) /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */ for (t = 0; t < 16; ++t) - W[t] = ntoh64(words[t]); + W[t] = SWAP_BE64(words[t]); for (/*t = 16*/; t < 80; ++t) W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16]; @@ -475,7 +465,7 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) if (remaining >= 8) { /* Store the 64-bit counter of bits in the buffer in BE format */ uint64_t t = ctx->total64 << 3; - t = hton64(t); + t = SWAP_BE64(t); /* wbuffer is suitably aligned for this */ *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t; } @@ -509,10 +499,10 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) /* Store the 128-bit counter of bits in the buffer in BE format */ uint64_t t; t = ctx->total64[0] << 3; - t = hton64(t); + t = SWAP_BE64(t); *(uint64_t *) (&ctx->wbuffer[128 - 8]) = t; t = (ctx->total64[1] << 3) | (ctx->total64[0] >> 61); - t = hton64(t); + t = SWAP_BE64(t); *(uint64_t *) (&ctx->wbuffer[128 - 16]) = t; } sha512_process_block128(ctx); @@ -524,7 +514,7 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) if (BB_LITTLE_ENDIAN) { unsigned i; for (i = 0; i < ARRAY_SIZE(ctx->hash); ++i) - ctx->hash[i] = hton64(ctx->hash[i]); + ctx->hash[i] = SWAP_BE64(ctx->hash[i]); } memcpy(resbuf, ctx->hash, sizeof(ctx->hash)); } diff --git a/networking/udhcp/dumpleases.c b/networking/udhcp/dumpleases.c index 341815a69..46610fc63 100644 --- a/networking/udhcp/dumpleases.c +++ b/networking/udhcp/dumpleases.c @@ -6,17 +6,6 @@ #include "dhcpd.h" #include "unicode.h" -#if BB_LITTLE_ENDIAN -static inline uint64_t hton64(uint64_t v) -{ - return SWAP_BE64(v); -} -#else -#define hton64(v) (v) -#endif -#define ntoh64(v) hton64(v) - - int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int dumpleases_main(int argc UNUSED_PARAM, char **argv) { @@ -54,7 +43,7 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv) /* "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 */ xread(fd, &written_at, sizeof(written_at)); - written_at = ntoh64(written_at); + written_at = SWAP_BE64(written_at); curr = time(NULL); if (curr < written_at) written_at = curr; /* lease file from future! :) */ diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index 68b2085a9..49bcafb9c 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c @@ -11,16 +11,6 @@ #include "common.h" #include "dhcpd.h" -#if BB_LITTLE_ENDIAN -static inline uint64_t hton64(uint64_t v) -{ - return SWAP_BE64(v); -} -#else -#define hton64(v) (v) -#endif -#define ntoh64(v) hton64(v) - /* on these functions, make sure your datatype matches */ static int FAST_FUNC read_str(const char *line, void *arg) { @@ -140,7 +130,7 @@ void FAST_FUNC write_leases(void) curr = written_at = time(NULL); - written_at = hton64(written_at); + written_at = SWAP_BE64(written_at); full_write(fd, &written_at, sizeof(written_at)); for (i = 0; i < server_config.max_leases; i++) { @@ -190,7 +180,7 @@ void FAST_FUNC read_leases(const char *file) if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at)) goto ret; - written_at = ntoh64(written_at); + written_at = SWAP_BE64(written_at); time_passed = time(NULL) - written_at; /* Strange written_at, or lease file from old version of udhcpd -- cgit v1.2.3-55-g6feb From e08ef581af510312dacba40e78a6af7ca36ffbc5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 18 Oct 2010 11:50:17 +0200 Subject: md5: length should be stored in *little-endian* order! fixing... Signed-off-by: Denys Vlasenko --- libbb/hash_md5.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libbb/hash_md5.c b/libbb/hash_md5.c index 9de27f1d9..d7410fa2c 100644 --- a/libbb/hash_md5.c +++ b/libbb/hash_md5.c @@ -415,9 +415,9 @@ void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) memset(ctx->wbuffer + bufpos, 0, remaining); /* Do we have enough space for the length count? */ if (remaining >= 8) { - /* Store the 64-bit counter of bits in the buffer in BE format */ + /* Store the 64-bit counter of bits in the buffer in LE format */ uint64_t t = ctx->total64 << 3; - t = SWAP_BE64(t); + t = SWAP_LE64(t); /* wbuffer is suitably aligned for this */ *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t; } -- cgit v1.2.3-55-g6feb