[git commit] libbb: shuffle ascii64 code around, shrink i2a64()

Denys Vlasenko vda.linux at googlemail.com
Sun Jul 6 19:59:03 UTC 2025


commit: https://git.busybox.net/busybox/commit/?id=5e17e3c6f49cef45a86ed9438941ca2d4f6ae906
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
num2str64_4chars_msb_first                             -      55     +55
num2str64_lsb_first                                    -      33     +33
i2a64                                                 42      25     -17
to64                                                  33       -     -33
to64_msb_first                                        55       -     -55
------------------------------------------------------------------------------
(add/remove: 2/2 grow/shrink: 0/1 up/down: 88/-105)           Total: -17 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 libbb/pw_encrypt.c     | 74 +-------------------------------------------------
 libbb/pw_encrypt_des.c | 15 ++--------
 libbb/pw_encrypt_md5.c |  4 +--
 libbb/pw_encrypt_sha.c |  2 +-
 4 files changed, 7 insertions(+), 88 deletions(-)

diff --git a/libbb/pw_encrypt.c b/libbb/pw_encrypt.c
index af84606bf..4acc33039 100644
--- a/libbb/pw_encrypt.c
+++ b/libbb/pw_encrypt.c
@@ -13,68 +13,7 @@
 #endif
 #include "libbb.h"
 
-/* 0..63 ->
- * "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- */
-int FAST_FUNC i2a64(int i)
-{
-	i &= 0x3f;
-	if (i == 0)
-		return '.';
-	if (i == 1)
-		return '/';
-	if (i < 12)
-		return ('0' - 2 + i);
-	if (i < 38)
-		return ('A' - 12 + i);
-	return ('a' - 38 + i);
-}
-
-/* Returns >=64 for invalid chars */
-int FAST_FUNC a2i64(char c)
-{
-	unsigned char ch = c;
-	if (ch >= 'a')
-		/* "a..z" to 38..63 */
-		/* anything after "z": positive int >= 64 */
-		return (ch - 'a' + 38);
-
-	if (ch > 'Z')
-		/* after "Z" but before "a": positive byte >= 64 */
-		return ch;
-
-	if (ch >= 'A')
-		/* "A..Z" to 12..37 */
-		return (ch - 'A' + 12);
-
-	if (ch > '9')
-		return 64;
-
-	/* "./0123456789" to 0,1,2..11 */
-	/* anything before "." becomes positive byte >= 64 */
-	return (unsigned char)(ch - '.');
-}
-
-int FAST_FUNC crypt_make_rand64encoded(char *p, int cnt /*, int x */)
-{
-	/* was: x += ... */
-	unsigned x = getpid() + monotonic_us();
-	do {
-		/* x = (x*1664525 + 1013904223) % 2^32 generator is lame
-		 * (low-order bit is not "random", etc...),
-		 * but for our purposes it is good enough */
-		x = x*1664525 + 1013904223;
-		/* BTW, Park and Miller's "minimal standard generator" is
-		 * x = x*16807 % ((2^31)-1)
-		 * It has no problem with visibly alternating lowest bit
-		 * but is also weak in cryptographic sense + needs div,
-		 * which needs more code (and slower) on many CPUs */
-		*p++ = i2a64(x >> 16);
-		*p++ = i2a64(x >> 22);
-	} while (--cnt);
-	*p = '\0';
-	return x;
-}
+#include "pw_ascii64.c"
 
 char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
 {
@@ -139,17 +78,6 @@ char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
 }
 
 #if ENABLE_USE_BB_CRYPT
-
-static char*
-to64(char *s, unsigned v, int n)
-{
-	while (--n >= 0) {
-		*s++ = i2a64(v);
-		v >>= 6;
-	}
-	return s;
-}
-
 /*
  * DES and MD5 crypt implementations are taken from uclibc.
  * They were modified to not use static buffers.
diff --git a/libbb/pw_encrypt_des.c b/libbb/pw_encrypt_des.c
index bfa039bb5..ca8aa9bcc 100644
--- a/libbb/pw_encrypt_des.c
+++ b/libbb/pw_encrypt_des.c
@@ -671,15 +671,6 @@ do_des(struct des_ctx *ctx, /*uint32_t l_in, uint32_t r_in,*/ uint32_t *l_out, u
 
 #define DES_OUT_BUFSIZE 21
 
-static void
-to64_msb_first(char *s, unsigned v)
-{
-	*s++ = i2a64(v >> 18); /* bits 23..18 */
-	*s++ = i2a64(v >> 12); /* bits 17..12 */
-	*s++ = i2a64(v >> 6); /* bits 11..6 */
-	*s   = i2a64(v); /* bits 5..0 */
-}
-
 static char *
 NOINLINE
 des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
@@ -726,11 +717,11 @@ des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
 	/* Now encode the result. */
 	/* Each call takes low-order 24 bits and stores 4 chars */
 	/* bits 31..8 of r0 */
-	to64_msb_first(output + 2, (r0 >> 8));
+	num2str64_4chars_msb_first(output + 2, (r0 >> 8));
 	/* bits 7..0 of r0 and 31..16 of r1 */
-	to64_msb_first(output + 6, (r0 << 16) | (r1 >> 16));
+	num2str64_4chars_msb_first(output + 6, (r0 << 16) | (r1 >> 16));
 	/* bits 15..0 of r1 and two zero bits (plus extra zero byte) */
-	to64_msb_first(output + 10, (r1 << 8));
+	num2str64_4chars_msb_first(output + 10, (r1 << 8));
 	/* extra zero byte is encoded as '.', fixing it */
 	output[13] = '\0';
 
diff --git a/libbb/pw_encrypt_md5.c b/libbb/pw_encrypt_md5.c
index 1e52ecaea..92d039f96 100644
--- a/libbb/pw_encrypt_md5.c
+++ b/libbb/pw_encrypt_md5.c
@@ -149,9 +149,9 @@ md5_crypt(char result[MD5_OUT_BUFSIZE], const unsigned char *pw, const unsigned
 	final[16] = final[5];
 	for (i = 0; i < 5; i++) {
 		unsigned l = (final[i] << 16) | (final[i+6] << 8) | final[i+12];
-		p = to64(p, l, 4);
+		p = num2str64_lsb_first(p, l, 4);
 	}
-	p = to64(p, final[11], 2);
+	p = num2str64_lsb_first(p, final[11], 2);
 	*p = '\0';
 
 	/* Don't leave anything around in vm they could use. */
diff --git a/libbb/pw_encrypt_sha.c b/libbb/pw_encrypt_sha.c
index 5457d7ab6..516293920 100644
--- a/libbb/pw_encrypt_sha.c
+++ b/libbb/pw_encrypt_sha.c
@@ -198,7 +198,7 @@ sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
 #define b64_from_24bit(B2, B1, B0, N) \
 do { \
 	unsigned w = ((B2) << 16) | ((B1) << 8) | (B0); \
-	resptr = to64(resptr, w, N); \
+	resptr = num2str64_lsb_first(resptr, w, N); \
 } while (0)
 	if (_32or64 == 32) { /* sha256 */
 		unsigned i = 0;


More information about the busybox-cvs mailing list