summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2016-08-11 15:59:49 +0100
committerCarl Hetherington <cth@carlh.net>2016-08-11 15:59:49 +0100
commit5d6770aea92d798a31fdbda128411ce2001a4209 (patch)
treeb4ec279ddc900dbbb2dbf57a36d19e8034505402
parent3f40eb5821858264f2c2fbf3eba86f743f9ab8ed (diff)
Don't use stringstream in DecryptedKDM.
-rw-r--r--src/decrypted_kdm.cc45
-rw-r--r--src/decrypted_kdm.h8
-rw-r--r--test/kdm_test.cc30
3 files changed, 60 insertions, 23 deletions
diff --git a/src/decrypted_kdm.cc b/src/decrypted_kdm.cc
index 4ffad923..38fc77cc 100644
--- a/src/decrypted_kdm.cc
+++ b/src/decrypted_kdm.cc
@@ -73,34 +73,33 @@ put (uint8_t ** d, uint8_t const * s, int N)
(*d) += N;
}
-static void
-put_uuid (uint8_t ** d, string id)
+void
+DecryptedKDM::put_uuid (uint8_t ** d, string id)
{
- id.erase (std::remove (id.begin(), id.end(), '-'));
- for (int i = 0; i < 32; i += 2) {
- locked_stringstream s;
- s << id[i] << id[i + 1];
- int h;
- s >> hex >> h;
- **d = h;
- (*d)++;
- }
+ /* 32 hex digits plus some hyphens */
+ DCP_ASSERT (id.length() == 36);
+ sscanf (
+ id.c_str(),
+ "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
+ *d + 0, *d + 1, *d + 2, *d + 3, *d + 4, *d + 5, *d + 6, *d + 7,
+ *d + 8, *d + 9, *d + 10, *d + 11, *d + 12, *d + 13, *d + 14, *d + 15
+ );
+
+ *d += 16;
}
-static string
-get_uuid (unsigned char ** p)
+string
+DecryptedKDM::get_uuid (unsigned char ** p)
{
- locked_stringstream g;
-
- for (int i = 0; i < 16; ++i) {
- g << setw(2) << setfill('0') << hex << static_cast<int> (**p);
- (*p)++;
- if (i == 3 || i == 5 || i == 7 || i == 9) {
- g << '-';
- }
- }
+ char buffer[37];
+ snprintf (
+ buffer, sizeof(buffer), "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
+ (*p)[0], (*p)[1], (*p)[2], (*p)[3], (*p)[4], (*p)[5], (*p)[6], (*p)[7],
+ (*p)[8], (*p)[9], (*p)[10], (*p)[11], (*p)[12], (*p)[13], (*p)[14], (*p)[15]
+ );
- return g.str ();
+ *p += 16;
+ return buffer;
}
static string
diff --git a/src/decrypted_kdm.h b/src/decrypted_kdm.h
index 4ac95dc6..356b9883 100644
--- a/src/decrypted_kdm.h
+++ b/src/decrypted_kdm.h
@@ -46,6 +46,8 @@
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
+class decrypted_kdm_test;
+
namespace dcp {
class DecryptedKDMKey;
@@ -138,6 +140,12 @@ public:
}
private:
+
+ friend class ::decrypted_kdm_test;
+
+ static void put_uuid (uint8_t ** d, std::string id);
+ static std::string get_uuid (unsigned char ** p);
+
LocalTime _not_valid_before;
LocalTime _not_valid_after;
boost::optional<std::string> _annotation_text;
diff --git a/test/kdm_test.cc b/test/kdm_test.cc
index 82701d96..d8ae4222 100644
--- a/test/kdm_test.cc
+++ b/test/kdm_test.cc
@@ -69,3 +69,33 @@ BOOST_AUTO_TEST_CASE (kdm_passthrough_test)
BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
#endif
}
+
+/** Test some of the utility methods of DecryptedKDM */
+BOOST_AUTO_TEST_CASE (decrypted_kdm_test)
+{
+ uint8_t* data = new uint8_t[16];
+ uint8_t* p = data;
+ dcp::DecryptedKDM::put_uuid (&p, "8971c838-d0c3-405d-bc57-43afa9d91242");
+
+ BOOST_CHECK_EQUAL (data[0], 0x89);
+ BOOST_CHECK_EQUAL (data[1], 0x71);
+ BOOST_CHECK_EQUAL (data[2], 0xc8);
+ BOOST_CHECK_EQUAL (data[3], 0x38);
+ BOOST_CHECK_EQUAL (data[4], 0xd0);
+ BOOST_CHECK_EQUAL (data[5], 0xc3);
+ BOOST_CHECK_EQUAL (data[6], 0x40);
+ BOOST_CHECK_EQUAL (data[7], 0x5d);
+ BOOST_CHECK_EQUAL (data[8], 0xbc);
+ BOOST_CHECK_EQUAL (data[9], 0x57);
+ BOOST_CHECK_EQUAL (data[10], 0x43);
+ BOOST_CHECK_EQUAL (data[11], 0xaf);
+ BOOST_CHECK_EQUAL (data[12], 0xa9);
+ BOOST_CHECK_EQUAL (data[13], 0xd9);
+ BOOST_CHECK_EQUAL (data[14], 0x12);
+ BOOST_CHECK_EQUAL (data[15], 0x42);
+
+ p = data;
+ BOOST_CHECK_EQUAL (dcp::DecryptedKDM::get_uuid (&p), "8971c838-d0c3-405d-bc57-43afa9d91242");
+
+ delete[] data;
+}