+ 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;
+}
+
+/** 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.
+ *
+ * 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
+}
+
+boost::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 boost::optional<boost::filesystem::path> ();
+ }
+
+ boost::filesystem::path rel;
+ while (j != file.end ()) {
+ rel /= *j++;