]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 2570] cleanup: reduced logging noise, moved some functions into libntp
authorJuergen Perlinger <perlinger@ntp.org>
Wed, 26 Mar 2014 10:01:31 +0000 (11:01 +0100)
committerJuergen Perlinger <perlinger@ntp.org>
Wed, 26 Mar 2014 10:01:31 +0000 (11:01 +0100)
bk: 5332a57bhkuzzUZG8DXvMFgOFdGVOg

13 files changed:
ChangeLog
include/Makefile.am
include/ntp_calendar.h
include/vint64ops.h [new file with mode: 0644]
libntp/Makefile.am
libntp/ntp_calendar.c
libntp/vint64ops.c [new file with mode: 0644]
ntpd/ntp_leapsec.c
ntpd/ntp_leapsec.h
ntpd/ntp_util.c
tests/libntp/Makefile.am
tests/libntp/vi64ops.cpp [new file with mode: 0644]
tests/ntpd/leapsec.cpp

index a837954220faf2d19263c3616404f9e3481f3086..b4e517cb1ab213fb6deb7492afd4721152d8061a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,5 @@
+* [Bug 2570] cleanup: reduced logging noise, moved some functions
+  into libntp.
 (4.2.7p434) 2014/03/21 Released by Harlan Stenn <stenn@ntp.org>
 * [Bug 2577] Update VS2013 solution and project files.
 (4.2.7p433) 2014/03/10 Released by Harlan Stenn <stenn@ntp.org>
index 61efd97fbb23456e0e53dd9d7dac6e7375b0ee15..5ab3e6e9d0df1cb92648e6cda42621c4f8dae9cc 100644 (file)
@@ -72,5 +72,6 @@ noinst_HEADERS =      \
        timetoa.h       \
        timevalops.h    \
        trimble.h       \
+       vint64ops.h     \
        $(NULL)
 
index a2d0d84d08ea95484162ffa7b55c3da00859e331..eb3937516fe6d9048b05b30885a32c4ce04191a7 100644 (file)
@@ -305,9 +305,15 @@ ntpcal_time_to_date(struct calendar *jd, const vint64 *ts);
 extern int32_t
 ntpcal_periodic_extend(int32_t pivot, int32_t value, int32_t cycle);
 
+extern int
+ntpcal_ntp64_to_date(struct calendar *jd, const vint64 *ntp);
+
 extern int
 ntpcal_ntp_to_date(struct calendar *jd,        uint32_t ntp, const time_t *pivot);
 
+extern vint64
+ntpcal_date_to_ntp64(const struct calendar *jd);
+
 extern uint32_t
 ntpcal_date_to_ntp(const struct calendar *jd);
 
@@ -323,9 +329,15 @@ isocal_weeks_in_years(int32_t years);
 extern ntpcal_split
 isocal_split_eraweeks(int32_t weeks);
 
+extern int
+isocal_ntp64_to_date(struct isodate *id, const vint64 *ntp);
+
 extern int
 isocal_ntp_to_date(struct isodate *id, uint32_t ntp, const time_t *pivot);
 
+extern vint64
+isocal_date_to_ntp64(const struct isodate *id);
+
 extern uint32_t
 isocal_date_to_ntp(const struct isodate *id);
 
diff --git a/include/vint64ops.h b/include/vint64ops.h
new file mode 100644 (file)
index 0000000..2c3deff
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * vint64ops.h - operations on 'vint64' values
+ *
+ * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
+ * The contents of 'html/copyright.html' apply.
+ * ----------------------------------------------------------------------
+ * This is an attempt to get the vint64 calculations stuff centralised.
+ */
+#ifndef VINT64OPS_H
+#define VINT64OPS_H
+
+/* signed/unsigned compare. returns 1/0/-1 if lhs >/=/< rhs */
+extern int icmpv64(const vint64 * lhs, const vint64 * rhs);
+extern int ucmpv64(const vint64 * lhs, const vint64 * rhs);
+
+/* add / subtract */
+extern vint64 addv64(const vint64 *lhs, const vint64 *rhs);
+extern vint64 addv64i32(const vint64 * lhs, int32_t rhs);
+extern vint64 addv64u32(const vint64 * lhs, uint32_t rhs);
+
+extern vint64 subv64(const vint64 *lhs, const vint64 *rhs);
+extern vint64 subv64i32(const vint64 * lhs, int32_t rhs);
+extern vint64 subv64u32(const vint64 * lhs, uint32_t rhs);
+
+/* parsing. works like strtoul() or strtoull() */
+extern vint64 strtouv64(const char * begp, char ** endp, int base);
+
+#endif /*!defined(VINT64OPS_H)*/
index 069f85722cb5dd125adac46d13ebcb0f9a4cc577..515fa9a72f4598e909ae2a6c124f0535b8139679 100644 (file)
@@ -102,6 +102,7 @@ libntp_a_SRCS =                                             \
        timetoa.c                                       \
        timevalops.c                                    \
        uglydate.c                                      \
