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;
}
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;
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[];
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";
*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;
}
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.)
#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.
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 {
}
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;
}