From e7380880cf993956c51dda9a99e3d3957dfa989d Mon Sep 17 00:00:00 2001 From: Steven Seungcheol Lee Date: Wed, 7 Sep 2022 11:10:01 +0200 Subject: [PATCH] util/types: Add 128 bit conversion helpers Signed-off-by: Steven Seungcheol Lee [dwagner: refactoring] Signed-off-by: Daniel Wagner --- util/types.c | 31 +++++++++++++++++++++++++++++++ util/types.h | 2 ++ 2 files changed, 33 insertions(+) diff --git a/util/types.c b/util/types.c index 1f1c344f..e52c35a8 100644 --- a/util/types.c +++ b/util/types.c @@ -5,6 +5,18 @@ #include "types.h" +__uint128_t le128_to_cpu(__u8 *data) +{ + int i; + __uint128_t result = 0; + + for (i = 0; i < 16; i++) { + result *= 256; + result += data[15 - i]; + } + return result; +} + long double int128_to_double(__u8 *data) { int i; @@ -29,6 +41,25 @@ uint64_t int48_to_long(__u8 *data) return result; } +char str_uint128[40]; +char *uint128_t_to_string(__uint128_t val) +{ + char str_rev[40]; /* __uint128_t maximum string length is 39 */ + int i, j; + + for (i = 0; val > 0; i++) { + str_rev[i] = (val % 10) + 48; + val /= 10; + } + + for (j = 0; i >= 0;) { + str_uint128[j++] = str_rev[--i]; + } + str_uint128[j] = '\0'; + + return str_uint128; +} + const char *util_uuid_to_string(uuid_t uuid) { /* large enough to hold uuid str (37) + null-termination byte */ diff --git a/util/types.h b/util/types.h index 8b63452d..be98059f 100644 --- a/util/types.h +++ b/util/types.h @@ -15,9 +15,11 @@ static inline long kelvin_to_celsius(long t) return t + ABSOLUTE_ZERO_CELSIUS; } +__uint128_t le128_to_cpu(__u8 *data); long double int128_to_double(__u8 *data); uint64_t int48_to_long(__u8 *data); +char *uint128_t_to_string(__uint128_t val); const char *util_uuid_to_string(uuid_t uuid); const char *util_fw_to_string(char *c); -- 2.50.1