From: Denis Vlasenko Date: Fri, 12 Jan 2007 22:10:34 +0000 (-0000) Subject: random small size optimizations X-Git-Tag: 1_4_0~41 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=3a34d0c08a77ee48edc3f4353cc49b95aba85c2f;p=oweals%2Fbusybox.git random small size optimizations --- 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; 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; } 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); 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_input[]; 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[]; 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"; 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"; 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) *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; } 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) 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.) 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) #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY static int fromhex(unsigned char c) { - if (c >= 'a' && c <= 'f') - return 10 + (c - 'a'); - if (c >= '0' && c <= '9') + if (isdigit(c)) return (c - '0'); + if (c >= 'a' && c <= 'f') + 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. 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 l = 0; for (i=0; i> 4) &15]; - s[i*2+2] = hex[pack[i] &15]; - } + *s++ = '@'; + bin2hex(s, pack, 12); return 25; }