summaryrefslogtreecommitdiff
path: root/conf/cf-lex.l
diff options
context:
space:
mode:
authorOndrej Zajicek <santiago@crfreenet.org>2023-08-24 03:04:58 +0200
committerOndrej Zajicek <santiago@crfreenet.org>2023-08-24 04:19:07 +0200
commiteddc0ffdab239c61cc0e064b6ebd33dfadcef3cd (patch)
tree831a5b64ed6bbd34d8c3e721665599e7197a4dec /conf/cf-lex.l
parente3c0eca95642a846ab65261424a51dd99d954017 (diff)
Lib: Add functions for reading and writing of bytestrings
Based on patch from Alexander Zubkov, thanks!
Diffstat (limited to 'conf/cf-lex.l')
-rw-r--r--conf/cf-lex.l46
1 files changed, 15 insertions, 31 deletions
diff --git a/conf/cf-lex.l b/conf/cf-lex.l
index 9025a84d..965e1e3f 100644
--- a/conf/cf-lex.l
+++ b/conf/cf-lex.l
@@ -256,38 +256,22 @@ WHITE [ \t]
}
({XIGIT}{2}){16,}|{XIGIT}{2}(:{XIGIT}{2}){15,}|hex:({XIGIT}{2}(:?{XIGIT}{2})*)? {
- char *s, *sb = yytext;
- size_t len = 0, i;
- struct bytestring *bytes;
- byte *b;
-
- /* skip 'hex:' prefix */
- if (sb[0] == 'h' && sb[1] == 'e' && sb[2] == 'x' && sb[3] == ':')
- sb += 4;
-
- s = sb;
- while (*s) {
- len++;
- s += 2;
- if (*s == ':')
- s++;
- }
- bytes = cfg_allocz(sizeof(*bytes) + len);
+ char *s = yytext;
+ struct bytestring *bs;
- bytes->length = len;
- b = &bytes->data[0];
- s = sb;
- errno = 0;
- for (i = 0; i < len; i++) {
- *b = bstrtobyte16(s);
- if (errno == ERANGE)
- cf_error("Invalid hex string");
- b++;
- s += 2;
- if (*s == ':')
- s++;
- }
- cf_lval.bs = bytes;
+ /* Skip 'hex:' prefix */
+ if (s[0] == 'h' && s[1] == 'e' && s[2] == 'x' && s[3] == ':')
+ s += 4;
+
+ int len = bstrhextobin(s, NULL);
+ if (len < 0)
+ cf_error("Invalid hex string");
+
+ bs = cfg_allocz(sizeof(struct bytestring) + len);
+ bs->length = bstrhextobin(s, bs->data);
+ ASSERT(bs->length == len);
+
+ cf_lval.bs = bs;
return BYTESTRING;
}