svn commit: trunk/busybox: coreutils include libbb miscutils networ etc...

vda at busybox.net vda at busybox.net
Fri Jan 12 22:10:35 UTC 2007


Author: vda
Date: 2007-01-12 14:10:34 -0800 (Fri, 12 Jan 2007)
New Revision: 17263

Log:
random small size optimizations


Modified:
   trunk/busybox/coreutils/md5_sha1_sum.c
   trunk/busybox/include/libbb.h
   trunk/busybox/libbb/messages.c
   trunk/busybox/libbb/read.c
   trunk/busybox/libbb/xfuncs.c
   trunk/busybox/miscutils/hdparm.c
   trunk/busybox/networking/libiproute/ll_addr.c
   trunk/busybox/runit/runit_lib.c


Changeset:
Modified: trunk/busybox/coreutils/md5_sha1_sum.c
===================================================================
--- trunk/busybox/coreutils/md5_sha1_sum.c	2007-01-12 21:03:07 UTC (rev 17262)
+++ trunk/busybox/coreutils/md5_sha1_sum.c	2007-01-12 22:10:34 UTC (rev 17263)
@@ -18,11 +18,9 @@
 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
 				unsigned hash_length)
 {
-	int len = 0;
-	char *hex_value = xmalloc((hash_length * 2) + 2);
-	while (hash_length--) {
-		len += sprintf(hex_value + len, "%02x", *hash_value++);
-	}
+	/* xzalloc zero-terminates */
+	char *hex_value = xzalloc((hash_length * 2) + 1);
+	bin2hex(hex_value, (char*)hash_value, hash_length);
 	return hex_value;
 }
 

Modified: trunk/busybox/include/libbb.h
===================================================================
--- trunk/busybox/include/libbb.h	2007-01-12 21:03:07 UTC (rev 17262)
+++ trunk/busybox/include/libbb.h	2007-01-12 22:10:34 UTC (rev 17263)
@@ -391,11 +391,13 @@
 extern FILE *fopen_or_warn_stdin(const char *filename);
 
 
-extern void smart_ulltoa5(unsigned long long ul, char buf[5]);
 extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
 extern char *utoa(unsigned n);
 extern void itoa_to_buf(int n, char *buf, unsigned buflen);
 extern char *itoa(int n);
+extern void smart_ulltoa5(unsigned long long ul, char buf[5]);
+/* Put a string of hex bytes (ala "1b"), return advanced pointer */
+extern char *bin2hex(char *buf, const char *cp, int count);
 
 struct suffix_mult {
 	const char *suffix;
@@ -693,6 +695,8 @@
 extern const char bb_msg_standard_output[];
 
 extern const char bb_str_default[];
+/* NB: (bb_hexdigits_upcase[i] | 0x10) -> lowercase hex digit */
+extern const char bb_hexdigits_upcase[];
 
 extern const char bb_path_mtab_file[];
 extern const char bb_path_nologin_file[];

Modified: trunk/busybox/libbb/messages.c
===================================================================
--- trunk/busybox/libbb/messages.c	2007-01-12 21:03:07 UTC (rev 17262)
+++ trunk/busybox/libbb/messages.c	2007-01-12 22:10:34 UTC (rev 17263)
@@ -28,6 +28,7 @@
 const char bb_msg_standard_output[] = "standard output";
 
 const char bb_str_default[] = "default";
+const char bb_hexdigits_upcase[] = "0123456789ABCDEF";
 
 const char bb_path_passwd_file[] = "/etc/passwd";
 const char bb_path_shadow_file[] = "/etc/shadow";

Modified: trunk/busybox/libbb/read.c
===================================================================
--- trunk/busybox/libbb/read.c	2007-01-12 21:03:07 UTC (rev 17262)
+++ trunk/busybox/libbb/read.c	2007-01-12 22:10:34 UTC (rev 17263)
@@ -88,7 +88,7 @@
 		*p++ = '\0';
 		// avoid incorrect (unsigned) widening
 		offset = (off_t)(p-buffer) - (off_t)size;
-		// set fd position the right after the \n
+		// set fd position right after '\n'
 		if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1)
 			return NULL;
 	}

