hmac pad fix
[asdcplib.git] / src / KM_util.cpp
index 43b00a4211ccd0cfe3c9f81425719eb8a087ade8..b155362f61f4251752a73c9eb13a01632c7ae365 100755 (executable)
@@ -40,8 +40,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <map>
 #include <string>
 
-#define CONFIG_RANDOM_UUID
-
 const char*
 Kumu::Version()
 {
@@ -729,7 +727,7 @@ Kumu::Timestamp::AddDays(i32_t days)
       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(86400) * (ui64_t)days );
+      current_ul.QuadPart += ( seconds_to_ns100(86400) * (i64_t)days );
       memcpy(&current_ft, &current_ul, sizeof(current_ft));
       FileTimeToSystemTime(&current_ft, &current_st);
       SYSTIME_TO_TIMESTAMP(&current_st, *this);
@@ -749,7 +747,47 @@ Kumu::Timestamp::AddHours(i32_t hours)
       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(3600) * (ui64_t)hours );
+      current_ul.QuadPart += ( seconds_to_ns100(3600) * (i64_t)hours );
+      memcpy(&current_ft, &current_ul, sizeof(current_ft));
+      FileTimeToSystemTime(&current_ft, &current_st);
+      SYSTIME_TO_TIMESTAMP(&current_st, *this);
+    }
+}
+
+//
+void
+Kumu::Timestamp::AddMinutes(i32_t minutes)
+{
+  SYSTEMTIME current_st;
+  FILETIME current_ft;
+  ULARGE_INTEGER current_ul;
+
+  if ( minutes != 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(60) * (i64_t)minutes );
+      memcpy(&current_ft, &current_ul, sizeof(current_ft));
+      FileTimeToSystemTime(&current_ft, &current_st);
+      SYSTIME_TO_TIMESTAMP(&current_st, *this);
+    }
+}
+
+//
+void
+Kumu::Timestamp::AddSeconds(i32_t seconds)
+{
+  SYSTEMTIME current_st;
+  FILETIME current_ft;
+  ULARGE_INTEGER current_ul;
+
+  if ( minutes != 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);
@@ -854,6 +892,40 @@ Kumu::Timestamp::AddHours(i32_t hours)
     }
 }
 
+//
+void
+Kumu::Timestamp::AddMinutes(i32_t minutes)
+{
+  Kumu::TAI::caltime ct;
+  Kumu::TAI::tai t;
+
+  if ( minutes != 0 )
+    {
+      TIMESTAMP_TO_CALTIME(*this, &ct)
+      t = ct;
+      t.add_minutes(minutes);
+      ct = t;
+      CALTIME_TO_TIMESTAMP(&ct, *this)
+    }
+}
+
+//
+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
 
 
@@ -917,15 +989,42 @@ Kumu::Timestamp::operator!=(const Timestamp& rhs) const
 // 
 const char*
 Kumu::Timestamp::EncodeString(char* str_buf, ui32_t buf_len) const
+{
+  return EncodeStringWithOffset(str_buf, buf_len, 0);
+}
+
+// 
+const char*
+Kumu::Timestamp::EncodeStringWithOffset(char* str_buf, ui32_t buf_len,
+                                       i32_t offset_minutes) const
 {
   if ( buf_len < ( DateTimeLen + 1 ) )
     return 0;
 
-  // 2004-05-01T13:20:00-00:00
+  // ensure offset is within +/- 14 hours
+  if ((offset_minutes < -14 * 60) || (offset_minutes > 14 * 60))
+    return 0;
+
+  // set the apparent time
+  Kumu::Timestamp tmp_t(*this);
+  tmp_t.AddMinutes(offset_minutes);
+
+  char direction = '+';
+  if (offset_minutes < 0) {
+    direction = '-';
+    // need absolute offset from zero
+    offset_minutes = -offset_minutes;
+  }
+
+  // 2004-05-01T13:20:00+00:00
   snprintf(str_buf, buf_len,
-          "%04hu-%02hu-%02huT%02hu:%02hu:%02hu+00:00",
-          Year, Month, Day, Hour, Minute, Second);
-  
+          "%04hu-%02hu-%02huT%02hu:%02hu:%02hu%c%02hu:%02hu",
+          tmp_t.Year, tmp_t.Month, tmp_t.Day,
+          tmp_t.Hour, tmp_t.Minute, tmp_t.Second,
+          direction,
+          offset_minutes / 60,
+          offset_minutes % 60);
+
   return str_buf;
 }
 
@@ -985,17 +1084,25 @@ Kumu::Timestamp::DecodeString(const char* datestr)
            return false;
 
          char_count += 6;
+
          ui32_t TZ_hh = atoi(datestr + 20);
          ui32_t TZ_mm = atoi(datestr + 23);
-      
-         if ( TZ_mm != 0 )
-           Kumu::DefaultLogSink().Warn("Ignoring minutes in timezone offset: %u\n", TZ_mm);
-         
-         if ( TZ_hh > 12 )
+         if ((TZ_hh > 14) || (TZ_mm > 59) || ((TZ_hh == 14) && (TZ_mm > 0)))
            return false;
 
-         else 
-           AddHours( (datestr[19] == '-' ? (0 - TZ_hh) : TZ_hh));
+         i32_t TZ_offset = 60 * TZ_hh + TZ_mm;
+         if (datestr[19] == '-')
+           TZ_offset = -TZ_offset;
+         /* at this point, TZ_offset reflects the contents of the string */
+
+         /* a negative offset is behind UTC and so needs to increment to
+          * convert, while a positive offset must do the reverse */
+         TmpStamp.AddMinutes(-TZ_offset);
+       }
+      else if (datestr[19] == 'Z')
+       {
+         /* act as if the offset were +00:00 */
+         char_count++;
        }
     }
 
@@ -1055,6 +1162,45 @@ Kumu::Timestamp::Archive(MemIOWriter* Writer) const
   return true;
 }
 
+//
+long
+Kumu::Timestamp::GetSecondsSinceEpoch(void) const
+{
+#ifdef KM_WIN32
+  SYSTEMTIME timeST;
+  TIMESTAMP_TO_SYSTIME(*this, &timeST);
+  FILETIME timeFT;
+  SystemTimeToFileTime(&timeST, &timeFT);
+  ULARGE_INTEGER timeUL;
+  timeUL.LowPart = timeFT.dwLowDateTime;
+  timeUL.HighPart = timeFT.dwHighDateTime;
+
+  SYSTEMTIME epochST;
+  epochST.wYear = 1970;
+  epochST.wMonth = 0;
+  epochST.wDayOfWeek = 4;
+  epochST.wDay = 1;
+  epochST.wHour = 0;
+  epochST.wMinute = 0;
+  epochST.wSecond = 0;
+  epochST.wMilliseconds = 0;
+  FILETIME epochFT;
+  SystemTimeToFileTime(&epochST, &epochFT);
+  ULARGE_INTEGER epochUL;
+  epochUL.LowPart = epochFT.dwLowDateTime;
+  epochUL.HighPart = epochFT.dwHighDateTime;
+
+  return (timeUL.QuadPart - epochUL.QuadPart) / 10000000;
+#else
+  Kumu::TAI::caltime ct;
+  Kumu::TAI::tai t;
+  TIMESTAMP_TO_CALTIME(*this, &ct);
+  t = ct;
+
+  return (long) (t.x - ui64_C(4611686018427387914));
+#endif
+}
+
 //------------------------------------------------------------------------------------------
 
 Kumu::MemIOWriter::MemIOWriter(ByteString* Buf)