#include <KM_memio.h>
#include <KM_fileio.h>
#include <KM_log.h>
+#include <KM_tai.h>
#include <ctype.h>
#include <list>
+#include <map>
#include <string>
+const char*
+Kumu::Version()
+{
+ return PACKAGE_VERSION;
+}
+
+
+//------------------------------------------------------------------------------------------
+
+// Result_t Internals
+
+struct map_entry_t
+{
+ int rcode;
+ Kumu::Result_t* result;
+};
+
+const ui32_t MapMax = 1024;
+const ui32_t MapSize = MapMax * (sizeof(struct map_entry_t));
+static bool s_MapInit = false;
+static struct map_entry_t s_ResultMap[MapSize];
+
+//
+const Kumu::Result_t&
+Kumu::Result_t::Find(int v)
+{
+ if ( v == 0 )
+ return RESULT_OK;
+
+ for ( ui32_t i = 0; s_ResultMap[i].result != 0 && i < MapMax; i++ )
+ {
+ if ( s_ResultMap[i].rcode == v )
+ return *s_ResultMap[i].result;
+ }
+
+ DefaultLogSink().Error("Unknown result code: %ld\n", v);
+ return RESULT_FAIL;
+}
+
+//
+Kumu::Result_t
+Kumu::Result_t::Delete(int v)
+{
+ if ( v >= RESULT_NOTAFILE.Value() )
+ {
+ DefaultLogSink().Error("Cannot delete core result code: %ld\n", v);
+ return RESULT_FAIL;
+ }
+
+ for ( ui32_t i = 0; s_ResultMap[i].result != 0 && i < MapMax; i++ )
+ {
+ if ( s_ResultMap[i].rcode == v )
+ {
+ s_ResultMap[i].rcode = 0;
+ s_ResultMap[i++].result = 0;
+
+ for ( ; s_ResultMap[i].result != 0 && i < MapMax; i++ )
+ s_ResultMap[i-1] = s_ResultMap[i];
+
+ return RESULT_OK;
+ }
+ }
+
+ return RESULT_FALSE;
+}
+
+
+//
+Kumu::Result_t::Result_t(int v, const char* l) : value(v), label(l)
+{
+ assert(l);
+ assert(value < (int)MapMax);
+
+ if ( v == 0 )
+ return;
+
+ if ( ! s_MapInit )
+ {
+ s_MapInit = true;
+ s_ResultMap[0].rcode = v;
+ s_ResultMap[0].result = this;
+ s_ResultMap[1].rcode = 0;
+ s_ResultMap[1].result = 0;
+ return;
+ }
+
+ ui32_t i = 0;
+ while ( s_ResultMap[i].result != 0 && i < MapMax )
+ {
+ if ( s_ResultMap[i].rcode == v && s_ResultMap[i].result != 0 )
+ return;
+
+ i++;
+ }
+
+ assert(i+2 < MapMax);
+
+ s_ResultMap[i].rcode = v;
+ s_ResultMap[i].result = this;
+ s_ResultMap[i+1].rcode = 0;
+ s_ResultMap[i+1].result = 0;
+ return;
+}
+
+Kumu::Result_t::~Result_t() {}
+
+
+//------------------------------------------------------------------------------------------
+// DTrace internals
+
+static int s_DTraceSequence = 0;
+
+Kumu::DTrace_t::DTrace_t(const char* Label, Kumu::Result_t* Watch, int Line, const char* File)
+ : m_Label(Label), m_Watch(Watch), m_Line(Line), m_File(File)
+{
+ m_Sequence = s_DTraceSequence++;
+ DefaultLogSink().Debug("@enter %s[%d] (%s at %d)\n", m_Label, m_Sequence, m_File, m_Line);
+}
+
+Kumu::DTrace_t::~DTrace_t()
+{
+ if ( m_Watch != 0 )
+ DefaultLogSink().Debug("@exit %s[%d]: %s\n", m_Label, m_Sequence, m_Watch->Label());
+ else
+ DefaultLogSink().Debug("@exit %s[%d]\n", m_Label, m_Sequence);
+}
+
//------------------------------------------------------------------------------------------
i32_t
Kumu::hex2bin(const char* str, byte_t* buf, ui32_t buf_len, ui32_t* conv_size)
{
- KM_TEST_NULL(str);
- KM_TEST_NULL(buf);
- KM_TEST_NULL(conv_size);
+ KM_TEST_NULL_L(str);
+ KM_TEST_NULL_L(buf);
+ KM_TEST_NULL_L(conv_size);
*conv_size = 0;
//
void
-Kumu::GenRandomValue(SymmetricKey& ID)
+Kumu::GenRandomValue(SymmetricKey& Key)
{
byte_t tmp_buf[SymmetricKey_Length];
FortunaRNG RNG;
RNG.FillRandom(tmp_buf, SymmetricKey_Length);
- ID.Set(tmp_buf);
+ Key.Set(tmp_buf);
}
{ // sanity check BER length
if ( ber_len > 9 )
{
- DefaultLogSink().Error("BER size %lu exceeds maximum size of 9\n", ber_len);
+ DefaultLogSink().Error("BER size %u exceeds maximum size of 9\n", ber_len);
return false;
}
if ( val & ber_masks[ber_len - 1] )
{
ui64Printer tmp_i(val);
- DefaultLogSink().Error("BER size %lu too small for value %s\n", tmp_i.c_str());
+ DefaultLogSink().Error("BER size %u too small for value %s\n", tmp_i.c_str());
return false;
}
}
return ( CompareFileTime(&lft, &rft) == -1 );
}
+//
+bool
+Kumu::Timestamp::operator>(const Timestamp& rhs) const
+{
+ SYSTEMTIME lhst, rhst;
+ FILETIME lft, rft;
+
+ TIMESTAMP_TO_SYSTIME(*this, &lhst);
+ TIMESTAMP_TO_SYSTIME(rhs, &rhst);
+ SystemTimeToFileTime(&lhst, &lft);
+ SystemTimeToFileTime(&rhst, &rft);
+ return ( CompareFileTime(&lft, &rft) == 1 );
+}
+
inline ui64_t
seconds_to_ns100(ui32_t seconds)
{
#include <time.h>
-#define TIMESTAMP_TO_TM(ts, t) \
- (t)->tm_year = (ts).Year - 1900; /* year - 1900 */ \
- (t)->tm_mon = (ts).Month - 1; /* month of year (0 - 11) */ \
- (t)->tm_mday = (ts).Day; /* day of month (1 - 31) */ \
- (t)->tm_hour = (ts).Hour; /* hours (0 - 23) */ \
- (t)->tm_min = (ts).Minute; /* minutes (0 - 59) */ \
- (t)->tm_sec = (ts).Second; /* seconds (0 - 60) */
+#define TIMESTAMP_TO_CALTIME(ts, ct) \
+ (ct)->date.year = (ts).Year; /* year */ \
+ (ct)->date.month = (ts).Month; /* month of year (1 - 12) */ \
+ (ct)->date.day = (ts).Day; /* day of month (1 - 31) */ \
+ (ct)->hour = (ts).Hour; /* hours (0 - 23) */ \
+ (ct)->minute = (ts).Minute; /* minutes (0 - 59) */ \
+ (ct)->second = (ts).Second; /* seconds (0 - 60) */ \
+ (ct)->offset = 0;
+
+#define CALTIME_TO_TIMESTAMP(ct, ts) \
+ assert((ct)->offset == 0); \
+ (ts).Year = (ct)->date.year; /* year */ \
+ (ts).Month = (ct)->date.month; /* month of year (1 - 12) */ \
+ (ts).Day = (ct)->date.day; /* day of month (1 - 31) */ \
+ (ts).Hour = (ct)->hour; /* hours (0 - 23) */ \
+ (ts).Minute = (ct)->minute; /* minutes (0 - 59) */ \
+ (ts).Second = (ct)->second; /* seconds (0 - 60) */
-#define TM_TO_TIMESTAMP(t, ts) \
- (ts).Year = (t)->tm_year + 1900; /* year - 1900 */ \
- (ts).Month = (t)->tm_mon + 1; /* month of year (0 - 11) */ \
- (ts).Day = (t)->tm_mday; /* day of month (1 - 31) */ \
- (ts).Hour = (t)->tm_hour; /* hours (0 - 23) */ \
- (ts).Minute = (t)->tm_min; /* minutes (0 - 59) */ \
- (ts).Second = (t)->tm_sec; /* seconds (0 - 60) */
//
Kumu::Timestamp::Timestamp() :
- Year(0), Month(0), Day(0), Hour(0), Minute(0), Second(0)
+ Year(0), Month(0), Day(0), Hour(0), Minute(0), Second(0)
{
- time_t t_now = time(0);
- struct tm* now = gmtime(&t_now);
- TM_TO_TIMESTAMP(now, *this);
+ Kumu::TAI::tai now;
+ Kumu::TAI::caltime ct;
+ now.now();
+ ct = now;
+ CALTIME_TO_TIMESTAMP(&ct, *this)
}
//
bool
Kumu::Timestamp::operator<(const Timestamp& rhs) const
{
- struct tm lhtm, rhtm;
- TIMESTAMP_TO_TM(*this, &lhtm);
- TIMESTAMP_TO_TM(rhs, &rhtm);
- return ( timegm(&lhtm) < timegm(&rhtm) );
+ Kumu::TAI::caltime lh_ct, rh_ct;
+ TIMESTAMP_TO_CALTIME(*this, &lh_ct)
+ TIMESTAMP_TO_CALTIME(rhs, &rh_ct)
+
+ Kumu::TAI::tai lh_tai, rh_tai;
+ lh_tai = lh_ct;
+ rh_tai = rh_ct;
+
+ return ( lh_tai.x < rh_tai.x );
+}
+
+//
+bool
+Kumu::Timestamp::operator>(const Timestamp& rhs) const
+{
+ Kumu::TAI::caltime lh_ct, rh_ct;
+ TIMESTAMP_TO_CALTIME(*this, &lh_ct)
+ TIMESTAMP_TO_CALTIME(rhs, &rh_ct)
+
+ Kumu::TAI::tai lh_tai, rh_tai;
+ lh_tai = lh_ct;
+ rh_tai = rh_ct;
+
+ return ( lh_tai.x > rh_tai.x );
}
//
void
Kumu::Timestamp::AddDays(i32_t days)
{
- struct tm current;
+ Kumu::TAI::caltime ct;
+ Kumu::TAI::tai t;
if ( days != 0 )
{
- TIMESTAMP_TO_TM(*this, ¤t);
- time_t adj_time = timegm(¤t);
- adj_time += 86400 * days;
- struct tm* now = gmtime(&adj_time);
- TM_TO_TIMESTAMP(now, *this);
+ TIMESTAMP_TO_CALTIME(*this, &ct)
+ t = ct;
+ t.add_days(days);
+ ct = t;
+ CALTIME_TO_TIMESTAMP(&ct, *this)
}
}
void
Kumu::Timestamp::AddHours(i32_t hours)
{
- struct tm current;
+ Kumu::TAI::caltime ct;
+ Kumu::TAI::tai t;
if ( hours != 0 )
{
- TIMESTAMP_TO_TM(*this, ¤t);
- time_t adj_time = timegm(¤t);
- adj_time += 3600 * hours;
- struct tm* now = gmtime(&adj_time);
- TM_TO_TIMESTAMP(now, *this);
+ TIMESTAMP_TO_CALTIME(*this, &ct)
+ t = ct;
+ t.add_hours(hours);
+ ct = t;
+ CALTIME_TO_TIMESTAMP(&ct, *this)
}
}
#endif // KM_WIN32
-Kumu::Timestamp::Timestamp(const Timestamp& rhs)
+Kumu::Timestamp::Timestamp(const Timestamp& rhs) : IArchive()
{
Year = rhs.Year;
Month = rhs.Month;
// 2004-05-01T13:20:00-00:00
snprintf(str_buf, buf_len,
- "%04hu-%02hu-%02huT%02hu:%02hu:%02hu-00:00",
+ "%04hu-%02hu-%02huT%02hu:%02hu:%02hu+00:00",
Year, Month, Day, Hour, Minute, Second);
return str_buf;
}
+//
+bool
+Kumu::Timestamp::DecodeString(const char* datestr)
+{
+ Timestamp TmpStamp;
+
+ if ( ! ( isdigit(datestr[0]) && isdigit(datestr[1]) && isdigit(datestr[2]) && isdigit(datestr[3]) )
+ || datestr[4] != '-'
+ || ! ( isdigit(datestr[5]) && isdigit(datestr[6]) )
+ || datestr[7] != '-'
+ || ! ( isdigit(datestr[8]) && isdigit(datestr[9]) ) )
+ return false;
+
+ ui32_t char_count = 10;
+ TmpStamp.Year = atoi(datestr);
+ TmpStamp.Month = atoi(datestr + 5);
+ TmpStamp.Day = atoi(datestr + 8);
+ TmpStamp.Hour = TmpStamp.Minute = TmpStamp.Second = 0;
+
+ if ( datestr[10] == 'T' )
+ {
+ if ( ! ( isdigit(datestr[11]) && isdigit(datestr[12]) )
+ || datestr[13] != ':'
+ || ! ( isdigit(datestr[14]) && isdigit(datestr[15]) ) )
+ return false;
+
+ char_count += 6;
+ TmpStamp.Hour = atoi(datestr + 11);
+ TmpStamp.Minute = atoi(datestr + 14);
+
+ if ( datestr[16] == ':' )
+ {
+ if ( ! ( isdigit(datestr[17]) && isdigit(datestr[18]) ) )
+ return false;
+
+ char_count += 3;
+ TmpStamp.Second = atoi(datestr + 17);
+ }
+
+ if ( datestr[19] == '.' )
+ {
+ if ( ! ( isdigit(datestr[20]) && isdigit(datestr[21]) && isdigit(datestr[22]) ) )
+ return false;
+
+ // we don't carry the ms value
+ datestr += 4;
+ }
+
+ if ( datestr[19] == '-' || datestr[19] == '+' )
+ {
+ if ( ! ( isdigit(datestr[20]) && isdigit(datestr[21]) )
+ || datestr[22] != ':'
+ || ! ( isdigit(datestr[23]) && isdigit(datestr[24]) ) )
+ 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 )
+ return false;
+
+ else
+ AddHours( (datestr[19] == '-' ? (0 - TZ_hh) : TZ_hh));
+ }
+ }
+
+ if ( datestr[char_count] != 0 )
+ {
+ Kumu::DefaultLogSink().Error("Unexpected extra characters in string: %s (%ld)\n",
+ datestr, char_count);
+ return false;
+ }
+
+#ifdef KM_WIN32
+ SYSTEMTIME st;
+ FILETIME ft;
+ TIMESTAMP_TO_SYSTIME(TmpStamp, &st);
+ if ( SystemTimeToFileTime(&st, &ft) == 0 )
+ return false;
+ SYSTIME_TO_TIMESTAMP(&st, *this);
+#else
+ Kumu::TAI::tai t;
+ Kumu::TAI::caltime ct;
+ TIMESTAMP_TO_CALTIME(TmpStamp, &ct);
+ t = ct; // back and forth to tai to normalize offset
+ ct = t;
+ CALTIME_TO_TIMESTAMP(&ct, *this)
+#endif
+
+ return true;
+}
+
//
bool
Kumu::Timestamp::HasValue() const
return true;
}
-#if 0
-//
-bool
-Kumu::UnarchiveString(MemIOReader* Reader, std::string&)
-{
- assert(Reader);
- ui32_t str_length;
- if ( ! Reader->ReadUi32BE(&str_length) ) return false;
- assign((const char*)Reader->CurrentData(), str_length);
- if ( ! Reader->SkipOffset(str_length) ) return false;
- return true;
-}
-
-//
-bool
-Kumu::String::Archive(MemIOWriter* Writer) const
-{
- assert(Writer);
- if ( ! Writer->WriteUi32BE(length()) ) return false;
- if ( ! Writer->WriteRaw((const byte_t*)c_str(), length()) ) return false;
-
- return true;
-}
-#endif
-
//------------------------------------------------------------------------------------------
Kumu::MemIOWriter::MemIOWriter(ByteString* Buf)
: m_p(0), m_capacity(0), m_size(0)
{
m_p = Buf->RoData();
- m_capacity = Buf->Capacity();
+ m_capacity = Buf->Length();
assert(m_p); assert(m_capacity);
}
}
+// copy the given data into the ByteString, set Length value.
+// Returns error if the ByteString is too small.
+Kumu::Result_t
+Kumu::ByteString::Set(const ByteString& Buf)
+{
+ if ( m_Capacity < Buf.m_Capacity )
+ return RESULT_ALLOC;
+
+ memcpy(m_Data, Buf.m_Data, Buf.m_Length);
+ m_Length = Buf.m_Length;
+ return RESULT_OK;
+}
+
+
// Sets the size of the internally allocate buffer.
-// Resets content length to zero.
Kumu::Result_t
Kumu::ByteString::Capacity(ui32_t cap_size)
{
- if ( m_Capacity < cap_size )
+ if ( m_Capacity >= cap_size )
+ return RESULT_OK;
+
+ byte_t* tmp_data = 0;
+ if ( m_Data != 0 )
{
- if ( m_Data != 0 )
+ if ( m_Length > 0 )
+ tmp_data = m_Data;
+ else
free(m_Data);
+ }
- m_Data = (byte_t*)malloc(cap_size);
-
- if ( m_Data == 0 )
- return RESULT_ALLOC;
-
- m_Capacity = cap_size;
- m_Length = 0;
+ if ( ( m_Data = (byte_t*)malloc(cap_size) ) == 0 )
+ return RESULT_ALLOC;
+
+ if ( tmp_data != 0 )
+ {
+ assert(m_Length > 0);
+ memcpy(m_Data, tmp_data, m_Length);
+ free(tmp_data);
}
-
+
+ m_Capacity = cap_size;
return RESULT_OK;
}