FIPS 186-2 fixes
[asdcplib.git] / src / KM_util.h
index 6b2e78e9ec5b71a0ec3248cf34bdab28130939da..0f2af46faada351878d2eee58c179cd9f34b667a 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005-2006, John Hurst
+Copyright (c) 2005-2009, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -36,9 +36,12 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <KM_error.h>
 #include <string.h>
 #include <string>
+#include <list>
 
 namespace Kumu
 {
+  // The version number declaration and explanation are in ../configure.ac
+  const char* Version();
 
   // a class that represents the string form of a value
   template <class T, int SIZE = 16>
@@ -169,11 +172,59 @@ namespace Kumu
     {
     public:
       virtual ~IArchive(){}
-      virtual bool HasValue() const = 0;
-      virtual bool Archive(MemIOWriter* Writer) const = 0;
-      virtual bool Unarchive(MemIOReader* Reader) = 0;
+      virtual bool   HasValue() const = 0;
+      virtual ui32_t ArchiveLength() const = 0;
+      virtual bool   Archive(MemIOWriter* Writer) const = 0;
+      virtual bool   Unarchive(MemIOReader* Reader) = 0;
     };
 
+  //
+  template <class T>
+  class ArchivableList : public std::list<T>, public IArchive
+    {
+    public:
+      ArchivableList() {}
+      virtual ~ArchivableList() {}
+
+      bool HasValue() const { return ! this->empty(); }
+
+      ui32_t ArchiveLength() const
+      {
+       ui32_t arch_size = sizeof(ui32_t);
+
+       typename ArchivableList<T>::const_iterator i = this->begin();
+       for ( ; i != this->end(); i++ )
+         arch_size += i->ArchiveLength();
+
+       return arch_size;
+      }
+
+      bool Unarchive(Kumu::MemIOReader* Reader)
+       {
+         if ( Reader == 0 ) return false;
+         ui32_t read_size = 0;
+         if ( ! Reader->ReadUi32BE(&read_size) ) return false;
+         for ( ui32_t i = 0; i < read_size; i++ )
+           {
+             T TmpTP;
+             if ( ! TmpTP.Unarchive(Reader) ) return false;
+             this->push_back(TmpTP);
+           }
+
+         return true;
+       }
+
+      bool Archive(Kumu::MemIOWriter* Writer) const
+       {
+         if ( Writer == 0 ) return false;
+         if ( ! Writer->WriteUi32BE(this->size()) ) return false;
+         typename ArchivableList<T>::const_iterator i = this->begin();
+         for ( ; i != this->end(); i++ )
+           if ( ! i->Archive(Writer) ) return false;
+
+         return true;
+       }
+    };
 
   //
   // the base of all identifier classes, Identifier is not usually used directly
@@ -189,7 +240,7 @@ namespace Kumu
     public:
       Identifier() : m_HasValue(false) { memset(m_Value, 0, SIZE); }
       Identifier(const byte_t* value) : m_HasValue(true) { memcpy(m_Value, value, SIZE); }
-      Identifier(const Identifier& rhs) {
+      Identifier(const Identifier& rhs) : IArchive() {
        m_HasValue = rhs.m_HasValue;
        memcpy(m_Value, rhs.m_Value, SIZE);
       }
@@ -203,6 +254,7 @@ namespace Kumu
       }
 
       inline void Set(const byte_t* value) { m_HasValue = true; memcpy(m_Value, value, SIZE); }
+      inline void Reset() { m_HasValue = false; memset(m_Value, 0, SIZE); }
       inline const byte_t* Value() const { return m_Value; }
       inline ui32_t Size() const { return SIZE; }
 
@@ -231,6 +283,8 @@ namespace Kumu
       inline bool DecodeHex(const char* str) {
        ui32_t char_count;
        m_HasValue = ( hex2bin(str, m_Value, SIZE, &char_count) == 0 );
+       if ( m_HasValue && char_count != SIZE )
+         m_HasValue = false;
        return m_HasValue;
       }
 
@@ -245,6 +299,8 @@ namespace Kumu
       inline bool DecodeBase64(const char* str) {
        ui32_t char_count;
        m_HasValue = ( base64decode(str, m_Value, SIZE, &char_count) == 0 );
+       if ( m_HasValue && char_count != SIZE )
+         m_HasValue = false;
        return m_HasValue;
       }
 
@@ -254,6 +310,8 @@ namespace Kumu
 
       inline bool HasValue() const { return m_HasValue; }
 
+      inline ui32_t ArchiveLength() const { return SIZE; }
+
       inline bool Unarchive(Kumu::MemIOReader* Reader) {
        m_HasValue = Reader->ReadRaw(m_Value, SIZE);
        return m_HasValue;
@@ -264,7 +322,7 @@ namespace Kumu
       }
     };
 
