commit ca676107d5735204b3b420f1524dd6e9c8556967
parent 5b2d51ff64270788966fc59436ab71f498c42a44
Author: Santtu Lakkala <santtu.lakkala@digital14.com>
Date: Wed, 13 Sep 2023 13:28:29 +0300
Cleanup struct bytes usage
Diffstat:
M | token.c | | | 29 | ++++++++++++++--------------- |
M | util.c | | | 11 | ++++++----- |
M | util.h | | | 16 | +++++++++++++++- |
3 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/token.c b/token.c
@@ -13,9 +13,8 @@ const char *digest_names[] = {
"SHA512",
};
-static inline char dehex(const char *s)
+static inline uint8_t dehex(const uint8_t *s)
{
- const uint8_t *u = (const uint8_t *)s;
static const uint8_t vals[256] = {
['0'] = 1, ['1'] = 2, ['2'] = 3, ['3'] = 4, ['4'] = 5,
['5'] = 6, ['6'] = 7, ['7'] = 8, ['8'] = 9, ['9'] = 10,
@@ -24,18 +23,18 @@ static inline char dehex(const char *s)
['e'] = 15, ['f'] = 16,
};
- if (!vals[u[0]] || !vals[u[1]])
+ if (!vals[s[0]] || !vals[s[1]])
return 0;
- return (vals[u[0]] - 1) << 4 |
- (vals[u[1]] - 1);
+ return (vals[s[0]] - 1) << 4 |
+ (vals[s[1]] - 1);
}
static struct bytes uridecode(struct bytes data, bool getarg)
{
- char *w = (char *)data.data;
- char *end = w + data.len;
- const char *r = w;
+ uint8_t *w = data.data;
+ uint8_t *end = w + data.len;
+ const uint8_t *r = w;
while (r < end) {
if (*r == '%') {
@@ -52,7 +51,7 @@ static struct bytes uridecode(struct bytes data, bool getarg)
*w++ = *r++;
}
- return (struct bytes){ data.data, w - (char *)data.data };
+ return (struct bytes){ data.data, w - data.data };
}
@@ -92,11 +91,11 @@ struct token token_parse_uri(char *data) {
if (!i)
return rv;
- rv.desc = uridecode((struct bytes){ (uint8_t *)str, i - str }, false);
+ rv.desc = uridecode(bytesnc(str, i - str), false);
if ((v = memchr(rv.desc.data, ':', rv.desc.len))) {
- rv.issuer = (struct bytes){ rv.desc.data, v++ - (char *)rv.desc.data };
- rv.desc = (struct bytes){ (void *)v, i - v };
+ rv.issuer = (struct bytes){ rv.desc.data, pdiff(rv.desc.data, v++) };
+ rv.desc = bytesnc(v, i - v);
}
while (*i++) {
@@ -104,7 +103,7 @@ struct token token_parse_uri(char *data) {
if (rv.key.len)
croak("Multiple secrets in URI");
i = v + strcspn(v, "&");
- rv.key = debase32(v, i - v);
+ rv.key = debase32(bytesnc(v, i - v));
} else if ((v = if_prefix(i, "digits="))) {
if (!(rv.digits = strtou8(v, &i)))
return rv;
@@ -113,9 +112,9 @@ struct token token_parse_uri(char *data) {
return rv;
} else if ((v = if_prefix(i, "issuer="))) {
i = v + strcspn(v, "&");
- struct bytes newiss = uridecode((struct bytes){ (void *)v, i - v }, true);
+ struct bytes newiss = uridecode(bytesnc(v, i - v), true);
if (rv.issuer.len && (newiss.len != rv.issuer.len ||
- memcmp(rv.issuer.data, newiss.data, newiss.len))) {
+ !bytesequal(rv.issuer, newiss))) {
errno = EINVAL;
return rv;
}
diff --git a/util.c b/util.c
@@ -133,10 +133,10 @@ void croak(const char *fmt, ...)
exit(1);
}
-struct bytes debase32(char *buffer, size_t len)
+struct bytes debase32(struct bytes data)
{
- uint8_t *wp = (uint8_t *)buffer;
- const uint8_t *rp = (const uint8_t *)buffer;
+ uint8_t *wp = data.data;
+ const uint8_t *rp;
uint16_t v = 0;
size_t b = 0;
@@ -155,7 +155,7 @@ struct bytes debase32(char *buffer, size_t len)
['5'] = 30, ['6'] = 31, ['7'] = 32
};
- for (rp = (uint8_t *)buffer; (char *)rp - buffer < (ptrdiff_t)len && *rp && *rp != '='; rp++) {
+ for (rp = data.data; rp < data.data + data.len && *rp && *rp != '='; rp++) {
uint8_t c = val[*rp];
if (!c)
return (struct bytes){ 0 };
@@ -166,7 +166,8 @@ struct bytes debase32(char *buffer, size_t len)
b -= 8;
}
}
- return (struct bytes){ (void *)buffer, (char *)wp - buffer };
+
+ return (struct bytes){ data.data, wp - data.data };
}
void randmem(void *mem, size_t n)
diff --git a/util.h b/util.h
@@ -84,6 +84,11 @@ static inline void *mempushb(void *dst, struct bytes data)
return mempush(dst, data.data, data.len);
}
+static inline int bytesequal(struct bytes a, struct bytes b)
+{
+ return a.len == b.len && !memcmp(a.data, b.data, a.len);
+}
+
static inline char *if_prefix(const char *s, const char *prefix)
{
while (*prefix)
@@ -93,6 +98,15 @@ static inline char *if_prefix(const char *s, const char *prefix)
}
void croak(const char *fmt, ...);
-struct bytes debase32(char *buffer, size_t len);
+struct bytes debase32(struct bytes data);
+
+static inline ptrdiff_t pdiff(const void *a, const void *b) {
+ return (const char *)b - (const char *)a;
+}
+
+static inline struct bytes bytesnc(char *data, size_t len)
+{
+ return (struct bytes){ (void *)data, len };
+}
#endif