[PATCH 2/2] agent: Use npth_clock_gettime on all platforms.

Damien Goutte-Gattat dgouttegattat at incenp.org
Thu Jul 13 16:24:39 CEST 2017


* agent/protect.c (struct calibrate_time_s): Removed.
(calibrate_get_time): Removed.
(calibrate_elapsed_time): Remove Windows-specific code.
(calibrate_s2k_count_one): Use npth_clock_gettime directly.
--

Since we are already using the npth_clock_gettime wrapper, we
no longer need a Windows-specific code path.  We can rely
entirely on nPth to abstract away the underlying system.

Signed-off-by: Damien Goutte-Gattat <dgouttegattat at incenp.org>
---
 agent/protect.c | 74 +++++++--------------------------------------------------
 1 file changed, 8 insertions(+), 66 deletions(-)

diff --git a/agent/protect.c b/agent/protect.c
index 42fb479..08d5ca0 100644
--- a/agent/protect.c
+++ b/agent/protect.c
@@ -27,14 +27,7 @@
 #include <assert.h>
 #include <unistd.h>
 #include <sys/stat.h>
-#ifdef HAVE_W32_SYSTEM
-# ifdef HAVE_WINSOCK2_H
-#  include <winsock2.h>
-# endif
-# include <windows.h>
-#else
-# include <npth.h>
-#endif
+#include <npth.h>
 
 #include "agent.h"
 
@@ -70,17 +63,6 @@ static const struct {
 };
 
 
-/* A helper object for time measurement.  */
-#ifdef HAVE_W32_SYSTEM
-struct calibrate_time_s
-{
-  FILETIME creation_time, exit_time, kernel_time, user_time;
-};
-#else
- #define calibrate_time_s timespec
-#endif
-
-
 static int
 hash_passphrase (const char *passphrase, int hashalgo,
                  int s2kmode,
@@ -90,55 +72,15 @@ hash_passphrase (const char *passphrase, int hashalgo,
 
 
 
-/* Get the process time and store it in DATA.  */
-static void
-calibrate_get_time (struct calibrate_time_s *data)
-{
-#ifdef HAVE_W32_SYSTEM
-# ifdef HAVE_W32CE_SYSTEM
-  GetThreadTimes (GetCurrentThread (),
-                  &data->creation_time, &data->exit_time,
-                  &data->kernel_time, &data->user_time);
-# else
-  GetProcessTimes (GetCurrentProcess (),
-                   &data->creation_time, &data->exit_time,
-                   &data->kernel_time, &data->user_time);
-# endif
-#else
-  npth_clock_gettime(data);
-#endif
-}
-
-
 static unsigned long
-calibrate_elapsed_time (struct calibrate_time_s *starttime)
+calibrate_elapsed_time (struct timespec *starttime)
 {
-  struct calibrate_time_s stoptime;
-
-  calibrate_get_time (&stoptime);
-#ifdef HAVE_W32_SYSTEM
-  {
-    unsigned long long t1, t2;
-
-    t1 = (((unsigned long long)starttime->kernel_time.dwHighDateTime << 32)
-          + starttime->kernel_time.dwLowDateTime);
-    t1 += (((unsigned long long)starttime->user_time.dwHighDateTime << 32)
-           + starttime->user_time.dwLowDateTime);
-    t2 = (((unsigned long long)stoptime.kernel_time.dwHighDateTime << 32)
-          + stoptime.kernel_time.dwLowDateTime);
-    t2 += (((unsigned long long)stoptime.user_time.dwHighDateTime << 32)
-           + stoptime.user_time.dwLowDateTime);
-    return (unsigned long)((t2 - t1)/10000);
-  }
-#else
-  {
-    struct calibrate_time_s difftime;
+  struct timespec stoptime, difftime;
 
-    npth_timersub(&stoptime, starttime, &difftime);
+  npth_clock_gettime (&stoptime);
+  npth_timersub(&stoptime, starttime, &difftime);
 
-    return difftime.tv_sec * 1000 + difftime.tv_nsec / 1000000.0;
-  }
-#endif
+  return difftime.tv_sec * 1000 + difftime.tv_nsec / 1000000.0;
 }
 
 
@@ -149,9 +91,9 @@ calibrate_s2k_count_one (unsigned long count)
 {
   int rc;
   char keybuf[PROT_CIPHER_KEYLEN];
-  struct calibrate_time_s starttime;
+  struct timespec starttime;
 
-  calibrate_get_time (&starttime);
+  npth_clock_gettime (&starttime);
   rc = hash_passphrase ("123456789abcdef0", GCRY_MD_SHA1,
                         3, "saltsalt", count, keybuf, sizeof keybuf);
   if (rc)
-- 
2.9.0




More information about the Gnupg-devel mailing list