From: Eric Leblond Date: Sun, 2 Mar 2025 16:31:08 +0000 (+0100) Subject: util/byte: add HexToRaw function X-Git-Tag: suricata-8.0.0-rc1~71 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=53ac35337a095cff724d0f49428122863c561d86;p=thirdparty%2Fsuricata.git util/byte: add HexToRaw function --- diff --git a/src/datasets.c b/src/datasets.c index 93712914e6..7addb37274 100644 --- a/src/datasets.c +++ b/src/datasets.c @@ -95,36 +95,6 @@ static Dataset *DatasetSearchByName(const char *name) return NULL; } -static int HexToRaw(const uint8_t *in, size_t ins, uint8_t *out, size_t outs) -{ - if (ins < 2) - return -1; - if (ins % 2 != 0) - return -1; - if (outs != ins / 2) - return -1; - - uint8_t hash[outs]; - memset(hash, 0, outs); - size_t i, x; - for (x = 0, i = 0; i < ins; i+=2, x++) { - char buf[3] = { 0, 0, 0 }; - buf[0] = in[i]; - buf[1] = in[i+1]; - - long value = strtol(buf, NULL, 16); - if (value >= 0 && value <= 255) - hash[x] = (uint8_t)value; - else { - SCLogError("hash byte out of range %ld", value); - return -1; - } - } - - memcpy(out, hash, outs); - return 0; -} - static int DatasetLoadIPv4(Dataset *set) { if (strlen(set->load) == 0) diff --git a/src/util-byte.c b/src/util-byte.c index 1b06ccd628..def6d087c4 100644 --- a/src/util-byte.c +++ b/src/util-byte.c @@ -803,6 +803,36 @@ int StringParseI8RangeCheck( return ret; } +int HexToRaw(const uint8_t *in, size_t ins, uint8_t *out, size_t outs) +{ + if (ins < 2) + return -1; + if (ins % 2 != 0) + return -1; + if (outs != ins / 2) + return -1; + + uint8_t hash[outs]; + memset(hash, 0, outs); + size_t i, x; + for (x = 0, i = 0; i < ins; i += 2, x++) { + char buf[3] = { 0, 0, 0 }; + buf[0] = in[i]; + buf[1] = in[i + 1]; + + long value = strtol(buf, NULL, 16); + if (value >= 0 && value <= 255) + hash[x] = (uint8_t)value; + else { + SCLogError("hash byte out of range %ld", value); + return -1; + } + } + + memcpy(out, hash, outs); + return 0; +} + /* UNITTESTS */ #ifdef UNITTESTS diff --git a/src/util-byte.h b/src/util-byte.h index e0f0a67c27..eca7b47f9e 100644 --- a/src/util-byte.h +++ b/src/util-byte.h @@ -503,4 +503,6 @@ static inline int WARN_UNUSED ByteExtract(uint64_t *res, int e, uint16_t len, co return len; } +int HexToRaw(const uint8_t *in, size_t ins, uint8_t *out, size_t outs); + #endif /* SURICATA_UTIL_BYTE_H */