commit 588972ba93f716c0027b2a0806ce40c25578144d
parent 08f5deb4f4060a24b7619de46f64e164615725f8
Author: sin <sin@2f30.org>
Date: Thu, 17 Dec 2015 13:09:04 +0000
Add int2hex()
Diffstat:
2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/sbtd.h b/sbtd.h
@@ -40,6 +40,7 @@ void setbit(uint32_t *, int);
void clrbit(uint32_t *, int);
int tstbit(uint32_t *, int);
uint8_t hex2int(int);
+uint8_t int2hex(int);
uint8_t *hex2bin(const char *, uint8_t *, size_t);
char *bin2hex(const uint8_t *, char *, size_t);
void sha1sum(uint8_t *, unsigned long, uint8_t *);
diff --git a/util.c b/util.c
@@ -68,6 +68,13 @@ hex2int(int c)
errx(1, "%c is not hex", c);
}
+uint8_t
+int2hex(int c)
+{
+ const char *hex = "0123456789ABCDEF";
+ return hex[c & 0xf];
+}
+
uint8_t *
hex2bin(const char *h, uint8_t *b, size_t n)
{
@@ -81,12 +88,11 @@ hex2bin(const char *h, uint8_t *b, size_t n)
char *
bin2hex(const uint8_t *b, char *h, size_t n)
{
- const char *c = "0123456789ABCDEF";
size_t i;
for (i = 0; i < n; i++) {
- h[i * 2] = c[(b[i] >> 4) & 0xf];
- h[i * 2 + 1] = c[b[i] &0xf];
+ h[i * 2] = int2hex(b[i] >> 4);
+ h[i * 2 + 1] = int2hex(b[i]);
}
h[i * 2] = '\0';
return h;
@@ -126,7 +132,6 @@ readfile(char *file, char **b, size_t *n)
char *
urlencode(char *s)
{
- const char *c = "0123456789ABCDEF";
char *p = s, *buf, *bp;
bp = buf = emalloc(strlen(s) * 3 + 1);
@@ -141,8 +146,8 @@ urlencode(char *s)
*bp++ = '+';
} else {
*bp++ = '%';
- *bp++ = c[(*p >> 4) & 0xf];
- *bp++ = c[*p & 0xf];
+ *bp++ = int2hex(*p >> 4);
+ *bp++ = int2hex(*p);
}
p++;
}