From c48a5c607d8bdb422224a9767925a30d486a1109 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 18 Oct 2010 14:48:30 +0200 Subject: hash_md5_sha: use common finalization routine for MD5 and sha1/256. -15 bytes Signed-off-by: Denys Vlasenko --- include/libbb.h | 46 +++++++-------- libbb/hash_md5_sha.c | 163 +++++++++++++++++++++++++++------------------------ 2 files changed, 108 insertions(+), 101 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index c161ed7e2..3c8764b5c 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1515,35 +1515,14 @@ enum { }; void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags); -typedef struct sha1_ctx_t { - uint32_t hash[8]; /* 5, +3 elements for sha256 */ - uint64_t total64; /* must be directly after hash[] */ - uint8_t wbuffer[64]; /* NB: always correctly aligned for uint64_t */ - void (*process_block)(struct sha1_ctx_t*) FAST_FUNC; -} sha1_ctx_t; -void sha1_begin(sha1_ctx_t *ctx) FAST_FUNC; -void sha1_hash(sha1_ctx_t *ctx, const void *data, size_t length) FAST_FUNC; -void sha1_end(sha1_ctx_t *ctx, void *resbuf) FAST_FUNC; -typedef struct sha1_ctx_t sha256_ctx_t; -void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC; -#define sha256_hash sha1_hash -#define sha256_end sha1_end -typedef struct sha512_ctx_t { - uint64_t hash[8]; - uint64_t total64[2]; /* must be directly after hash[] */ - uint8_t wbuffer[128]; /* NB: always correctly aligned for uint64_t */ -} sha512_ctx_t; -void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC; -void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; -void sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC; #if 1 typedef struct md5_ctx_t { + char wbuffer[64]; /* NB: always correctly aligned for uint64_t */ + uint64_t total64; uint32_t A; uint32_t B; uint32_t C; uint32_t D; - uint64_t total64; - char wbuffer[64]; /* NB: always correctly aligned for uint64_t */ } md5_ctx_t; #else /* libbb/md5prime.c uses a bit different one: */ @@ -1556,6 +1535,27 @@ typedef struct md5_ctx_t { void md5_begin(md5_ctx_t *ctx) FAST_FUNC; void md5_hash(md5_ctx_t *ctx, const void *data, size_t length) FAST_FUNC; void md5_end(md5_ctx_t *ctx, void *resbuf) FAST_FUNC; +typedef struct sha1_ctx_t { + uint8_t wbuffer[64]; /* NB: always correctly aligned for uint64_t */ + uint64_t total64; /* must be directly before hash[] */ + uint32_t hash[8]; /* 5, +3 elements for sha256 */ + void (*process_block)(struct sha1_ctx_t*) FAST_FUNC; +} sha1_ctx_t; +void sha1_begin(sha1_ctx_t *ctx) FAST_FUNC; +void sha1_hash(sha1_ctx_t *ctx, const void *data, size_t length) FAST_FUNC; +void sha1_end(sha1_ctx_t *ctx, void *resbuf) FAST_FUNC; +typedef struct sha1_ctx_t sha256_ctx_t; +void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC; +#define sha256_hash sha1_hash +#define sha256_end sha1_end +typedef struct sha512_ctx_t { + uint64_t total64[2]; /* must be directly before hash[] */ + uint64_t hash[8]; + uint8_t wbuffer[128]; /* NB: always correctly aligned for uint64_t */ +} sha512_ctx_t; +void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC; +void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; +void sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC; uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC; diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c index 3e708ef7e..b07ba55f7 100644 --- a/libbb/hash_md5_sha.c +++ b/libbb/hash_md5_sha.c @@ -1,31 +1,10 @@ /* vi: set sw=4 ts=4: */ /* - * Based on shasum from http://www.netsw.org/crypto/hash/ - * Majorly hacked up to use Dr Brian Gladman's sha1 code + * Utility routines. * - * Copyright (C) 2002 Dr Brian Gladman , Worcester, UK. - * Copyright (C) 2003 Glenn L. McGrath - * Copyright (C) 2003 Erik Andersen + * Copyright (C) 2010 Denys Vlasenko * * Licensed under GPLv2 or later, see file LICENSE in this source tree. - * - * --------------------------------------------------------------------------- - * Issue Date: 10/11/2002 - * - * This is a byte oriented version of SHA1 that operates on arrays of bytes - * stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor - * - * --------------------------------------------------------------------------- - * - * SHA256 and SHA512 parts are: - * Released into the Public Domain by Ulrich Drepper . - * Shrank by Denys Vlasenko. - * - * --------------------------------------------------------------------------- - * - * The best way to test random blocksizes is to go to coreutils/md5_sha1_sum.c - * and replace "4096" with something like "2000 + time(NULL) % 2097", - * then rebuild and compare "shaNNNsum bigfile" results. */ #include "libbb.h" @@ -53,6 +32,70 @@ static ALWAYS_INLINE uint64_t rotr64(uint64_t x, unsigned n) } +typedef struct common64_ctx_t { + char wbuffer[64]; /* NB: always correctly aligned for uint64_t */ + uint64_t total64; +} common64_ctx_t; + +typedef void FAST_FUNC process_block64_func(void*); + +static void FAST_FUNC common64_end(void *vctx, process_block64_func process_block64, int swap_needed) +{ + common64_ctx_t *ctx = vctx; + unsigned bufpos = ctx->total64 & 63; + /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ + ctx->wbuffer[bufpos++] = 0x80; + + /* This loop iterates either once or twice, no more, no less */ + while (1) { + unsigned remaining = 64 - bufpos; + 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 */ + uint64_t t = ctx->total64 << 3; + if (swap_needed) + t = bb_bswap_64(t); + /* wbuffer is suitably aligned for this */ + *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t; + } + process_block64(ctx); + if (remaining >= 8) + break; + bufpos = 0; + } +} + + +/* + * Based on shasum from http://www.netsw.org/crypto/hash/ + * Majorly hacked up to use Dr Brian Gladman's sha1 code + * + * Copyright (C) 2002 Dr Brian Gladman , Worcester, UK. + * Copyright (C) 2003 Glenn L. McGrath + * Copyright (C) 2003 Erik Andersen + * + * Licensed under GPLv2 or later, see file LICENSE in this source tree. + * + * --------------------------------------------------------------------------- + * Issue Date: 10/11/2002 + * + * This is a byte oriented version of SHA1 that operates on arrays of bytes + * stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor + * + * --------------------------------------------------------------------------- + * + * SHA256 and SHA512 parts are: + * Released into the Public Domain by Ulrich Drepper . + * Shrank by Denys Vlasenko. + * + * --------------------------------------------------------------------------- + * + * The best way to test random blocksizes is to go to coreutils/md5_sha1_sum.c + * and replace "4096" with something like "2000 + time(NULL) % 2097", + * then rebuild and compare "shaNNNsum bigfile" results. + */ + static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx) { unsigned t; @@ -308,6 +351,8 @@ void FAST_FUNC sha1_begin(sha1_ctx_t *ctx) } static const uint32_t init256[] = { + 0, + 0, 0x6a09e667, 0xbb67ae85, 0x3c6ef372, @@ -316,10 +361,10 @@ static const uint32_t init256[] = { 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, - 0, - 0, }; static const uint32_t init512_lo[] = { + 0, + 0, 0xf3bcc908, 0x84caa73b, 0xfe94f82b, @@ -328,16 +373,14 @@ static const uint32_t init512_lo[] = { 0x2b3e6c1f, 0xfb41bd6b, 0x137e2179, - 0, - 0, }; /* Initialize structure containing state of computation. (FIPS 180-2:5.3.2) */ void FAST_FUNC sha256_begin(sha256_ctx_t *ctx) { - memcpy(ctx->hash, init256, sizeof(init256)); - /*ctx->total64 = 0; - done by extending init256 with two 32-bit zeros */ + memcpy(&ctx->total64, init256, sizeof(init256)); + /*ctx->total64 = 0; - done by prepending two 32-bit zeros to init256 */ ctx->process_block = sha256_process_block64; } @@ -346,9 +389,10 @@ void FAST_FUNC sha256_begin(sha256_ctx_t *ctx) void FAST_FUNC sha512_begin(sha512_ctx_t *ctx) { int i; - /* Two extra iterations zero out ctx->total64[] */ - for (i = 0; i < 8+2; i++) - ctx->hash[i] = ((uint64_t)(init256[i]) << 32) + init512_lo[i]; + /* Two extra iterations zero out ctx->total64[2] */ + uint64_t *tp = ctx->total64; + for (i = 0; i < 2+8; i++) + tp[i] = ((uint64_t)(init256[i]) << 32) + init512_lo[i]; /*ctx->total64[0] = ctx->total64[1] = 0; - already done */ } @@ -448,37 +492,19 @@ void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) /* Used also for sha256 */ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) { - unsigned bufpos = ctx->total64 & 63; + unsigned hash_size; - /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ - ctx->wbuffer[bufpos++] = 0x80; + /* SHA stores total in BE, need to swap on LE arches: */ + common64_end(ctx, (process_block64_func*) ctx->process_block, /*swap_needed:*/ BB_LITTLE_ENDIAN); - /* This loop iterates either once or twice, no more, no less */ - while (1) { - unsigned remaining = 64 - bufpos; - 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 */ - uint64_t t = ctx->total64 << 3; - t = SWAP_BE64(t); - /* wbuffer is suitably aligned for this */ - *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t; - } - ctx->process_block(ctx); - if (remaining >= 8) - break; - bufpos = 0; - } - - bufpos = (ctx->process_block == sha1_process_block64) ? 5 : 8; + hash_size = (ctx->process_block == sha1_process_block64) ? 5 : 8; /* This way we do not impose alignment constraints on resbuf: */ if (BB_LITTLE_ENDIAN) { unsigned i; - for (i = 0; i < bufpos; ++i) + for (i = 0; i < hash_size; ++i) ctx->hash[i] = SWAP_BE32(ctx->hash[i]); } - memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * bufpos); + memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * hash_size); } void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) @@ -566,7 +592,7 @@ void FAST_FUNC md5_begin(md5_ctx_t *ctx) #define FI(b, c, d) (c ^ (b | ~d)) /* Hash a single block, 64 bytes long and 4-byte aligned */ -static void md5_process_block64(md5_ctx_t *ctx) +static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) { #if MD5_SIZE_VS_SPEED > 0 /* Before we start, one word to the strange constants. @@ -927,27 +953,8 @@ void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) */ void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) { - unsigned bufpos = ctx->total64 & 63; - /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ - ctx->wbuffer[bufpos++] = 0x80; - - /* This loop iterates either once or twice, no more, no less */ - while (1) { - unsigned remaining = 64 - bufpos; - 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 LE format */ - uint64_t t = ctx->total64 << 3; - t = SWAP_LE64(t); - /* wbuffer is suitably aligned for this */ - *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t; - } - md5_process_block64(ctx); - if (remaining >= 8) - break; - bufpos = 0; - } + /* MD5 stores total in LE, need to swap on BE arches: */ + common64_end(ctx, (process_block64_func*) md5_process_block64, /*swap_needed:*/ BB_BIG_ENDIAN); /* The MD5 result is in little endian byte order. * We (ab)use the fact that A-D are consecutive in memory. -- cgit v1.2.3-55-g6feb From d982da79deb36185c420f94417b57e7b8a4af04c Mon Sep 17 00:00:00 2001 From: Baruch Siach Date: Mon, 18 Oct 2010 11:35:30 +0200 Subject: nanddump: make oobbuf allocation dynamic In accordance with upstream mtd-utils commit 96a5eeaf (mtd-utils: nanddump: Dynamic buffer, increase pagesize/oobsize). Signed-off-by: Baruch Siach Signed-off-by: Denys Vlasenko --- miscutils/nandwrite.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/miscutils/nandwrite.c b/miscutils/nandwrite.c index 6c85ea346..de30a0c1f 100644 --- a/miscutils/nandwrite.c +++ b/miscutils/nandwrite.c @@ -60,7 +60,6 @@ #define OPT_f (1 << 3) #define OPT_l (1 << 4) -#define NAND_MAX_OOBSIZE 256 /* helper for writing out 0xff for bad blocks pad */ static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob) { @@ -103,7 +102,7 @@ int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int nandwrite_main(int argc UNUSED_PARAM, char **argv) { /* Buffer for OOB data */ - unsigned char oobbuf[NAND_MAX_OOBSIZE]; + unsigned char *oobbuf; unsigned opts; int fd; ssize_t cnt; @@ -135,10 +134,6 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv) fd = xopen(argv[0], O_RDWR); xioctl(fd, MEMGETINFO, &meminfo); - oob.start = 0; - oob.length = meminfo.oobsize; - oob.ptr = oobbuf; - mtdoffset = xstrtou(opt_s, 0); if (IS_NANDDUMP && (opts & OPT_l)) { unsigned length = xstrtou(opt_l, 0); @@ -153,6 +148,11 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv) bb_error_msg_and_die("start address is not page aligned"); filebuf = xmalloc(meminfo_writesize); + oobbuf = xmalloc(meminfo.oobsize); + + oob.start = 0; + oob.length = meminfo.oobsize; + oob.ptr = oobbuf; blockstart = mtdoffset & ~(meminfo.erasesize - 1); if (blockstart != mtdoffset) { -- cgit v1.2.3-55-g6feb From 302ad1450e104460abd3aae60b7f40d8a88f43df Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 19 Oct 2010 02:16:12 +0200 Subject: libbb/hash_md5_sha: use common ctx and code for md5 and sha1/256 function old new delta sha256_process_block64 421 433 +12 md5_crypt 578 587 +9 md5_begin 43 50 +7 md5_hash 99 97 -2 sha1_end 85 82 -3 md5_end 36 31 -5 common64_end 93 86 -7 sha1_hash 97 - -97 Signed-off-by: Denys Vlasenko --- include/libbb.h | 44 +- libbb/hash_md5_sha.c | 1542 ++++++++++++++++++++++++-------------------------- 2 files changed, 752 insertions(+), 834 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 3c8764b5c..01dc33e63 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1515,44 +1515,28 @@ enum { }; void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags); -#if 1 typedef struct md5_ctx_t { - char wbuffer[64]; /* NB: always correctly aligned for uint64_t */ - uint64_t total64; - uint32_t A; - uint32_t B; - uint32_t C; - uint32_t D; -} md5_ctx_t; -#else -/* libbb/md5prime.c uses a bit different one: */ -typedef struct md5_ctx_t { - uint32_t state[4]; /* state (ABCD) */ - uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ - unsigned char buffer[64]; /* input buffer */ + uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */ + void (*process_block)(struct md5_ctx_t*) FAST_FUNC; + uint64_t total64; /* must be directly before hash[] */ + uint32_t hash[8]; /* 4 elements for md5, 5 for sha1, 8 for sha256 */ } md5_ctx_t; -#endif +typedef struct md5_ctx_t sha1_ctx_t; +typedef struct md5_ctx_t sha256_ctx_t; +typedef struct sha512_ctx_t { + uint64_t total64[2]; /* must be directly before hash[] */ + uint64_t hash[8]; + uint8_t wbuffer[128]; /* always correctly aligned for uint64_t */ +} sha512_ctx_t; void md5_begin(md5_ctx_t *ctx) FAST_FUNC; void md5_hash(md5_ctx_t *ctx, const void *data, size_t length) FAST_FUNC; void md5_end(md5_ctx_t *ctx, void *resbuf) FAST_FUNC; -typedef struct sha1_ctx_t { - uint8_t wbuffer[64]; /* NB: always correctly aligned for uint64_t */ - uint64_t total64; /* must be directly before hash[] */ - uint32_t hash[8]; /* 5, +3 elements for sha256 */ - void (*process_block)(struct sha1_ctx_t*) FAST_FUNC; -} sha1_ctx_t; void sha1_begin(sha1_ctx_t *ctx) FAST_FUNC; -void sha1_hash(sha1_ctx_t *ctx, const void *data, size_t length) FAST_FUNC; +#define sha1_hash md5_hash void sha1_end(sha1_ctx_t *ctx, void *resbuf) FAST_FUNC; -typedef struct sha1_ctx_t sha256_ctx_t; void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC; -#define sha256_hash sha1_hash -#define sha256_end sha1_end -typedef struct sha512_ctx_t { - uint64_t total64[2]; /* must be directly before hash[] */ - uint64_t hash[8]; - uint8_t wbuffer[128]; /* NB: always correctly aligned for uint64_t */ -} sha512_ctx_t; +#define sha256_hash md5_hash +#define sha256_end sha1_end void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC; void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; void sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC; diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c index b07ba55f7..f5f875a64 100644 --- a/libbb/hash_md5_sha.c +++ b/libbb/hash_md5_sha.c @@ -32,16 +32,38 @@ static ALWAYS_INLINE uint64_t rotr64(uint64_t x, unsigned n) } -typedef struct common64_ctx_t { - char wbuffer[64]; /* NB: always correctly aligned for uint64_t */ - uint64_t total64; -} common64_ctx_t; +/* Feed data through a temporary buffer. + * The internal buffer remembers previous data until it has 64 + * bytes worth to pass on. + */ +static void FAST_FUNC common64_hash(md5_ctx_t *ctx, const void *buffer, size_t len) +{ + unsigned bufpos = ctx->total64 & 63; + + ctx->total64 += len; -typedef void FAST_FUNC process_block64_func(void*); + while (1) { + unsigned remaining = 64 - bufpos; + if (remaining > len) + remaining = len; + /* Copy data into aligned buffer */ + memcpy(ctx->wbuffer + bufpos, buffer, remaining); + len -= remaining; + buffer = (const char *)buffer + remaining; + bufpos += remaining; + /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */ + bufpos -= 64; + if (bufpos != 0) + break; + /* Buffer is filled up, process it */ + ctx->process_block(ctx); + /*bufpos = 0; - already is */ + } +} -static void FAST_FUNC common64_end(void *vctx, process_block64_func process_block64, int swap_needed) +/* Process the remaining bytes in the buffer */ +static void FAST_FUNC common64_end(md5_ctx_t *ctx, int swap_needed) { - common64_ctx_t *ctx = vctx; unsigned bufpos = ctx->total64 & 63; /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ ctx->wbuffer[bufpos++] = 0x80; @@ -59,7 +81,7 @@ static void FAST_FUNC common64_end(void *vctx, process_block64_func process_bloc /* wbuffer is suitably aligned for this */ *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t; } - process_block64(ctx); + ctx->process_block(ctx); if (remaining >= 8) break; bufpos = 0; @@ -68,866 +90,748 @@ static void FAST_FUNC common64_end(void *vctx, process_block64_func process_bloc /* - * Based on shasum from http://www.netsw.org/crypto/hash/ - * Majorly hacked up to use Dr Brian Gladman's sha1 code + * Compute MD5 checksum of strings according to the + * definition of MD5 in RFC 1321 from April 1992. * - * Copyright (C) 2002 Dr Brian Gladman , Worcester, UK. + * Written by Ulrich Drepper , 1995. + * + * Copyright (C) 1995-1999 Free Software Foundation, Inc. + * Copyright (C) 2001 Manuel Novoa III * Copyright (C) 2003 Glenn L. McGrath * Copyright (C) 2003 Erik Andersen * * Licensed under GPLv2 or later, see file LICENSE in this source tree. - * - * --------------------------------------------------------------------------- - * Issue Date: 10/11/2002 - * - * This is a byte oriented version of SHA1 that operates on arrays of bytes - * stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor - * - * --------------------------------------------------------------------------- - * - * SHA256 and SHA512 parts are: - * Released into the Public Domain by Ulrich Drepper . - * Shrank by Denys Vlasenko. - * - * --------------------------------------------------------------------------- - * - * The best way to test random blocksizes is to go to coreutils/md5_sha1_sum.c - * and replace "4096" with something like "2000 + time(NULL) % 2097", - * then rebuild and compare "shaNNNsum bigfile" results. */ -static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx) -{ - unsigned t; - uint32_t W[80], a, b, c, d, e; - const uint32_t *words = (uint32_t*) ctx->wbuffer; +/* 0: fastest, 3: smallest */ +#if CONFIG_MD5_SIZE_VS_SPEED < 0 +# define MD5_SIZE_VS_SPEED 0 +#elif CONFIG_MD5_SIZE_VS_SPEED > 3 +# define MD5_SIZE_VS_SPEED 3 +#else +# define MD5_SIZE_VS_SPEED CONFIG_MD5_SIZE_VS_SPEED +#endif - for (t = 0; t < 16; ++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); - } +/* These are the four functions used in the four steps of the MD5 algorithm + * and defined in the RFC 1321. The first function is a little bit optimized + * (as found in Colin Plumbs public domain implementation). + * #define FF(b, c, d) ((b & c) | (~b & d)) + */ +#undef FF +#undef FG +#undef FH +#undef FI +#define FF(b, c, d) (d ^ (b & (c ^ d))) +#define FG(b, c, d) FF(d, b, c) +#define FH(b, c, d) (b ^ c ^ d) +#define FI(b, c, d) (c ^ (b | ~d)) - a = ctx->hash[0]; - b = ctx->hash[1]; - c = ctx->hash[2]; - d = ctx->hash[3]; - e = ctx->hash[4]; +/* Hash a single block, 64 bytes long and 4-byte aligned */ +static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) +{ +#if MD5_SIZE_VS_SPEED > 0 + /* Before we start, one word to the strange constants. + They are defined in RFC 1321 as + T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64 + */ + static const uint32_t C_array[] = { + /* round 1 */ + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, + /* round 2 */ + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, + 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, + /* round 3 */ + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, + /* round 4 */ + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 + }; + static const char P_array[] ALIGN1 = { +# if MD5_SIZE_VS_SPEED > 1 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */ +# endif + 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */ + 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */ + 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */ + }; +#endif + uint32_t *words = (void*) ctx->wbuffer; + uint32_t A = ctx->hash[0]; + uint32_t B = ctx->hash[1]; + uint32_t C = ctx->hash[2]; + uint32_t D = ctx->hash[3]; -#undef ch -#undef parity -#undef maj -#undef rnd -#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) -#define parity(x,y,z) ((x) ^ (y) ^ (z)) -#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y)))) -/* A normal version as set out in the FIPS. */ -#define rnd(f,k) \ - do { \ - uint32_t T = a; \ - a = rotl32(a, 5) + f(b, c, d) + e + k + W[t]; \ - e = d; \ - d = c; \ - c = rotl32(b, 30); \ - b = T; \ - } while (0) +#if MD5_SIZE_VS_SPEED >= 2 /* 2 or 3 */ - for (t = 0; t < 20; ++t) - rnd(ch, 0x5a827999); + static const char S_array[] ALIGN1 = { + 7, 12, 17, 22, + 5, 9, 14, 20, + 4, 11, 16, 23, + 6, 10, 15, 21 + }; + const uint32_t *pc; + const char *pp; + const char *ps; + int i; + uint32_t temp; - for (/*t = 20*/; t < 40; ++t) - rnd(parity, 0x6ed9eba1); +# if BB_BIG_ENDIAN + for (i = 0; i < 16; i++) + words[i] = SWAP_LE32(words[i]); +# endif - for (/*t = 40*/; t < 60; ++t) - rnd(maj, 0x8f1bbcdc); +# if MD5_SIZE_VS_SPEED == 3 + pc = C_array; + pp = P_array; + ps = S_array - 4; - for (/*t = 60*/; t < 80; ++t) - rnd(parity, 0xca62c1d6); -#undef ch -#undef parity -#undef maj -#undef rnd + for (i = 0; i < 64; i++) { + if ((i & 0x0f) == 0) + ps += 4; + temp = A; + switch (i >> 4) { + case 0: + temp += FF(B, C, D); + break; + case 1: + temp += FG(B, C, D); + break; + case 2: + temp += FH(B, C, D); + break; + case 3: + temp += FI(B, C, D); + } + temp += words[(int) (*pp++)] + *pc++; + temp = rotl32(temp, ps[i & 3]); + temp += B; + A = D; + D = C; + C = B; + B = temp; + } +# else /* MD5_SIZE_VS_SPEED == 2 */ + pc = C_array; + pp = P_array; + ps = S_array; - ctx->hash[0] += a; - ctx->hash[1] += b; - ctx->hash[2] += c; - ctx->hash[3] += d; - ctx->hash[4] += e; -} + for (i = 0; i < 16; i++) { + temp = A + FF(B, C, D) + words[(int) (*pp++)] + *pc++; + temp = rotl32(temp, ps[i & 3]); + temp += B; + A = D; + D = C; + C = B; + B = temp; + } + ps += 4; + for (i = 0; i < 16; i++) { + temp = A + FG(B, C, D) + words[(int) (*pp++)] + *pc++; + temp = rotl32(temp, ps[i & 3]); + temp += B; + A = D; + D = C; + C = B; + B = temp; + } + ps += 4; + for (i = 0; i < 16; i++) { + temp = A + FH(B, C, D) + words[(int) (*pp++)] + *pc++; + temp = rotl32(temp, ps[i & 3]); + temp += B; + A = D; + D = C; + C = B; + B = temp; + } + ps += 4; + for (i = 0; i < 16; i++) { + temp = A + FI(B, C, D) + words[(int) (*pp++)] + *pc++; + temp = rotl32(temp, ps[i & 3]); + temp += B; + A = D; + D = C; + C = B; + B = temp; + } +# endif + /* Add checksum to the starting values */ + ctx->hash[0] += A; + ctx->hash[1] += B; + ctx->hash[2] += C; + ctx->hash[3] += D; -/* Constants for SHA512 from FIPS 180-2:4.2.3. - * SHA256 constants from FIPS 180-2:4.2.2 - * are the most significant half of first 64 elements - * of the same array. - */ -static const uint64_t sha_K[80] = { - 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, - 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, - 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, - 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, - 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, - 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, - 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, - 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, - 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, - 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, - 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, - 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, - 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, - 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, - 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, - 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, - 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, - 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, - 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, - 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, - 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, - 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, - 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, - 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, - 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, - 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, - 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, - 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, - 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, - 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, - 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, - 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, - 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, /* [64]+ are used for sha512 only */ - 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, - 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, - 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, - 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, - 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, - 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, - 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL -}; - -#undef Ch -#undef Maj -#undef S0 -#undef S1 -#undef R0 -#undef R1 - -static void FAST_FUNC sha256_process_block64(sha256_ctx_t *ctx) -{ - unsigned t; - uint32_t W[64], a, b, c, d, e, f, g, h; - const uint32_t *words = (uint32_t*) ctx->wbuffer; - - /* Operators defined in FIPS 180-2:4.1.2. */ -#define Ch(x, y, z) ((x & y) ^ (~x & z)) -#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) -#define S0(x) (rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22)) -#define S1(x) (rotr32(x, 6) ^ rotr32(x, 11) ^ rotr32(x, 25)) -#define R0(x) (rotr32(x, 7) ^ rotr32(x, 18) ^ (x >> 3)) -#define R1(x) (rotr32(x, 17) ^ rotr32(x, 19) ^ (x >> 10)) - - /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */ - for (t = 0; t < 16; ++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]; - - a = ctx->hash[0]; - b = ctx->hash[1]; - c = ctx->hash[2]; - d = ctx->hash[3]; - e = ctx->hash[4]; - f = ctx->hash[5]; - g = ctx->hash[6]; - h = ctx->hash[7]; - - /* The actual computation according to FIPS 180-2:6.2.2 step 3. */ - for (t = 0; t < 64; ++t) { - /* Need to fetch upper half of sha_K[t] - * (I hope compiler is clever enough to just fetch - * upper half) - */ - uint32_t K_t = sha_K[t] >> 32; - uint32_t T1 = h + S1(e) + Ch(e, f, g) + K_t + W[t]; - uint32_t T2 = S0(a) + Maj(a, b, c); - h = g; - g = f; - f = e; - e = d + T1; - d = c; - c = b; - b = a; - a = T1 + T2; - } -#undef Ch -#undef Maj -#undef S0 -#undef S1 -#undef R0 -#undef R1 - /* Add the starting values of the context according to FIPS 180-2:6.2.2 - step 4. */ - ctx->hash[0] += a; - ctx->hash[1] += b; - ctx->hash[2] += c; - ctx->hash[3] += d; - ctx->hash[4] += e; - ctx->hash[5] += f; - ctx->hash[6] += g; - ctx->hash[7] += h; -} - -static void FAST_FUNC sha512_process_block128(sha512_ctx_t *ctx) -{ - unsigned t; - uint64_t W[80]; - /* On i386, having assignments here (not later as sha256 does) - * produces 99 bytes smaller code with gcc 4.3.1 - */ - uint64_t a = ctx->hash[0]; - uint64_t b = ctx->hash[1]; - uint64_t c = ctx->hash[2]; - uint64_t d = ctx->hash[3]; - uint64_t e = ctx->hash[4]; - uint64_t f = ctx->hash[5]; - uint64_t g = ctx->hash[6]; - uint64_t h = ctx->hash[7]; - const uint64_t *words = (uint64_t*) ctx->wbuffer; - - /* Operators defined in FIPS 180-2:4.1.2. */ -#define Ch(x, y, z) ((x & y) ^ (~x & z)) -#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) -#define S0(x) (rotr64(x, 28) ^ rotr64(x, 34) ^ rotr64(x, 39)) -#define S1(x) (rotr64(x, 14) ^ rotr64(x, 18) ^ rotr64(x, 41)) -#define R0(x) (rotr64(x, 1) ^ rotr64(x, 8) ^ (x >> 7)) -#define R1(x) (rotr64(x, 19) ^ rotr64(x, 61) ^ (x >> 6)) - - /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */ - for (t = 0; t < 16; ++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]; - - /* The actual computation according to FIPS 180-2:6.3.2 step 3. */ - for (t = 0; t < 80; ++t) { - uint64_t T1 = h + S1(e) + Ch(e, f, g) + sha_K[t] + W[t]; - uint64_t T2 = S0(a) + Maj(a, b, c); - h = g; - g = f; - f = e; - e = d + T1; - d = c; - c = b; - b = a; - a = T1 + T2; - } -#undef Ch -#undef Maj -#undef S0 -#undef S1 -#undef R0 -#undef R1 - /* Add the starting values of the context according to FIPS 180-2:6.3.2 - step 4. */ - ctx->hash[0] += a; - ctx->hash[1] += b; - ctx->hash[2] += c; - ctx->hash[3] += d; - ctx->hash[4] += e; - ctx->hash[5] += f; - ctx->hash[6] += g; - ctx->hash[7] += h; -} - - -void FAST_FUNC sha1_begin(sha1_ctx_t *ctx) -{ - ctx->hash[0] = 0x67452301; - ctx->hash[1] = 0xefcdab89; - ctx->hash[2] = 0x98badcfe; - ctx->hash[3] = 0x10325476; - ctx->hash[4] = 0xc3d2e1f0; - ctx->total64 = 0; - ctx->process_block = sha1_process_block64; -} - -static const uint32_t init256[] = { - 0, - 0, - 0x6a09e667, - 0xbb67ae85, - 0x3c6ef372, - 0xa54ff53a, - 0x510e527f, - 0x9b05688c, - 0x1f83d9ab, - 0x5be0cd19, -}; -static const uint32_t init512_lo[] = { - 0, - 0, - 0xf3bcc908, - 0x84caa73b, - 0xfe94f82b, - 0x5f1d36f1, - 0xade682d1, - 0x2b3e6c1f, - 0xfb41bd6b, - 0x137e2179, -}; - -/* Initialize structure containing state of computation. - (FIPS 180-2:5.3.2) */ -void FAST_FUNC sha256_begin(sha256_ctx_t *ctx) -{ - memcpy(&ctx->total64, init256, sizeof(init256)); - /*ctx->total64 = 0; - done by prepending two 32-bit zeros to init256 */ - ctx->process_block = sha256_process_block64; -} +#else /* MD5_SIZE_VS_SPEED == 0 or 1 */ -/* Initialize structure containing state of computation. - (FIPS 180-2:5.3.3) */ -void FAST_FUNC sha512_begin(sha512_ctx_t *ctx) -{ + uint32_t A_save = A; + uint32_t B_save = B; + uint32_t C_save = C; + uint32_t D_save = D; +# if MD5_SIZE_VS_SPEED == 1 + const uint32_t *pc; + const char *pp; int i; - /* Two extra iterations zero out ctx->total64[2] */ - uint64_t *tp = ctx->total64; - for (i = 0; i < 2+8; i++) - tp[i] = ((uint64_t)(init256[i]) << 32) + init512_lo[i]; - /*ctx->total64[0] = ctx->total64[1] = 0; - already done */ -} - - -/* Used also for sha256 */ -void FAST_FUNC sha1_hash(sha1_ctx_t *ctx, const void *buffer, size_t len) -{ - unsigned bufpos = ctx->total64 & 63; - unsigned remaining; - - ctx->total64 += len; -#if 0 - remaining = 64 - bufpos; +# endif - /* Hash whole blocks */ - while (len >= remaining) { - memcpy(ctx->wbuffer + bufpos, buffer, remaining); - buffer = (const char *)buffer + remaining; - len -= remaining; - remaining = 64; - bufpos = 0; - ctx->process_block(ctx); - } + /* First round: using the given function, the context and a constant + the next context is computed. Because the algorithm's processing + unit is a 32-bit word and it is determined to work on words in + little endian byte order we perhaps have to change the byte order + before the computation. To reduce the work for the next steps + we save swapped words in WORDS array. */ +# undef OP +# define OP(a, b, c, d, s, T) \ + do { \ + a += FF(b, c, d) + (*words IF_BIG_ENDIAN(= SWAP_LE32(*words))) + T; \ + words++; \ + a = rotl32(a, s); \ + a += b; \ + } while (0) - /* Save last, partial blosk */ - memcpy(ctx->wbuffer + bufpos, buffer, len); -#else - /* Tiny bit smaller code */ - while (1) { - remaining = 64 - bufpos; - if (remaining > len) - remaining = len; - /* Copy data into aligned buffer */ - memcpy(ctx->wbuffer + bufpos, buffer, remaining); - len -= remaining; - buffer = (const char *)buffer + remaining; - bufpos += remaining; - /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */ - bufpos -= 64; - if (bufpos != 0) - break; - /* Buffer is filled up, process it */ - ctx->process_block(ctx); - /*bufpos = 0; - already is */ + /* Round 1 */ +# if MD5_SIZE_VS_SPEED == 1 + pc = C_array; + for (i = 0; i < 4; i++) { + OP(A, B, C, D, 7, *pc++); + OP(D, A, B, C, 12, *pc++); + OP(C, D, A, B, 17, *pc++); + OP(B, C, D, A, 22, *pc++); } -#endif -} +# else + OP(A, B, C, D, 7, 0xd76aa478); + OP(D, A, B, C, 12, 0xe8c7b756); + OP(C, D, A, B, 17, 0x242070db); + OP(B, C, D, A, 22, 0xc1bdceee); + OP(A, B, C, D, 7, 0xf57c0faf); + OP(D, A, B, C, 12, 0x4787c62a); + OP(C, D, A, B, 17, 0xa8304613); + OP(B, C, D, A, 22, 0xfd469501); + OP(A, B, C, D, 7, 0x698098d8); + OP(D, A, B, C, 12, 0x8b44f7af); + OP(C, D, A, B, 17, 0xffff5bb1); + OP(B, C, D, A, 22, 0x895cd7be); + OP(A, B, C, D, 7, 0x6b901122); + OP(D, A, B, C, 12, 0xfd987193); + OP(C, D, A, B, 17, 0xa679438e); + OP(B, C, D, A, 22, 0x49b40821); +# endif + words -= 16; -void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) -{ - unsigned bufpos = ctx->total64[0] & 127; - unsigned remaining; + /* For the second to fourth round we have the possibly swapped words + in WORDS. Redefine the macro to take an additional first + argument specifying the function to use. */ +# undef OP +# define OP(f, a, b, c, d, k, s, T) \ + do { \ + a += f(b, c, d) + words[k] + T; \ + a = rotl32(a, s); \ + a += b; \ + } while (0) - /* First increment the byte count. FIPS 180-2 specifies the possible - length of the file up to 2^128 _bits_. - We compute the number of _bytes_ and convert to bits later. */ - ctx->total64[0] += len; - if (ctx->total64[0] < len) - ctx->total64[1]++; -#if 0 - remaining = 128 - bufpos; + /* Round 2 */ +# if MD5_SIZE_VS_SPEED == 1 + pp = P_array; + for (i = 0; i < 4; i++) { + OP(FG, A, B, C, D, (int) (*pp++), 5, *pc++); + OP(FG, D, A, B, C, (int) (*pp++), 9, *pc++); + OP(FG, C, D, A, B, (int) (*pp++), 14, *pc++); + OP(FG, B, C, D, A, (int) (*pp++), 20, *pc++); + } +# else + OP(FG, A, B, C, D, 1, 5, 0xf61e2562); + OP(FG, D, A, B, C, 6, 9, 0xc040b340); + OP(FG, C, D, A, B, 11, 14, 0x265e5a51); + OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa); + OP(FG, A, B, C, D, 5, 5, 0xd62f105d); + OP(FG, D, A, B, C, 10, 9, 0x02441453); + OP(FG, C, D, A, B, 15, 14, 0xd8a1e681); + OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8); + OP(FG, A, B, C, D, 9, 5, 0x21e1cde6); + OP(FG, D, A, B, C, 14, 9, 0xc33707d6); + OP(FG, C, D, A, B, 3, 14, 0xf4d50d87); + OP(FG, B, C, D, A, 8, 20, 0x455a14ed); + OP(FG, A, B, C, D, 13, 5, 0xa9e3e905); + OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8); + OP(FG, C, D, A, B, 7, 14, 0x676f02d9); + OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a); +# endif - /* Hash whole blocks */ - while (len >= remaining) { - memcpy(ctx->wbuffer + bufpos, buffer, remaining); - buffer = (const char *)buffer + remaining; - len -= remaining; - remaining = 128; - bufpos = 0; - sha512_process_block128(ctx); + /* Round 3 */ +# if MD5_SIZE_VS_SPEED == 1 + for (i = 0; i < 4; i++) { + OP(FH, A, B, C, D, (int) (*pp++), 4, *pc++); + OP(FH, D, A, B, C, (int) (*pp++), 11, *pc++); + OP(FH, C, D, A, B, (int) (*pp++), 16, *pc++); + OP(FH, B, C, D, A, (int) (*pp++), 23, *pc++); } +# else + OP(FH, A, B, C, D, 5, 4, 0xfffa3942); + OP(FH, D, A, B, C, 8, 11, 0x8771f681); + OP(FH, C, D, A, B, 11, 16, 0x6d9d6122); + OP(FH, B, C, D, A, 14, 23, 0xfde5380c); + OP(FH, A, B, C, D, 1, 4, 0xa4beea44); + OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9); + OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60); + OP(FH, B, C, D, A, 10, 23, 0xbebfbc70); + OP(FH, A, B, C, D, 13, 4, 0x289b7ec6); + OP(FH, D, A, B, C, 0, 11, 0xeaa127fa); + OP(FH, C, D, A, B, 3, 16, 0xd4ef3085); + OP(FH, B, C, D, A, 6, 23, 0x04881d05); + OP(FH, A, B, C, D, 9, 4, 0xd9d4d039); + OP(FH, D, A, B, C, 12, 11, 0xe6db99e5); + OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8); + OP(FH, B, C, D, A, 2, 23, 0xc4ac5665); +# endif - /* Save last, partial blosk */ - memcpy(ctx->wbuffer + bufpos, buffer, len); -#else - while (1) { - remaining = 128 - bufpos; - if (remaining > len) - remaining = len; - /* Copy data into aligned buffer */ - memcpy(ctx->wbuffer + bufpos, buffer, remaining); - len -= remaining; - buffer = (const char *)buffer + remaining; - bufpos += remaining; - /* clever way to do "if (bufpos != 128) break; ... ; bufpos = 0;" */ - bufpos -= 128; - if (bufpos != 0) - break; - /* Buffer is filled up, process it */ - sha512_process_block128(ctx); - /*bufpos = 0; - already is */ + /* Round 4 */ +# if MD5_SIZE_VS_SPEED == 1 + for (i = 0; i < 4; i++) { + OP(FI, A, B, C, D, (int) (*pp++), 6, *pc++); + OP(FI, D, A, B, C, (int) (*pp++), 10, *pc++); + OP(FI, C, D, A, B, (int) (*pp++), 15, *pc++); + OP(FI, B, C, D, A, (int) (*pp++), 21, *pc++); } +# else + OP(FI, A, B, C, D, 0, 6, 0xf4292244); + OP(FI, D, A, B, C, 7, 10, 0x432aff97); + OP(FI, C, D, A, B, 14, 15, 0xab9423a7); + OP(FI, B, C, D, A, 5, 21, 0xfc93a039); + OP(FI, A, B, C, D, 12, 6, 0x655b59c3); + OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92); + OP(FI, C, D, A, B, 10, 15, 0xffeff47d); + OP(FI, B, C, D, A, 1, 21, 0x85845dd1); + OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f); + OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0); + OP(FI, C, D, A, B, 6, 15, 0xa3014314); + OP(FI, B, C, D, A, 13, 21, 0x4e0811a1); + OP(FI, A, B, C, D, 4, 6, 0xf7537e82); + OP(FI, D, A, B, C, 11, 10, 0xbd3af235); + OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb); + OP(FI, B, C, D, A, 9, 21, 0xeb86d391); +# undef OP +# endif + /* Add checksum to the starting values */ + ctx->hash[0] = A_save + A; + ctx->hash[1] = B_save + B; + ctx->hash[2] = C_save + C; + ctx->hash[3] = D_save + D; #endif } +#undef FF +#undef FG +#undef FH +#undef FI - -/* Used also for sha256 */ -void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) +/* Initialize structure containing state of computation. + * (RFC 1321, 3.3: Step 3) + */ +void FAST_FUNC md5_begin(md5_ctx_t *ctx) { - unsigned hash_size; - - /* SHA stores total in BE, need to swap on LE arches: */ - common64_end(ctx, (process_block64_func*) ctx->process_block, /*swap_needed:*/ BB_LITTLE_ENDIAN); - - hash_size = (ctx->process_block == sha1_process_block64) ? 5 : 8; - /* This way we do not impose alignment constraints on resbuf: */ - if (BB_LITTLE_ENDIAN) { - unsigned i; - for (i = 0; i < hash_size; ++i) - ctx->hash[i] = SWAP_BE32(ctx->hash[i]); - } - memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * hash_size); + ctx->hash[0] = 0x67452301; + ctx->hash[1] = 0xefcdab89; + ctx->hash[2] = 0x98badcfe; + ctx->hash[3] = 0x10325476; + ctx->total64 = 0; + ctx->process_block = md5_process_block64; } -void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) +/* Used also for sha1 and sha256 */ +void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) { - unsigned bufpos = ctx->total64[0] & 127; - - /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0... */ - ctx->wbuffer[bufpos++] = 0x80; + common64_hash(ctx, buffer, len); +} - while (1) { - unsigned remaining = 128 - bufpos; - memset(ctx->wbuffer + bufpos, 0, remaining); - if (remaining >= 16) { - /* Store the 128-bit counter of bits in the buffer in BE format */ - uint64_t t; - t = ctx->total64[0] << 3; - t = SWAP_BE64(t); - *(uint64_t *) (&ctx->wbuffer[128 - 8]) = t; - t = (ctx->total64[1] << 3) | (ctx->total64[0] >> 61); - t = SWAP_BE64(t); - *(uint64_t *) (&ctx->wbuffer[128 - 16]) = t; - } - sha512_process_block128(ctx); - if (remaining >= 16) - break; - bufpos = 0; - } +/* Process the remaining bytes in the buffer and put result from CTX + * in first 16 bytes following RESBUF. The result is always in little + * endian byte order, so that a byte-wise output yields to the wanted + * ASCII representation of the message digest. + */ +void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) +{ + /* MD5 stores total in LE, need to swap on BE arches: */ + common64_end(ctx, /*swap_needed:*/ BB_BIG_ENDIAN); - if (BB_LITTLE_ENDIAN) { - unsigned i; - for (i = 0; i < ARRAY_SIZE(ctx->hash); ++i) - ctx->hash[i] = SWAP_BE64(ctx->hash[i]); - } - memcpy(resbuf, ctx->hash, sizeof(ctx->hash)); + /* The MD5 result is in little endian byte order. + * We (ab)use the fact that A-D are consecutive in memory. + */ +#if BB_BIG_ENDIAN + ctx->hash[0] = SWAP_LE32(ctx->hash[0]); + ctx->hash[1] = SWAP_LE32(ctx->hash[1]); + ctx->hash[2] = SWAP_LE32(ctx->hash[2]); + ctx->hash[3] = SWAP_LE32(ctx->hash[3]); +#endif + memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * 4); } /* - * Compute MD5 checksum of strings according to the - * definition of MD5 in RFC 1321 from April 1992. - * - * Written by Ulrich Drepper , 1995. + * Based on shasum from http://www.netsw.org/crypto/hash/ + * Majorly hacked up to use Dr Brian Gladman's sha1 code * - * Copyright (C) 1995-1999 Free Software Foundation, Inc. - * Copyright (C) 2001 Manuel Novoa III + * Copyright (C) 2002 Dr Brian Gladman , Worcester, UK. * Copyright (C) 2003 Glenn L. McGrath * Copyright (C) 2003 Erik Andersen * * Licensed under GPLv2 or later, see file LICENSE in this source tree. + * + * --------------------------------------------------------------------------- + * Issue Date: 10/11/2002 + * + * This is a byte oriented version of SHA1 that operates on arrays of bytes + * stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor + * + * --------------------------------------------------------------------------- + * + * SHA256 and SHA512 parts are: + * Released into the Public Domain by Ulrich Drepper . + * Shrank by Denys Vlasenko. + * + * --------------------------------------------------------------------------- + * + * The best way to test random blocksizes is to go to coreutils/md5_sha1_sum.c + * and replace "4096" with something like "2000 + time(NULL) % 2097", + * then rebuild and compare "shaNNNsum bigfile" results. */ -/* 0: fastest, 3: smallest */ -#if CONFIG_MD5_SIZE_VS_SPEED < 0 -# define MD5_SIZE_VS_SPEED 0 -#elif CONFIG_MD5_SIZE_VS_SPEED > 3 -# define MD5_SIZE_VS_SPEED 3 -#else -# define MD5_SIZE_VS_SPEED CONFIG_MD5_SIZE_VS_SPEED -#endif - -/* Initialize structure containing state of computation. - * (RFC 1321, 3.3: Step 3) - */ -void FAST_FUNC md5_begin(md5_ctx_t *ctx) +static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx) { - ctx->A = 0x67452301; - ctx->B = 0xefcdab89; - ctx->C = 0x98badcfe; - ctx->D = 0x10325476; - ctx->total64 = 0; + unsigned t; + uint32_t W[80], a, b, c, d, e; + const uint32_t *words = (uint32_t*) ctx->wbuffer; + + for (t = 0; t < 16; ++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); + } + + a = ctx->hash[0]; + b = ctx->hash[1]; + c = ctx->hash[2]; + d = ctx->hash[3]; + e = ctx->hash[4]; + +#undef ch +#undef parity +#undef maj +#undef rnd +#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) +#define parity(x,y,z) ((x) ^ (y) ^ (z)) +#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y)))) +/* A normal version as set out in the FIPS. */ +#define rnd(f,k) \ + do { \ + uint32_t T = a; \ + a = rotl32(a, 5) + f(b, c, d) + e + k + W[t]; \ + e = d; \ + d = c; \ + c = rotl32(b, 30); \ + b = T; \ + } while (0) + + for (t = 0; t < 20; ++t) + rnd(ch, 0x5a827999); + + for (/*t = 20*/; t < 40; ++t) + rnd(parity, 0x6ed9eba1); + + for (/*t = 40*/; t < 60; ++t) + rnd(maj, 0x8f1bbcdc); + + for (/*t = 60*/; t < 80; ++t) + rnd(parity, 0xca62c1d6); +#undef ch +#undef parity +#undef maj +#undef rnd + + ctx->hash[0] += a; + ctx->hash[1] += b; + ctx->hash[2] += c; + ctx->hash[3] += d; + ctx->hash[4] += e; } -/* These are the four functions used in the four steps of the MD5 algorithm - * and defined in the RFC 1321. The first function is a little bit optimized - * (as found in Colin Plumbs public domain implementation). - * #define FF(b, c, d) ((b & c) | (~b & d)) +/* Constants for SHA512 from FIPS 180-2:4.2.3. + * SHA256 constants from FIPS 180-2:4.2.2 + * are the most significant half of first 64 elements + * of the same array. */ -#undef FF -#undef FG -#undef FH -#undef FI -#define FF(b, c, d) (d ^ (b & (c ^ d))) -#define FG(b, c, d) FF(d, b, c) -#define FH(b, c, d) (b ^ c ^ d) -#define FI(b, c, d) (c ^ (b | ~d)) - -/* Hash a single block, 64 bytes long and 4-byte aligned */ -static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) -{ -#if MD5_SIZE_VS_SPEED > 0 - /* Before we start, one word to the strange constants. - They are defined in RFC 1321 as - T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64 - */ - static const uint32_t C_array[] = { - /* round 1 */ - 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, - 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, - 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, - 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, - /* round 2 */ - 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, - 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, - 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, - 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, - /* round 3 */ - 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, - 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, - 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, - 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, - /* round 4 */ - 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, - 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, - 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, - 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 - }; - static const char P_array[] ALIGN1 = { -# if MD5_SIZE_VS_SPEED > 1 - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */ -# endif - 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */ - 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */ - 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */ - }; -#endif - uint32_t *words = (void*) ctx->wbuffer; - uint32_t A = ctx->A; - uint32_t B = ctx->B; - uint32_t C = ctx->C; - uint32_t D = ctx->D; +static const uint64_t sha_K[80] = { + 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, + 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, + 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, + 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, + 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, + 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, + 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, + 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, + 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, + 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, + 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, + 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, + 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, + 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, + 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, + 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, + 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, + 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, + 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, + 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, + 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, + 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, /* [64]+ are used for sha512 only */ + 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, + 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, + 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, + 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, + 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, + 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL +}; -#if MD5_SIZE_VS_SPEED >= 2 /* 2 or 3 */ +#undef Ch +#undef Maj +#undef S0 +#undef S1 +#undef R0 +#undef R1 - static const char S_array[] ALIGN1 = { - 7, 12, 17, 22, - 5, 9, 14, 20, - 4, 11, 16, 23, - 6, 10, 15, 21 - }; - const uint32_t *pc; - const char *pp; - const char *ps; - int i; - uint32_t temp; +static void FAST_FUNC sha256_process_block64(sha256_ctx_t *ctx) +{ + unsigned t; + uint32_t W[64], a, b, c, d, e, f, g, h; + const uint32_t *words = (uint32_t*) ctx->wbuffer; -# if BB_BIG_ENDIAN - for (i = 0; i < 16; i++) - words[i] = SWAP_LE32(words[i]); -# endif + /* Operators defined in FIPS 180-2:4.1.2. */ +#define Ch(x, y, z) ((x & y) ^ (~x & z)) +#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) +#define S0(x) (rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22)) +#define S1(x) (rotr32(x, 6) ^ rotr32(x, 11) ^ rotr32(x, 25)) +#define R0(x) (rotr32(x, 7) ^ rotr32(x, 18) ^ (x >> 3)) +#define R1(x) (rotr32(x, 17) ^ rotr32(x, 19) ^ (x >> 10)) -# if MD5_SIZE_VS_SPEED == 3 - pc = C_array; - pp = P_array; - ps = S_array - 4; + /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */ + for (t = 0; t < 16; ++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]; - for (i = 0; i < 64; i++) { - if ((i & 0x0f) == 0) - ps += 4; - temp = A; - switch (i >> 4) { - case 0: - temp += FF(B, C, D); - break; - case 1: - temp += FG(B, C, D); - break; - case 2: - temp += FH(B, C, D); - break; - case 3: - temp += FI(B, C, D); - } - temp += words[(int) (*pp++)] + *pc++; - temp = rotl32(temp, ps[i & 3]); - temp += B; - A = D; - D = C; - C = B; - B = temp; - } -# else /* MD5_SIZE_VS_SPEED == 2 */ - pc = C_array; - pp = P_array; - ps = S_array; + a = ctx->hash[0]; + b = ctx->hash[1]; + c = ctx->hash[2]; + d = ctx->hash[3]; + e = ctx->hash[4]; + f = ctx->hash[5]; + g = ctx->hash[6]; + h = ctx->hash[7]; - for (i = 0; i < 16; i++) { - temp = A + FF(B, C, D) + words[(int) (*pp++)] + *pc++; - temp = rotl32(temp, ps[i & 3]); - temp += B; - A = D; - D = C; - C = B; - B = temp; - } - ps += 4; - for (i = 0; i < 16; i++) { - temp = A + FG(B, C, D) + words[(int) (*pp++)] + *pc++; - temp = rotl32(temp, ps[i & 3]); - temp += B; - A = D; - D = C; - C = B; - B = temp; - } - ps += 4; - for (i = 0; i < 16; i++) { - temp = A + FH(B, C, D) + words[(int) (*pp++)] + *pc++; - temp = rotl32(temp, ps[i & 3]); - temp += B; - A = D; - D = C; - C = B; - B = temp; - } - ps += 4; - for (i = 0; i < 16; i++) { - temp = A + FI(B, C, D) + words[(int) (*pp++)] + *pc++; - temp = rotl32(temp, ps[i & 3]); - temp += B; - A = D; - D = C; - C = B; - B = temp; + /* The actual computation according to FIPS 180-2:6.2.2 step 3. */ + for (t = 0; t < 64; ++t) { + /* Need to fetch upper half of sha_K[t] + * (I hope compiler is clever enough to just fetch + * upper half) + */ + uint32_t K_t = sha_K[t] >> 32; + uint32_t T1 = h + S1(e) + Ch(e, f, g) + K_t + W[t]; + uint32_t T2 = S0(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + T1; + d = c; + c = b; + b = a; + a = T1 + T2; } -# endif - /* Add checksum to the starting values */ - ctx->A += A; - ctx->B += B; - ctx->C += C; - ctx->D += D; +#undef Ch +#undef Maj +#undef S0 +#undef S1 +#undef R0 +#undef R1 + /* Add the starting values of the context according to FIPS 180-2:6.2.2 + step 4. */ + ctx->hash[0] += a; + ctx->hash[1] += b; + ctx->hash[2] += c; + ctx->hash[3] += d; + ctx->hash[4] += e; + ctx->hash[5] += f; + ctx->hash[6] += g; + ctx->hash[7] += h; +} -#else /* MD5_SIZE_VS_SPEED == 0 or 1 */ +static void FAST_FUNC sha512_process_block128(sha512_ctx_t *ctx) +{ + unsigned t; + uint64_t W[80]; + /* On i386, having assignments here (not later as sha256 does) + * produces 99 bytes smaller code with gcc 4.3.1 + */ + uint64_t a = ctx->hash[0]; + uint64_t b = ctx->hash[1]; + uint64_t c = ctx->hash[2]; + uint64_t d = ctx->hash[3]; + uint64_t e = ctx->hash[4]; + uint64_t f = ctx->hash[5]; + uint64_t g = ctx->hash[6]; + uint64_t h = ctx->hash[7]; + const uint64_t *words = (uint64_t*) ctx->wbuffer; - uint32_t A_save = A; - uint32_t B_save = B; - uint32_t C_save = C; - uint32_t D_save = D; -# if MD5_SIZE_VS_SPEED == 1 - const uint32_t *pc; - const char *pp; - int i; -# endif + /* Operators defined in FIPS 180-2:4.1.2. */ +#define Ch(x, y, z) ((x & y) ^ (~x & z)) +#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) +#define S0(x) (rotr64(x, 28) ^ rotr64(x, 34) ^ rotr64(x, 39)) +#define S1(x) (rotr64(x, 14) ^ rotr64(x, 18) ^ rotr64(x, 41)) +#define R0(x) (rotr64(x, 1) ^ rotr64(x, 8) ^ (x >> 7)) +#define R1(x) (rotr64(x, 19) ^ rotr64(x, 61) ^ (x >> 6)) - /* First round: using the given function, the context and a constant - the next context is computed. Because the algorithm's processing - unit is a 32-bit word and it is determined to work on words in - little endian byte order we perhaps have to change the byte order - before the computation. To reduce the work for the next steps - we save swapped words in WORDS array. */ -# undef OP -# define OP(a, b, c, d, s, T) \ - do { \ - a += FF(b, c, d) + (*words IF_BIG_ENDIAN(= SWAP_LE32(*words))) + T; \ - words++; \ - a = rotl32(a, s); \ - a += b; \ - } while (0) + /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */ + for (t = 0; t < 16; ++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]; + + /* The actual computation according to FIPS 180-2:6.3.2 step 3. */ + for (t = 0; t < 80; ++t) { + uint64_t T1 = h + S1(e) + Ch(e, f, g) + sha_K[t] + W[t]; + uint64_t T2 = S0(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + T1; + d = c; + c = b; + b = a; + a = T1 + T2; + } +#undef Ch +#undef Maj +#undef S0 +#undef S1 +#undef R0 +#undef R1 + /* Add the starting values of the context according to FIPS 180-2:6.3.2 + step 4. */ + ctx->hash[0] += a; + ctx->hash[1] += b; + ctx->hash[2] += c; + ctx->hash[3] += d; + ctx->hash[4] += e; + ctx->hash[5] += f; + ctx->hash[6] += g; + ctx->hash[7] += h; +} - /* Round 1 */ -# if MD5_SIZE_VS_SPEED == 1 - pc = C_array; - for (i = 0; i < 4; i++) { - OP(A, B, C, D, 7, *pc++); - OP(D, A, B, C, 12, *pc++); - OP(C, D, A, B, 17, *pc++); - OP(B, C, D, A, 22, *pc++); - } -# else - OP(A, B, C, D, 7, 0xd76aa478); - OP(D, A, B, C, 12, 0xe8c7b756); - OP(C, D, A, B, 17, 0x242070db); - OP(B, C, D, A, 22, 0xc1bdceee); - OP(A, B, C, D, 7, 0xf57c0faf); - OP(D, A, B, C, 12, 0x4787c62a); - OP(C, D, A, B, 17, 0xa8304613); - OP(B, C, D, A, 22, 0xfd469501); - OP(A, B, C, D, 7, 0x698098d8); - OP(D, A, B, C, 12, 0x8b44f7af); - OP(C, D, A, B, 17, 0xffff5bb1); - OP(B, C, D, A, 22, 0x895cd7be); - OP(A, B, C, D, 7, 0x6b901122); - OP(D, A, B, C, 12, 0xfd987193); - OP(C, D, A, B, 17, 0xa679438e); - OP(B, C, D, A, 22, 0x49b40821); -# endif - words -= 16; - /* For the second to fourth round we have the possibly swapped words - in WORDS. Redefine the macro to take an additional first - argument specifying the function to use. */ -# undef OP -# define OP(f, a, b, c, d, k, s, T) \ - do { \ - a += f(b, c, d) + words[k] + T; \ - a = rotl32(a, s); \ - a += b; \ - } while (0) +void FAST_FUNC sha1_begin(sha1_ctx_t *ctx) +{ + ctx->hash[0] = 0x67452301; + ctx->hash[1] = 0xefcdab89; + ctx->hash[2] = 0x98badcfe; + ctx->hash[3] = 0x10325476; + ctx->hash[4] = 0xc3d2e1f0; + ctx->total64 = 0; + ctx->process_block = sha1_process_block64; +} - /* Round 2 */ -# if MD5_SIZE_VS_SPEED == 1 - pp = P_array; - for (i = 0; i < 4; i++) { - OP(FG, A, B, C, D, (int) (*pp++), 5, *pc++); - OP(FG, D, A, B, C, (int) (*pp++), 9, *pc++); - OP(FG, C, D, A, B, (int) (*pp++), 14, *pc++); - OP(FG, B, C, D, A, (int) (*pp++), 20, *pc++); - } -# else - OP(FG, A, B, C, D, 1, 5, 0xf61e2562); - OP(FG, D, A, B, C, 6, 9, 0xc040b340); - OP(FG, C, D, A, B, 11, 14, 0x265e5a51); - OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa); - OP(FG, A, B, C, D, 5, 5, 0xd62f105d); - OP(FG, D, A, B, C, 10, 9, 0x02441453); - OP(FG, C, D, A, B, 15, 14, 0xd8a1e681); - OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8); - OP(FG, A, B, C, D, 9, 5, 0x21e1cde6); - OP(FG, D, A, B, C, 14, 9, 0xc33707d6); - OP(FG, C, D, A, B, 3, 14, 0xf4d50d87); - OP(FG, B, C, D, A, 8, 20, 0x455a14ed); - OP(FG, A, B, C, D, 13, 5, 0xa9e3e905); - OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8); - OP(FG, C, D, A, B, 7, 14, 0x676f02d9); - OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a); -# endif +static const uint32_t init256[] = { + 0, + 0, + 0x6a09e667, + 0xbb67ae85, + 0x3c6ef372, + 0xa54ff53a, + 0x510e527f, + 0x9b05688c, + 0x1f83d9ab, + 0x5be0cd19, +}; +static const uint32_t init512_lo[] = { + 0, + 0, + 0xf3bcc908, + 0x84caa73b, + 0xfe94f82b, + 0x5f1d36f1, + 0xade682d1, + 0x2b3e6c1f, + 0xfb41bd6b, + 0x137e2179, +}; - /* Round 3 */ -# if MD5_SIZE_VS_SPEED == 1 - for (i = 0; i < 4; i++) { - OP(FH, A, B, C, D, (int) (*pp++), 4, *pc++); - OP(FH, D, A, B, C, (int) (*pp++), 11, *pc++); - OP(FH, C, D, A, B, (int) (*pp++), 16, *pc++); - OP(FH, B, C, D, A, (int) (*pp++), 23, *pc++); - } -# else - OP(FH, A, B, C, D, 5, 4, 0xfffa3942); - OP(FH, D, A, B, C, 8, 11, 0x8771f681); - OP(FH, C, D, A, B, 11, 16, 0x6d9d6122); - OP(FH, B, C, D, A, 14, 23, 0xfde5380c); - OP(FH, A, B, C, D, 1, 4, 0xa4beea44); - OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9); - OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60); - OP(FH, B, C, D, A, 10, 23, 0xbebfbc70); - OP(FH, A, B, C, D, 13, 4, 0x289b7ec6); - OP(FH, D, A, B, C, 0, 11, 0xeaa127fa); - OP(FH, C, D, A, B, 3, 16, 0xd4ef3085); - OP(FH, B, C, D, A, 6, 23, 0x04881d05); - OP(FH, A, B, C, D, 9, 4, 0xd9d4d039); - OP(FH, D, A, B, C, 12, 11, 0xe6db99e5); - OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8); - OP(FH, B, C, D, A, 2, 23, 0xc4ac5665); -# endif +/* Initialize structure containing state of computation. + (FIPS 180-2:5.3.2) */ +void FAST_FUNC sha256_begin(sha256_ctx_t *ctx) +{ + memcpy(&ctx->total64, init256, sizeof(init256)); + /*ctx->total64 = 0; - done by prepending two 32-bit zeros to init256 */ + ctx->process_block = sha256_process_block64; +} - /* Round 4 */ -# if MD5_SIZE_VS_SPEED == 1 - for (i = 0; i < 4; i++) { - OP(FI, A, B, C, D, (int) (*pp++), 6, *pc++); - OP(FI, D, A, B, C, (int) (*pp++), 10, *pc++); - OP(FI, C, D, A, B, (int) (*pp++), 15, *pc++); - OP(FI, B, C, D, A, (int) (*pp++), 21, *pc++); - } -# else - OP(FI, A, B, C, D, 0, 6, 0xf4292244); - OP(FI, D, A, B, C, 7, 10, 0x432aff97); - OP(FI, C, D, A, B, 14, 15, 0xab9423a7); - OP(FI, B, C, D, A, 5, 21, 0xfc93a039); - OP(FI, A, B, C, D, 12, 6, 0x655b59c3); - OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92); - OP(FI, C, D, A, B, 10, 15, 0xffeff47d); - OP(FI, B, C, D, A, 1, 21, 0x85845dd1); - OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f); - OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0); - OP(FI, C, D, A, B, 6, 15, 0xa3014314); - OP(FI, B, C, D, A, 13, 21, 0x4e0811a1); - OP(FI, A, B, C, D, 4, 6, 0xf7537e82); - OP(FI, D, A, B, C, 11, 10, 0xbd3af235); - OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb); - OP(FI, B, C, D, A, 9, 21, 0xeb86d391); -# undef OP -# endif - /* Add checksum to the starting values */ - ctx->A = A_save + A; - ctx->B = B_save + B; - ctx->C = C_save + C; - ctx->D = D_save + D; -#endif +/* Initialize structure containing state of computation. + (FIPS 180-2:5.3.3) */ +void FAST_FUNC sha512_begin(sha512_ctx_t *ctx) +{ + int i; + /* Two extra iterations zero out ctx->total64[2] */ + uint64_t *tp = ctx->total64; + for (i = 0; i < 2+8; i++) + tp[i] = ((uint64_t)(init256[i]) << 32) + init512_lo[i]; + /*ctx->total64[0] = ctx->total64[1] = 0; - already done */ } -#undef FF -#undef FG -#undef FH -#undef FI -/* Feed data through a temporary buffer to call md5_hash_aligned_block() - * with chunks of data that are 4-byte aligned and a multiple of 64 bytes. - * This function's internal buffer remembers previous data until it has 64 - * bytes worth to pass on. Call md5_end() to flush this buffer. */ -void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) +void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) { - unsigned bufpos = ctx->total64 & 63; + unsigned bufpos = ctx->total64[0] & 127; unsigned remaining; - /* RFC 1321 specifies the possible length of the file up to 2^64 bits. - * Here we only track the number of bytes. */ - ctx->total64 += len; + /* First increment the byte count. FIPS 180-2 specifies the possible + length of the file up to 2^128 _bits_. + We compute the number of _bytes_ and convert to bits later. */ + ctx->total64[0] += len; + if (ctx->total64[0] < len) + ctx->total64[1]++; #if 0 - remaining = 64 - bufpos; + remaining = 128 - bufpos; /* Hash whole blocks */ while (len >= remaining) { memcpy(ctx->wbuffer + bufpos, buffer, remaining); buffer = (const char *)buffer + remaining; len -= remaining; - remaining = 64; + remaining = 128; bufpos = 0; - md5_process_block64(ctx); + sha512_process_block128(ctx); } /* Save last, partial blosk */ memcpy(ctx->wbuffer + bufpos, buffer, len); #else - /* Tiny bit smaller code */ while (1) { - remaining = 64 - bufpos; + remaining = 128 - bufpos; if (remaining > len) remaining = len; /* Copy data into aligned buffer */ @@ -935,35 +839,65 @@ void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) len -= remaining; buffer = (const char *)buffer + remaining; bufpos += remaining; - /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */ - bufpos -= 64; + /* clever way to do "if (bufpos != 128) break; ... ; bufpos = 0;" */ + bufpos -= 128; if (bufpos != 0) break; /* Buffer is filled up, process it */ - md5_process_block64(ctx); + sha512_process_block128(ctx); /*bufpos = 0; - already is */ } #endif } -/* Process the remaining bytes in the buffer and put result from CTX - * in first 16 bytes following RESBUF. The result is always in little - * endian byte order, so that a byte-wise output yields to the wanted - * ASCII representation of the message digest. - */ -void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) +/* Used also for sha256 */ +void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) { - /* MD5 stores total in LE, need to swap on BE arches: */ - common64_end(ctx, (process_block64_func*) md5_process_block64, /*swap_needed:*/ BB_BIG_ENDIAN); + unsigned hash_size; - /* The MD5 result is in little endian byte order. - * We (ab)use the fact that A-D are consecutive in memory. - */ -#if BB_BIG_ENDIAN - ctx->A = SWAP_LE32(ctx->A); - ctx->B = SWAP_LE32(ctx->B); - ctx->C = SWAP_LE32(ctx->C); - ctx->D = SWAP_LE32(ctx->D); -#endif - memcpy(resbuf, &ctx->A, sizeof(ctx->A) * 4); + /* SHA stores total in BE, need to swap on LE arches: */ + common64_end(ctx, /*swap_needed:*/ BB_LITTLE_ENDIAN); + + hash_size = (ctx->process_block == sha1_process_block64) ? 5 : 8; + /* This way we do not impose alignment constraints on resbuf: */ + if (BB_LITTLE_ENDIAN) { + unsigned i; + for (i = 0; i < hash_size; ++i) + ctx->hash[i] = SWAP_BE32(ctx->hash[i]); + } + memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * hash_size); +} + +void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) +{ + unsigned bufpos = ctx->total64[0] & 127; + + /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0... */ + ctx->wbuffer[bufpos++] = 0x80; + + while (1) { + unsigned remaining = 128 - bufpos; + memset(ctx->wbuffer + bufpos, 0, remaining); + if (remaining >= 16) { + /* Store the 128-bit counter of bits in the buffer in BE format */ + uint64_t t; + t = ctx->total64[0] << 3; + t = SWAP_BE64(t); + *(uint64_t *) (&ctx->wbuffer[128 - 8]) = t; + t = (ctx->total64[1] << 3) | (ctx->total64[0] >> 61); + t = SWAP_BE64(t); + *(uint64_t *) (&ctx->wbuffer[128 - 16]) = t; + } + sha512_process_block128(ctx); + if (remaining >= 16) + break; + bufpos = 0; + } + + if (BB_LITTLE_ENDIAN) { + unsigned i; + for (i = 0; i < ARRAY_SIZE(ctx->hash); ++i) + ctx->hash[i] = SWAP_BE64(ctx->hash[i]); + } + memcpy(resbuf, ctx->hash, sizeof(ctx->hash)); } -- cgit v1.2.3-55-g6feb From 7ab94ca3516db95b53ee944c9bddb6c3b75641ae Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 19 Oct 2010 02:33:39 +0200 Subject: md5: remove outdated comment Signed-off-by: Denys Vlasenko --- libbb/hash_md5_sha.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c index f5f875a64..aeacddef8 100644 --- a/libbb/hash_md5_sha.c +++ b/libbb/hash_md5_sha.c @@ -461,9 +461,7 @@ void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) /* MD5 stores total in LE, need to swap on BE arches: */ common64_end(ctx, /*swap_needed:*/ BB_BIG_ENDIAN); - /* The MD5 result is in little endian byte order. - * We (ab)use the fact that A-D are consecutive in memory. - */ + /* The MD5 result is in little endian byte order */ #if BB_BIG_ENDIAN ctx->hash[0] = SWAP_LE32(ctx->hash[0]); ctx->hash[1] = SWAP_LE32(ctx->hash[1]); -- cgit v1.2.3-55-g6feb From ef6c6d8cfef071435ccf275ad404a501626b706c Mon Sep 17 00:00:00 2001 From: Thomas Chou Date: Tue, 19 Oct 2010 15:17:12 +0800 Subject: mkmakefile: make 3.82 fix Signed-off-by: Thomas Chou Signed-off-by: Denys Vlasenko --- scripts/mkmakefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/mkmakefile b/scripts/mkmakefile index 7f9d544f9..9fc51a7c9 100755 --- a/scripts/mkmakefile +++ b/scripts/mkmakefile @@ -31,6 +31,9 @@ all: Makefile:; -\$(filter-out all Makefile,\$(MAKECMDGOALS)) %/: +\$(filter-out all Makefile,\$(MAKECMDGOALS)): + \$(MAKE) -C \$(KERNELSRC) O=\$(KERNELOUTPUT) \$@ + +%/: \$(MAKE) -C \$(KERNELSRC) O=\$(KERNELOUTPUT) \$@ EOF -- cgit v1.2.3-55-g6feb From 0016bcee374606e79c48a1a97479b0521f947942 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 19 Oct 2010 23:07:49 +0200 Subject: klogd: do not log partial lines function old new delta overlapping_strcpy 15 18 +3 klogd_main 438 436 -2 Signed-off-by: Denys Vlasenko --- libbb/safe_strncpy.c | 11 ++++++++--- sysklogd/klogd.c | 19 ++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/libbb/safe_strncpy.c b/libbb/safe_strncpy.c index 8eb6a014f..5eb0db0bd 100644 --- a/libbb/safe_strncpy.c +++ b/libbb/safe_strncpy.c @@ -20,8 +20,13 @@ char* FAST_FUNC safe_strncpy(char *dst, const char *src, size_t size) /* Like strcpy but can copy overlapping strings. */ void FAST_FUNC overlapping_strcpy(char *dst, const char *src) { - while ((*dst = *src) != '\0') { - dst++; - src++; + /* Cheap optimization for dst == src case - + * better to have it here than in many callers. + */ + if (dst != src) { + while ((*dst = *src) != '\0') { + dst++; + src++; + } } } diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c index 6766b649d..0d4c2578d 100644 --- a/sysklogd/klogd.c +++ b/sysklogd/klogd.c @@ -132,7 +132,7 @@ int klogd_main(int argc UNUSED_PARAM, char **argv) int i = 0; char *opt_c; int opt; - int used = 0; + int used; opt = getopt32(argv, "c:n", &opt_c); if (opt & OPT_LEVEL) { @@ -159,6 +159,7 @@ int klogd_main(int argc UNUSED_PARAM, char **argv) syslog(LOG_NOTICE, "klogd started: %s", bb_banner); + used = 0; while (!bb_got_signal) { int n; int priority; @@ -175,22 +176,22 @@ int klogd_main(int argc UNUSED_PARAM, char **argv) } start[n] = '\0'; - /* klogctl buffer parsing modelled after code in dmesg.c */ /* Process each newline-terminated line in the buffer */ start = log_buffer; while (1) { char *newline = strchrnul(start, '\n'); if (*newline == '\0') { - /* This line is incomplete... */ - if (start != log_buffer) { - /* move it to the front of the buffer */ - overlapping_strcpy(log_buffer, start); - used = newline - start; - /* don't log it yet */ + /* This line is incomplete */ + + /* move it to the front of the buffer */ + overlapping_strcpy(log_buffer, start); + used = newline - start; + if (used < KLOGD_LOGBUF_SIZE-1) { + /* buffer isn't full */ break; } - /* ...but if buffer is full, log it anyway */ + /* buffer is full, log it anyway */ used = 0; newline = NULL; } else { -- cgit v1.2.3-55-g6feb From f04ca74ab5eb6ee57a573c6e1ab3e84d3f0a0af8 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 19 Oct 2010 23:08:33 +0200 Subject: dmesg: more correct skipping of ; use faster putchar for most output function old new delta dmesg_main 246 291 +45 Signed-off-by: Denys Vlasenko --- util-linux/dmesg.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/util-linux/dmesg.c b/util-linux/dmesg.c index 06a03d3fb..6e43a22f5 100644 --- a/util-linux/dmesg.c +++ b/util-linux/dmesg.c @@ -45,20 +45,25 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv) if (len == 0) return EXIT_SUCCESS; - /* Skip <#> at the start of lines, and make sure we end with a newline */ if (ENABLE_FEATURE_DMESG_PRETTY) { int last = '\n'; int in = 0; - do { - if (last == '\n' && buf[in] == '<') + /* Skip <#> at the start of lines */ + while (1) { + if (last == '\n' && buf[in] == '<') { in += 3; - else { - last = buf[in++]; - bb_putchar(last); + if (in >= len) + break; } - } while (in < len); + last = buf[in]; + putchar(last); + in++; + if (in >= len) + break; + } + /* Make sure we end with a newline */ if (last != '\n') bb_putchar('\n'); } else { -- cgit v1.2.3-55-g6feb From 9e244c760042e764d3504dc94cf79b41938d9b1d Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 01:38:56 +0200 Subject: udhcpc: move usage text to .c file. no code changes Signed-off-by: Denys Vlasenko --- include/usage.src.h | 78 ------------------------------------------------ networking/udhcp/dhcpc.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 78 deletions(-) diff --git a/include/usage.src.h b/include/usage.src.h index 5d7767bb4..b73ca4d89 100644 --- a/include/usage.src.h +++ b/include/usage.src.h @@ -4232,84 +4232,6 @@ INSERT "# tunctl\n" \ "# tunctl -d tun0\n" -#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 -# define IF_UDHCP_VERBOSE(...) __VA_ARGS__ -#else -# define IF_UDHCP_VERBOSE(...) -#endif -#define udhcpc_trivial_usage \ - "[-fbnq"IF_UDHCP_VERBOSE("v")"oCR] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE]\n" \ - " [-H HOSTNAME] [-c CID] [-V VENDOR] [-O DHCP_OPT]..." IF_FEATURE_UDHCP_PORT(" [-P N]") -#define udhcpc_full_usage "\n" \ - IF_LONG_OPTS( \ - "\n -i,--interface IFACE Interface to use (default eth0)" \ - "\n -p,--pidfile FILE Create pidfile" \ - "\n -r,--request IP IP address to request" \ - "\n -s,--script PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")" \ - "\n -t,--retries N Send up to N discover packets" \ - "\n -T,--timeout N Pause between packets (default 3 seconds)" \ - "\n -A,--tryagain N Wait N seconds after failure (default 20)" \ - "\n -f,--foreground Run in foreground" \ - USE_FOR_MMU( \ - "\n -b,--background Background if lease is not obtained" \ - ) \ - "\n -S,--syslog Log to syslog too" \ - "\n -n,--now Exit if lease is not obtained" \ - "\n -q,--quit Exit after obtaining lease" \ - "\n -R,--release Release IP on exit" \ - IF_FEATURE_UDHCP_PORT( \ - "\n -P,--client-port N Use port N (default 68)" \ - ) \ - IF_FEATURE_UDHCPC_ARPING( \ - "\n -a,--arping Use arping to validate offered address" \ - ) \ - "\n -O,--request-option OPT Request DHCP option OPT (cumulative)" \ - "\n -o,--no-default-options Don't request any options (unless -O is given)" \ - "\n -x OPT:VAL Include option OPT in sent packets (cumulative)" \ - "\n -F,--fqdn NAME Ask server to update DNS mapping for NAME" \ - "\n -H,-h,--hostname NAME Send NAME as client hostname (default none)" \ - "\n -V,--vendorclass VENDOR Vendor identifier (default 'udhcp VERSION')" \ - "\n -c,--clientid CLIENTID Client identifier (default own MAC)" \ - "\n -C,--clientid-none Don't send client identifier" \ - IF_UDHCP_VERBOSE( \ - "\n -v Verbose" \ - ) \ - ) \ - IF_NOT_LONG_OPTS( \ - "\n -i IFACE Interface to use (default eth0)" \ - "\n -p FILE Create pidfile" \ - "\n -r IP IP address to request" \ - "\n -s PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")" \ - "\n -t N Send up to N discover packets" \ - "\n -T N Pause between packets (default 3 seconds)" \ - "\n -A N Wait N seconds (default 20) after failure" \ - "\n -x OPT:VAL Include option OPT in sent packets" \ - "\n -O OPT Request DHCP option OPT (cumulative)" \ - "\n -o Don't request any options (unless -O is given)" \ - "\n -f Run in foreground" \ - USE_FOR_MMU( \ - "\n -b Background if lease is not obtained" \ - ) \ - "\n -S Log to syslog too" \ - "\n -n Exit if lease is not obtained" \ - "\n -q Exit after obtaining lease" \ - "\n -R Release IP on exit" \ - IF_FEATURE_UDHCP_PORT( \ - "\n -P N Use port N (default 68)" \ - ) \ - IF_FEATURE_UDHCPC_ARPING( \ - "\n -a Use arping to validate offered address" \ - ) \ - "\n -F NAME Ask server to update DNS mapping for NAME" \ - "\n -H,-h NAME Send NAME as client hostname (default none)" \ - "\n -V VENDOR Vendor identifier (default 'udhcp VERSION')" \ - "\n -c CLIENTID Client identifier (default own MAC)" \ - "\n -C Don't send client identifier" \ - IF_UDHCP_VERBOSE( \ - "\n -v Verbose" \ - ) \ - ) \ - #define udhcpd_trivial_usage \ "[-fS]" IF_FEATURE_UDHCP_PORT(" [-P N]") " [CONFFILE]" \ diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 27d6ad1a8..3630129ed 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -765,6 +765,84 @@ static void client_background(void) } #endif +//usage:#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 +//usage:# define IF_UDHCP_VERBOSE(...) __VA_ARGS__ +//usage:#else +//usage:# define IF_UDHCP_VERBOSE(...) +//usage:#endif +//usage:#define udhcpc_trivial_usage +//usage: "[-fbnq"IF_UDHCP_VERBOSE("v")"oCR] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE]\n" +//usage: " [-H HOSTNAME] [-c CID] [-V VENDOR] [-O DHCP_OPT]..." IF_FEATURE_UDHCP_PORT(" [-P N]") +//usage:#define udhcpc_full_usage "\n" +//usage: IF_LONG_OPTS( +//usage: "\n -i,--interface IFACE Interface to use (default eth0)" +//usage: "\n -p,--pidfile FILE Create pidfile" +//usage: "\n -r,--request IP IP address to request" +//usage: "\n -s,--script PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")" +//usage: "\n -t,--retries N Send up to N discover packets" +//usage: "\n -T,--timeout N Pause between packets (default 3 seconds)" +//usage: "\n -A,--tryagain N Wait N seconds after failure (default 20)" +//usage: "\n -f,--foreground Run in foreground" +//usage: USE_FOR_MMU( +//usage: "\n -b,--background Background if lease is not obtained" +//usage: ) +//usage: "\n -S,--syslog Log to syslog too" +//usage: "\n -n,--now Exit if lease is not obtained" +//usage: "\n -q,--quit Exit after obtaining lease" +//usage: "\n -R,--release Release IP on exit" +//usage: IF_FEATURE_UDHCP_PORT( +//usage: "\n -P,--client-port N Use port N (default 68)" +//usage: ) +//usage: IF_FEATURE_UDHCPC_ARPING( +//usage: "\n -a,--arping Use arping to validate offered address" +//usage: ) +//usage: "\n -O,--request-option OPT Request DHCP option OPT (cumulative)" +//usage: "\n -o,--no-default-options Don't request any options (unless -O is given)" +//usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)" +//usage: "\n -F,--fqdn NAME Ask server to update DNS mapping for NAME" +//usage: "\n -H,-h,--hostname NAME Send NAME as client hostname (default none)" +//usage: "\n -V,--vendorclass VENDOR Vendor identifier (default 'udhcp VERSION')" +//usage: "\n -c,--clientid CLIENTID Client identifier (default own MAC)" +//usage: "\n -C,--clientid-none Don't send client identifier" +//usage: IF_UDHCP_VERBOSE( +//usage: "\n -v Verbose" +//usage: ) +//usage: ) +//usage: IF_NOT_LONG_OPTS( +//usage: "\n -i IFACE Interface to use (default eth0)" +//usage: "\n -p FILE Create pidfile" +//usage: "\n -r IP IP address to request" +//usage: "\n -s PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")" +//usage: "\n -t N Send up to N discover packets" +//usage: "\n -T N Pause between packets (default 3 seconds)" +//usage: "\n -A N Wait N seconds (default 20) after failure" +//usage: "\n -x OPT:VAL Include option OPT in sent packets" +//usage: "\n -O OPT Request DHCP option OPT (cumulative)" +//usage: "\n -o Don't request any options (unless -O is given)" +//usage: "\n -f Run in foreground" +//usage: USE_FOR_MMU( +//usage: "\n -b Background if lease is not obtained" +//usage: ) +//usage: "\n -S Log to syslog too" +//usage: "\n -n Exit if lease is not obtained" +//usage: "\n -q Exit after obtaining lease" +//usage: "\n -R Release IP on exit" +//usage: IF_FEATURE_UDHCP_PORT( +//usage: "\n -P N Use port N (default 68)" +//usage: ) +//usage: IF_FEATURE_UDHCPC_ARPING( +//usage: "\n -a Use arping to validate offered address" +//usage: ) +//usage: "\n -F NAME Ask server to update DNS mapping for NAME" +//usage: "\n -H,-h NAME Send NAME as client hostname (default none)" +//usage: "\n -V VENDOR Vendor identifier (default 'udhcp VERSION')" +//usage: "\n -c CLIENTID Client identifier (default own MAC)" +//usage: "\n -C Don't send client identifier" +//usage: IF_UDHCP_VERBOSE( +//usage: "\n -v Verbose" +//usage: ) +//usage: ) + int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int udhcpc_main(int argc UNUSED_PARAM, char **argv) { -- cgit v1.2.3-55-g6feb From 1cbdc03411f286f4f4f27cfd3b28c56d364e4ae3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 01:42:37 +0200 Subject: udhcpc: remove -c CLIENTID, it is hard to use, -x 61:hexstring does the same better function old new delta packed_usage 27802 27808 +6 static.udhcpc_longopts 261 250 -11 udhcpc_main 2799 2780 -19 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/2 up/down: 6/-30) Total: -24 bytes Signed-off-by: Denys Vlasenko --- networking/udhcp/common.c | 2 +- networking/udhcp/dhcpc.c | 76 ++++++++++++++++++++++------------------------- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index b6b274d91..e34f926bb 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c @@ -69,7 +69,7 @@ const struct dhcp_optflag dhcp_optflags[] = { { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */ { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */ { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */ -//FIXME: handling of this option is not exactly correct: + /* not really a string */ { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */ { 0, 0 } /* zeroed terminating entry */ }; diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 3630129ed..864fcf690 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -772,7 +772,7 @@ static void client_background(void) //usage:#endif //usage:#define udhcpc_trivial_usage //usage: "[-fbnq"IF_UDHCP_VERBOSE("v")"oCR] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE]\n" -//usage: " [-H HOSTNAME] [-c CID] [-V VENDOR] [-O DHCP_OPT]..." IF_FEATURE_UDHCP_PORT(" [-P N]") +//usage: " [-H HOSTNAME] [-V VENDOR] [-x OPT:VAL]... [-O OPT]..." IF_FEATURE_UDHCP_PORT(" [-P N]") //usage:#define udhcpc_full_usage "\n" //usage: IF_LONG_OPTS( //usage: "\n -i,--interface IFACE Interface to use (default eth0)" @@ -796,14 +796,14 @@ static void client_background(void) //usage: IF_FEATURE_UDHCPC_ARPING( //usage: "\n -a,--arping Use arping to validate offered address" //usage: ) -//usage: "\n -O,--request-option OPT Request DHCP option OPT (cumulative)" +//usage: "\n -O,--request-option OPT Request option OPT from server (cumulative)" //usage: "\n -o,--no-default-options Don't request any options (unless -O is given)" //usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)" +//usage: "\n Examples: -x hostname:bbox -x 61:0100ffee11cc55" //usage: "\n -F,--fqdn NAME Ask server to update DNS mapping for NAME" //usage: "\n -H,-h,--hostname NAME Send NAME as client hostname (default none)" //usage: "\n -V,--vendorclass VENDOR Vendor identifier (default 'udhcp VERSION')" -//usage: "\n -c,--clientid CLIENTID Client identifier (default own MAC)" -//usage: "\n -C,--clientid-none Don't send client identifier" +//usage: "\n -C,--clientid-none Don't send MAC as client identifier" //usage: IF_UDHCP_VERBOSE( //usage: "\n -v Verbose" //usage: ) @@ -816,8 +816,9 @@ static void client_background(void) //usage: "\n -t N Send up to N discover packets" //usage: "\n -T N Pause between packets (default 3 seconds)" //usage: "\n -A N Wait N seconds (default 20) after failure" -//usage: "\n -x OPT:VAL Include option OPT in sent packets" -//usage: "\n -O OPT Request DHCP option OPT (cumulative)" +//usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)" +//usage: "\n Examples: -x hostname:bbox -x 61:0100ffee11cc55" +//usage: "\n -O OPT Request option OPT from server (cumulative)" //usage: "\n -o Don't request any options (unless -O is given)" //usage: "\n -f Run in foreground" //usage: USE_FOR_MMU( @@ -836,8 +837,7 @@ static void client_background(void) //usage: "\n -F NAME Ask server to update DNS mapping for NAME" //usage: "\n -H,-h NAME Send NAME as client hostname (default none)" //usage: "\n -V VENDOR Vendor identifier (default 'udhcp VERSION')" -//usage: "\n -c CLIENTID Client identifier (default own MAC)" -//usage: "\n -C Don't send client identifier" +//usage: "\n -C Don't send MAC as client identifier" //usage: IF_UDHCP_VERBOSE( //usage: "\n -v Verbose" //usage: ) @@ -847,7 +847,7 @@ int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int udhcpc_main(int argc UNUSED_PARAM, char **argv) { uint8_t *temp, *message; - const char *str_c, *str_V, *str_h, *str_F, *str_r; + const char *str_V, *str_h, *str_F, *str_r; IF_FEATURE_UDHCP_PORT(char *str_P;) llist_t *list_O = NULL; llist_t *list_x = NULL; @@ -870,7 +870,6 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) #if ENABLE_LONG_OPTS static const char udhcpc_longopts[] ALIGN1 = - "clientid\0" Required_argument "c" "clientid-none\0" No_argument "C" "vendorclass\0" Required_argument "V" "hostname\0" Required_argument "H" @@ -896,29 +895,28 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) ; #endif enum { - OPT_c = 1 << 0, - OPT_C = 1 << 1, - OPT_V = 1 << 2, - OPT_H = 1 << 3, - OPT_h = 1 << 4, - OPT_F = 1 << 5, - OPT_i = 1 << 6, - OPT_n = 1 << 7, - OPT_p = 1 << 8, - OPT_q = 1 << 9, - OPT_R = 1 << 10, - OPT_r = 1 << 11, - OPT_s = 1 << 12, - OPT_T = 1 << 13, - OPT_t = 1 << 14, - OPT_S = 1 << 15, - OPT_A = 1 << 16, - OPT_O = 1 << 17, - OPT_o = 1 << 18, - OPT_x = 1 << 19, - OPT_f = 1 << 20, + OPT_C = 1 << 0, + OPT_V = 1 << 1, + OPT_H = 1 << 2, + OPT_h = 1 << 3, + OPT_F = 1 << 4, + OPT_i = 1 << 5, + OPT_n = 1 << 6, + OPT_p = 1 << 7, + OPT_q = 1 << 8, + OPT_R = 1 << 9, + OPT_r = 1 << 10, + OPT_s = 1 << 11, + OPT_T = 1 << 12, + OPT_t = 1 << 13, + OPT_S = 1 << 14, + OPT_A = 1 << 15, + OPT_O = 1 << 16, + OPT_o = 1 << 17, + OPT_x = 1 << 18, + OPT_f = 1 << 19, /* The rest has variable bit positions, need to be clever */ - OPTBIT_f = 20, + OPTBIT_f = 19, USE_FOR_MMU( OPTBIT_b,) IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,) IF_FEATURE_UDHCP_PORT( OPTBIT_P,) @@ -935,19 +933,19 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) str_V = "udhcp "BB_VER; /* Parse command line */ - /* Cc: mutually exclusive; O,x: list; -T,-t,-A take numeric param */ - opt_complementary = "c--C:C--c:O::x::T+:t+:A+" + /* O,x: list; -T,-t,-A take numeric param */ + opt_complementary = "O::x::T+:t+:A+" #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 ":vv" #endif ; IF_LONG_OPTS(applet_long_options = udhcpc_longopts;) - opt = getopt32(argv, "c:CV:H:h:F:i:np:qRr:s:T:t:SA:O:ox:f" + opt = getopt32(argv, "CV:H:h:F:i:np:qRr:s:T:t:SA:O:ox:f" USE_FOR_MMU("b") IF_FEATURE_UDHCPC_ARPING("a") IF_FEATURE_UDHCP_PORT("P:") "v" - , &str_c, &str_V, &str_h, &str_h, &str_F + , &str_V, &str_h, &str_h, &str_F , &client_config.interface, &client_config.pidfile, &str_r /* i,p */ , &client_config.script /* s */ , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */ @@ -1009,10 +1007,8 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) return 1; } - if (opt & OPT_c) { - client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0); - } else if (!(opt & OPT_C)) { - /* not set and not suppressed, set the default client ID */ + if (!(opt & OPT_C) && !udhcp_find_option(client_config.options, DHCP_CLIENT_ID)) { + /* not suppressed and not set, set the default client ID */ client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7); client_config.clientid[OPT_DATA] = 1; /* type: ethernet */ memcpy(client_config.clientid + OPT_DATA+1, client_config.client_mac, 6); -- cgit v1.2.3-55-g6feb From d3c5ab703bf6f42ec11fb770b86aaf927cf6b32d Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 02:03:30 +0200 Subject: udhcpc: remove now-unneeded definitions of vendor and client-id opts function old new delta dhcp_optflags 68 64 -4 Signed-off-by: Denys Vlasenko --- networking/udhcp/common.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index e34f926bb..311f79e7e 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c @@ -68,9 +68,10 @@ const struct dhcp_optflag dhcp_optflags[] = { { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */ { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */ { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */ - { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */ - /* not really a string */ - { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */ +//looks like these opts will work just fine even without these defs: +// { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */ +// /* not really a string: */ +// { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */ { 0, 0 } /* zeroed terminating entry */ }; -- cgit v1.2.3-55-g6feb From bec588878b6d435c33d9b0aae6247715c259e3a4 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 13:21:22 +0200 Subject: *: s/open3_or_warn/open_or_warn/ where makes sense Signed-off-by: Denys Vlasenko --- coreutils/fsync.c | 2 +- miscutils/crond.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/coreutils/fsync.c b/coreutils/fsync.c index d1fe2b584..b8b463cd1 100644 --- a/coreutils/fsync.c +++ b/coreutils/fsync.c @@ -27,7 +27,7 @@ int fsync_main(int argc UNUSED_PARAM, char **argv) status = EXIT_SUCCESS; do { - int fd = open3_or_warn(*argv, O_NOATIME | O_NOCTTY | O_RDONLY, 0); + int fd = open_or_warn(*argv, O_NOATIME | O_NOCTTY | O_RDONLY); if (fd == -1) { status = EXIT_FAILURE; diff --git a/miscutils/crond.c b/miscutils/crond.c index fddddcd8c..7f2d54c9f 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c @@ -123,7 +123,7 @@ static void crondlog(const char *ctl, ...) /* Syslog mode: all to syslog (logmode = LOGMODE_SYSLOG), */ if (!DebugOpt && G.log_filename) { /* Otherwise (log to file): we reopen log file at every write: */ - int logfd = open3_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND, 0666); + int logfd = open_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND); if (logfd >= 0) xmove_fd(logfd, STDERR_FILENO); } -- cgit v1.2.3-55-g6feb From 5be79ff27a5852567a9bdec80d67b061ad828290 Mon Sep 17 00:00:00 2001 From: Alexander Shishkin Date: Wed, 20 Oct 2010 13:22:24 +0200 Subject: add-shell, remove-shell: new applets function old new delta add_remove_shell_main - 259 +259 packed_usage 27408 27438 +30 applet_names 2326 2349 +23 applet_main 1364 1372 +8 applet_nameofs 682 686 +4 run_applet_and_exit 700 703 +3 dont_add - 2 +2 applet_install_loc 171 172 +1 ------------------------------------------------------------------------------ (add/remove: 3/0 grow/shrink: 6/0 up/down: 330/0) Total: 330 bytes Signed-off-by: Alexander Shishkin Signed-off-by: Denys Vlasenko --- loginutils/add-remove-shell.c | 127 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 loginutils/add-remove-shell.c diff --git a/loginutils/add-remove-shell.c b/loginutils/add-remove-shell.c new file mode 100644 index 000000000..986fe57c5 --- /dev/null +++ b/loginutils/add-remove-shell.c @@ -0,0 +1,127 @@ +/* + * add-shell and remove-shell implementation for busybox + * + * Copyright (C) 2010 Nokia Corporation. All rights reserved. + * Written by Alexander Shishkin + * + * Licensed under GPLv2 or later, see the LICENSE file in this source tree + * for details. + */ + +//applet:IF_ADD_SHELL( APPLET_ODDNAME(add-shell , add_remove_shell, _BB_DIR_USR_BIN, _BB_SUID_DROP, add_shell )) +//applet:IF_REMOVE_SHELL(APPLET_ODDNAME(remove-shell, add_remove_shell, _BB_DIR_USR_BIN, _BB_SUID_DROP, remove_shell)) + +//kbuild:lib-$(CONFIG_ADD_SHELL) += add-remove-shell.o +//kbuild:lib-$(CONFIG_REMOVE_SHELL) += add-remove-shell.o + +//config:config ADD_SHELL +//config: bool "add-shell" +//config: default y if DESKTOP +//config: help +//config: Add shells to /etc/shells. +//config: +//config:config REMOVE_SHELL +//config: bool "remove-shell" +//config: default y if DESKTOP +//config: help +//config: Remove shells from /etc/shells. + +//usage:#define add_shell_trivial_usage +//usage: "SHELL..." +//usage:#define add_shell_full_usage "\n\n" +//usage: "Add SHELLs to /etc/shells" + +//usage:#define remove_shell_trivial_usage +//usage: "SHELL..." +//usage:#define remove_shell_full_usage "\n\n" +//usage: "Remove SHELLs from /etc/shells" + +#include "libbb.h" + +#define SHELLS_FILE "/etc/shells" + +#define REMOVE_SHELL (ENABLE_REMOVE_SHELL && (!ENABLE_ADD_SHELL || applet_name[0] == 'r')) +#define ADD_SHELL (ENABLE_ADD_SHELL && (!ENABLE_REMOVE_SHELL || applet_name[0] == 'a')) + +/* NB: we use the _address_, not the value, of this string + * as a "special value of pointer" in the code. + */ +static const char dont_add[] ALIGN1 = "\n"; + +int add_remove_shell_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int add_remove_shell_main(int argc UNUSED_PARAM, char **argv) +{ + FILE *orig_fp; + char *orig_fn; + char *new_fn; + + argv++; + + orig_fn = xmalloc_follow_symlinks(SHELLS_FILE); + if (!orig_fn) + return EXIT_FAILURE; + orig_fp = fopen_for_read(orig_fn); + + new_fn = xasprintf("%s.tmp", orig_fn); + xmove_fd(xopen(new_fn, O_WRONLY | O_CREAT | O_EXCL), STDOUT_FILENO); + + /* TODO: + struct stat sb; + fstat(fileno(orig_fp), &sb); + xfchown(STDOUT_FILENO, sb.st_uid, sb.st_gid); + xfchmod(STDOUT_FILENO, sb.st_mode); + */ + + if (orig_fp) { + /* Copy old file, possibly skipping removed shell names */ + char *line; + while ((line = xmalloc_fgetline(orig_fp)) != NULL) { + char **cpp = argv; + while (*cpp) { + if (strcmp(*cpp, line) == 0) { + /* Old file has this shell name */ + if (REMOVE_SHELL) { + /* we are remove-shell */ + /* delete this name by not copying it */ + goto next_line; + } + /* we are add-shell */ + /* mark this name as "do not add" */ + *cpp = (char*)dont_add; + } + cpp++; + } + /* copy shell name from old to new file */ + printf("%s\n", line); + next_line: + free(line); + } + if (ENABLE_FEATURE_CLEAN_UP) + fclose(orig_fp); + } + + if (ADD_SHELL) { + char **cpp = argv; + while (*cpp) { + if (*cpp != dont_add) + printf("%s\n", *cpp); + cpp++; + } + } + + /* Ensure we wrote out everything */ + if (fclose(stdout) != 0) { + xunlink(new_fn); + bb_perror_msg_and_die("%s: write error", new_fn); + } + + /* Small hole: if rename fails, /etc/shells.tmp is not removed */ + xrename(new_fn, orig_fn); + + if (ENABLE_FEATURE_CLEAN_UP) { + free(orig_fn); + free(new_fn); + } + + return EXIT_SUCCESS; +} -- cgit v1.2.3-55-g6feb From ff19d525a98418915e5e10e0e0bb6e40f7259b76 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 15:14:32 +0200 Subject: add/remove-shell: use O_TRUNC instead of O_EXCL Signed-off-by: Denys Vlasenko --- loginutils/add-remove-shell.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/loginutils/add-remove-shell.c b/loginutils/add-remove-shell.c index 986fe57c5..757e50503 100644 --- a/loginutils/add-remove-shell.c +++ b/loginutils/add-remove-shell.c @@ -63,11 +63,19 @@ int add_remove_shell_main(int argc UNUSED_PARAM, char **argv) orig_fp = fopen_for_read(orig_fn); new_fn = xasprintf("%s.tmp", orig_fn); - xmove_fd(xopen(new_fn, O_WRONLY | O_CREAT | O_EXCL), STDOUT_FILENO); + /* + * O_TRUNC or O_EXCL? At the first glance, O_EXCL looks better, + * since it prevents races. But: (1) it requires a retry loop, + * (2) if /etc/shells.tmp is *stale*, then retry loop + * with O_EXCL will never succeed - it should have a timeout, + * after which it should revert to O_TRUNC. + * For now, I settle for O_TRUNC instead. + */ + xmove_fd(xopen(new_fn, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO); /* TODO: struct stat sb; - fstat(fileno(orig_fp), &sb); + xfstat(fileno(orig_fp), &sb); xfchown(STDOUT_FILENO, sb.st_uid, sb.st_gid); xfchmod(STDOUT_FILENO, sb.st_mode); */ -- cgit v1.2.3-55-g6feb From c59e06e47199fedc97fbaa080f9ea63049c7176b Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 16:10:59 +0200 Subject: udhcpc: better help text function old new delta packed_usage 27452 27486 +34 Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpc.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 864fcf690..9713e817e 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -777,7 +777,6 @@ static void client_background(void) //usage: IF_LONG_OPTS( //usage: "\n -i,--interface IFACE Interface to use (default eth0)" //usage: "\n -p,--pidfile FILE Create pidfile" -//usage: "\n -r,--request IP IP address to request" //usage: "\n -s,--script PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")" //usage: "\n -t,--retries N Send up to N discover packets" //usage: "\n -T,--timeout N Pause between packets (default 3 seconds)" @@ -786,10 +785,10 @@ static void client_background(void) //usage: USE_FOR_MMU( //usage: "\n -b,--background Background if lease is not obtained" //usage: ) -//usage: "\n -S,--syslog Log to syslog too" //usage: "\n -n,--now Exit if lease is not obtained" //usage: "\n -q,--quit Exit after obtaining lease" //usage: "\n -R,--release Release IP on exit" +//usage: "\n -S,--syslog Log to syslog too" //usage: IF_FEATURE_UDHCP_PORT( //usage: "\n -P,--client-port N Use port N (default 68)" //usage: ) @@ -798,8 +797,12 @@ static void client_background(void) //usage: ) //usage: "\n -O,--request-option OPT Request option OPT from server (cumulative)" //usage: "\n -o,--no-default-options Don't request any options (unless -O is given)" +//usage: "\n -r,--request IP Request this IP address" //usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)" -//usage: "\n Examples: -x hostname:bbox -x 61:0100ffee11cc55" +//usage: "\n Examples of string, numeric, and hex byte opts:" +//usage: "\n -x hostname:bbox - option 12" +//usage: "\n -x lease:3600 - option 51 (lease time)" +//usage: "\n -x 0x3d:0100BEEFC0FFEE - option 61 (client id)" //usage: "\n -F,--fqdn NAME Ask server to update DNS mapping for NAME" //usage: "\n -H,-h,--hostname NAME Send NAME as client hostname (default none)" //usage: "\n -V,--vendorclass VENDOR Vendor identifier (default 'udhcp VERSION')" @@ -811,29 +814,32 @@ static void client_background(void) //usage: IF_NOT_LONG_OPTS( //usage: "\n -i IFACE Interface to use (default eth0)" //usage: "\n -p FILE Create pidfile" -//usage: "\n -r IP IP address to request" //usage: "\n -s PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")" //usage: "\n -t N Send up to N discover packets" //usage: "\n -T N Pause between packets (default 3 seconds)" //usage: "\n -A N Wait N seconds (default 20) after failure" -//usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)" -//usage: "\n Examples: -x hostname:bbox -x 61:0100ffee11cc55" -//usage: "\n -O OPT Request option OPT from server (cumulative)" -//usage: "\n -o Don't request any options (unless -O is given)" //usage: "\n -f Run in foreground" //usage: USE_FOR_MMU( //usage: "\n -b Background if lease is not obtained" //usage: ) -//usage: "\n -S Log to syslog too" //usage: "\n -n Exit if lease is not obtained" //usage: "\n -q Exit after obtaining lease" //usage: "\n -R Release IP on exit" +//usage: "\n -S Log to syslog too" //usage: IF_FEATURE_UDHCP_PORT( //usage: "\n -P N Use port N (default 68)" //usage: ) //usage: IF_FEATURE_UDHCPC_ARPING( //usage: "\n -a Use arping to validate offered address" //usage: ) +//usage: "\n -O OPT Request option OPT from server (cumulative)" +//usage: "\n -o Don't request any options (unless -O is given)" +//usage: "\n -r IP Request this IP address" +//usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)" +//usage: "\n Examples of string, numeric, and hex byte opts:" +//usage: "\n -x hostname:bbox - option 12" +//usage: "\n -x lease:3600 - option 51 (lease time)" +//usage: "\n -x 0x3d:0100BEEFC0FFEE - option 61 (client id)" //usage: "\n -F NAME Ask server to update DNS mapping for NAME" //usage: "\n -H,-h NAME Send NAME as client hostname (default none)" //usage: "\n -V VENDOR Vendor identifier (default 'udhcp VERSION')" -- cgit v1.2.3-55-g6feb From 2c3b71a0d2e6e94d8fb75c6879b1b8203cea9e5a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 18:04:36 +0200 Subject: dhcp: typo fix: UPD_DHCP_SIZE -> UDP_DHCP_SIZE Signed-off-by: Denys Vlasenko --- networking/udhcp/common.h | 6 +++--- networking/udhcp/packet.c | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h index 9020b9c96..f8f18ff01 100644 --- a/networking/udhcp/common.h +++ b/networking/udhcp/common.h @@ -63,14 +63,14 @@ struct udp_dhcp_packet { } PACKED; enum { - IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, - UPD_DHCP_SIZE = sizeof(struct udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, + IP_UDP_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, + UDP_DHCP_SIZE = sizeof(struct udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, DHCP_SIZE = sizeof(struct dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, }; /* Let's see whether compiler understood us right */ struct BUG_bad_sizeof_struct_ip_udp_dhcp_packet { - char c[IP_UPD_DHCP_SIZE == 576 ? 1 : -1]; + char c[IP_UDP_DHCP_SIZE == 576 ? 1 : -1]; }; diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c index d8f9c5daa..2b7528cc7 100644 --- a/networking/udhcp/packet.c +++ b/networking/udhcp/packet.c @@ -216,19 +216,19 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, packet.udp.source = htons(source_port); packet.udp.dest = htons(dest_port); /* size, excluding IP header: */ - packet.udp.len = htons(UPD_DHCP_SIZE - padding); + packet.udp.len = htons(UDP_DHCP_SIZE - padding); /* for UDP checksumming, ip.len is set to UDP packet len */ packet.ip.tot_len = packet.udp.len; - packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE - padding); + packet.udp.check = udhcp_checksum(&packet, IP_UDP_DHCP_SIZE - padding); /* but for sending, it is set to IP packet len */ - packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE - padding); + packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding); packet.ip.ihl = sizeof(packet.ip) >> 2; packet.ip.version = IPVERSION; packet.ip.ttl = IPDEFTTL; packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip)); udhcp_dump_packet(dhcp_pkt); - result = sendto(fd, &packet, IP_UPD_DHCP_SIZE - padding, /*flags:*/ 0, + result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0, (struct sockaddr *) &dest_sll, sizeof(dest_sll)); msg = "sendto"; ret_close: -- cgit v1.2.3-55-g6feb From b3af65b95de883e9be403e065f57b867d8ea8d43 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 21:37:23 +0200 Subject: udhcpc: emit maxsize option in all non-NAK type packets Before, we were sending them only in DISCOVER packets. Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpc.c | 108 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 90 insertions(+), 18 deletions(-) diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 9713e817e..f685a1d22 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -346,31 +346,34 @@ static ALWAYS_INLINE uint32_t random_xid(void) /* Initialize the packet with the proper defaults */ static void init_packet(struct dhcp_packet *packet, char type) { + /* Fill in: op, htype, hlen, cookie fields; message type option: */ udhcp_init_header(packet, type); + + packet->xid = random_xid(); + memcpy(packet->chaddr, client_config.client_mac, 6); if (client_config.clientid) udhcp_add_binary_option(packet, client_config.clientid); +} + +static void add_client_options(struct dhcp_packet *packet) +{ + uint8_t c; + int i, end, len; + + udhcp_add_simple_option(packet, DHCP_MAX_SIZE, htons(IP_UDP_DHCP_SIZE)); if (client_config.hostname) udhcp_add_binary_option(packet, client_config.hostname); if (client_config.fqdn) udhcp_add_binary_option(packet, client_config.fqdn); - if (type != DHCPDECLINE - && type != DHCPRELEASE - && client_config.vendorclass - ) { + if (client_config.vendorclass) udhcp_add_binary_option(packet, client_config.vendorclass); - } -} -static void add_client_options(struct dhcp_packet *packet) -{ /* Add a "param req" option with the list of options we'd like to have * from stubborn DHCP servers. Pull the data from the struct in common.c. * No bounds checking because it goes towards the head of the packet. */ - uint8_t c; - int end = udhcp_end_option(packet->options); - int i, len = 0; - + end = udhcp_end_option(packet->options); + len = 0; for (i = 0; (c = dhcp_optflags[i].code) != 0; i++) { if (( (dhcp_optflags[i].flags & OPTION_REQ) && !client_config.no_default_options @@ -432,13 +435,20 @@ static int send_discover(uint32_t xid, uint32_t requested) { struct dhcp_packet packet; + /* Fill in: op, htype, hlen, cookie, chaddr fields, + * random xid field (we override it below), + * client-id option (unless -C), message type option: + */ init_packet(&packet, DHCPDISCOVER); + packet.xid = xid; if (requested) udhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested); - /* Explicitly saying that we want RFC-compliant packets helps - * some buggy DHCP servers to NOT send bigger packets */ - udhcp_add_simple_option(&packet, DHCP_MAX_SIZE, htons(576)); + + /* Add options: maxsize, + * optionally: hostname, fqdn, vendorclass, + * "param req" option according to -O, options specified with -x + */ add_client_options(&packet); bb_info_msg("Sending discover..."); @@ -454,10 +464,33 @@ static int send_select(uint32_t xid, uint32_t server, uint32_t requested) struct dhcp_packet packet; struct in_addr addr; +/* + * RFC 2131 4.3.2 DHCPREQUEST message + * ... + * If the DHCPREQUEST message contains a 'server identifier' + * option, the message is in response to a DHCPOFFER message. + * Otherwise, the message is a request to verify or extend an + * existing lease. If the client uses a 'client identifier' + * in a DHCPREQUEST message, it MUST use that same 'client identifier' + * in all subsequent messages. If the client included a list + * of requested parameters in a DHCPDISCOVER message, it MUST + * include that list in all subsequent messages. + */ + /* Fill in: op, htype, hlen, cookie, chaddr fields, + * random xid field (we override it below), + * client-id option (unless -C), message type option: + */ init_packet(&packet, DHCPREQUEST); + packet.xid = xid; udhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested); + udhcp_add_simple_option(&packet, DHCP_SERVER_ID, server); + + /* Add options: maxsize, + * optionally: hostname, fqdn, vendorclass, + * "param req" option according to -O, and options specified with -x + */ add_client_options(&packet); addr.s_addr = requested; @@ -470,9 +503,33 @@ static int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) { struct dhcp_packet packet; +/* + * RFC 2131 4.3.2 DHCPREQUEST message + * ... + * DHCPREQUEST generated during RENEWING state: + * + * 'server identifier' MUST NOT be filled in, 'requested IP address' + * option MUST NOT be filled in, 'ciaddr' MUST be filled in with + * client's IP address. In this situation, the client is completely + * configured, and is trying to extend its lease. This message will + * be unicast, so no relay agents will be involved in its + * transmission. Because 'giaddr' is therefore not filled in, the + * DHCP server will trust the value in 'ciaddr', and use it when + * replying to the client. + */ + /* Fill in: op, htype, hlen, cookie, chaddr fields, + * random xid field (we override it below), + * client-id option (unless -C), message type option: + */ init_packet(&packet, DHCPREQUEST); + packet.xid = xid; packet.ciaddr = ciaddr; + + /* Add options: maxsize, + * optionally: hostname, fqdn, vendorclass, + * "param req" option according to -O, and options specified with -x + */ add_client_options(&packet); bb_info_msg("Sending renew..."); @@ -489,9 +546,20 @@ static int send_decline(uint32_t xid, uint32_t server, uint32_t requested) { struct dhcp_packet packet; + /* Fill in: op, htype, hlen, cookie, chaddr, random xid fields, + * client-id option (unless -C), message type option: + */ init_packet(&packet, DHCPDECLINE); + + /* RFC 2131 says DHCPDECLINE's xid is randomly selected by client, + * but in case the server is buggy and wants DHCPDECLINE's xid + * to match the xid which started entire handshake, + * we use the same xid we used in initial DHCPDISCOVER: + */ packet.xid = xid; + /* DHCPDECLINE uses "requested ip", not ciaddr, to store offered IP */ udhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested); + udhcp_add_simple_option(&packet, DHCP_SERVER_ID, server); bb_info_msg("Sending decline..."); @@ -504,8 +572,12 @@ static int send_release(uint32_t server, uint32_t ciaddr) { struct dhcp_packet packet; + /* Fill in: op, htype, hlen, cookie, chaddr, random xid fields, + * client-id option (unless -C), message type option: + */ init_packet(&packet, DHCPRELEASE); - packet.xid = random_xid(); + + /* DHCPRELEASE uses ciaddr, not "requested ip", to store IP being released */ packet.ciaddr = ciaddr; udhcp_add_simple_option(&packet, DHCP_SERVER_ID, server); @@ -1254,7 +1326,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) /* Ignore packets that aren't for us */ if (packet.hlen != 6 - || memcmp(packet.chaddr, client_config.client_mac, 6) + || memcmp(packet.chaddr, client_config.client_mac, 6) != 0 ) { //FIXME: need to also check that last 10 bytes are zero log1("chaddr does not match, ignoring packet"); // log2? @@ -1280,7 +1352,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) } /* it IS unaligned sometimes, don't "optimize" */ move_from_unaligned32(server_addr, temp); - xid = packet.xid; + /*xid = packet.xid; - already is */ requested_ip = packet.yiaddr; /* enter requesting state */ -- cgit v1.2.3-55-g6feb From a5048fa386b4c54f269c0a23c579970acdd1edd5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 21:38:29 +0200 Subject: udhcpc: periodically reread our ifindex and mac Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpc.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index f685a1d22..f0c8ace2d 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -1167,6 +1167,16 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) * resend discover/renew/whatever */ if (retval == 0) { + /* When running on a bridge, the ifindex may have changed + * (e.g. if member interfaces were added/removed + * or if the status of the bridge changed). + * Refresh ifindex and client_mac: + */ + udhcp_read_interface(client_config.interface, + &client_config.ifindex, + NULL, + client_config.client_mac); + /* We will restart the wait in any case */ already_waited_sec = 0; -- cgit v1.2.3-55-g6feb From c72c1d7b317ecd7dc93bae86ad24e40402b9a2d1 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 22:08:16 +0200 Subject: udhcpc: reduce stack usage by de-inlining routines with on-stack pkt buf Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpc.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index f0c8ace2d..7b679d10f 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -362,12 +362,6 @@ static void add_client_options(struct dhcp_packet *packet) int i, end, len; udhcp_add_simple_option(packet, DHCP_MAX_SIZE, htons(IP_UDP_DHCP_SIZE)); - if (client_config.hostname) - udhcp_add_binary_option(packet, client_config.hostname); - if (client_config.fqdn) - udhcp_add_binary_option(packet, client_config.fqdn); - if (client_config.vendorclass) - udhcp_add_binary_option(packet, client_config.vendorclass); /* Add a "param req" option with the list of options we'd like to have * from stubborn DHCP servers. Pull the data from the struct in common.c. @@ -390,6 +384,13 @@ static void add_client_options(struct dhcp_packet *packet) packet->options[end + OPT_DATA + len] = DHCP_END; } + if (client_config.vendorclass) + udhcp_add_binary_option(packet, client_config.vendorclass); + if (client_config.hostname) + udhcp_add_binary_option(packet, client_config.hostname); + if (client_config.fqdn) + udhcp_add_binary_option(packet, client_config.fqdn); + /* Add -x options if any */ { struct option_set *curr = client_config.options; @@ -431,7 +432,7 @@ static int raw_bcast_from_client_config_ifindex(struct dhcp_packet *packet) } /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ -static int send_discover(uint32_t xid, uint32_t requested) +static NOINLINE int send_discover(uint32_t xid, uint32_t requested) { struct dhcp_packet packet; @@ -459,7 +460,7 @@ static int send_discover(uint32_t xid, uint32_t requested) /* RFC 2131 3.1 paragraph 3: * "The client _broadcasts_ a DHCPREQUEST message..." */ -static int send_select(uint32_t xid, uint32_t server, uint32_t requested) +static NOINLINE int send_select(uint32_t xid, uint32_t server, uint32_t requested) { struct dhcp_packet packet; struct in_addr addr; @@ -542,7 +543,7 @@ static int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) #if ENABLE_FEATURE_UDHCPC_ARPING /* Broadcast a DHCP decline message */ -static int send_decline(uint32_t xid, uint32_t server, uint32_t requested) +static NOINLINE int send_decline(uint32_t xid, uint32_t server, uint32_t requested) { struct dhcp_packet packet; @@ -1356,7 +1357,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) /* TODO: why we don't just fetch server's IP from IP header? */ temp = udhcp_get_option(&packet, DHCP_SERVER_ID); if (!temp) { - bb_error_msg("no server ID in message"); + bb_error_msg("no server ID, ignoring packet"); continue; /* still selecting - this server looks bad */ } -- cgit v1.2.3-55-g6feb From 5d374e9b148b85954816e9f79e4be160ed3054bd Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 20 Oct 2010 22:26:38 +0200 Subject: udhcpc: exit if iface disappeared; use correct MAC if it changes function old new delta udhcpc_main 2560 2618 +58 Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpc.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 7b679d10f..cef0ab957 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -928,6 +928,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) uint8_t *temp, *message; const char *str_V, *str_h, *str_F, *str_r; IF_FEATURE_UDHCP_PORT(char *str_P;) + void *clientid_mac_ptr; llist_t *list_O = NULL; llist_t *list_x = NULL; int tryagain_timeout = 20; @@ -1004,7 +1005,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,) }; - /* Default options. */ + /* Default options */ IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;) IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;) client_config.interface = "eth0"; @@ -1086,11 +1087,13 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) return 1; } + clientid_mac_ptr = NULL; if (!(opt & OPT_C) && !udhcp_find_option(client_config.options, DHCP_CLIENT_ID)) { /* not suppressed and not set, set the default client ID */ client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7); client_config.clientid[OPT_DATA] = 1; /* type: ethernet */ - memcpy(client_config.clientid + OPT_DATA+1, client_config.client_mac, 6); + clientid_mac_ptr = client_config.clientid + OPT_DATA+1; + memcpy(clientid_mac_ptr, client_config.client_mac, 6); } if (str_V[0] != '\0') client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0); @@ -1173,10 +1176,15 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) * or if the status of the bridge changed). * Refresh ifindex and client_mac: */ - udhcp_read_interface(client_config.interface, - &client_config.ifindex, - NULL, - client_config.client_mac); + if (udhcp_read_interface(client_config.interface, + &client_config.ifindex, + NULL, + client_config.client_mac) + ) { + return 1; /* iface is gone? */ + } + if (clientid_mac_ptr) + memcpy(clientid_mac_ptr, client_config.client_mac, 6); /* We will restart the wait in any case */ already_waited_sec = 0; -- cgit v1.2.3-55-g6feb From 894ef6003242bcb0b5ae4ba766475ed048b9f5ca Mon Sep 17 00:00:00 2001 From: Leonid Lisovskiy Date: Wed, 20 Oct 2010 22:36:51 +0200 Subject: ntpd: fix usage text and a typo in constant name Signed-off-by: Leonid Lisovskiy Signed-off-by: Denys Vlasenko --- include/usage.src.h | 4 +++- networking/ntpd.c | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/usage.src.h b/include/usage.src.h index b73ca4d89..2445c1b9b 100644 --- a/include/usage.src.h +++ b/include/usage.src.h @@ -2788,7 +2788,7 @@ INSERT "Address: 127.0.0.1\n" #define ntpd_trivial_usage \ - "[-dnqwl] [-S PROG] [-p PEER]..." + "[-dnqNw"IF_FEATURE_NTPD_SERVER("l")"] [-S PROG] [-p PEER]..." #define ntpd_full_usage "\n\n" \ "NTP client/server\n" \ "\nOptions:" \ @@ -2797,7 +2797,9 @@ INSERT "\n -q Quit after clock is set" \ "\n -N Run at high priority" \ "\n -w Do not set time (only query peers), implies -n" \ + IF_FEATURE_NTPD_SERVER( \ "\n -l Run as server on port 123" \ + ) \ "\n -S PROG Run PROG after stepping time, stratum change, and every 11 mins" \ "\n -p PEER Obtain time from PEER (may be repeated)" \ diff --git a/networking/ntpd.c b/networking/ntpd.c index ca4afa045..b7bd239b5 100644 --- a/networking/ntpd.c +++ b/networking/ntpd.c @@ -49,7 +49,7 @@ /* High-level description of the algorithm: * * We start running with very small poll_exp, BURSTPOLL, - * in order to quickly accumulate INITIAL_SAMLPES datapoints + * in order to quickly accumulate INITIAL_SAMPLES datapoints * for each peer. Then, time is stepped if the offset is larger * than STEP_THRESHOLD, otherwise it isn't; anyway, we enlarge * poll_exp to MINPOLL and enter frequency measurement step: @@ -77,7 +77,7 @@ #define RETRY_INTERVAL 5 /* on error, retry in N secs */ #define RESPONSE_INTERVAL 15 /* wait for reply up to N secs */ -#define INITIAL_SAMLPES 4 /* how many samples do we want for init */ +#define INITIAL_SAMPLES 4 /* how many samples do we want for init */ /* Clock discipline parameters and constants */ @@ -1972,14 +1972,14 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv) idx2peer = xzalloc(sizeof(idx2peer[0]) * cnt); pfd = xzalloc(sizeof(pfd[0]) * cnt); - /* Countdown: we never sync before we sent INITIAL_SAMLPES+1 + /* Countdown: we never sync before we sent INITIAL_SAMPLES+1 * packets to each peer. * NB: if some peer is not responding, we may end up sending * fewer packets to it and more to other peers. - * NB2: sync usually happens using INITIAL_SAMLPES packets, + * NB2: sync usually happens using INITIAL_SAMPLES packets, * since last reply does not come back instantaneously. */ - cnt = G.peer_cnt * (INITIAL_SAMLPES + 1); + cnt = G.peer_cnt * (INITIAL_SAMPLES + 1); while (!bb_got_signal) { llist_t *item; -- cgit v1.2.3-55-g6feb From a7027bf89e57d59958641ee7033e0f85f26ef412 Mon Sep 17 00:00:00 2001 From: Alexander Shishkin Date: Thu, 21 Oct 2010 00:24:05 +0200 Subject: stat: remove superfluous setXXent() calls, plug memory leak function old new delta print_stat 875 865 -10 Signed-off-by: Alexander Shishkin Signed-off-by: Denys Vlasenko --- coreutils/stat.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/coreutils/stat.c b/coreutils/stat.c index 777197292..d176d07ea 100644 --- a/coreutils/stat.c +++ b/coreutils/stat.c @@ -247,14 +247,12 @@ static void FAST_FUNC print_stat(char *pformat, const char m, strcat(pformat, "lu"); printf(pformat, (unsigned long) statbuf->st_uid); } else if (m == 'U') { - setpwent(); pw_ent = getpwuid(statbuf->st_uid); printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN"); } else if (m == 'g') { strcat(pformat, "lu"); printf(pformat, (unsigned long) statbuf->st_gid); } else if (m == 'G') { - setgrent(); gw_ent = getgrgid(statbuf->st_gid); printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN"); } else if (m == 't') { @@ -591,20 +589,20 @@ static bool do_stat(const char *filename, const char *format) # endif } else { char *linkname = NULL; - struct passwd *pw_ent; struct group *gw_ent; - setgrent(); + gw_ent = getgrgid(statbuf.st_gid); - setpwent(); pw_ent = getpwuid(statbuf.st_uid); if (S_ISLNK(statbuf.st_mode)) linkname = xmalloc_readlink_or_warn(filename); - if (linkname) + if (linkname) { printf(" File: '%s' -> '%s'\n", filename, linkname); - else + free(linkname); + } else { printf(" File: '%s'\n", filename); + } printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n" "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu", -- cgit v1.2.3-55-g6feb From 2348e09557c5ff9cac134cfeeffd149f90a44971 Mon Sep 17 00:00:00 2001 From: Alexander Shishkin Date: Thu, 21 Oct 2010 00:25:45 +0200 Subject: tail: free tailbuf upon cleaning up Signed-off-by: Alexander Shishkin Signed-off-by: Denys Vlasenko --- coreutils/tail.c | 1 + 1 file changed, 1 insertion(+) diff --git a/coreutils/tail.c b/coreutils/tail.c index 44698f304..df881a37a 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c @@ -346,6 +346,7 @@ int tail_main(int argc, char **argv) } if (ENABLE_FEATURE_CLEAN_UP) { free(fds); + free(tailbuf); } return G.status; } -- cgit v1.2.3-55-g6feb From f0c7e0c8d53fc1cbbccf0b23c5e5242a9707c6e3 Mon Sep 17 00:00:00 2001 From: Gilles Espinasse Date: Thu, 21 Oct 2010 00:39:46 +0200 Subject: patch: implement -E option Signed-off-by: Gilles Espinasse Signed-off-by: Denys Vlasenko --- editors/patch.c | 25 ++++++++++++++++--------- include/usage.src.h | 12 +++++++----- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/editors/patch.c b/editors/patch.c index 66a9474fe..fff06907f 100644 --- a/editors/patch.c +++ b/editors/patch.c @@ -17,7 +17,6 @@ * -o outfile output here instead of in place * -r rejectfile write rejected hunks to this file * - * -E remove empty files --remove-empty-files * -f force (no questions asked) * -F fuzz (number, default 2) * [file] which file to patch @@ -42,7 +41,7 @@ config PATCH hunks to stderr, and exits with nonzero status if any hunks fail. A file compared against /dev/null (or with a date <= the epoch) is - created/deleted as appropriate. + created or deleted if -E or --remove-empty-files set. */ #include "libbb.h" @@ -243,15 +242,16 @@ struct globals { } while (0) -#define FLAG_STR "Rup:i:Nx" +#define FLAG_STR "Rup:i:NEx" /* FLAG_REVERSE must be == 1! Code uses this fact. */ #define FLAG_REVERSE (1 << 0) #define FLAG_u (1 << 1) #define FLAG_PATHLEN (1 << 2) #define FLAG_INPUT (1 << 3) #define FLAG_IGNORE (1 << 4) +#define FLAG_RMEMPTY (1 << 5) //non-standard: -#define FLAG_DEBUG (1 << 5) +#define FLAG_DEBUG (1 << 6) // Dispose of a line of input, either by writing it out or discarding it. @@ -551,7 +551,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) // If this is the first hunk, open the file. if (TT.filein == -1) { - int oldsum, newsum, del = 0; + int oldsum, newsum, empty = 0; char *name; oldsum = TT.oldline + TT.oldlen; @@ -564,7 +564,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) { name = reverse ? newname : oldname; - del++; + empty++; } // handle -p path truncation. @@ -576,10 +576,17 @@ int patch_main(int argc UNUSED_PARAM, char **argv) } } - if (del) { - printf("removing %s\n", name); - xunlink(name); + if (empty) { + // File is empty after the patches have been applied state = 0; + if (option_mask32 & FLAG_RMEMPTY) { + // If flag -E or --remove-empty-files is set + printf("removing %s\n", name); + xunlink(name); + } else { + printf("patching file %s\n", name); + xclose(xopen(name, O_WRONLY | O_TRUNC)); + } // If we've got a file to open, do so. } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) { // If the old file was null, we're creating a new one. diff --git a/include/usage.src.h b/include/usage.src.h index 2445c1b9b..f5ddd7ba5 100644 --- a/include/usage.src.h +++ b/include/usage.src.h @@ -2857,17 +2857,19 @@ INSERT "[OPTIONS] [ORIGFILE [PATCHFILE]]" #define patch_full_usage "\n\n" \ IF_LONG_OPTS( \ - " -p,--strip N Strip N leading components from file names" \ - "\n -i,--input DIFF Read DIFF instead of stdin" \ - "\n -R,--reverse Reverse patch" \ - "\n -N,--forward Ignore already applied patches" \ - "\n --dry-run Don't actually change files" \ + " -p,--strip N Strip N leading components from file names" \ + "\n -i,--input DIFF Read DIFF instead of stdin" \ + "\n -R,--reverse Reverse patch" \ + "\n -N,--forward Ignore already applied patches" \ + "\n --dry-run Don't actually change files" \ + "\n -E,--remove-empty-files Remove output files if they become empty" \ ) \ IF_NOT_LONG_OPTS( \ " -p N Strip N leading components from file names" \ "\n -i DIFF Read DIFF instead of stdin" \ "\n -R Reverse patch" \ "\n -N Ignore already applied patches" \ + "\n -E Remove output files if they become empty" \ ) #define patch_example_usage \ -- cgit v1.2.3-55-g6feb From e7599d1cc7885017fa53955e257af248d183f2e3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Oct 2010 00:42:15 +0200 Subject: revert last change (wrong author) Signed-off-by: Denys Vlasenko --- editors/patch.c | 25 +++++++++---------------- include/usage.src.h | 12 +++++------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/editors/patch.c b/editors/patch.c index fff06907f..66a9474fe 100644 --- a/editors/patch.c +++ b/editors/patch.c @@ -17,6 +17,7 @@ * -o outfile output here instead of in place * -r rejectfile write rejected hunks to this file * + * -E remove empty files --remove-empty-files * -f force (no questions asked) * -F fuzz (number, default 2) * [file] which file to patch @@ -41,7 +42,7 @@ config PATCH hunks to stderr, and exits with nonzero status if any hunks fail. A file compared against /dev/null (or with a date <= the epoch) is - created or deleted if -E or --remove-empty-files set. + created/deleted as appropriate. */ #include "libbb.h" @@ -242,16 +243,15 @@ struct globals { } while (0) -#define FLAG_STR "Rup:i:NEx" +#define FLAG_STR "Rup:i:Nx" /* FLAG_REVERSE must be == 1! Code uses this fact. */ #define FLAG_REVERSE (1 << 0) #define FLAG_u (1 << 1) #define FLAG_PATHLEN (1 << 2) #define FLAG_INPUT (1 << 3) #define FLAG_IGNORE (1 << 4) -#define FLAG_RMEMPTY (1 << 5) //non-standard: -#define FLAG_DEBUG (1 << 6) +#define FLAG_DEBUG (1 << 5) // Dispose of a line of input, either by writing it out or discarding it. @@ -551,7 +551,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) // If this is the first hunk, open the file. if (TT.filein == -1) { - int oldsum, newsum, empty = 0; + int oldsum, newsum, del = 0; char *name; oldsum = TT.oldline + TT.oldlen; @@ -564,7 +564,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) { name = reverse ? newname : oldname; - empty++; + del++; } // handle -p path truncation. @@ -576,17 +576,10 @@ int patch_main(int argc UNUSED_PARAM, char **argv) } } - if (empty) { - // File is empty after the patches have been applied + if (del) { + printf("removing %s\n", name); + xunlink(name); state = 0; - if (option_mask32 & FLAG_RMEMPTY) { - // If flag -E or --remove-empty-files is set - printf("removing %s\n", name); - xunlink(name); - } else { - printf("patching file %s\n", name); - xclose(xopen(name, O_WRONLY | O_TRUNC)); - } // If we've got a file to open, do so. } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) { // If the old file was null, we're creating a new one. diff --git a/include/usage.src.h b/include/usage.src.h index f5ddd7ba5..2445c1b9b 100644 --- a/include/usage.src.h +++ b/include/usage.src.h @@ -2857,19 +2857,17 @@ INSERT "[OPTIONS] [ORIGFILE [PATCHFILE]]" #define patch_full_usage "\n\n" \ IF_LONG_OPTS( \ - " -p,--strip N Strip N leading components from file names" \ - "\n -i,--input DIFF Read DIFF instead of stdin" \ - "\n -R,--reverse Reverse patch" \ - "\n -N,--forward Ignore already applied patches" \ - "\n --dry-run Don't actually change files" \ - "\n -E,--remove-empty-files Remove output files if they become empty" \ + " -p,--strip N Strip N leading components from file names" \ + "\n -i,--input DIFF Read DIFF instead of stdin" \ + "\n -R,--reverse Reverse patch" \ + "\n -N,--forward Ignore already applied patches" \ + "\n --dry-run Don't actually change files" \ ) \ IF_NOT_LONG_OPTS( \ " -p N Strip N leading components from file names" \ "\n -i DIFF Read DIFF instead of stdin" \ "\n -R Reverse patch" \ "\n -N Ignore already applied patches" \ - "\n -E Remove output files if they become empty" \ ) #define patch_example_usage \ -- cgit v1.2.3-55-g6feb From 08187356d7f6a79d5fb4aa83b90476997e585ad3 Mon Sep 17 00:00:00 2001 From: Lukas Huba Date: Thu, 21 Oct 2010 00:43:00 +0200 Subject: patch: implement -E option Signed-off-by: Lukas Huba Signed-off-by: Denys Vlasenko --- editors/patch.c | 25 ++++++++++++++++--------- include/usage.src.h | 12 +++++++----- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/editors/patch.c b/editors/patch.c index 66a9474fe..fff06907f 100644 --- a/editors/patch.c +++ b/editors/patch.c @@ -17,7 +17,6 @@ * -o outfile output here instead of in place * -r rejectfile write rejected hunks to this file * - * -E remove empty files --remove-empty-files * -f force (no questions asked) * -F fuzz (number, default 2) * [file] which file to patch @@ -42,7 +41,7 @@ config PATCH hunks to stderr, and exits with nonzero status if any hunks fail. A file compared against /dev/null (or with a date <= the epoch) is - created/deleted as appropriate. + created or deleted if -E or --remove-empty-files set. */ #include "libbb.h" @@ -243,15 +242,16 @@ struct globals { } while (0) -#define FLAG_STR "Rup:i:Nx" +#define FLAG_STR "Rup:i:NEx" /* FLAG_REVERSE must be == 1! Code uses this fact. */ #define FLAG_REVERSE (1 << 0) #define FLAG_u (1 << 1) #define FLAG_PATHLEN (1 << 2) #define FLAG_INPUT (1 << 3) #define FLAG_IGNORE (1 << 4) +#define FLAG_RMEMPTY (1 << 5) //non-standard: -#define FLAG_DEBUG (1 << 5) +#define FLAG_DEBUG (1 << 6) // Dispose of a line of input, either by writing it out or discarding it. @@ -551,7 +551,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) // If this is the first hunk, open the file. if (TT.filein == -1) { - int oldsum, newsum, del = 0; + int oldsum, newsum, empty = 0; char *name; oldsum = TT.oldline + TT.oldlen; @@ -564,7 +564,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) { name = reverse ? newname : oldname; - del++; + empty++; } // handle -p path truncation. @@ -576,10 +576,17 @@ int patch_main(int argc UNUSED_PARAM, char **argv) } } - if (del) { - printf("removing %s\n", name); - xunlink(name); + if (empty) { + // File is empty after the patches have been applied state = 0; + if (option_mask32 & FLAG_RMEMPTY) { + // If flag -E or --remove-empty-files is set + printf("removing %s\n", name); + xunlink(name); + } else { + printf("patching file %s\n", name); + xclose(xopen(name, O_WRONLY | O_TRUNC)); + } // If we've got a file to open, do so. } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) { // If the old file was null, we're creating a new one. diff --git a/include/usage.src.h b/include/usage.src.h index 2445c1b9b..f5ddd7ba5 100644 --- a/include/usage.src.h +++ b/include/usage.src.h @@ -2857,17 +2857,19 @@ INSERT "[OPTIONS] [ORIGFILE [PATCHFILE]]" #define patch_full_usage "\n\n" \ IF_LONG_OPTS( \ - " -p,--strip N Strip N leading components from file names" \ - "\n -i,--input DIFF Read DIFF instead of stdin" \ - "\n -R,--reverse Reverse patch" \ - "\n -N,--forward Ignore already applied patches" \ - "\n --dry-run Don't actually change files" \ + " -p,--strip N Strip N leading components from file names" \ + "\n -i,--input DIFF Read DIFF instead of stdin" \ + "\n -R,--reverse Reverse patch" \ + "\n -N,--forward Ignore already applied patches" \ + "\n --dry-run Don't actually change files" \ + "\n -E,--remove-empty-files Remove output files if they become empty" \ ) \ IF_NOT_LONG_OPTS( \ " -p N Strip N leading components from file names" \ "\n -i DIFF Read DIFF instead of stdin" \ "\n -R Reverse patch" \ "\n -N Ignore already applied patches" \ + "\n -E Remove output files if they become empty" \ ) #define patch_example_usage \ -- cgit v1.2.3-55-g6feb From 0bb35e19a73ecbb9694172300a5530dbb8156bb4 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Oct 2010 12:33:10 +0200 Subject: udhcpd: reduce stack usage by ~700 bytes. +28 bytes code size Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpc.c | 7 ++++++- networking/udhcp/dhcpd.c | 12 ++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index cef0ab957..78aabedf2 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -432,6 +432,7 @@ static int raw_bcast_from_client_config_ifindex(struct dhcp_packet *packet) } /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ +/* NOINLINE: limit stack usage in caller */ static NOINLINE int send_discover(uint32_t xid, uint32_t requested) { struct dhcp_packet packet; @@ -460,6 +461,7 @@ static NOINLINE int send_discover(uint32_t xid, uint32_t requested) /* RFC 2131 3.1 paragraph 3: * "The client _broadcasts_ a DHCPREQUEST message..." */ +/* NOINLINE: limit stack usage in caller */ static NOINLINE int send_select(uint32_t xid, uint32_t server, uint32_t requested) { struct dhcp_packet packet; @@ -500,7 +502,8 @@ static NOINLINE int send_select(uint32_t xid, uint32_t server, uint32_t requeste } /* Unicast or broadcast a DHCP renew message */ -static int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) +/* NOINLINE: limit stack usage in caller */ +static NOINLINE int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) { struct dhcp_packet packet; @@ -543,6 +546,7 @@ static int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) #if ENABLE_FEATURE_UDHCPC_ARPING /* Broadcast a DHCP decline message */ +/* NOINLINE: limit stack usage in caller */ static NOINLINE int send_decline(uint32_t xid, uint32_t server, uint32_t requested) { struct dhcp_packet packet; @@ -588,6 +592,7 @@ static int send_release(uint32_t server, uint32_t ciaddr) } /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */ +/* NOINLINE: limit stack usage in caller */ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) { int bytes; diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index 043220de9..f0878652c 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -132,7 +132,8 @@ static uint32_t select_lease_time(struct dhcp_packet *packet) } /* We got a DHCP DISCOVER. Send an OFFER. */ -static void send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease) +/* NOINLINE: limit stack usage in caller */ +static NOINLINE void send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease) { struct dhcp_packet packet; uint32_t lease_time_sec; @@ -202,7 +203,8 @@ static void send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, send_packet(&packet, /*force_bcast:*/ 0); } -static void send_NAK(struct dhcp_packet *oldpacket) +/* NOINLINE: limit stack usage in caller */ +static NOINLINE void send_NAK(struct dhcp_packet *oldpacket) { struct dhcp_packet packet; @@ -212,7 +214,8 @@ static void send_NAK(struct dhcp_packet *oldpacket) send_packet(&packet, /*force_bcast:*/ 1); } -static void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) +/* NOINLINE: limit stack usage in caller */ +static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) { struct dhcp_packet packet; uint32_t lease_time_sec; @@ -243,7 +246,8 @@ static void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) } } -static void send_inform(struct dhcp_packet *oldpacket) +/* NOINLINE: limit stack usage in caller */ +static NOINLINE void send_inform(struct dhcp_packet *oldpacket) { struct dhcp_packet packet; -- cgit v1.2.3-55-g6feb From d3036ef608fa1d0a0763dfb5388ae96dcc8e2d99 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 22 Oct 2010 13:15:15 +0200 Subject: pmap: get rid of a warning Signed-off-by: Denys Vlasenko --- procps/pmap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/procps/pmap.c b/procps/pmap.c index cfa94ed82..bb5f9e7c2 100644 --- a/procps/pmap.c +++ b/procps/pmap.c @@ -44,7 +44,7 @@ enum { static void print_smaprec(struct smaprec *currec, void *data) { - unsigned opt = (unsigned)data; + unsigned opt = (uintptr_t)data; printf("%0" AFMT "lx ", currec->smap_start); @@ -74,7 +74,7 @@ static int procps_get_maps(pid_t pid, unsigned opt) memset(&total, 0, sizeof(total)); - ret = procps_read_smaps(pid, &total, print_smaprec, (void*)opt); + ret = procps_read_smaps(pid, &total, print_smaprec, (void*)(uintptr_t)opt); if (ret) return ret; -- cgit v1.2.3-55-g6feb From 46b6cd763053a929680500d5ce7a347adbc608a4 Mon Sep 17 00:00:00 2001 From: Alexander Shishkin Date: Thu, 21 Oct 2010 13:32:27 +0300 Subject: nbd-client: kill unused variable (total 0 bytes) Signed-off-by: Alexander Shishkin Signed-off-by: Denys Vlasenko --- networking/nbd-client.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/networking/nbd-client.c b/networking/nbd-client.c index 5ac190c32..8b856eda7 100644 --- a/networking/nbd-client.c +++ b/networking/nbd-client.c @@ -46,7 +46,9 @@ int nbdclient_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int nbdclient_main(int argc, char **argv) { unsigned long timeout = 0; +#if BB_MMU int nofork = 0; +#endif char *host, *port, *device; struct nbd_header_t { uint64_t magic1; // "NBDMAGIC" -- cgit v1.2.3-55-g6feb From cbfeaac7afe31323d46c52da3b98a949232d708e Mon Sep 17 00:00:00 2001 From: Alexander Shishkin Date: Thu, 21 Oct 2010 23:44:47 +0300 Subject: smemcap: close /proc handle upon cleaning up Signed-off-by: Alexander Shishkin Signed-off-by: Denys Vlasenko --- procps/smemcap.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/procps/smemcap.c b/procps/smemcap.c index f951a5fb6..196c91f54 100644 --- a/procps/smemcap.c +++ b/procps/smemcap.c @@ -125,5 +125,8 @@ int smemcap_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) } } + if (ENABLE_FEATURE_CLEAN_UP) + closedir(d); + return EXIT_SUCCESS; } -- cgit v1.2.3-55-g6feb