Modified: trunk/busybox/libbb/xfuncs.c
===================================================================
--- trunk/busybox/libbb/xfuncs.c	2007-01-12 21:03:07 UTC (rev 17262)
+++ trunk/busybox/libbb/xfuncs.c	2007-01-12 22:10:34 UTC (rev 17263)
@@ -333,6 +333,19 @@
 	return local_buf;
 }
 
+// Emit a string of hex representation of bytes
+char *bin2hex(char *p, const char *cp, int count)
+{
+	while (count) {
+		unsigned char c = *cp++;
+		/* put lowercase hex digits */
+		*p++ = 0x10 | bb_hexdigits_upcase[c >> 4];
+		*p++ = 0x10 | bb_hexdigits_upcase[c & 0xf];
+		count--;
+	}
+	return p;
+}
+
 // Die with an error message if we can't set gid.  (Because resource limits may
 // limit this user to a given number of processes, and if that fills up the
 // setgid() will fail and we'll _still_be_root_, which is bad.)

Modified: trunk/busybox/miscutils/hdparm.c
===================================================================
--- trunk/busybox/miscutils/hdparm.c	2007-01-12 21:03:07 UTC (rev 17262)
+++ trunk/busybox/miscutils/hdparm.c	2007-01-12 22:10:34 UTC (rev 17263)
@@ -2025,28 +2025,28 @@
 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
 static int fromhex(unsigned char c)
 {
+	if (isdigit(c))
+		return (c - '0');
 	if (c >= 'a' && c <= 'f')
-		return 10 + (c - 'a');
-	if (c >= '0' && c <= '9')
-		return (c - '0');
+		return (c - ('a' - 10));
 	bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
 }
 
 static void identify_from_stdin(void)
 {
 	uint16_t sbuf[256];
-	unsigned char  buf[1280], *b = (unsigned char *)buf;
-	int i, count = read(0, buf, 1280);
+	unsigned char buf[1280];
+	unsigned char *b = (unsigned char *)buf;
+	int i;
 
-	if (count != 1280)
-		bb_error_msg_and_die("read(%d bytes) failed (rc=%d)", 1280, count);
+	xread(0, buf, 1280);
 
 	// Convert the newline-separated hex data into an identify block.
 
 	for (i = 0; i<256; i++)  {
 		int j;
 		for (j = 0; j < 4; j++)
-			sbuf[i] = (sbuf[i] <<4) + fromhex(*(b++));
+			sbuf[i] = (sbuf[i] << 4) + fromhex(*(b++));
 	}
 
 	// Parse the data.

Modified: trunk/busybox/networking/libiproute/ll_addr.c
===================================================================
--- trunk/busybox/networking/libiproute/ll_addr.c	2007-01-12 21:03:07 UTC (rev 17262)
+++ trunk/busybox/networking/libiproute/ll_addr.c	2007-01-12 22:10:34 UTC (rev 17263)
@@ -31,7 +31,7 @@
 	l = 0;
 	for (i=0; i<alen; i++) {
 		if (i==0) {
-			snprintf(buf+l, blen, "%02x", addr[i]);
+			snprintf(buf+l, blen, ":%02x"+1, addr[i]);
 			blen -= 2;
 			l += 2;
 		} else {

Modified: trunk/busybox/runit/runit_lib.c
===================================================================
--- trunk/busybox/runit/runit_lib.c	2007-01-12 21:03:07 UTC (rev 17262)
+++ trunk/busybox/runit/runit_lib.c	2007-01-12 22:10:34 UTC (rev 17263)
@@ -294,16 +294,11 @@
 }
 
 unsigned fmt_taia(char *s, struct taia *t) {
-	static char hex[16] = "0123456789abcdef";
 	static char pack[TAIA_PACK];
-	int i;
 
 	taia_pack(pack, t);
-	s[0] = '@';
-	for (i = 0; i < 12; ++i) {
-		s[i*2+1] = hex[(pack[i] >> 4) &15];
-		s[i*2+2] = hex[pack[i] &15];
-	}
+	*s++ = '@';
+	bin2hex(s, pack, 12);
 	return 25;
 }
 




More information about the busybox-cvs mailing list