From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Fri, 29 Oct 2021 13:45:51 +0000 (+0100) Subject: add nctoh64() utility X-Git-Tag: 1.2~98 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8fd9c22859b9266174ea218229cc30dd78f145e3;p=thirdparty%2Fnqptp.git add nctoh64() utility --- diff --git a/general-utilities.c b/general-utilities.c index 87dc13d..304de0b 100644 --- a/general-utilities.c +++ b/general-utilities.c @@ -34,6 +34,16 @@ void hcton64(uint64_t num, uint8_t *p) { memcpy(p, &rev, sizeof(uint32_t)); } +uint64_t nctoh64(const uint8_t *p) { // read 4 characters from *p and do ntohl on them + // this is to avoid possible aliasing violations + uint64_t value = nctohl(p); + uint64_t value_low = nctohl(p+4); + value = value << 32; + value = value + value_low; + return value; +} + + uint32_t nctohl(const uint8_t *p) { // read 4 characters from *p and do ntohl on them // this is to avoid possible aliasing violations uint32_t holder; diff --git a/general-utilities.h b/general-utilities.h index b371614..79f34f3 100644 --- a/general-utilities.h +++ b/general-utilities.h @@ -37,7 +37,10 @@ #endif void hcton64(uint64_t num, uint8_t *p); -uint32_t nctohl(const uint8_t *p); // read 4 characters from *p and do ntohl on them, avoiding aliasing + +//these are designed to avoid aliasing check errors +uint64_t nctoh64(const uint8_t *p); +uint32_t nctohl(const uint8_t *p); uint16_t nctohs(const uint8_t *p); uint64_t timespec_to_ns(struct timespec *tn); uint64_t get_time_now();