-  
+
   // UUID
   //
   const ui32_t UUID_Length = 16;
@@ -288,6 +346,8 @@ namespace Kumu
   void GenRandomUUID(byte_t* buf); // buf must be UUID_Length or longer
   void GenRandomValue(UUID&);
   
+  typedef ArchivableList<UUID> UUIDList;
+
   // a self-wiping key container
   //
   const ui32_t SymmetricKey_Length = 16;
@@ -308,7 +368,7 @@ namespace Kumu
   void GenRandomValue(SymmetricKey&);
 
   //
-  // 2004-05-01T13:20:00-00:00
+  // 2004-05-01T13:20:00+00:00
   const ui32_t DateTimeLen = 25; //  the number of chars in the xs:dateTime format (sans milliseconds)
 
   // UTC time+date representation
@@ -333,26 +393,34 @@ namespace Kumu
       bool operator==(const Timestamp& rhs) const;
       bool operator!=(const Timestamp& rhs) const;
 
-      // Write the timestamp value to the given buffer in the form 2004-05-01T13:20:00-00:00
+      // Write the timestamp value to the given buffer in the form 2004-05-01T13:20:00+00:00
       // returns 0 if the buffer is smaller than DateTimeLen
       const char* EncodeString(char* str_buf, ui32_t buf_len) const;
+      const char* EncodeStringWithOffset(char* str_buf, ui32_t buf_len,
+                                        i32_t offset_minutes = 0) const;
 
       // decode and set value from string formatted by EncodeString
       bool        DecodeString(const char* datestr);
 
-      // Add the given number of days or hours to the timestamp value.
+      // Add the given number of days, hours, minutes, or seconds to the timestamp value.
       // Values less than zero will cause the timestamp to decrease
       void AddDays(i32_t);
       void AddHours(i32_t);
+      void AddMinutes(i32_t);
+      void AddSeconds(i32_t);
 
       // Read and write the timestamp value as a byte string having
       // the following format:
       // | 16 bits int, big-endian |    8 bits   |   8 bits  |   8 bits   |    8 bits    |    8 bits    |
       // |        Year A.D         | Month(1-12) | Day(1-31) | Hour(0-23) | Minute(0-59) | Second(0-59) |
       //
-      virtual bool HasValue() const;
-      virtual bool Archive(MemIOWriter* Writer) const;
-      virtual bool Unarchive(MemIOReader* Reader);
+      virtual bool   HasValue() const;
+      virtual ui32_t ArchiveLength() const { return 8L; }
+      virtual bool   Archive(MemIOWriter* Writer) const;
+      virtual bool   Unarchive(MemIOReader* Reader);
+
+      // Get the number of seconds since the Unix epoch (1970-01-01T00:00:00+00:00)
+      long GetSecondsSinceEpoch(void) const;
     };
 
   //
@@ -370,8 +438,7 @@ namespace Kumu
       ByteString(ui32_t cap);
       virtual ~ByteString();
 
-      // Sets the size of the internally allocated buffer.
-      // Resets content Size to zero.
+      // Sets or resets the size of the internally allocated buffer.
       Result_t Capacity(ui32_t cap);
 
       Result_t Append(const ByteString&);
@@ -395,9 +462,12 @@ namespace Kumu
       // copy the given data into the ByteString, set Length value.
       // Returns error if the ByteString is too small.
       Result_t Set(const byte_t* buf, ui32_t buf_len);
+      Result_t Set(const ByteString& Buf);
 
       inline virtual bool HasValue() const { return m_Length > 0; }
 
+      inline virtual ui32_t ArchiveLength() const { return m_Length; }
+
       inline virtual bool Archive(MemIOWriter* Writer) const {
        assert(Writer);
        if ( ! Writer->WriteUi32BE(m_Length) ) return false;
@@ -407,9 +477,11 @@ namespace Kumu
 
       inline virtual bool Unarchive(MemIOReader* Reader) {
        assert(Reader);
-       if ( ! Reader->ReadUi32BE(&m_Length) ) return false;
-       if ( KM_FAILURE(Capacity(m_Length)) ) return false;
-       if ( ! Reader->ReadRaw(m_Data, m_Length) ) return false;
+       ui32_t tmp_len;
+       if ( ! Reader->ReadUi32BE(&tmp_len) ) return false;
+       if ( KM_FAILURE(Capacity(tmp_len)) ) return false;
+       if ( ! Reader->ReadRaw(m_Data, tmp_len) ) return false;
+       m_Length = tmp_len;
        return true;
       }
     };