summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2016-01-14 22:27:20 +0000
committerCarl Hetherington <cth@carlh.net>2016-01-14 23:02:44 +0000
commit379fb7a2ef1aaa0e92d0c5c76ea78b86f8554808 (patch)
treef7e6e15682a358d446a8a1d4297acef0f72f1486 /src
parent664f5734adfbf3bae1cbfd84c3b648bdf70f0490 (diff)
Patches for testing to allow predictable random number and date generation.
Diffstat (limited to 'src')
-rw-r--r--src/AS_DCP_TimedText.cpp16
-rwxr-xr-xsrc/KM_prng.cpp31
-rwxr-xr-xsrc/KM_prng.h1
-rwxr-xr-xsrc/KM_util.cpp44
-rwxr-xr-xsrc/KM_util.h25
5 files changed, 79 insertions, 38 deletions
diff --git a/src/AS_DCP_TimedText.cpp b/src/AS_DCP_TimedText.cpp
index 10651a8..77bc3f1 100644
--- a/src/AS_DCP_TimedText.cpp
+++ b/src/AS_DCP_TimedText.cpp
@@ -25,7 +25,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*! \file AS_DCP_TimedText.cpp
- \version $Id: AS_DCP_TimedText.cpp,v 1.38 2015/10/09 23:41:11 jhurst Exp $
+ \version $Id: AS_DCP_TimedText.cpp,v 1.38 2015/10/09 23:41:11 jhurst Exp $
\brief AS-DCP library, PCM essence reader and writer implementation
*/
@@ -99,7 +99,7 @@ ASDCP::TimedText::DescriptorDump(ASDCP::TimedText::TimedTextDescriptor const& TD
{
TmpID.Set((*ri).ResourceID);
fprintf(stream, " %s: %s\n",
- TmpID.EncodeHex(buf, 64),
+ TmpID.EncodeHex(buf, 64),
MIME2str((*ri).Type));
}
}
@@ -131,7 +131,7 @@ class ASDCP::TimedText::MXFReader::h__Reader : public ASDCP::h__ASDCPReader
ASDCP_NO_COPY_CONSTRUCT(h__Reader);
public:
- TimedTextDescriptor m_TDesc;
+ TimedTextDescriptor m_TDesc;
h__Reader(const Dictionary& d) : ASDCP::h__ASDCPReader(d), m_EssenceDescriptor(0) {
memset(&m_TDesc.AssetID, 0, UUIDlen);
@@ -204,7 +204,7 @@ ASDCP::Result_t
ASDCP::TimedText::MXFReader::h__Reader::OpenRead(const std::string& filename)
{
Result_t result = OpenMXFRead(filename);
-
+
if( ASDCP_SUCCESS(result) )
{
if ( m_EssenceDescriptor == 0 )
@@ -599,7 +599,7 @@ ASDCP::TimedText::MXFWriter::h__Writer::SetSourceStream(ASDCP::TimedText::TimedT
AddEssenceDescriptor(UL(m_Dict->ul(MDD_TimedTextWrappingClip)));
result = m_HeaderPart.WriteToFile(m_File, m_HeaderSize);
-
+
if ( KM_SUCCESS(result) )
result = CreateBodyPart(m_TDesc.EditRate);
}
@@ -627,13 +627,13 @@ ASDCP::TimedText::MXFWriter::h__Writer::WriteTimedTextResource(const std::string
ui32_t str_size = XMLDoc.size();
FrameBuffer FrameBuf(str_size);
-
+
memcpy(FrameBuf.Data(), XMLDoc.c_str(), str_size);
FrameBuf.Size(str_size);
IndexTableSegment::IndexEntry Entry;
Entry.StreamOffset = m_StreamOffset;
-
+
if ( ASDCP_SUCCESS(result) )
result = WriteEKLVPacket(FrameBuf, m_EssenceUL, Ctx, HMAC);
@@ -763,7 +763,7 @@ ASDCP::TimedText::MXFWriter::OpenWrite(const std::string& filename, const Writer
m_Writer = new h__Writer(DefaultSMPTEDict());
m_Writer->m_Info = Info;
-
+
Result_t result = m_Writer->OpenWrite(filename, HeaderSize);
if ( ASDCP_SUCCESS(result) )
diff --git a/src/KM_prng.cpp b/src/KM_prng.cpp
index 5212595..1612746 100755
--- a/src/KM_prng.cpp
+++ b/src/KM_prng.cpp
@@ -64,6 +64,7 @@ public:
AES_KEY m_Context;
byte_t m_ctr_buf[RNG_BLOCK_SIZE];
Mutex m_Lock;
+ unsigned int m_cth_test_rng_state;
h__RNG()
{
@@ -97,8 +98,9 @@ public:
} // end AutoMutex context
set_key(rng_key);
+ reset();
}
-
+
//
void
set_key(const byte_t* key_fodder)
@@ -116,7 +118,7 @@ public:
AES_set_encrypt_key(sha_buf, RNG_KEY_SIZE_BITS, &m_Context);
*(ui32_t*)(m_ctr_buf + 12) = 1;
}
-
+
//
void
fill_rand(byte_t* buf, ui32_t len)
@@ -131,14 +133,27 @@ public:
*(ui32_t*)(m_ctr_buf + 12) += 1;
gen_count += RNG_BLOCK_SIZE;
}
-
+
if ( len != gen_count ) // partial count needed?
{
byte_t tmp[RNG_BLOCK_SIZE];
AES_encrypt(m_ctr_buf, tmp, &m_Context);
memcpy(buf + gen_count, tmp, len - gen_count);
}
+
+ if (cth_test)
+ {
+#ifdef __unix__
+ for (unsigned int i = 0; i < len; ++i)
+ buf[i] = rand_r(&m_cth_test_rng_state);
+#endif
+ }
}
+
+ void reset()
+ {
+ m_cth_test_rng_state = 1;
+ }
};
@@ -173,13 +188,13 @@ Kumu::FortunaRNG::FillRandom(byte_t* buf, ui32_t len)
s_RNG->fill_rand(buf, gen_size);
buf += gen_size;
len -= gen_size;
-
+
// re-seed the generator
byte_t rng_key[RNG_KEY_SIZE];
s_RNG->fill_rand(rng_key, RNG_KEY_SIZE);
s_RNG->set_key(rng_key);
}
-
+
return front_of_buffer;
}
@@ -192,6 +207,12 @@ Kumu::FortunaRNG::FillRandom(Kumu::ByteString& Buffer)
return Buffer.Data();
}
+void
+Kumu::FortunaRNG::Reset()
+{
+ s_RNG->reset();
+}
+
//------------------------------------------------------------------------------------------
//
diff --git a/src/KM_prng.h b/src/KM_prng.h
index 2a909d6..a695f7b 100755
--- a/src/KM_prng.h
+++ b/src/KM_prng.h
@@ -45,6 +45,7 @@ namespace Kumu
~FortunaRNG();
const byte_t* FillRandom(byte_t* buf, ui32_t len);
const byte_t* FillRandom(ByteString&);
+ void Reset();
};
diff --git a/src/KM_util.cpp b/src/KM_util.cpp
index e6d04a7..e7585ea 100755
--- a/src/KM_util.cpp
+++ b/src/KM_util.cpp
@@ -40,6 +40,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <map>
#include <string>
+bool Kumu::cth_test = false;
+
const char*
Kumu::Version()
{
@@ -320,7 +322,7 @@ Kumu::base64encode(const byte_t* buf, ui32_t buf_len, char* strbuf, ui32_t strbu
diff = buf_len - i;
assert(diff > 0);
assert(diff < 3);
-
+
strbuf[out_char++] = base64_chars[( buf[0] >> 2 )];
if ( diff == 1 )
@@ -575,7 +577,7 @@ Kumu::bin2UUIDhex(const byte_t* bin_buf, ui32_t bin_len, char* str_buf, ui32_t s
// add in the hyphens and trainling null
for ( i = 8; i < 24; i += 5 )
str_buf[i] = '-';
-
+
str_buf[36] = 0;
return str_buf;
}
@@ -589,6 +591,13 @@ Kumu::GenRandomValue(UUID& ID)
ID.Set(tmp_buf);
}
+void
+Kumu::ResetTestRNG()
+{
+ FortunaRNG RNG;
+ RNG.Reset();
+}
+
//
void
Kumu::GenRandomUUID(byte_t* buf)
@@ -647,7 +656,7 @@ bool
Kumu::read_BER(const byte_t* buf, ui64_t* val)
{
ui8_t ber_size, i;
-
+
if ( buf == 0 || val == 0 )
return false;
@@ -671,7 +680,7 @@ Kumu::read_BER(const byte_t* buf, ui64_t* val)
static const ui64_t ber_masks[9] =
- { ui64_C(0xffffffffffffffff), ui64_C(0xffffffffffffff00),
+ { ui64_C(0xffffffffffffffff), ui64_C(0xffffffffffffff00),
ui64_C(0xffffffffffff0000), ui64_C(0xffffffffff000000),
ui64_C(0xffffffff00000000), ui64_C(0xffffff0000000000),
ui64_C(0xffff000000000000), ui64_C(0xff00000000000000),
@@ -716,7 +725,7 @@ Kumu::write_BER(byte_t* buf, ui64_t val, ui32_t 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] ) != 0 )
{
ui64Printer tmp_i(val);
@@ -745,7 +754,14 @@ Kumu::write_BER(byte_t* buf, ui64_t val, ui32_t ber_len)
//
Kumu::Timestamp::Timestamp() : m_TZOffsetMinutes(0) {
- m_Timestamp.now();
+ if (cth_test)
+ {
+ m_Timestamp.x = 42;
+ }
+ else
+ {
+ m_Timestamp.now();
+ }
}
Kumu::Timestamp::Timestamp(const Timestamp& rhs) {
@@ -830,7 +846,7 @@ Kumu::Timestamp::SetTZOffsetMinutes(const i32_t& minutes)
return true;
}
-//
+//
const char*
Kumu::Timestamp::EncodeString(char* str_buf, ui32_t buf_len) const
{
@@ -859,7 +875,7 @@ Kumu::Timestamp::EncodeString(char* str_buf, ui32_t buf_len) const
if ( m_TZOffsetMinutes < 0 )
direction = '-';
}
-
+
// 2004-05-01T13:20:00+00:00
snprintf(str_buf, buf_len,
"%04hu-%02hhu-%02hhuT%02hhu:%02hhu:%02hhu%c%02u:%02u",
@@ -889,7 +905,7 @@ Kumu::Timestamp::DecodeString(const char* datestr)
YMDhms.date.year = strtol(datestr, 0, 10);
YMDhms.date.month = strtol(datestr + 5, 0, 10);
YMDhms.date.day = strtol(datestr + 8, 0, 10);
-
+
if ( datestr[10] == 'T' )
{
if ( ! ( isdigit(datestr[11]) && isdigit(datestr[12]) )
@@ -962,7 +978,7 @@ Kumu::Timestamp::DecodeString(const char* datestr)
datestr, char_count);
return false;
}
-
+
m_Timestamp = YMDhms;
m_TZOffsetMinutes = YMDhms.offset;
return true;
@@ -1004,7 +1020,7 @@ Kumu::Timestamp::Archive(MemIOWriter* Writer) const
ui8_t month, day, hour, minute, second, tick = 0;
GetComponents(year, month, day, hour, minute, second);
- if ( ! Writer->WriteUi16BE(year) ) return false;
+ if ( ! Writer->WriteUi16BE(year) ) return false;
if ( ! Writer->WriteUi8(month) ) return false;
if ( ! Writer->WriteUi8(day) ) return false;
if ( ! Writer->WriteUi8(hour) ) return false;
@@ -1140,7 +1156,7 @@ Kumu::ByteString::Capacity(ui32_t cap_size)
else
free(m_Data);
}
-
+
if ( ( m_Data = (byte_t*)malloc(cap_size) ) == 0 )
return RESULT_ALLOC;
@@ -1150,7 +1166,7 @@ Kumu::ByteString::Capacity(ui32_t cap_size)
memcpy(m_Data, tmp_data, m_Length);
free(tmp_data);
}
-
+
m_Capacity = cap_size;
return RESULT_OK;
}
@@ -1241,7 +1257,7 @@ Kumu::km_token_split(const std::string& str, const std::string& separator)
pstr = r + separator.size();
r = strstr(pstr, separator.c_str());
}
-
+
components.push_back(std::string(pstr));
return components;
}
diff --git a/src/KM_util.h b/src/KM_util.h
index 8b1db49..c894d36 100755
--- a/src/KM_util.h
+++ b/src/KM_util.h
@@ -40,6 +40,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace Kumu
{
+ extern bool cth_test;
+
// The version number declaration and explanation are in ../configure.ac
const char* Version();
@@ -53,7 +55,7 @@ namespace Kumu
protected:
const char* m_format;
char m_strbuf[SIZE];
-
+
public:
IntPrinter(const char* format, T value) {
assert(format);
@@ -299,7 +301,7 @@ namespace Kumu
if ( m_Value[i] != rhs.m_Value[i] )
return m_Value[i] < rhs.m_Value[i];
}
-
+
return false;
}
@@ -375,10 +377,11 @@ namespace Kumu
return bin2UUIDhex(m_Value, Size(), buf, buf_len);
}
};
-
+
void GenRandomUUID(byte_t* buf); // buf must be UUID_Length or longer
void GenRandomValue(UUID&);
-
+ void ResetTestRNG();
+
typedef ArchivableList<UUID> UUIDList;
// a self-wiping key container
@@ -427,7 +430,7 @@ namespace Kumu
// always UTC
void GetComponents(ui16_t& Year, ui8_t& Month, ui8_t& Day,
- ui8_t& Hour, ui8_t& Minute, ui8_t& Second) const;
+ ui8_t& Hour, ui8_t& Minute, ui8_t& Second) const;
void SetComponents(const ui16_t& Year, const ui8_t& Month, const ui8_t& Day,
const ui8_t& Hour, const ui8_t& Minute, const ui8_t& Second);
@@ -470,12 +473,12 @@ namespace Kumu
class ByteString : public IArchive
{
KM_NO_COPY_CONSTRUCT(ByteString);
-
+
protected:
byte_t* m_Data; // pointer to memory area containing frame data
ui32_t m_Capacity; // size of memory area pointed to by m_Data
ui32_t m_Length; // length of byte string in memory area pointed to by m_Data
-
+
public:
ByteString();
ByteString(ui32_t cap);
@@ -486,19 +489,19 @@ namespace Kumu
Result_t Append(const ByteString&);
Result_t Append(const byte_t* buf, ui32_t buf_len);
-
+
// returns the size of the buffer
inline ui32_t Capacity() const { return m_Capacity; }
// returns a const pointer to the essence data
inline const byte_t* RoData() const { assert(m_Data); return m_Data; }
-
+
// returns a non-const pointer to the essence data
inline byte_t* Data() { assert(m_Data); return m_Data; }
-
+
// set the length of the buffer's contents
inline ui32_t Length(ui32_t l) { return m_Length = l; }
-
+
// returns the length of the buffer's contents
inline ui32_t Length() const { return m_Length; }