diff --git a/include/re_fmt.h b/include/re_fmt.h index 720880b..54a274d 100644 --- a/include/re_fmt.h +++ b/include/re_fmt.h @@ -31,6 +31,7 @@ void pl_set_mbuf(struct pl *pl, const struct mbuf *mb); uint32_t pl_u32(const struct pl *pl); uint32_t pl_x32(const struct pl *pl); uint64_t pl_u64(const struct pl *pl); +uint64_t pl_x64(const struct pl *pl); bool pl_isset(const struct pl *pl); int pl_strcpy(const struct pl *pl, char *str, size_t size); int pl_strdup(char **dst, const struct pl *src); diff --git a/src/fmt/pl.c b/src/fmt/pl.c index 65c1839..33ee83e 100644 --- a/src/fmt/pl.c +++ b/src/fmt/pl.c @@ -148,6 +148,44 @@ uint64_t pl_u64(const struct pl *pl) } +/** + * Convert a hex pointer-length object to a numeric 64-bit value + * + * @param pl Pointer-length object + * + * @return 64-bit value + */ +uint64_t pl_x64(const struct pl *pl) +{ + uint64_t v=0, mul=1; + const char *p; + + if (!pl || !pl->p) + return 0; + + p = &pl->p[pl->l]; + while (p > pl->p) { + + const char ch = *--p; + uint8_t c; + + if ('0' <= ch && ch <= '9') + c = ch - '0'; + else if ('A' <= ch && ch <= 'F') + c = ch - 'A' + 10; + else if ('a' <= ch && ch <= 'f') + c = ch - 'a' + 10; + else + return 0; + + v += mul * c; + mul *= 16; + } + + return v; +} + + /** * Check if pointer-length object is set *