aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-12 22:10:34 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-12 22:10:34 +0000
commit3a34d0c08a77ee48edc3f4353cc49b95aba85c2f (patch)
tree09708579e18a033c6722c5194c46116705f47b83
parent21b080daa8c180a43d10d6b3dee47134ef21e581 (diff)
downloadbusybox-w32-3a34d0c08a77ee48edc3f4353cc49b95aba85c2f.tar.gz
busybox-w32-3a34d0c08a77ee48edc3f4353cc49b95aba85c2f.tar.bz2
busybox-w32-3a34d0c08a77ee48edc3f4353cc49b95aba85c2f.zip
random small size optimizations
-rw-r--r--coreutils/md5_sha1_sum.c8
-rw-r--r--include/libbb.h6
-rw-r--r--libbb/messages.c1
-rw-r--r--libbb/read.c2
-rw-r--r--libbb/xfuncs.c13
-rw-r--r--miscutils/hdparm.c16
-rw-r--r--networking/libiproute/ll_addr.c2
-rw-r--r--runit/runit_lib.c9
8 files changed, 34 insertions, 23 deletions
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index 6fe1b0286..014ecefd0 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -18,11 +18,9 @@ typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
18static unsigned char *hash_bin_to_hex(unsigned char *hash_value, 18static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
19 unsigned hash_length) 19 unsigned hash_length)
20{ 20{
21 int len = 0; 21 /* xzalloc zero-terminates */
22 char *hex_value = xmalloc((hash_length * 2) + 2); 22 char *hex_value = xzalloc((hash_length * 2) + 1);
23 while (hash_length--) { 23 bin2hex(hex_value, (char*)hash_value, hash_length);
24 len += sprintf(hex_value + len, "%02x", *hash_value++);
25 }
26 return hex_value; 24 return hex_value;
27} 25}
28 26
diff --git a/include/libbb.h b/include/libbb.h
index c088946d9..72261b760 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -391,11 +391,13 @@ extern FILE *fopen_or_warn(const char *filename, const char *mode);
391extern FILE *fopen_or_warn_stdin(const char *filename); 391extern FILE *fopen_or_warn_stdin(const char *filename);
392 392
393 393
394extern void smart_ulltoa5(unsigned long long ul, char buf[5]);
395extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen); 394extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
396extern char *utoa(unsigned n); 395extern char *utoa(unsigned n);
397extern void itoa_to_buf(int n, char *buf, unsigned buflen); 396extern void itoa_to_buf(int n, char *buf, unsigned buflen);
398extern char *itoa(int n); 397extern char *itoa(int n);
398extern void smart_ulltoa5(unsigned long long ul, char buf[5]);
399/* Put a string of hex bytes (ala "1b"), return advanced pointer */
400extern char *bin2hex(char *buf, const char *cp, int count);
399 401
400struct suffix_mult { 402struct suffix_mult {
401 const char *suffix; 403 const char *suffix;
@@ -693,6 +695,8 @@ extern const char bb_msg_standard_input[];
693extern const char bb_msg_standard_output[]; 695extern const char bb_msg_standard_output[];
694 696
695extern const char bb_str_default[]; 697extern const char bb_str_default[];
698/* NB: (bb_hexdigits_upcase[i] | 0x10) -> lowercase hex digit */
699extern const char bb_hexdigits_upcase[];
696 700
697extern const char bb_path_mtab_file[]; 701extern const char bb_path_mtab_file[];
698extern const char bb_path_nologin_file[]; 702extern const char bb_path_nologin_file[];
diff --git a/libbb/messages.c b/libbb/messages.c
index c640faf5b..6c3d2f608 100644
--- a/libbb/messages.c
+++ b/libbb/messages.c
@@ -28,6 +28,7 @@ const char bb_msg_standard_input[] = "standard input";
28const char bb_msg_standard_output[] = "standard output"; 28const char bb_msg_standard_output[] = "standard output";
29 29
30const char bb_str_default[] = "default"; 30const char bb_str_default[] = "default";
31const char bb_hexdigits_upcase[] = "0123456789ABCDEF";
31 32
32const char bb_path_passwd_file[] = "/etc/passwd"; 33const char bb_path_passwd_file[] = "/etc/passwd";
33const char bb_path_shadow_file[] = "/etc/shadow"; 34const char bb_path_shadow_file[] = "/etc/shadow";
diff --git a/libbb/read.c b/libbb/read.c
index 50e0354ad..861828da1 100644
--- a/libbb/read.c
+++ b/libbb/read.c
@@ -88,7 +88,7 @@ char *reads(int fd, char *buffer, size_t size)
88 *p++ = '\0'; 88 *p++ = '\0';
89 // avoid incorrect (unsigned) widening 89 // avoid incorrect (unsigned) widening
90 offset = (off_t)(p-buffer) - (off_t)size; 90 offset = (off_t)(p-buffer) - (off_t)size;
91 // set fd position the right after the \n 91 // set fd position right after '\n'
92 if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1) 92 if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1)
93 return NULL; 93 return NULL;
94 } 94 }
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 827cbe870..207537929 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -333,6 +333,19 @@ char *itoa(int n)
333 return local_buf; 333 return local_buf;
334} 334}
335 335
336// Emit a string of hex representation of bytes
337char *bin2hex(char *p, const char *cp, int count)
338{
339 while (count) {
340 unsigned char c = *cp++;
341 /* put lowercase hex digits */
342 *p++ = 0x10 | bb_hexdigits_upcase[c >> 4];
343 *p++ = 0x10 | bb_hexdigits_upcase[c & 0xf];
344 count--;
345 }
346 return p;
347}
348
336// Die with an error message if we can't set gid. (Because resource limits may 349// Die with an error message if we can't set gid. (Because resource limits may
337// limit this user to a given number of processes, and if that fills up the 350// limit this user to a given number of processes, and if that fills up the
338// setgid() will fail and we'll _still_be_root_, which is bad.) 351// setgid() will fail and we'll _still_be_root_, which is bad.)
diff --git a/miscutils/hdparm.c b/miscutils/hdparm.c
index 16485b900..e60e642d6 100644
--- a/miscutils/hdparm.c
+++ b/miscutils/hdparm.c
@@ -2025,28 +2025,28 @@ static void process_dev(char *devname)
2025#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY 2025#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2026static int fromhex(unsigned char c) 2026static int fromhex(unsigned char c)
2027{ 2027{
2028 if (c >= 'a' && c <= 'f') 2028 if (isdigit(c))
2029 return 10 + (c - 'a');
2030 if (c >= '0' && c <= '9')
2031 return (c - '0'); 2029 return (c - '0');
2030 if (c >= 'a' && c <= 'f')
2031 return (c - ('a' - 10));
2032 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c); 2032 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2033} 2033}
2034 2034
2035static void identify_from_stdin(void) 2035static void identify_from_stdin(void)
2036{ 2036{
2037 uint16_t sbuf[256]; 2037 uint16_t sbuf[256];
2038 unsigned char buf[1280], *b = (unsigned char *)buf; 2038 unsigned char buf[1280];
2039 int i, count = read(0, buf, 1280); 2039 unsigned char *b = (unsigned char *)buf;
2040 int i;
2040 2041
2041 if (count != 1280) 2042 xread(0, buf, 1280);
2042 bb_error_msg_and_die("read(%d bytes) failed (rc=%d)", 1280, count);
2043 2043
2044 // Convert the newline-separated hex data into an identify block. 2044 // Convert the newline-separated hex data into an identify block.
2045 2045
2046 for (i = 0; i<256; i++) { 2046 for (i = 0; i<256; i++) {
2047 int j; 2047 int j;
2048 for (j = 0; j < 4; j++) 2048 for (j = 0; j < 4; j++)
2049 sbuf[i] = (sbuf[i] <<4) + fromhex(*(b++)); 2049 sbuf[i] = (sbuf[i] << 4) + fromhex(*(b++));
2050 } 2050 }
2051 2051
2052 // Parse the data. 2052 // Parse the data.
diff --git a/networking/libiproute/ll_addr.c b/networking/libiproute/ll_addr.c
index ba0a65a18..b4a218780 100644
--- a/networking/libiproute/ll_addr.c
+++ b/networking/libiproute/ll_addr.c
@@ -31,7 +31,7 @@ const char *ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int
31 l = 0; 31 l = 0;
32 for (i=0; i<alen; i++) { 32 for (i=0; i<alen; i++) {
33 if (i==0) { 33 if (i==0) {
34 snprintf(buf+l, blen, "%02x", addr[i]); 34 snprintf(buf+l, blen, ":%02x"+1, addr[i]);
35 blen -= 2; 35 blen -= 2;
36 l += 2; 36 l += 2;
37 } else { 37 } else {
diff --git a/runit/runit_lib.c b/runit/runit_lib.c
index b885a3412..2953235f0 100644
--- a/runit/runit_lib.c
+++ b/runit/runit_lib.c
@@ -294,16 +294,11 @@ unsigned fmt_ptime(char *s, struct taia *ta) {
294} 294}
295 295
296unsigned fmt_taia(char *s, struct taia *t) { 296unsigned fmt_taia(char *s, struct taia *t) {
297 static char hex[16] = "0123456789abcdef";
298 static char pack[TAIA_PACK]; 297 static char pack[TAIA_PACK];
299 int i;
300 298
301 taia_pack(pack, t); 299 taia_pack(pack, t);
302 s[0] = '@'; 300 *s++ = '@';
303 for (i = 0; i < 12; ++i) { 301 bin2hex(s, pack, 12);
304 s[i*2+1] = hex[(pack[i] >> 4) &15];
305 s[i*2+2] = hex[pack[i] &15];
306 }
307 return 25; 302 return 25;
308} 303}
309 304