Change colourspace handling round a bit:
[libdcp.git] / src / util.cc
index 1e32fbc9f6ce36ea95eaaa36b0751bc373a0c667..58eacbfbca1cc0b7847db8d85a601652a8dd9131 100644 (file)
@@ -26,7 +26,6 @@
 #include "types.h"
 #include "argb_frame.h"
 #include "certificates.h"
-#include "gamma_lut.h"
 #include "xyz_frame.h"
 #include "compose.hpp"
 #include "KM_util.h"
@@ -40,6 +39,7 @@
 #include <libxml++/document.h>
 #include <openssl/sha.h>
 #include <boost/filesystem.hpp>
+#include <boost/algorithm/string.hpp>
 #include <stdexcept>
 #include <sstream>
 #include <iostream>
@@ -58,6 +58,7 @@ using std::ostream;
 using boost::shared_ptr;
 using boost::optional;
 using boost::function;
+using boost::algorithm::trim;
 using namespace dcp;
 
 /** Create a UUID.
@@ -314,66 +315,6 @@ dcp::base64_decode (string const & in, unsigned char* out, int out_length)
        return N;
 }
 
-/** Convert a struct tm to a string of the form
- *  2014-01-26T21:39:00+01:00
- *  @param tm struct tm.
- *  @return Time as a string.
- */
-string
-dcp::tm_to_string (struct tm* tm)
-{
-       char buffer[64];
-       strftime (buffer, 64, "%Y-%m-%dT%H:%M:%S", tm);
-
-       int offset = 0;
-
-#ifdef LIBDCP_POSIX
-       offset = tm->tm_gmtoff / 60;
-#else
-       TIME_ZONE_INFORMATION tz;
-       GetTimeZoneInformation (&tz);
-       offset = tz.Bias;
-#endif
-       
-       return string (buffer) + utc_offset_to_string (offset);
-}
-
-/** @param b Offset from UTC to local time in minutes.
- *  @return string of the form e.g. -01:00.
- */
-string
-dcp::utc_offset_to_string (int b)
-{
-       bool const negative = (b < 0);
-       b = negative ? -b : b;
-
-       int const hours = b / 60;
-       int const minutes = b % 60;
-
-       stringstream o;
-       if (negative) {
-               o << "-";
-       } else {
-               o << "+";
-       }
-
-       o << setw(2) << setfill('0') << hours << ":" << setw(2) << setfill('0') << minutes;
-       return o.str ();
-}
-
-/** Convert a boost::posix_time::ptime to a string of the form
- *  2014-01-26T21:39:00+01:00.
- *  @param t boost::posix_time::ptime.
- *  @return Time as a string.
- */
-string
-dcp::ptime_to_string (boost::posix_time::ptime t)
-{
-       struct tm t_tm = boost::posix_time::to_tm (t);
-       return tm_to_string (&t_tm);
-}
-
-
 /** @param p Path to open.
  *  @param t mode flags, as for fopen(3).
  *  @return FILE pointer or 0 on error.
@@ -416,3 +357,60 @@ dcp::relative_to_root (boost::filesystem::path root, boost::filesystem::path fil
 
        return rel;
 }
+
+bool
+dcp::ids_equal (string a, string b)
+{
+       transform (a.begin(), a.end(), a.begin(), ::tolower);
+       transform (b.begin(), b.end(), b.begin(), ::tolower);
+       trim (a);
+       trim (b);
+       return a == b;
+}
+
+string
+dcp::file_to_string (boost::filesystem::path p, uintmax_t max_length)
+{
+       uintmax_t len = boost::filesystem::file_size (p);
+       if (len > max_length) {
+               throw MiscError ("Unexpectedly long file");
+       }
+       
+       char* c = new char[len + 1];
+                          
+       FILE* f = fopen_boost (p, "r");
+       if (!f) {
+               return "";
+       }
+
+       fread (c, 1, len, f);
+       fclose (f);
+       c[len] = '\0';
+
+       string s (c);
+       delete[] c;
+
+       return s;
+}
+
+/** @param key RSA private key in PEM format (optionally with -----BEGIN... / -----END...)
+ *  @return SHA1 fingerprint of key
+ */
+string
+dcp::private_key_fingerprint (string key)
+{
+       boost::replace_all (key, "-----BEGIN RSA PRIVATE KEY-----\n", "");
+       boost::replace_all (key, "\n-----END RSA PRIVATE KEY-----\n", "");
+
+       unsigned char buffer[4096];
+       int const N = base64_decode (key, buffer, sizeof (buffer));
+
+       SHA_CTX sha;
+       SHA1_Init (&sha);
+       SHA1_Update (&sha, buffer, N);
+       uint8_t digest[20];
+       SHA1_Final (digest, &sha);
+
+       char digest_base64[64];
+       return Kumu::base64encode (digest, 20, digest_base64, 64);
+}