From: claudio Date: Wed, 21 Jun 2023 07:49:24 +0000 (+0000) Subject: Convert string2binary() to use new ibuf api instead of working with an X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=b53550541fabc53d215a5c762e9ba83d123033d0;p=openbsd Convert string2binary() to use new ibuf api instead of working with an extra buffer first. OK tb@ --- diff --git a/usr.sbin/relayd/util.c b/usr.sbin/relayd/util.c index 8a06dc4a419..65d6660e40d 100644 --- a/usr.sbin/relayd/util.c +++ b/usr.sbin/relayd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.3 2019/09/15 19:23:29 rob Exp $ */ +/* $OpenBSD: util.c,v 1.4 2023/06/21 07:49:24 claudio Exp $ */ /* * Copyright (c) 2006 - 2015 Reyk Floeter @@ -289,44 +289,42 @@ getmonotime(struct timeval *tv) struct ibuf * string2binary(const char *string) { - unsigned long i, j, x; - unsigned char *binary = NULL; struct ibuf *ibuf = NULL; - char hex[3]; - int len; + unsigned char ch, r; + size_t i, len; - if (strlen(string) % 2 != 0) { - return NULL; - } - - binary = calloc(strlen(string), sizeof(unsigned char)); - if (binary == NULL) { - return NULL; - } + len = strlen(string); + if (len % 2 != 0) + goto fail; + if ((ibuf = ibuf_open(len / 2)) == NULL) + goto fail; - hex[2] = '\0'; - j = 0; - for (i = 0; i < strlen(string); i++) { - if (isxdigit(string[i]) == 0 || isxdigit(string[i+1]) == 0) { - free(binary); - return NULL; - } else { - hex[0] = string[i]; - hex[1] = string[i+1]; - x = strtoul(hex, NULL, 16); - binary[j++] = (unsigned char)x; - i++; + while (*string) { + r = 0; + for (i = 0; i < 2; i++) { + ch = string[i]; + if (isdigit(ch)) + ch -= '0'; + else if (islower(ch)) + ch -= ('a' - 10); + else if (isupper(ch)) + ch -= ('A' - 10); + else + goto fail; + if (ch > 0xf) + goto fail; + r = r << 4 | ch; } + if (ibuf_add_n8(ibuf, r) == -1) + goto fail; + string += 2; } - len = strlen(string) / 2; - if ((ibuf = ibuf_open(len)) == NULL || - ibuf_add(ibuf, binary, len) == -1) { - ibuf_free(ibuf); - free(binary); - return NULL; - } - free(binary); + return ibuf; + +fail: + ibuf_free(ibuf); + return NULL; } void