diff options
author | pfg <pfg@FreeBSD.org> | 2014-07-21 22:37:33 +0000 |
---|---|---|
committer | pfg <pfg@FreeBSD.org> | 2014-07-21 22:37:33 +0000 |
commit | 832f66713038de9143d814e4a87f26dd2032ee6d (patch) | |
tree | 79204e414214fb1bcafa2e7fd142ab3dffc5b7a9 /lib/libc/gen | |
parent | cd7f6c035288b834a106285e18dcdf78de463854 (diff) | |
download | FreeBSD-src-832f66713038de9143d814e4a87f26dd2032ee6d.zip FreeBSD-src-832f66713038de9143d814e4a87f26dd2032ee6d.tar.gz |
MFC r268642:
libc/gen: small updates to code originating at OpenBSD
arc4random.c
- CVS rev. 1.22
Change arc4random_uniform() to calculate ``2**32 % upper_bound'' as
``-upper_bound % upper_bound''. Simplifies the code and makes it the
same on both ILP32 and LP64 architectures, and also slightly faster on
LP64 architectures by using a 32-bit remainder instead of a 64-bit
remainder.
- CVS rev. 1.23
Spacing
readpassphrase.c
-CVS rev. v 1.24
most obvious unsigned char casts for ctype
Obtained from: OpenBSD
Diffstat (limited to 'lib/libc/gen')
-rw-r--r-- | lib/libc/gen/arc4random.c | 19 | ||||
-rw-r--r-- | lib/libc/gen/readpassphrase.c | 8 |
2 files changed, 8 insertions, 19 deletions
diff --git a/lib/libc/gen/arc4random.c b/lib/libc/gen/arc4random.c index 59e410b..59c4f7f 100644 --- a/lib/libc/gen/arc4random.c +++ b/lib/libc/gen/arc4random.c @@ -1,4 +1,4 @@ -/* $OpenBSD: arc4random.c,v 1.22 2010/12/22 08:23:42 otto Exp $ */ +/* $OpenBSD: arc4random.c,v 1.24 2013/06/11 16:59:50 deraadt Exp $ */ /* * Copyright (c) 1996, David Mazieres <dm@uun.org> @@ -182,8 +182,7 @@ arc4_stir_if_needed(void) { pid_t pid = getpid(); - if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid) - { + if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid) { arc4_stir_pid = pid; arc4_stir(); } @@ -276,18 +275,8 @@ arc4random_uniform(u_int32_t upper_bound) if (upper_bound < 2) return 0; -#if (ULONG_MAX > 0xffffffffUL) - min = 0x100000000UL % upper_bound; -#else - /* Calculate (2**32 % upper_bound) avoiding 64-bit math */ - if (upper_bound > 0x80000000) - min = 1 + ~upper_bound; /* 2**32 - upper_bound */ - else { - /* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */ - min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound; - } -#endif - + /* 2**32 % x == (2**32 - x) % x */ + min = -upper_bound % upper_bound; /* * This could theoretically loop forever but each retry has * p > 0.5 (worst case, usually far better) of selecting a diff --git a/lib/libc/gen/readpassphrase.c b/lib/libc/gen/readpassphrase.c index 86c7811..95ae725 100644 --- a/lib/libc/gen/readpassphrase.c +++ b/lib/libc/gen/readpassphrase.c @@ -1,4 +1,4 @@ -/* $OpenBSD: readpassphrase.c,v 1.23 2010/05/14 13:30:34 millert Exp $ */ +/* $OpenBSD: readpassphrase.c,v 1.24 2013/11/24 23:51:29 deraadt Exp $ */ /* * Copyright (c) 2000-2002, 2007, 2010 @@ -122,11 +122,11 @@ restart: if (p < end) { if ((flags & RPP_SEVENBIT)) ch &= 0x7f; - if (isalpha(ch)) { + if (isalpha((unsigned char)ch)) { if ((flags & RPP_FORCELOWER)) - ch = (char)tolower(ch); + ch = (char)tolower((unsigned char)ch); if ((flags & RPP_FORCEUPPER)) - ch = (char)toupper(ch); + ch = (char)toupper((unsigned char)ch); } *p++ = ch; } |