Fix missing Key ID when writing details of encrypted Atmos assets to CPL.
[libdcp.git] / src / util.cc
index ac2246c30f851c0abb99146180e43eb6f9eb6a77..f755c21e5903f3ae55df23d6004393eee10b42cf 100644 (file)
@@ -55,6 +55,7 @@
 #include <openssl/sha.h>
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
+#include <boost/foreach.hpp>
 #include <stdexcept>
 #include <iostream>
 #include <iomanip>
@@ -88,6 +89,17 @@ dcp::make_uuid ()
        return string (buffer);
 }
 
+string
+dcp::make_digest (Data data)
+{
+       SHA_CTX sha;
+       SHA1_Init (&sha);
+       SHA1_Update (&sha, data.data().get(), data.size());
+       byte_t byte_buffer[SHA_DIGEST_LENGTH];
+       SHA1_Final (byte_buffer, &sha);
+       char digest[64];
+       return Kumu::base64encode (byte_buffer, SHA_DIGEST_LENGTH, digest, 64);
+}
 
 /** Create a digest for a file.
  *  @param filename File name.
@@ -137,75 +149,6 @@ dcp::make_digest (boost::filesystem::path filename, function<void (float)> progr
        return Kumu::base64encode (byte_buffer, SHA_DIGEST_LENGTH, digest, 64);
 }
 
-/** Convert a content kind to a string which can be used in a
- *  &lt;ContentKind&gt; node.
- *  @param kind ContentKind.
- *  @return string.
- */
-string
-dcp::content_kind_to_string (ContentKind kind)
-{
-       switch (kind) {
-       case FEATURE:
-               return "feature";
-       case SHORT:
-               return "short";
-       case TRAILER:
-               return "trailer";
-       case TEST:
-               return "test";
-       case TRANSITIONAL:
-               return "transitional";
-       case RATING:
-               return "rating";
-       case TEASER:
-               return "teaser";
-       case POLICY:
-               return "policy";
-       case PUBLIC_SERVICE_ANNOUNCEMENT:
-               return "psa";
-       case ADVERTISEMENT:
-               return "advertisement";
-       }
-
-       DCP_ASSERT (false);
-}
-
-/** Convert a string from a &lt;ContentKind&gt; node to a libdcp ContentKind.
- *  Reasonably tolerant about varying case.
- *  @param kind Content kind string.
- *  @return libdcp ContentKind.
- */
-dcp::ContentKind
-dcp::content_kind_from_string (string kind)
-{
-       transform (kind.begin(), kind.end(), kind.begin(), ::tolower);
-
-       if (kind == "feature") {
-               return FEATURE;
-       } else if (kind == "short") {
-               return SHORT;
-       } else if (kind == "trailer") {
-               return TRAILER;
-       } else if (kind == "test") {
-               return TEST;
-       } else if (kind == "transitional") {
-               return TRANSITIONAL;
-       } else if (kind == "rating") {
-               return RATING;
-       } else if (kind == "teaser") {
-               return TEASER;
-       } else if (kind == "policy") {
-               return POLICY;
-       } else if (kind == "psa") {
-               return PUBLIC_SERVICE_ANNOUNCEMENT;
-       } else if (kind == "advertisement") {
-               return ADVERTISEMENT;
-       }
-
-       DCP_ASSERT (false);
-}
-
 /** @param s A string.
  *  @return true if the string contains only space, newline or tab characters, or is empty.
  */
@@ -232,8 +175,8 @@ dcp::init ()
        }
 
 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
-       if (xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
-               throw MiscError ("unable to load default xmlsec-crypto library");
+       if (xmlSecCryptoDLLoadLibrary(BAD_CAST "openssl") < 0) {
+               throw MiscError ("unable to load openssl xmlsec-crypto library");
        }
 #endif
 
@@ -406,3 +349,81 @@ dcp::openjpeg_version ()
 {
        return opj_version ();
 }
+
+string
+dcp::spaces (int n)
+{
+       string s = "";
+       for (int i = 0; i < n; ++i) {
+               s += " ";
+       }
+       return s;
+}
+
+void
+dcp::indent (xmlpp::Element* element, int initial)
+{
+       xmlpp::Node* last = 0;
+       BOOST_FOREACH (xmlpp::Node * n, element->get_children()) {
+               xmlpp::Element* e = dynamic_cast<xmlpp::Element*>(n);
+               if (e) {
+                       element->add_child_text_before (e, "\n" + spaces(initial + 2));
+                       indent (e, initial + 2);
+                       last = n;
+               }
+       }
+       if (last) {
+               element->add_child_text (last, "\n" + spaces(initial));
+       }
+}
+
+/** @return true if the day represented by \ref a is less than or
+ *  equal to the one represented by \ref b, ignoring the time parts.
+ */
+bool
+dcp::day_less_than_or_equal (LocalTime a, LocalTime b)
+{
+       if (a.year() != b.year()) {
+               return a.year() < b.year();
+       }
+
+       if (a.month() != b.month()) {
+               return a.month() < b.month();
+       }
+
+       return a.day() <= b.day();
+}
+
+/** @return true if the day represented by \ref a is greater than or
+ *  equal to the one represented by \ref b, ignoring the time parts.
+ */
+bool
+dcp::day_greater_than_or_equal (LocalTime a, LocalTime b)
+{
+       if (a.year() != b.year()) {
+               return a.year() > b.year();
+       }
+
+       if (a.month() != b.month()) {
+               return a.month() > b.month();
+       }
+
+       return a.day() >= b.day();
+}
+
+/** Try quite hard to find a string which starts with \ref base and is
+ *  not in \ref existing.
+ */
+string
+dcp::unique_string (list<string> existing, string base)
+{
+       int const max_tries = existing.size() + 1;
+       for (int i = 0; i < max_tries; ++i) {
+               string trial = String::compose("%1%2", base, i);
+               if (find(existing.begin(), existing.end(), trial) == existing.end()) {
+                       return trial;
+               }
+       }
+
+       DCP_ASSERT (false);
+}