random small size optimizations

This commit is contained in:
Denis Vlasenko 2007-01-12 22:10:34 +00:00
parent 21b080daa8
commit 3a34d0c08a
8 changed files with 34 additions and 23 deletions

View File

@ -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;
}

View File

@ -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[];

View File

@ -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";

View File

@ -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;
}

View File

@ -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.)

View File

@ -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.

View File

@ -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<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 {

View File

@ -294,16 +294,11 @@ unsigned fmt_ptime(char *s, struct taia *ta) {
}
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;
}