aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2011-01-04 19:49:31 +0700
committerNguyễn Thái Ngọc Duy <pclouds@gmail.com>2011-01-04 19:49:31 +0700
commit808c59093f66849fd65c283ec2f68745a98cd291 (patch)
tree53a7fca34f88c1513334deea30457ca8902f1faf
parent85031e551c05fa13e268a085983fb7f61f11dde2 (diff)
parente08ef581af510312dacba40e78a6af7ca36ffbc5 (diff)
downloadbusybox-w32-808c59093f66849fd65c283ec2f68745a98cd291.tar.gz
busybox-w32-808c59093f66849fd65c283ec2f68745a98cd291.tar.bz2
busybox-w32-808c59093f66849fd65c283ec2f68745a98cd291.zip
Merge commit 'eb7fe6dbf5bc93a229379a8047539dd8b90e0974^'
-rw-r--r--include/libbb.h9
-rw-r--r--include/platform.h9
-rw-r--r--libbb/Kbuild.src1
-rw-r--r--libbb/bb_bswap_64.c16
-rw-r--r--libbb/hash_md5.c10
-rw-r--r--libbb/hash_sha.c26
-rw-r--r--networking/udhcp/dumpleases.c13
-rw-r--r--networking/udhcp/files.c14
8 files changed, 45 insertions, 53 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 9ece2f9ec..0ea7700f2 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -37,8 +37,6 @@
37#include <termios.h> 37#include <termios.h>
38#include <time.h> 38#include <time.h>
39#include <unistd.h> 39#include <unistd.h>
40/* Try to pull in PATH_MAX */
41#include <limits.h>
42#include <sys/param.h> 40#include <sys/param.h>
43#ifdef HAVE_MNTENT_H 41#ifdef HAVE_MNTENT_H
44# include <mntent.h> 42# include <mntent.h>
@@ -265,6 +263,11 @@ extern int *const bb_errno;
265#define errno (*bb_errno) 263#define errno (*bb_errno)
266#endif 264#endif
267 265
266#if !(ULONG_MAX > 0xffffffff)
267/* Only 32-bit CPUs need this, 64-bit ones use inlined version */
268uint64_t bb_bswap_64(uint64_t x) FAST_FUNC;
269#endif
270
268unsigned long long monotonic_ns(void) FAST_FUNC; 271unsigned long long monotonic_ns(void) FAST_FUNC;
269unsigned long long monotonic_us(void) FAST_FUNC; 272unsigned long long monotonic_us(void) FAST_FUNC;
270unsigned long long monotonic_ms(void) FAST_FUNC; 273unsigned long long monotonic_ms(void) FAST_FUNC;
@@ -1557,7 +1560,7 @@ typedef struct md5_ctx_t {
1557 uint32_t C; 1560 uint32_t C;
1558 uint32_t D; 1561 uint32_t D;
1559 uint64_t total64; 1562 uint64_t total64;
1560 char wbuffer[64]; 1563 char wbuffer[64]; /* NB: always correctly aligned for uint64_t */
1561} md5_ctx_t; 1564} md5_ctx_t;
1562#else 1565#else
1563/* libbb/md5prime.c uses a bit different one: */ 1566/* libbb/md5prime.c uses a bit different one: */
diff --git a/include/platform.h b/include/platform.h
index ed8c137e1..c20d2aef6 100644
--- a/include/platform.h
+++ b/include/platform.h
@@ -160,6 +160,7 @@
160 160
161/* ---- Endian Detection ------------------------------------ */ 161/* ---- Endian Detection ------------------------------------ */
162 162
163#include <limits.h>
163#if defined(__digital__) && defined(__unix__) 164#if defined(__digital__) && defined(__unix__)
164# include <sex.h> 165# include <sex.h>
165#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ 166#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
@@ -207,6 +208,10 @@
207# error "Can't determine endianness" 208# error "Can't determine endianness"
208#endif 209#endif
209 210
211#if ULONG_MAX > 0xffffffff
212# define bb_bswap_64(x) bswap_64(x)
213#endif
214
210/* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */ 215/* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */
211#if BB_BIG_ENDIAN 216#if BB_BIG_ENDIAN
212# define SWAP_BE16(x) (x) 217# define SWAP_BE16(x) (x)
@@ -214,13 +219,13 @@
214# define SWAP_BE64(x) (x) 219# define SWAP_BE64(x) (x)
215# define SWAP_LE16(x) bswap_16(x) 220# define SWAP_LE16(x) bswap_16(x)
216# define SWAP_LE32(x) bswap_32(x) 221# define SWAP_LE32(x) bswap_32(x)
217# define SWAP_LE64(x) bswap_64(x) 222# define SWAP_LE64(x) bb_bswap_64(x)
218# define IF_BIG_ENDIAN(...) __VA_ARGS__ 223# define IF_BIG_ENDIAN(...) __VA_ARGS__
219# define IF_LITTLE_ENDIAN(...) 224# define IF_LITTLE_ENDIAN(...)
220#else 225#else
221# define SWAP_BE16(x) bswap_16(x) 226# define SWAP_BE16(x) bswap_16(x)
222# define SWAP_BE32(x) bswap_32(x) 227# define SWAP_BE32(x) bswap_32(x)
223# define SWAP_BE64(x) bswap_64(x) 228# define SWAP_BE64(x) bb_bswap_64(x)
224# define SWAP_LE16(x) (x) 229# define SWAP_LE16(x) (x)
225# define SWAP_LE32(x) (x) 230# define SWAP_LE32(x) (x)
226# define SWAP_LE64(x) (x) 231# define SWAP_LE64(x) (x)
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index db0e382a8..a0d7389e8 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -14,6 +14,7 @@ lib-y += appletlib.o
14lib-y += ask_confirmation.o 14lib-y += ask_confirmation.o
15lib-y += bb_askpass.o 15lib-y += bb_askpass.o
16lib-y += bb_basename.o 16lib-y += bb_basename.o
17lib-y += bb_bswap_64.o
17lib-y += bb_do_delay.o 18lib-y += bb_do_delay.o
18lib-y += bb_pwd.o 19lib-y += bb_pwd.o
19lib-y += bb_qsort.o 20lib-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 @@
1/*
2 * Utility routines.
3 *
4 * Copyright (C) 2010 Denys Vlasenko
5 *
6 * Licensed under GPLv2, see file LICENSE in this source tree.
7 */
8
9#include "libbb.h"
10
11#if !(ULONG_MAX > 0xffffffff)
12uint64_t FAST_FUNC bb_bswap_64(uint64_t x)
13{
14 return bswap_64(x);
15}
16#endif
diff --git a/libbb/hash_md5.c b/libbb/hash_md5.c
index 051c8ede4..d7410fa2c 100644
--- a/libbb/hash_md5.c
+++ b/libbb/hash_md5.c
@@ -415,13 +415,11 @@ void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf)
415 memset(ctx->wbuffer + bufpos, 0, remaining); 415 memset(ctx->wbuffer + bufpos, 0, remaining);
416 /* Do we have enough space for the length count? */ 416 /* Do we have enough space for the length count? */
417 if (remaining >= 8) { 417 if (remaining >= 8) {
418 /* Store the 64-bit counter of bits in the buffer in BE format */ 418 /* Store the 64-bit counter of bits in the buffer in LE format */
419 uint64_t t = ctx->total64 << 3; 419 uint64_t t = ctx->total64 << 3;
420 unsigned i; 420 t = SWAP_LE64(t);
421 for (i = 0; i < 8; i++) { 421 /* wbuffer is suitably aligned for this */
422 ctx->wbuffer[56 + i] = t; 422 *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t;
423 t >>= 8;
424 }
425 } 423 }
426 md5_process_block64(ctx); 424 md5_process_block64(ctx);
427 if (remaining >= 8) 425 if (remaining >= 8)
diff --git a/libbb/hash_sha.c b/libbb/hash_sha.c
index d79291148..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)
51{ 51{
52 return (x >> n) | (x << (64 - n)); 52 return (x >> n) | (x << (64 - n));
53} 53}
54#if BB_LITTLE_ENDIAN
55/* ALWAYS_INLINE below would hurt code size, using plain inline: */
56static inline uint64_t hton64(uint64_t v)
57{
58 return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
59}
60#else
61#define hton64(v) (v)
62#endif
63#define ntoh64(v) hton64(v)
64 54
65 55
66/* Some arch headers have conflicting defines */ 56/* Some arch headers have conflicting defines */
@@ -76,7 +66,7 @@ static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx)
76 const uint32_t *words = (uint32_t*) ctx->wbuffer; 66 const uint32_t *words = (uint32_t*) ctx->wbuffer;
77 67
78 for (t = 0; t < 16; ++t) 68 for (t = 0; t < 16; ++t)
79 W[t] = ntohl(words[t]); 69 W[t] = SWAP_BE32(words[t]);
80 for (/*t = 16*/; t < 80; ++t) { 70 for (/*t = 16*/; t < 80; ++t) {
81 uint32_t T = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]; 71 uint32_t T = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
82 W[t] = rotl32(T, 1); 72 W[t] = rotl32(T, 1);
@@ -198,7 +188,7 @@ static void FAST_FUNC sha256_process_block64(sha256_ctx_t *ctx)
198 188
199 /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */ 189 /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */
200 for (t = 0; t < 16; ++t) 190 for (t = 0; t < 16; ++t)
201 W[t] = ntohl(words[t]); 191 W[t] = SWAP_BE32(words[t]);
202 for (/*t = 16*/; t < 64; ++t) 192 for (/*t = 16*/; t < 64; ++t)
203 W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16]; 193 W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16];
204 194
@@ -274,7 +264,7 @@ static void FAST_FUNC sha512_process_block128(sha512_ctx_t *ctx)
274 264
275 /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */ 265 /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */
276 for (t = 0; t < 16; ++t) 266 for (t = 0; t < 16; ++t)
277 W[t] = ntoh64(words[t]); 267 W[t] = SWAP_BE64(words[t]);
278 for (/*t = 16*/; t < 80; ++t) 268 for (/*t = 16*/; t < 80; ++t)
279 W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16]; 269 W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16];
280 270
@@ -475,7 +465,7 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf)
475 if (remaining >= 8) { 465 if (remaining >= 8) {
476 /* Store the 64-bit counter of bits in the buffer in BE format */ 466 /* Store the 64-bit counter of bits in the buffer in BE format */
477 uint64_t t = ctx->total64 << 3; 467 uint64_t t = ctx->total64 << 3;
478 t = hton64(t); 468 t = SWAP_BE64(t);
479 /* wbuffer is suitably aligned for this */ 469 /* wbuffer is suitably aligned for this */
480 *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t; 470 *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t;
481 } 471 }
@@ -490,7 +480,7 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf)
490 if (BB_LITTLE_ENDIAN) { 480 if (BB_LITTLE_ENDIAN) {
491 unsigned i; 481 unsigned i;
492 for (i = 0; i < bufpos; ++i) 482 for (i = 0; i < bufpos; ++i)
493 ctx->hash[i] = htonl(ctx->hash[i]); 483 ctx->hash[i] = SWAP_BE32(ctx->hash[i]);
494 } 484 }
495 memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * bufpos); 485 memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * bufpos);
496} 486}
@@ -509,10 +499,10 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
509 /* Store the 128-bit counter of bits in the buffer in BE format */ 499 /* Store the 128-bit counter of bits in the buffer in BE format */
510 uint64_t t; 500 uint64_t t;
511 t = ctx->total64[0] << 3; 501 t = ctx->total64[0] << 3;
512 t = hton64(t); 502 t = SWAP_BE64(t);
513 *(uint64_t *) (&ctx->wbuffer[128 - 8]) = t; 503 *(uint64_t *) (&ctx->wbuffer[128 - 8]) = t;
514 t = (ctx->total64[1] << 3) | (ctx->total64[0] >> 61); 504 t = (ctx->total64[1] << 3) | (ctx->total64[0] >> 61);
515 t = hton64(t); 505 t = SWAP_BE64(t);
516 *(uint64_t *) (&ctx->wbuffer[128 - 16]) = t; 506 *(uint64_t *) (&ctx->wbuffer[128 - 16]) = t;
517 } 507 }
518 sha512_process_block128(ctx); 508 sha512_process_block128(ctx);
@@ -524,7 +514,7 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
524 if (BB_LITTLE_ENDIAN) { 514 if (BB_LITTLE_ENDIAN) {
525 unsigned i; 515 unsigned i;
526 for (i = 0; i < ARRAY_SIZE(ctx->hash); ++i) 516 for (i = 0; i < ARRAY_SIZE(ctx->hash); ++i)
527 ctx->hash[i] = hton64(ctx->hash[i]); 517 ctx->hash[i] = SWAP_BE64(ctx->hash[i]);
528 } 518 }
529 memcpy(resbuf, ctx->hash, sizeof(ctx->hash)); 519 memcpy(resbuf, ctx->hash, sizeof(ctx->hash));
530} 520}
diff --git a/networking/udhcp/dumpleases.c b/networking/udhcp/dumpleases.c
index 2eaadb6eb..46610fc63 100644
--- a/networking/udhcp/dumpleases.c
+++ b/networking/udhcp/dumpleases.c
@@ -6,17 +6,6 @@
6#include "dhcpd.h" 6#include "dhcpd.h"
7#include "unicode.h" 7#include "unicode.h"
8 8
9#if BB_LITTLE_ENDIAN
10static inline uint64_t hton64(uint64_t v)
11{
12 return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
13}
14#else
15#define hton64(v) (v)
16#endif
17#define ntoh64(v) hton64(v)
18
19
20int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 9int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
21int dumpleases_main(int argc UNUSED_PARAM, char **argv) 10int dumpleases_main(int argc UNUSED_PARAM, char **argv)
22{ 11{
@@ -54,7 +43,7 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv)
54 /* "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 */ 43 /* "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 */
55 44
56 xread(fd, &written_at, sizeof(written_at)); 45 xread(fd, &written_at, sizeof(written_at));
57 written_at = ntoh64(written_at); 46 written_at = SWAP_BE64(written_at);
58 curr = time(NULL); 47 curr = time(NULL);
59 if (curr < written_at) 48 if (curr < written_at)
60 written_at = curr; /* lease file from future! :) */ 49 written_at = curr; /* lease file from future! :) */
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index f5348f658..49bcafb9c 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -11,16 +11,6 @@
11#include "common.h" 11#include "common.h"
12#include "dhcpd.h" 12#include "dhcpd.h"
13 13
14#if BB_LITTLE_ENDIAN
15static inline uint64_t hton64(uint64_t v)
16{
17 return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
18}
19#else
20#define hton64(v) (v)
21#endif
22#define ntoh64(v) hton64(v)
23
24/* on these functions, make sure your datatype matches */ 14/* on these functions, make sure your datatype matches */
25static int FAST_FUNC read_str(const char *line, void *arg) 15static int FAST_FUNC read_str(const char *line, void *arg)
26{ 16{
@@ -140,7 +130,7 @@ void FAST_FUNC write_leases(void)
140 130
141 curr = written_at = time(NULL); 131 curr = written_at = time(NULL);
142 132
143 written_at = hton64(written_at); 133 written_at = SWAP_BE64(written_at);
144 full_write(fd, &written_at, sizeof(written_at)); 134 full_write(fd, &written_at, sizeof(written_at));
145 135
146 for (i = 0; i < server_config.max_leases; i++) { 136 for (i = 0; i < server_config.max_leases; i++) {
@@ -190,7 +180,7 @@ void FAST_FUNC read_leases(const char *file)
190 180
191 if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at)) 181 if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
192 goto ret; 182 goto ret;
193 written_at = ntoh64(written_at); 183 written_at = SWAP_BE64(written_at);
194 184
195 time_passed = time(NULL) - written_at; 185 time_passed = time(NULL) - written_at;
196 /* Strange written_at, or lease file from old version of udhcpd 186 /* Strange written_at, or lease file from old version of udhcpd