release candidate
[asdcplib.git] / src / KM_util.cpp
index 7fcd6e34b54a16636d59b5897b157341f3720e80..2624f07fcb1bf6533153c787c9892360b07e9564 100755 (executable)
@@ -537,9 +537,6 @@ Kumu::GenRandomValue(SymmetricKey& Key)
 
 
 //------------------------------------------------------------------------------------------
-// read a ber value from the buffer and compare with test value.
-// Advances buffer to first character after BER value.
-
 // read a ber value from the buffer and compare with test value.
 // Advances buffer to first character after BER value.
 //
@@ -605,6 +602,20 @@ static const ui64_t ber_masks[9] =
     0
   };
 
+//
+ui32_t
+Kumu::get_BER_length_for_value(ui64_t val)
+{
+  for ( ui32_t i = 0; i < 9; i++ )
+    {
+      if ( ( val & ber_masks[i] ) == 0 )
+       return i + 1;
+    }
+
+  ui64Printer tmp_i(val);
+  DefaultLogSink().Error("BER integer encoding not supported for large value %s\n", tmp_i.c_str());
+  return 0;
+}
 
 //
 bool
@@ -626,14 +637,14 @@ Kumu::write_BER(byte_t* buf, ui64_t val, ui32_t ber_len)
     { // sanity check BER length
       if ( ber_len > 9 )
         {
-          DefaultLogSink().Error("BER size %u exceeds maximum size of 9\n", ber_len);
+          DefaultLogSink().Error("BER integer length %u exceeds maximum size of 9\n", ber_len);
           return false;
         }
       
-      if ( val & ber_masks[ber_len - 1] )
+      if ( ( val & ber_masks[ber_len - 1] ) != 0 )
         {
          ui64Printer tmp_i(val);
-          DefaultLogSink().Error("BER size %u too small for value %s\n", tmp_i.c_str());
+          DefaultLogSink().Error("BER integer length %u too small for value %s\n", ber_len, tmp_i.c_str());
           return false;
         }
     }
@@ -774,6 +785,26 @@ Kumu::Timestamp::AddMinutes(i32_t minutes)
     }
 }
 
+//
+void
+Kumu::Timestamp::AddSeconds(i32_t seconds)
+{
+  SYSTEMTIME current_st;
+  FILETIME current_ft;
+  ULARGE_INTEGER current_ul;
+
+  if ( seconds != 0 )
+    {
+      TIMESTAMP_TO_SYSTIME(*this, &current_st);
+      SystemTimeToFileTime(&current_st, &current_ft);
+      memcpy(&current_ul, &current_ft, sizeof(current_ul));
+      current_ul.QuadPart += ( seconds_to_ns100(1) * (i64_t)seconds );
+      memcpy(&current_ft, &current_ul, sizeof(current_ft));
+      FileTimeToSystemTime(&current_ft, &current_st);
+      SYSTIME_TO_TIMESTAMP(&current_st, *this);
+    }
+}
+
 #else // KM_WIN32
 
 #include <time.h>
@@ -889,6 +920,23 @@ Kumu::Timestamp::AddMinutes(i32_t minutes)
     }
 }
 
+//
+void
+Kumu::Timestamp::AddSeconds(i32_t seconds)
+{
+  Kumu::TAI::caltime ct;
+  Kumu::TAI::tai t;
+
+  if ( seconds != 0 )
+    {
+      TIMESTAMP_TO_CALTIME(*this, &ct)
+      t = ct;
+      t.add_seconds(seconds);
+      ct = t;
+      CALTIME_TO_TIMESTAMP(&ct, *this)
+    }
+}
+
 #endif // KM_WIN32