]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
cmdmon: define 64-bit integer
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 23 Mar 2023 10:37:11 +0000 (11:37 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 30 Mar 2023 13:17:50 +0000 (15:17 +0200)
Add a structure for 64-bit integers without requiring 64-bit alignment
to be usable in CMD_Reply without struct packing.

Add utility functions for conversion to/from network order. Avoid using
be64toh() and htobe64() as they don't seem to be available on all
supported systems.

candm.h
test/unit/util.c
util.c
util.h

diff --git a/candm.h b/candm.h
index 2fba7f4716091d02ef9245d7017687f42f057dc5..b71af80c5f11cf65022fec4da22c2cf96f659f96 100644 (file)
--- a/candm.h
+++ b/candm.h
@@ -122,6 +122,12 @@ typedef struct {
 /* This is used in tv_sec_high for 32-bit timestamps */
 #define TV_NOHIGHSEC 0x7fffffff
 
+/* Structure for 64-bit integers (not requiring 64-bit alignment) */
+typedef struct {
+  uint32_t high;
+  uint32_t low;
+} Integer64;
+
 /* 32-bit floating-point format consisting of 7-bit signed exponent
    and 25-bit signed coefficient without hidden bit.
    The result is calculated as: 2^(exp - 25) * coef */
index c8bdadb163800d4fd0b29d8562ce5f2f7e04ce6c..6fadefb72fb09eb70e84f21b2872953c4774bbee 100644 (file)
@@ -40,6 +40,7 @@ test_unit(void)
   double x, y, nan, inf;
   IPAddr ip, ip2, ip3;
   IPSockAddr ip_saddr;
+  Integer64 integer64;
   Timespec tspec;
   Float f;
   int i, j, c;
@@ -565,6 +566,10 @@ test_unit(void)
   UTI_TimespecNetworkToHost(&tspec, &ts2);
   TEST_CHECK(!UTI_CompareTimespecs(&ts, &ts2));
 
+  integer64 = UTI_Integer64HostToNetwork(0x1234567890ABCDEFULL);
+  TEST_CHECK(memcmp(&integer64, "\x12\x34\x56\x78\x90\xab\xcd\xef", 8) == 0);
+  TEST_CHECK(UTI_Integer64NetworkToHost(integer64) == 0x1234567890ABCDEFULL);
+
   TEST_CHECK(UTI_CmacNameToAlgorithm("AES128") == CMC_AES128);
   TEST_CHECK(UTI_CmacNameToAlgorithm("AES256") == CMC_AES256);
   TEST_CHECK(UTI_CmacNameToAlgorithm("NOSUCHCMAC") == CMC_INVALID);
diff --git a/util.c b/util.c
index 0321720e42830d52c2efc308ae7ad2706847c5d6..6b3320bac07be3d18bae7fce947991af965298fd 100644 (file)
--- a/util.c
+++ b/util.c
@@ -909,6 +909,25 @@ UTI_TimespecHostToNetwork(const struct timespec *src, Timespec *dest)
 
 /* ================================================== */
 
+uint64_t
+UTI_Integer64NetworkToHost(Integer64 i)
+{
+  return (uint64_t)ntohl(i.high) << 32 | ntohl(i.low);
+}
+
+/* ================================================== */
+
+Integer64
+UTI_Integer64HostToNetwork(uint64_t i)
+{
+  Integer64 r;
+  r.high = htonl(i >> 32);
+  r.low = htonl(i);
+  return r;
+}
+
+/* ================================================== */
+
 #define FLOAT_EXP_BITS 7
 #define FLOAT_EXP_MIN (-(1 << (FLOAT_EXP_BITS - 1)))
 #define FLOAT_EXP_MAX (-FLOAT_EXP_MIN - 1)
diff --git a/util.h b/util.h
index d8e25dee19123872a0f22a0673a9cae31261ebea..d38abf063c8a6adbbb7223362b50f0ab51879b75 100644 (file)
--- a/util.h
+++ b/util.h
@@ -172,6 +172,9 @@ extern double UTI_Log2ToDouble(int l);
 extern void UTI_TimespecNetworkToHost(const Timespec *src, struct timespec *dest);
 extern void UTI_TimespecHostToNetwork(const struct timespec *src, Timespec *dest);
 
+uint64_t UTI_Integer64NetworkToHost(Integer64 i);
+Integer64 UTI_Integer64HostToNetwork(uint64_t i);
+
 extern double UTI_FloatNetworkToHost(Float x);
 extern Float UTI_FloatHostToNetwork(double x);