+       vint64ops.c                                     \
        work_fork.c                                     \
        work_thread.c                                   \
        ymd2yd.c                                        \
index 84533300e56b5c83f1d14f048ec09b887a73c42f..e557f4ec682e1c58029d0d709e4f9df0d5e830ee 100644 (file)
@@ -1268,6 +1268,20 @@ ntpcal_date_to_time(
  *
  * ==================================================================
  */
+int
+ntpcal_ntp64_to_date(
+       struct calendar *jd,
+       const vint64    *ntp
+       )
+{
+       ntpcal_split ds;
+       
+       ds = ntpcal_daysplit(ntp);
+       ds.hi += ntpcal_daysec_to_date(jd, ds.lo);
+
+       return ntpcal_rd_to_date(jd, ds.hi + DAY_NTP_STARTS);
+}
+
 int
 ntpcal_ntp_to_date(
        struct calendar *jd,
@@ -1275,19 +1289,28 @@ ntpcal_ntp_to_date(
        const time_t    *piv
        )
 {
-       vint64       vl;
-       ntpcal_split ds;
+       vint64  ntp64;
        
        /*
         * Unfold ntp time around current time into NTP domain. Split
         * into days and seconds, shift days into CE domain and
         * process the parts.
         */
-       vl = ntpcal_ntp_to_ntp(ntp, piv);
-       ds = ntpcal_daysplit(&vl);
-       ds.hi += ntpcal_daysec_to_date(jd, ds.lo);
+       ntp64 = ntpcal_ntp_to_ntp(ntp, piv);
+       return ntpcal_ntp64_to_date(jd, &ntp64);
+}
 
-       return ntpcal_rd_to_date(jd, ds.hi + DAY_NTP_STARTS);
+
+vint64
+ntpcal_date_to_ntp64(
+       const struct calendar *jd
+       )
+{
+       /*
+        * Convert date to NTP. Ignore yearday, use d/m/y only.
+        */
+       return ntpcal_dayjoin(ntpcal_date_to_rd(jd) - DAY_NTP_STARTS,
+                             ntpcal_date_to_daysec(jd));
 }
 
 
@@ -1297,12 +1320,13 @@ ntpcal_date_to_ntp(
        )
 {
        /*
-        * Convert date to NTP. Ignore yearday, use d/m/y only.
+        * Get lower half of 64-bit NTP timestamp from date/time.
         */
-       return ntpcal_dayjoin(ntpcal_date_to_rd(jd) - DAY_NTP_STARTS,
-                             ntpcal_date_to_daysec(jd)).d_s.lo;
+       return ntpcal_date_to_ntp64(jd).d_s.lo;
 }
 
+
+
 /*
  * ==================================================================
  *
@@ -1503,23 +1527,19 @@ isocal_split_eraweeks(
  * stamp.
  */
 int
-isocal_ntp_to_date(
+isocal_ntp64_to_date(
        struct isodate *id,
-       uint32_t                ntp,
-       const time_t   *piv
+       const vint64   *ntp
        )
 {
-       vint64       vl;
        ntpcal_split ds;
        int32_t      ts[3];
        
        /*
-        * Unfold ntp time around current time into NTP domain. Split
-        * into days and seconds, shift days into CE domain and
-        * process the parts.
+        * Split NTP time into days and seconds, shift days into CE
+        * domain and process the parts.
         */
-       vl = ntpcal_ntp_to_ntp(ntp, piv);
-       ds = ntpcal_daysplit(&vl);
+       ds = ntpcal_daysplit(ntp);
 
        /* split time part */
        ds.hi += priv_timesplit(ts, ds.lo);
@@ -1544,12 +1564,29 @@ isocal_ntp_to_date(
        return (ds.hi >= 0 && ds.hi < 0xFFFFU);
 }
 
+int
+isocal_ntp_to_date(
+       struct isodate *id,
+       uint32_t        ntp,
+       const time_t   *piv
+       )
+{
+       vint64  ntp64;
+       
+       /*
+        * Unfold ntp time around current time into NTP domain, then
+        * convert the full time stamp.
+        */
+       ntp64 = ntpcal_ntp_to_ntp(ntp, piv);
+       return isocal_ntp64_to_date(id, &ntp64);
+}
+
 /*
  * Convert a ISO date spec into a second in the NTP time scale,
  * properly truncated to 32 bit.
  */
-uint32_t
-isocal_date_to_ntp(
+vint64
+isocal_date_to_ntp64(
        const struct isodate *id
        )
 {
@@ -1561,7 +1598,18 @@ isocal_date_to_ntp(
        /* days is RDN of ISO date now */
        secs = ntpcal_etime_to_seconds(id->hour, id->minute, id->second);
 
-       return ntpcal_dayjoin(days - DAY_NTP_STARTS, secs).d_s.lo;
+       return ntpcal_dayjoin(days - DAY_NTP_STARTS, secs);
+}
+
+uint32_t
+isocal_date_to_ntp(
+       const struct isodate *id
+       )
+{
+       /*
+        * Get lower half of 64-bit NTP timestamp from date/time.
+        */
+       return isocal_date_to_ntp64(id).d_s.lo;
 }
 
 /* -*-EOF-*- */
diff --git a/libntp/vint64ops.c b/libntp/vint64ops.c
new file mode 100644 (file)
index 0000000..5adbebb
--- /dev/null
@@ -0,0 +1,284 @@
+/*
+ * vint64ops.c - operations on 'vint64' values
+ *
+ * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
+ * The contents of 'html/copyright.html' apply.
+ * ----------------------------------------------------------------------
+ * This is an attempt to get the vint64 calculations stuff centralised.
+ */
+
+#include <config.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+#include <errno.h>
+
+#include "ntp_types.h"
+#include "ntp_fp.h"
+#include "vint64ops.h"
+
+/* ---------------------------------------------------------------------
+ * GCC is rather sticky with its 'const' attribute. We have to do it more
+ * explicit than with a cast if we want to get rid of a CONST qualifier.
+ * Greetings from the PASCAL world, where casting was only possible via
+ * untagged unions...
+ */
+static inline void*
+noconst(
+       const void* ptr
+       )
+{
+       union {
+               const void * cp;
+               void *       vp;
+       } tmp;
+       tmp.cp = ptr;
+       return tmp.vp;
+}
+
+/* -------------------------------------------------------------------------*/
+
+vint64
+strtouv64(
+       const char * begp,
+       char **      endp,
+       int          base
+       )
+{
+       vint64  res;
+       u_char  digit;
+       int     sig, num;
+       const u_char *src;
+       
+       num = sig = 0;
+       src = (const u_char*)begp;
+       while (isspace(*src))
+               src++;
+
+       if (*src == '-') {
+               src++;
+               sig = 1;
+       } else  if (*src == '+') {
+               src++;
+       }
+
+       if (base == 0) {
+               base = 10;
+               if (*src == '0') {
+                       base = 8;
+                       if (toupper(*++src) == 'X') {
+                               src++;
+                               base = 16;
+                       }
+               }
+       } else if (base == 16) { /* remove optional leading '0x' or '0X' */
+               if (src[0] == '0' && toupper(src[1]) == 'X')
+                       src += 2;
+       } else if (base <= 2 || base > 36) {
+               memset(&res, 0xFF, sizeof(res));
+               errno = ERANGE;
+               return res;
+       }
+       
+       memset(&res, 0, sizeof(res));
+       while (*src) {
+               if (isdigit(*src))
+                       digit = *src - '0';
+               else if (isupper(*src))
+                       digit = *src - 'A' + 10;
+               else if (islower(*src))
+                       digit = *src - 'a' + 10;
+               else
+                       break;
+               if (digit >= base)
+                       break;
+               num = 1;
+#if defined(HAVE_INT64)
+               res.Q_s = res.Q_s * base + digit;
+#else
+               /* res *= base, using 16x16->32 bit
+                * multiplication. Slow but portable.
+                */ 
+               {
+                       uint32_t accu;
+                       accu       = (uint32_t)res.W_s.ll * base;
+                       res.W_s.ll = (uint16_t)accu;
+                       accu       = (accu >> 16)
+                                  + (uint32_t)res.W_s.lh * base;
+                       res.W_s.lh = (uint16_t)accu;
+                       /* the upper bits can be done in one step: */
+                       res.D_s.hi = res.D_s.hi * base + (accu >> 16);
+               }
+               M_ADD(res.D_s.hi, res.D_s.lo, 0, digit);
+#endif
+               src++;
+       }
+       if (!num)
+               errno = EINVAL;
+       if (endp)
+               *endp = (char*)noconst(src);
+       if (sig)
+               M_NEG(res.D_s.hi, res.D_s.lo);
+       return res;
+}
+
+/* -------------------------------------------------------------------------*/
+
+int
+icmpv64(
+       const vint64 * lhs,
+       const vint64 * rhs
+       )
+{
+       int res;
+
+#if defined(HAVE_INT64)
+       res = (lhs->q_s > rhs->q_s)
+           - (lhs->q_s < rhs->q_s);
+#else  
+       res = (lhs->d_s.hi > rhs->d_s.hi)
+           - (lhs->d_s.hi < rhs->d_s.hi);
+       if ( ! res )
+               res = (lhs->D_s.lo > rhs->D_s.lo)
+                   - (lhs->D_s.lo < rhs->D_s.lo);
+#endif
+
+       return res;
+}
+
+/* -------------------------------------------------------------------------*/
+
+int
+ucmpv64(
+       const vint64 * lhs,
+       const vint64 * rhs
+       )
+{
+       int res;
+       
+#if defined(HAVE_INT64)
+       res = (lhs->Q_s > rhs->Q_s)
+           - (lhs->Q_s < rhs->Q_s);
+#else  
+       res = (lhs->D_s.hi > rhs->D_s.hi)
+           - (lhs->D_s.hi < rhs->D_s.hi);
+       if ( ! res )
+               res = (lhs->D_s.lo > rhs->D_s.lo)
+                   - (lhs->D_s.lo < rhs->D_s.lo);
+#endif
+       return res;
+}
+
+/* -------------------------------------------------------------------------*/
+
+vint64
+addv64(
+       const vint64 *lhs,
+       const vint64 *rhs
+       )
+{
+       vint64 res;
+
+#if defined(HAVE_INT64)
+       res.Q_s = lhs->Q_s + rhs->Q_s;
+#else
+       res = *lhs;
+       M_ADD(res.D_s.hi, res.D_s.lo, rhs->D_s.hi, rhs->D_s.lo);
+#endif
+       return res;
+}
+
+/* -------------------------------------------------------------------------*/
+
+vint64
+subv64(
+       const vint64 *lhs,
+       const vint64 *rhs
+       )
+{
+       vint64 res;
+
+#if defined(HAVE_INT64)
+       res.Q_s = lhs->Q_s - rhs->Q_s;
+#else
+       res = *lhs;
+       M_SUB(res.D_s.hi, res.D_s.lo, rhs->D_s.hi, rhs->D_s.lo);
+#endif
+       return res;
+}
+
+/* -------------------------------------------------------------------------*/
+
+vint64
+addv64i32(
+       const vint64 * lhs,
+       int32_t        rhs
+       )
+{
+       vint64 res;
+
+       res = *lhs;
+#if defined(HAVE_INT64)
+       res.q_s += rhs;
+#else
+       M_ADD(res.D_s.hi, res.D_s.lo,  -(rhs < 0), rhs);
+#endif
+       return res;
+}
+
+/* -------------------------------------------------------------------------*/
+
+vint64
+subv64i32(
+       const vint64 * lhs,
+       int32_t        rhs
+       )
+{
+       vint64 res;
+
+       res = *lhs;
+#if defined(HAVE_INT64)
+       res.q_s -= rhs;
+#else
+       M_SUB(res.D_s.hi, res.D_s.lo,  -(rhs < 0), rhs);
+#endif
+       return res;
+}
+
+/* -------------------------------------------------------------------------*/
+
+vint64
+addv64u32(
+       const vint64 * lhs,
+       uint32_t       rhs
+       )
+{
+       vint64 res;
+
+       res = *lhs;
+#if defined(HAVE_INT64)
+       res.Q_s += rhs;
+#else
+       M_ADD(res.D_s.hi, res.D_s.lo, 0, rhs);
+#endif
+       return res;
+}
+
+/* -------------------------------------------------------------------------*/
+
+vint64
+subv64u32(
+       const vint64 * lhs,
+       uint32_t       rhs
+       )
+{
+       vint64 res;
+
+       res = *lhs;
+#if defined(HAVE_INT64)
+       res.Q_s -= rhs;
+#else
+       M_SUB(res.D_s.hi, res.D_s.lo, 0, rhs);
+#endif
+       return res;
+}
index 695d3a7735ae9cb865ba8decd33c7667b6c2c748..975a4089e970f7ea3d9adcb639beb5a467283348 100644 (file)
@@ -18,6 +18,7 @@
 #include "ntp_calendar.h"
 #include "ntp_leapsec.h"
 #include "ntp.h"
+#include "vint64ops.h"
 
 #include "isc/sha1.h"
 
  * Greetings from the PASCAL world, where casting was only possible via
  * untagged unions...
  */
-static void* noconst(const void* ptr)
+static inline void*
+noconst(
+       const void* ptr
+       )
 {
        union {
                const void * cp;
@@ -37,263 +41,6 @@ static void* noconst(const void* ptr)
        return tmp.vp;
 }
 
-/* ---------------------------------------------------------------------
- * Things to put into libntp...
- */
-
-vint64
-strtouv64(
-       const char * begp,
-       char **      endp,
-       int          base)
-{
-       vint64  res;
-       u_char  digit;
-       int     sig, num;
-       const u_char *src;
-       
-       num = sig = 0;
-       src = (const u_char*)begp;
-       while (isspace(*src))
-               src++;
-
-       if (*src == '-') {
-               src++;
-               sig = 1;
-       } else  if (*src == '+') {
-               src++;
-       }
-
-       if (base == 0) {
-               base = 10;
-               if (*src == '0') {
-                       base = 8;
-                       if (toupper(*++src) == 'X') {
-                               src++;
-                               base = 16;
-                       }
-               }
-       } else if (base == 16) { /* remove optional leading '0x' or '0X' */
-               if (src[0] == '0' && toupper(src[1]) == 'X')
-                       src += 2;
-       } else if (base <= 2 || base > 36) {
-               memset(&res, 0xFF, sizeof(res));
-               errno = ERANGE;
-               return res;
-       }
-       
-       memset(&res, 0, sizeof(res));
-       while (*src) {
-               if (isdigit(*src))
-                       digit = *src - '0';
-               else if (isupper(*src))
-                       digit = *src - 'A' + 10;
-               else if (islower(*src))
-                       digit = *src - 'a' + 10;
-               else
-                       break;
-               if (digit >= base)
-                       break;
-               num = 1;
-#if defined(HAVE_INT64)
-               res.Q_s = res.Q_s * base + digit;
-#else
-               /* res *= base, using 16x16->32 bit
-                * multiplication. Slow but portable.
-                */ 
-               {
-                       uint32_t accu;
-                       accu       = (uint32_t)res.W_s.ll * base;
-                       res.W_s.ll = (uint16_t)accu;
-                       accu       = (accu >> 16)
-                                  + (uint32_t)res.W_s.lh * base;
-                       res.W_s.lh = (uint16_t)accu;
-                       /* the upper bits can be done in one step: */
-                       res.D_s.hi = res.D_s.hi * base + (accu >> 16);
-               }
-               M_ADD(res.D_s.hi, res.D_s.lo, 0, digit);
-#endif
-               src++;
-       }
-       if (!num)
-               errno = EINVAL;
-       if (endp)
-               *endp = (char*)noconst(src);
-       if (sig)
-               M_NEG(res.D_s.hi, res.D_s.lo);
-       return res;
-}
-
-int icmpv64(
-       const vint64 * lhs,
-       const vint64 * rhs)
-{
-       int res;
-
-#if defined(HAVE_INT64)
-       res = (lhs->q_s > rhs->q_s)
-           - (lhs->q_s < rhs->q_s);
-#else  
-       res = (lhs->d_s.hi > rhs->d_s.hi)
-           - (lhs->d_s.hi < rhs->d_s.hi);
-       if ( ! res )
-               res = (lhs->D_s.lo > rhs->D_s.lo)
-                   - (lhs->D_s.lo < rhs->D_s.lo);
-#endif
-
-       return res;
-}
-
-
-int ucmpv64(
-       const vint64 * lhs,
-       const vint64 * rhs)
-{
-       int res;
-       
-#if defined(HAVE_INT64)
-       res = (lhs->Q_s > rhs->Q_s)
-           - (lhs->Q_s < rhs->Q_s);
-#else  
-       res = (lhs->D_s.hi > rhs->D_s.hi)
-           - (lhs->D_s.hi < rhs->D_s.hi);
-       if ( ! res )
-               res = (lhs->D_s.lo > rhs->D_s.lo)
-                   - (lhs->D_s.lo < rhs->D_s.lo);
-#endif
-       return res;
-}
-
-#if 0
-static vint64
-addv64(
-    const vint64 *lhs,
-    const vint64 *rhs)
-{
-       vint64 res;
-
-#if defined(HAVE_INT64)
-       res.Q_s = lhs->Q_s + rhs->Q_s;
-#else
-       res = *lhs;
-       M_ADD(res.D_s.hi, res.D_s.lo, rhs->D_s.hi, rhs->D_s.lo);
-#endif
-       return res;
-}
-#endif
-
-static vint64
-subv64(
-    const vint64 *lhs,
-    const vint64 *rhs)
-{
-       vint64 res;
-
-#if defined(HAVE_INT64)
-       res.Q_s = lhs->Q_s - rhs->Q_s;
-#else
-       res = *lhs;
-       M_SUB(res.D_s.hi, res.D_s.lo, rhs->D_s.hi, rhs->D_s.lo);
-#endif
-       return res;
-}
-
-static vint64
-addv64i32(
-       const vint64 * lhs,
-       int32_t        rhs)
-{
-       vint64 res;
-
-       res = *lhs;
-#if defined(HAVE_INT64)
-       res.q_s += rhs;
-#else
-       M_ADD(res.D_s.hi, res.D_s.lo,  -(rhs < 0), rhs);
-#endif
-       return res;
-}
-
-#if 0
-static vint64
-subv64i32(
-       const vint64 * lhs,
-       int32_t        rhs)
-{
-       vint64 res;
-
-       res = *lhs;
-#if defined(HAVE_INT64)
-       res.q_s -= rhs;
-#else
-       M_SUB(res.D_s.hi, res.D_s.lo,  -(rhs < 0), rhs);
-#endif
-       return res;
-}
-#endif
-
-#if 0
-static vint64
-addv64u32(
-       const vint64 * lhs,
-       uint32_t       rhs)
-{
-       vint64 res;
-
-       res = *lhs;
-#if defined(HAVE_INT64)
-       res.Q_s += rhs;
-#else
-       M_ADD(res.D_s.hi, res.D_s.lo, 0, rhs);
-#endif
-       return res;
-}
-#endif
-
-static vint64
-subv64u32(
-       const vint64 * lhs,
-       uint32_t       rhs)
-{
-       vint64 res;
-
-       res = *lhs;
-#if defined(HAVE_INT64)
-       res.Q_s -= rhs;
-#else
-       M_SUB(res.D_s.hi, res.D_s.lo, 0, rhs);
-#endif
-       return res;
-}
-
-/* ---------------------------------------------------------------------
- * Things to put into ntp_calendar... (and consequently into libntp...)
- */
-
-/* ------------------------------------------------------------------ */
-static int
-ntpcal_ntp64_to_date(
-       struct calendar *jd,
-       const vint64    *ntp)
-{
-       ntpcal_split ds;
-       
-       ds = ntpcal_daysplit(ntp);
-       ds.hi += ntpcal_daysec_to_date(jd, ds.lo);
-
-       return ntpcal_rd_to_date(jd, ds.hi + DAY_NTP_STARTS);
-}
-
-/* ------------------------------------------------------------------ */
-static vint64
-ntpcal_date_to_ntp64(
-       const struct calendar *jd)
-{
-       return ntpcal_dayjoin(ntpcal_date_to_rd(jd) - DAY_NTP_STARTS,
-                             ntpcal_date_to_daysec(jd));
-}
-
-
 /* ---------------------------------------------------------------------
  * Our internal data structure
  */
@@ -622,46 +369,66 @@ leapsec_reset_frame(void)
  */
 int/*BOOL*/
 leapsec_load_file(
-       FILE * ifp   ,
-       int    blimit,
-       int    hcheck)
+       FILE       * ifp  ,
+       const char * fname)
 {
+       static const char * const logPrefix = "leapsecond file";
+
        leap_table_t * pt;
        int            rcheck;
 
-       hcheck = (hcheck != 0);
+       if (NULL == fname)
+               fname = "<unknown>";
+
        rcheck = leapsec_validate((leapsec_reader)getc, ifp);
        switch (rcheck)
        {
        case LSVALID_GOODHASH:
-               msyslog(LOG_NOTICE, "%s",
-                       "leapsecond file: good hash signature");
+               msyslog(LOG_NOTICE, "%s ('%s'): good hash signature",
+                       logPrefix, fname);
                break;
 
        case LSVALID_NOHASH:
-               msyslog(LOG_INFO, "%s",
-                       "leapsecond file: no hash signature");
+               msyslog(LOG_INFO, "%s ('%s'): no hash signature",
+                       logPrefix, fname);
                break;
        case LSVALID_BADHASH:
-               msyslog(LOG_ERR, "%s",
-                       "leapsecond file: signature mismatch");
+               msyslog(LOG_ERR, "%s ('%s'): signature mismatch",
+                       logPrefix, fname);
                break;
        case LSVALID_BADFORMAT:
-               msyslog(LOG_ERR, "%s",
-                       "leapsecond file: malformed hash signature");
+               msyslog(LOG_ERR, "%s ('%s'): malformed hash signature",
+                       logPrefix, fname);
                break;
        default:
-               msyslog(LOG_ERR, "leapsecond file: unknown error code %d",
-                       rcheck);
+               msyslog(LOG_ERR, "%s ('%s'): unknown error code %d",
+                       logPrefix, fname, rcheck);
                break;
        }
-       if (rcheck < hcheck)
-               return 0;
+       if (rcheck < 0)
+               return FALSE;
 
        rewind(ifp);
        pt = leapsec_get_table(TRUE);
-       return leapsec_load(pt, (leapsec_reader)getc, ifp, blimit)
-           && leapsec_set_table(pt);
+       if (!leapsec_load(pt, (leapsec_reader)getc, ifp, TRUE)) {
+               switch (errno) {
+               case EINVAL:
+                       msyslog(LOG_ERR, "%s ('%s'): bad transition time",
+                               logPrefix, fname);
+                       break;
+               case ERANGE:
+                       msyslog(LOG_ERR, "%s ('%s'): times not ascending",
+                               logPrefix, fname);
+                       break;
+               default:
+                       msyslog(LOG_ERR, "%s ('%s'): parsing error",
+                               logPrefix, fname);
+                       break;
+               }
+               return FALSE;
+       }
+       msyslog(LOG_NOTICE, "%s '%s' loaded", logPrefix, fname);        
+       return leapsec_set_table(pt);
 }
 
 /* ------------------------------------------------------------------ */
index 014028a7b442c83e5fca3e4b9a898c92d1508cd5..5e9fc60d3f3288204f72ace86065254188e0d56f 100644 (file)
 #ifndef NTP_LEAPSEC_H
 #define NTP_LEAPSEC_H
 
-/* these should probably go to libntp... */
-extern vint64 strtouv64(const char * src, char ** endp, int base);
-extern int    icmpv64(const vint64 * lhs, const vint64 * rhs);
-extern int    ucmpv64(const vint64 * lhs, const vint64 * rhs);
-
 /* function pointer types. Note that 'fprintf' and 'getc' can be casted
  * to the dumper resp. reader type, provided the auxiliary argument is a
  * valid FILE pointer in hat case.
@@ -136,7 +131,7 @@ extern void leapsec_dump(const leap_table_t*, leapsec_dumper func, void *farg);
 /* Read a leap second file. This is a convenience wrapper around the
  * generic load function, 'leapsec_load()'.
  */
-extern int/*BOOL*/ leapsec_load_file(FILE * fp, int blimit, int checked);
+extern int/*BOOL*/ leapsec_load_file(FILE * fp, const char * fname);
 
 /* Get the current leap data signature. This consists of the last
  * ransition, the table expiration, and the total TAI difference at the
index 5613f1b96592c3eb97e7bf4b25588c907fe649ff..3082f6218c7d7e2ecdf3468e409092fa9145328b 100644 (file)
@@ -493,11 +493,7 @@ stats_config(
                        msyslog(LOG_ERR,
                            "leapseconds: stat(%s) failed: %m",
                            leapseconds_file);
-               } else if (!leapsec_load_file(fp, TRUE, FALSE)) {
-                       msyslog(LOG_ERR,
-                               "format error leapseconds file %s",
-                               leapseconds_file);
-               } else {
+               } else if (leapsec_load_file(fp, leapseconds_file)) {
                        leap_signature_t lsig;
 
                        leapsec_getsig(&lsig);
@@ -902,12 +898,8 @@ check_leap_file(
                } else if (  (sp1->st_mtime != sp2->st_mtime)
                          || (sp1->st_ctime != sp2->st_ctime)) {
                        leapseconds_file_sb1 = leapseconds_file_sb2;
-                       if (!leapsec_load_file(fp, TRUE, FALSE)) {
-                               msyslog(LOG_ERR,
-                                       "format error leapseconds file %s",
-                                       leapseconds_file);
+                       if (!leapsec_load_file(fp, leapseconds_file))
                                rc = -1;
-                       }
                }
                if (rc >= 0) {
                        rc = leapsec_daystolive(now.l_ui, NULL);
index 532fa2dcdc44bbbeab56e37f3cc726f5fff2cd81..067e9f01cb22883e4c727afed9f21360f7fb8030 100644 (file)
@@ -60,6 +60,7 @@ tests_SOURCES = $(top_srcdir)/sntp/tests_main.cpp     \
                tstotv.cpp              \
                tvtots.cpp              \
                uglydate.cpp            \
+               vi64ops.cpp             \
                ymd2yd.cpp              \
                $(NULL)
 
diff --git a/tests/libntp/vi64ops.cpp b/tests/libntp/vi64ops.cpp
new file mode 100644 (file)
index 0000000..7b839ab
--- /dev/null
@@ -0,0 +1,64 @@
+#include "libntptest.h"
+
+extern "C" {
+#include "vint64ops.h"
+}
+
+class vi64Test : public libntptest {
+public:
+       ::testing::AssertionResult IsEqual(const vint64 &expected, const vint64 &actual) {
+               if (0 == memcmp(&expected, &actual, sizeof(vint64))) {
+                       return ::testing::AssertionSuccess();
+               } else {
+                       return ::testing::AssertionFailure()
+                           << "expected: "
+                           << std::hex << expected.D_s.hi << '.'
+                           << std::hex << expected.D_s.lo
+                           << " but was "
+                           << std::hex << actual.D_s.hi << '.'
+                           << std::hex << actual.D_s.lo;
+               }
+       }
+};
+
+// ----------------------------------------------------------------------
+// test number parser
+TEST_F(vi64Test, ParseVUI64_pos) {
+       vint64 act, exp;
+       const char *sp;
+       char       *ep;
+
+       sp         = "1234x";
+       exp.D_s.hi = 0;
+       exp.D_s.lo = 1234;
+       act        = strtouv64(sp, &ep, 0);
+       EXPECT_TRUE(IsEqual(exp, act));
+       EXPECT_EQ(*ep, 'x');
+}
+
+TEST_F(vi64Test, ParseVUI64_neg) {
+       vint64 act, exp;
+       const char *sp;
+       char       *ep;
+
+       sp         = "-1234x";
+       exp.D_s.hi = ~0;
+       exp.D_s.lo = -1234;
+       act        = strtouv64(sp, &ep, 0);
+       EXPECT_TRUE(IsEqual(exp, act));
+       EXPECT_EQ(*ep, 'x');
+}
+
+TEST_F(vi64Test, ParseVUI64_case) {
+       vint64 act, exp;
+       const char *sp;
+       char       *ep;
+
+       sp         = "0123456789AbCdEf";
+       exp.D_s.hi = 0x01234567;
+       exp.D_s.lo = 0x89ABCDEF;
+       act        = strtouv64(sp, &ep, 16);
+       EXPECT_TRUE(IsEqual(exp, act));
+       EXPECT_EQ(*ep, '\0');
+}
+
index ba18a305293d7fa98e0a6d8fba48a7c0d625a6c6..6ae0cee34c31e63e2a60ab2d9689e22b4fa8b8a6 100644 (file)
@@ -286,19 +286,6 @@ protected:
                                << CalendarToString(actual);
                }
        }
-       ::testing::AssertionResult IsEqual(const vint64 &expected, const vint64 &actual) {
-               if (0 == memcmp(&expected, &actual, sizeof(vint64))) {
-                       return ::testing::AssertionSuccess();
-               } else {
-                       return ::testing::AssertionFailure()
-                           << "expected: "
-                           << std::hex << expected.D_s.hi << '.'
-                           << std::hex << expected.D_s.lo
-                           << " but was "
-                           << std::hex << actual.D_s.hi << '.'
-                           << std::hex << actual.D_s.lo;
-               }
-       }
 };
 
 void leapsecTest::SetUp()
@@ -363,35 +350,6 @@ TEST_F(leapsecTest, ValidateNoLeadZero) {
 // BASIC FUNCTIONS
 // =====================================================================
 
-// ----------------------------------------------------------------------
-// test number parser
-TEST_F(leapsecTest, ParseVUI64) {
-       vint64 act, exp;
-       const char *sp;
-       char       *ep;
-
-       sp         = "1234x";
-       exp.D_s.hi = 0;
-       exp.D_s.lo = 1234;
-       act        = strtouv64(sp, &ep, 0);
-       EXPECT_TRUE(IsEqual(exp, act));
-       EXPECT_EQ(*ep, 'x');
-
-       sp         = "-1234x";
-       exp.D_s.hi = ~0;
-       exp.D_s.lo = -1234;
-       act        = strtouv64(sp, &ep, 0);
-       EXPECT_TRUE(IsEqual(exp, act));
-       EXPECT_EQ(*ep, 'x');
-
-       sp         = "0123456789AbCdEf";
-       exp.D_s.hi = 0x01234567;
-       exp.D_s.lo = 0x89ABCDEF;
-       act        = strtouv64(sp, &ep, 16);
-       EXPECT_TRUE(IsEqual(exp, act));
-       EXPECT_EQ(*ep, '\0');
-}
-
 // ----------------------------------------------------------------------
 // test table selection
 TEST_F(leapsecTest, tableSelect) {