X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Futil.cc;h=badbda4ab685c775f9633eb5ce4b7ebae661f9ad;hb=53f24c9f2c0c9b3d2a835e11def36c31a4072759;hp=305aae4f8211a500104b2b034609915390a267fe;hpb=5f1046a2164fff00a6fc74aecf4cacbca531d415;p=dcpomatic.git diff --git a/src/lib/util.cc b/src/lib/util.cc index 305aae4f8..badbda4ab 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -45,6 +45,9 @@ #include #include #include +#include +#include +#include extern "C" { #include #include @@ -61,6 +64,8 @@ extern "C" { #include "sound_processor.h" #include "config.h" #include "ratio.h" +#include "job.h" +#include "cross.h" #ifdef DCPOMATIC_WINDOWS #include "stack.hpp" #endif @@ -119,12 +124,6 @@ seconds_to_hms (int s) return hms.str (); } -string -time_to_hms (Time t) -{ - return seconds_to_hms (t / TIME_HZ); -} - /** @param s Number of seconds. * @return String containing an approximate description of s (e.g. "about 2 hours") */ @@ -207,15 +206,11 @@ void stacktrace (ostream& out, int levels) { void *array[200]; - size_t size; - char **strings; - size_t i; - - size = backtrace (array, 200); - strings = backtrace_symbols (array, size); + size_t size = backtrace (array, 200); + char** strings = backtrace_symbols (array, size); if (strings) { - for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) { + for (size_t i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) { out << N_(" ") << demangle (strings[i]) << "\n"; } @@ -283,6 +278,8 @@ dcpomatic_setup () #endif avfilter_register_all (); + + libdcp::init (); Ratio::setup_ratios (); DCPContentType::setup_dcp_content_types (); @@ -417,6 +414,58 @@ md5_digest (boost::filesystem::path file) return s.str (); } +/** @param job Optional job for which to report progress */ +string +md5_digest_directory (boost::filesystem::path directory, shared_ptr job) +{ + int const buffer_size = 64 * 1024; + char buffer[buffer_size]; + + MD5_CTX md5_context; + MD5_Init (&md5_context); + + int files = 0; + if (job) { + for (boost::filesystem::directory_iterator i(directory); i != boost::filesystem::directory_iterator(); ++i) { + ++files; + } + } + + int j = 0; + for (boost::filesystem::directory_iterator i(directory); i != boost::filesystem::directory_iterator(); ++i) { + ifstream f (i->path().string().c_str(), std::ios::binary); + if (!f.good ()) { + throw OpenFileError (i->path().string()); + } + + f.seekg (0, std::ios::end); + int bytes = f.tellg (); + f.seekg (0, std::ios::beg); + + while (bytes > 0) { + int const t = min (bytes, buffer_size); + f.read (buffer, t); + MD5_Update (&md5_context, buffer, t); + bytes -= t; + } + + if (job) { + job->set_progress (float (j) / files); + ++j; + } + } + + unsigned char digest[MD5_DIGEST_LENGTH]; + MD5_Final (digest, &md5_context); + + stringstream s; + for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) { + s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]); + } + + return s.str (); +} + static bool about_equal (float a, float b) { @@ -565,12 +614,6 @@ stride_round_up (int c, int const * stride, int t) return a - (a % t); } -int -stride_lookup (int c, int const * stride) -{ - return stride[c]; -} - /** Read a sequence of key / value pairs from a text stream; * the keys are the first words on the line, and the values are * the remainder of the line following the key. Lines beginning @@ -720,17 +763,17 @@ FrameRateConversion::FrameRateConversion (float source, int dcp) change_speed = !about_equal (source * factor(), dcp); if (!skip && !repeat && !change_speed) { - description = _("DCP and source have the same rate.\n"); + description = _("Content and DCP have the same rate.\n"); } else { if (skip) { - description = _("DCP will use every other frame of the source.\n"); + description = _("DCP will use every other frame of the content.\n"); } else if (repeat) { - description = _("Each source frame will be doubled in the DCP.\n"); + description = _("Each content frame will be doubled in the DCP.\n"); } if (change_speed) { float const pc = dcp * 100 / (source * factor()); - description += String::compose (_("DCP will run at %1%% of the source speed.\n"), pc); + description += String::compose (_("DCP will run at %1%% of the content speed.\n"), pc); } } } @@ -740,12 +783,12 @@ LocaleGuard::LocaleGuard () { char const * old = setlocale (LC_NUMERIC, 0); - if (old) { - _old = strdup (old); - if (strcmp (_old, "C")) { - setlocale (LC_NUMERIC, "C"); - } - } + if (old) { + _old = strdup (old); + if (strcmp (_old, "C")) { + setlocale (LC_NUMERIC, "C"); + } + } } LocaleGuard::~LocaleGuard () @@ -753,3 +796,79 @@ LocaleGuard::~LocaleGuard () setlocale (LC_NUMERIC, _old); free (_old); } + +bool +valid_image_file (boost::filesystem::path f) +{ + string ext = f.extension().string(); + transform (ext.begin(), ext.end(), ext.begin(), ::tolower); + return (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".tga"); +} + +string +tidy_for_filename (string f) +{ + string t; + for (size_t i = 0; i < f.length(); ++i) { + if (isalnum (f[i]) || f[i] == '_' || f[i] == '-') { + t += f[i]; + } else { + t += '_'; + } + } + + return t; +} + +shared_ptr +make_signer () +{ + boost::filesystem::path const sd = Config::instance()->signer_chain_directory (); + + /* Remake the chain if any of it is missing */ + + list files; + files.push_back ("ca.self-signed.pem"); + files.push_back ("intermediate.signed.pem"); + files.push_back ("leaf.signed.pem"); + files.push_back ("leaf.key"); + + list::const_iterator i = files.begin(); + while (i != files.end()) { + boost::filesystem::path p (sd); + sd /= *i; + if (!boost::filesystem::exists (sd)) { + boost::filesystem::remove_all (sd); + libdcp::make_signer_chain (sd, openssl_path ()); + break; + } + + ++i; + } + + libdcp::CertificateChain chain; + + { + boost::filesystem::path p (sd); + p /= "ca.self-signed.pem"; + chain.add (shared_ptr (new libdcp::Certificate (p))); + } + + { + boost::filesystem::path p (sd); + p /= "intermediate.signed.pem"; + chain.add (shared_ptr (new libdcp::Certificate (p))); + } + + { + boost::filesystem::path p (sd); + p /= "leaf.signed.pem"; + chain.add (shared_ptr (new libdcp::Certificate (p))); + } + + boost::filesystem::path signer_key (sd); + signer_key /= "leaf.key"; + + return shared_ptr (new libdcp::Signer (chain, signer_key)); +} +