+ BIO* bmem = BIO_new_mem_buf (in_buffer, p - in_buffer);
+ bmem = BIO_push (b64, bmem);
+ int const N = BIO_read (bmem, out, out_length);
+ BIO_free_all (bmem);
+
+ return N;
+}
+
+/** @param p Path to open.
+ * @param t mode flags, as for fopen(3).
+ * @return FILE pointer or 0 on error.
+ *
+ * Apparently there is no way to create an ofstream using a UTF-8
+ * filename under Windows. We are hence reduced to using fopen
+ * with this wrapper.
+ */
+FILE *
+dcp::fopen_boost (boost::filesystem::path p, string t)
+{
+#ifdef LIBDCP_WINDOWS
+ wstring w (t.begin(), t.end());
+ /* c_str() here should give a UTF-16 string */
+ return _wfopen (p.c_str(), w.c_str ());
+#else
+ return fopen (p.c_str(), t.c_str ());
+#endif
+}
+
+optional<boost::filesystem::path>
+dcp::relative_to_root (boost::filesystem::path root, boost::filesystem::path file)
+{
+ boost::filesystem::path::const_iterator i = root.begin ();
+ boost::filesystem::path::const_iterator j = file.begin ();
+
+ while (i != root.end() && j != file.end() && *i == *j) {
+ ++i;
+ ++j;
+ }
+
+ if (i != root.end ()) {
+ return optional<boost::filesystem::path> ();
+ }
+
+ boost::filesystem::path rel;
+ while (j != file.end ()) {
+ rel /= *j++;
+ }
+
+ 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);
+}
+
+xmlpp::Node *
+dcp::find_child (xmlpp::Node const * node, string name)
+{
+ xmlpp::Node::NodeList c = node->get_children ();
+ xmlpp::Node::NodeList::iterator i = c.begin();
+ while (i != c.end() && (*i)->get_name() != name) {
+ ++i;