Try to fix openssl use on Windows.
[dcpomatic.git] / src / lib / util.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3     Copyright (C) 2000-2007 Paul Davis
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 /** @file src/lib/util.cc
22  *  @brief Some utility functions and classes.
23  */
24
25 #include <sstream>
26 #include <iomanip>
27 #include <iostream>
28 #include <fstream>
29 #include <climits>
30 #ifdef DCPOMATIC_POSIX
31 #include <execinfo.h>
32 #include <cxxabi.h>
33 #endif
34 #include <libssh/libssh.h>
35 #include <signal.h>
36 #include <boost/algorithm/string.hpp>
37 #include <boost/bind.hpp>
38 #include <boost/lambda/lambda.hpp>
39 #include <boost/lexical_cast.hpp>
40 #include <boost/thread.hpp>
41 #include <boost/filesystem.hpp>
42 #include <glib.h>
43 #include <openjpeg.h>
44 #include <openssl/md5.h>
45 #include <magick/MagickCore.h>
46 #include <magick/version.h>
47 #include <libdcp/version.h>
48 #include <libdcp/util.h>
49 #include <libdcp/signer_chain.h>
50 #include <libdcp/signer.h>
51 extern "C" {
52 #include <libavcodec/avcodec.h>
53 #include <libavformat/avformat.h>
54 #include <libswscale/swscale.h>
55 #include <libavfilter/avfiltergraph.h>
56 #include <libpostproc/postprocess.h>
57 #include <libavutil/pixfmt.h>
58 }
59 #include "util.h"
60 #include "exceptions.h"
61 #include "scaler.h"
62 #include "dcp_content_type.h"
63 #include "filter.h"
64 #include "sound_processor.h"
65 #include "config.h"
66 #include "ratio.h"
67 #include "job.h"
68 #include "cross.h"
69 #ifdef DCPOMATIC_WINDOWS
70 #include "stack.hpp"
71 #endif
72
73 #include "i18n.h"
74
75 using std::string;
76 using std::stringstream;
77 using std::setfill;
78 using std::ostream;
79 using std::endl;
80 using std::vector;
81 using std::hex;
82 using std::setw;
83 using std::ifstream;
84 using std::ios;
85 using std::min;
86 using std::max;
87 using std::list;
88 using std::multimap;
89 using std::istream;
90 using std::numeric_limits;
91 using std::pair;
92 using std::ofstream;
93 using boost::shared_ptr;
94 using boost::thread;
95 using boost::lexical_cast;
96 using boost::optional;
97 using libdcp::Size;
98
99 static boost::thread::id ui_thread;
100 static boost::filesystem::path backtrace_file;
101
102 /** Convert some number of seconds to a string representation
103  *  in hours, minutes and seconds.
104  *
105  *  @param s Seconds.
106  *  @return String of the form H:M:S (where H is hours, M
107  *  is minutes and S is seconds).
108  */
109 string
110 seconds_to_hms (int s)
111 {
112         int m = s / 60;
113         s -= (m * 60);
114         int h = m / 60;
115         m -= (h * 60);
116
117         stringstream hms;
118         hms << h << N_(":");
119         hms.width (2);
120         hms << std::setfill ('0') << m << N_(":");
121         hms.width (2);
122         hms << std::setfill ('0') << s;
123
124         return hms.str ();
125 }
126
127 /** @param s Number of seconds.
128  *  @return String containing an approximate description of s (e.g. "about 2 hours")
129  */
130 string
131 seconds_to_approximate_hms (int s)
132 {
133         int m = s / 60;
134         s -= (m * 60);
135         int h = m / 60;
136         m -= (h * 60);
137
138         stringstream ap;
139         
140         if (h > 0) {
141                 if (m > 30) {
142                         ap << (h + 1) << N_(" ") << _("hours");
143                 } else {
144                         if (h == 1) {
145                                 ap << N_("1 ") << _("hour");
146                         } else {
147                                 ap << h << N_(" ") << _("hours");
148                         }
149                 }
150         } else if (m > 0) {
151                 if (m == 1) {
152                         ap << N_("1 ") << _("minute");
153                 } else {
154                         ap << m << N_(" ") << _("minutes");
155                 }
156         } else {
157                 ap << s << N_(" ") << _("seconds");
158         }
159
160         return ap.str ();
161 }
162
163 #ifdef DCPOMATIC_POSIX
164 /** @param l Mangled C++ identifier.
165  *  @return Demangled version.
166  */
167 static string
168 demangle (string l)
169 {
170         string::size_type const b = l.find_first_of (N_("("));
171         if (b == string::npos) {
172                 return l;
173         }
174
175         string::size_type const p = l.find_last_of (N_("+"));
176         if (p == string::npos) {
177                 return l;
178         }
179
180         if ((p - b) <= 1) {
181                 return l;
182         }
183         
184         string const fn = l.substr (b + 1, p - b - 1);
185
186         int status;
187         try {
188                 
189                 char* realname = abi::__cxa_demangle (fn.c_str(), 0, 0, &status);
190                 string d (realname);
191                 free (realname);
192                 return d;
193                 
194         } catch (std::exception) {
195                 
196         }
197         
198         return l;
199 }
200
201 /** Write a stacktrace to an ostream.
202  *  @param out Stream to write to.
203  *  @param levels Number of levels to go up the call stack.
204  */
205 void
206 stacktrace (ostream& out, int levels)
207 {
208         void *array[200];
209         size_t size = backtrace (array, 200);
210         char** strings = backtrace_symbols (array, size);
211      
212         if (strings) {
213                 for (size_t i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
214                         out << N_("  ") << demangle (strings[i]) << "\n";
215                 }
216                 
217                 free (strings);
218         }
219 }
220 #endif
221
222 /** @param v Version as used by FFmpeg.
223  *  @return A string representation of v.
224  */
225 static string
226 ffmpeg_version_to_string (int v)
227 {
228         stringstream s;
229         s << ((v & 0xff0000) >> 16) << N_(".") << ((v & 0xff00) >> 8) << N_(".") << (v & 0xff);
230         return s.str ();
231 }
232
233 /** Return a user-readable string summarising the versions of our dependencies */
234 string
235 dependency_version_summary ()
236 {
237         stringstream s;
238         s << N_("libopenjpeg ") << opj_version () << N_(", ")
239           << N_("libavcodec ") << ffmpeg_version_to_string (avcodec_version()) << N_(", ")
240           << N_("libavfilter ") << ffmpeg_version_to_string (avfilter_version()) << N_(", ")
241           << N_("libavformat ") << ffmpeg_version_to_string (avformat_version()) << N_(", ")
242           << N_("libavutil ") << ffmpeg_version_to_string (avutil_version()) << N_(", ")
243           << N_("libpostproc ") << ffmpeg_version_to_string (postproc_version()) << N_(", ")
244           << N_("libswscale ") << ffmpeg_version_to_string (swscale_version()) << N_(", ")
245           << MagickVersion << N_(", ")
246           << N_("libssh ") << ssh_version (0) << N_(", ")
247           << N_("libdcp ") << libdcp::version << N_(" git ") << libdcp::git_commit;
248
249         return s.str ();
250 }
251
252 double
253 seconds (struct timeval t)
254 {
255         return t.tv_sec + (double (t.tv_usec) / 1e6);
256 }
257
258 #ifdef DCPOMATIC_WINDOWS
259 LONG WINAPI exception_handler(struct _EXCEPTION_POINTERS *)
260 {
261         dbg::stack s;
262         ofstream f (backtrace_file.string().c_str());
263         std::copy(s.begin(), s.end(), std::ostream_iterator<dbg::stack_frame>(f, "\n"));
264         return EXCEPTION_CONTINUE_SEARCH;
265 }
266 #endif
267
268 /** Call the required functions to set up DCP-o-matic's static arrays, etc.
269  *  Must be called from the UI thread, if there is one.
270  */
271 void
272 dcpomatic_setup ()
273 {
274 #ifdef DCPOMATIC_WINDOWS
275         backtrace_file /= g_get_user_config_dir ();
276         backtrace_file /= "backtrace.txt";
277         SetUnhandledExceptionFilter(exception_handler);
278 #endif  
279         
280         avfilter_register_all ();
281
282         libdcp::init ();
283         
284         Ratio::setup_ratios ();
285         DCPContentType::setup_dcp_content_types ();
286         Scaler::setup_scalers ();
287         Filter::setup_filters ();
288         SoundProcessor::setup_sound_processors ();
289
290         ui_thread = boost::this_thread::get_id ();
291 }
292
293 #ifdef DCPOMATIC_WINDOWS
294 boost::filesystem::path
295 mo_path ()
296 {
297         wchar_t buffer[512];
298         GetModuleFileName (0, buffer, 512 * sizeof(wchar_t));
299         boost::filesystem::path p (buffer);
300         p = p.parent_path ();
301         p = p.parent_path ();
302         p /= "locale";
303         return p;
304 }
305 #endif
306
307 void
308 dcpomatic_setup_gettext_i18n (string lang)
309 {
310 #ifdef DCPOMATIC_POSIX
311         lang += ".UTF8";
312 #endif
313
314         if (!lang.empty ()) {
315                 /* Override our environment language; this is essential on
316                    Windows.
317                 */
318                 char cmd[64];
319                 snprintf (cmd, sizeof(cmd), "LANGUAGE=%s", lang.c_str ());
320                 putenv (cmd);
321                 snprintf (cmd, sizeof(cmd), "LANG=%s", lang.c_str ());
322                 putenv (cmd);
323         }
324
325         setlocale (LC_ALL, "");
326         textdomain ("libdcpomatic");
327
328 #ifdef DCPOMATIC_WINDOWS
329         bindtextdomain ("libdcpomatic", mo_path().string().c_str());
330         bind_textdomain_codeset ("libdcpomatic", "UTF8");
331 #endif  
332
333 #ifdef DCPOMATIC_POSIX
334         bindtextdomain ("libdcpomatic", POSIX_LOCALE_PREFIX);
335 #endif
336 }
337
338 /** @param s A string.
339  *  @return Parts of the string split at spaces, except when a space is within quotation marks.
340  */
341 vector<string>
342 split_at_spaces_considering_quotes (string s)
343 {
344         vector<string> out;
345         bool in_quotes = false;
346         string c;
347         for (string::size_type i = 0; i < s.length(); ++i) {
348                 if (s[i] == ' ' && !in_quotes) {
349                         out.push_back (c);
350                         c = N_("");
351                 } else if (s[i] == '"') {
352                         in_quotes = !in_quotes;
353                 } else {
354                         c += s[i];
355                 }
356         }
357
358         out.push_back (c);
359         return out;
360 }
361
362 string
363 md5_digest (void const * data, int size)
364 {
365         MD5_CTX md5_context;
366         MD5_Init (&md5_context);
367         MD5_Update (&md5_context, data, size);
368         unsigned char digest[MD5_DIGEST_LENGTH];
369         MD5_Final (digest, &md5_context);
370         
371         stringstream s;
372         for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
373                 s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
374         }
375
376         return s.str ();
377 }
378
379 /** @param file File name.
380  *  @return MD5 digest of file's contents.
381  */
382 string
383 md5_digest (boost::filesystem::path file)
384 {
385         ifstream f (file.string().c_str(), std::ios::binary);
386         if (!f.good ()) {
387                 throw OpenFileError (file.string());
388         }
389         
390         f.seekg (0, std::ios::end);
391         int bytes = f.tellg ();
392         f.seekg (0, std::ios::beg);
393
394         int const buffer_size = 64 * 1024;
395         char buffer[buffer_size];
396
397         MD5_CTX md5_context;
398         MD5_Init (&md5_context);
399         while (bytes > 0) {
400                 int const t = min (bytes, buffer_size);
401                 f.read (buffer, t);
402                 MD5_Update (&md5_context, buffer, t);
403                 bytes -= t;
404         }
405
406         unsigned char digest[MD5_DIGEST_LENGTH];
407         MD5_Final (digest, &md5_context);
408
409         stringstream s;
410         for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
411                 s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
412         }
413
414         return s.str ();
415 }
416
417 /** @param job Optional job for which to report progress */
418 string
419 md5_digest_directory (boost::filesystem::path directory, shared_ptr<Job> job)
420 {
421         int const buffer_size = 64 * 1024;
422         char buffer[buffer_size];
423
424         MD5_CTX md5_context;
425         MD5_Init (&md5_context);
426
427         int files = 0;
428         if (job) {
429                 for (boost::filesystem::directory_iterator i(directory); i != boost::filesystem::directory_iterator(); ++i) {
430                         ++files;
431                 }
432         }
433
434         int j = 0;
435         for (boost::filesystem::directory_iterator i(directory); i != boost::filesystem::directory_iterator(); ++i) {
436                 ifstream f (i->path().string().c_str(), std::ios::binary);
437                 if (!f.good ()) {
438                         throw OpenFileError (i->path().string());
439                 }
440         
441                 f.seekg (0, std::ios::end);
442                 int bytes = f.tellg ();
443                 f.seekg (0, std::ios::beg);
444
445                 while (bytes > 0) {
446                         int const t = min (bytes, buffer_size);
447                         f.read (buffer, t);
448                         MD5_Update (&md5_context, buffer, t);
449                         bytes -= t;
450                 }
451
452                 if (job) {
453                         job->set_progress (float (j) / files);
454                         ++j;
455                 }
456         }
457
458         unsigned char digest[MD5_DIGEST_LENGTH];
459         MD5_Final (digest, &md5_context);
460
461         stringstream s;
462         for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
463                 s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
464         }
465
466         return s.str ();
467 }
468
469 static bool
470 about_equal (float a, float b)
471 {
472         /* A film of F seconds at f FPS will be Ff frames;
473            Consider some delta FPS d, so if we run the same
474            film at (f + d) FPS it will last F(f + d) seconds.
475
476            Hence the difference in length over the length of the film will
477            be F(f + d) - Ff frames
478             = Ff + Fd - Ff frames
479             = Fd frames
480             = Fd/f seconds
481  
482            So if we accept a difference of 1 frame, ie 1/f seconds, we can
483            say that
484
485            1/f = Fd/f
486         ie 1 = Fd
487         ie d = 1/F
488  
489            So for a 3hr film, ie F = 3 * 60 * 60 = 10800, the acceptable
490            FPS error is 1/F ~= 0.0001 ~= 10-e4
491         */
492
493         return (fabs (a - b) < 1e-4);
494 }
495
496 /** @param An arbitrary audio frame rate.
497  *  @return The appropriate DCP-approved frame rate (48kHz or 96kHz).
498  */
499 int
500 dcp_audio_frame_rate (int fs)
501 {
502         if (fs <= 48000) {
503                 return 48000;
504         }
505
506         return 96000;
507 }
508
509 Socket::Socket (int timeout)
510         : _deadline (_io_service)
511         , _socket (_io_service)
512         , _timeout (timeout)
513 {
514         _deadline.expires_at (boost::posix_time::pos_infin);
515         check ();
516 }
517
518 void
519 Socket::check ()
520 {
521         if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) {
522                 _socket.close ();
523                 _deadline.expires_at (boost::posix_time::pos_infin);
524         }
525
526         _deadline.async_wait (boost::bind (&Socket::check, this));
527 }
528
529 /** Blocking connect.
530  *  @param endpoint End-point to connect to.
531  */
532 void
533 Socket::connect (boost::asio::ip::basic_resolver_entry<boost::asio::ip::tcp> const & endpoint)
534 {
535         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
536         boost::system::error_code ec = boost::asio::error::would_block;
537         _socket.async_connect (endpoint, boost::lambda::var(ec) = boost::lambda::_1);
538         do {
539                 _io_service.run_one();
540         } while (ec == boost::asio::error::would_block);
541
542         if (ec || !_socket.is_open ()) {
543                 throw NetworkError (_("connect timed out"));
544         }
545 }
546
547 /** Blocking write.
548  *  @param data Buffer to write.
549  *  @param size Number of bytes to write.
550  */
551 void
552 Socket::write (uint8_t const * data, int size)
553 {
554         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
555         boost::system::error_code ec = boost::asio::error::would_block;
556
557         boost::asio::async_write (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
558         
559         do {
560                 _io_service.run_one ();
561         } while (ec == boost::asio::error::would_block);
562
563         if (ec) {
564                 throw NetworkError (ec.message ());
565         }
566 }
567
568 void
569 Socket::write (uint32_t v)
570 {
571         v = htonl (v);
572         write (reinterpret_cast<uint8_t*> (&v), 4);
573 }
574
575 /** Blocking read.
576  *  @param data Buffer to read to.
577  *  @param size Number of bytes to read.
578  */
579 void
580 Socket::read (uint8_t* data, int size)
581 {
582         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
583         boost::system::error_code ec = boost::asio::error::would_block;
584
585         boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
586
587         do {
588                 _io_service.run_one ();
589         } while (ec == boost::asio::error::would_block);
590         
591         if (ec) {
592                 throw NetworkError (ec.message ());
593         }
594 }
595
596 uint32_t
597 Socket::read_uint32 ()
598 {
599         uint32_t v;
600         read (reinterpret_cast<uint8_t *> (&v), 4);
601         return ntohl (v);
602 }
603
604 /** Round a number up to the nearest multiple of another number.
605  *  @param c Index.
606  *  @param s Array of numbers to round, indexed by c.
607  *  @param t Multiple to round to.
608  *  @return Rounded number.
609  */
610 int
611 stride_round_up (int c, int const * stride, int t)
612 {
613         int const a = stride[c] + (t - 1);
614         return a - (a % t);
615 }
616
617 /** Read a sequence of key / value pairs from a text stream;
618  *  the keys are the first words on the line, and the values are
619  *  the remainder of the line following the key.  Lines beginning
620  *  with # are ignored.
621  *  @param s Stream to read.
622  *  @return key/value pairs.
623  */
624 multimap<string, string>
625 read_key_value (istream &s) 
626 {
627         multimap<string, string> kv;
628         
629         string line;
630         while (getline (s, line)) {
631                 if (line.empty ()) {
632                         continue;
633                 }
634
635                 if (line[0] == '#') {
636                         continue;
637                 }
638
639                 if (line[line.size() - 1] == '\r') {
640                         line = line.substr (0, line.size() - 1);
641                 }
642
643                 size_t const s = line.find (' ');
644                 if (s == string::npos) {
645                         continue;
646                 }
647
648                 kv.insert (make_pair (line.substr (0, s), line.substr (s + 1)));
649         }
650
651         return kv;
652 }
653
654 string
655 get_required_string (multimap<string, string> const & kv, string k)
656 {
657         if (kv.count (k) > 1) {
658                 throw StringError (N_("unexpected multiple keys in key-value set"));
659         }
660
661         multimap<string, string>::const_iterator i = kv.find (k);
662         
663         if (i == kv.end ()) {
664                 throw StringError (String::compose (_("missing key %1 in key-value set"), k));
665         }
666
667         return i->second;
668 }
669
670 int
671 get_required_int (multimap<string, string> const & kv, string k)
672 {
673         string const v = get_required_string (kv, k);
674         return lexical_cast<int> (v);
675 }
676
677 float
678 get_required_float (multimap<string, string> const & kv, string k)
679 {
680         string const v = get_required_string (kv, k);
681         return lexical_cast<float> (v);
682 }
683
684 string
685 get_optional_string (multimap<string, string> const & kv, string k)
686 {
687         if (kv.count (k) > 1) {
688                 throw StringError (N_("unexpected multiple keys in key-value set"));
689         }
690
691         multimap<string, string>::const_iterator i = kv.find (k);
692         if (i == kv.end ()) {
693                 return N_("");
694         }
695
696         return i->second;
697 }
698
699 int
700 get_optional_int (multimap<string, string> const & kv, string k)
701 {
702         if (kv.count (k) > 1) {
703                 throw StringError (N_("unexpected multiple keys in key-value set"));
704         }
705
706         multimap<string, string>::const_iterator i = kv.find (k);
707         if (i == kv.end ()) {
708                 return 0;
709         }
710
711         return lexical_cast<int> (i->second);
712 }
713
714 /** Trip an assert if the caller is not in the UI thread */
715 void
716 ensure_ui_thread ()
717 {
718         assert (boost::this_thread::get_id() == ui_thread);
719 }
720
721 /** @param v Content video frame.
722  *  @param audio_sample_rate Source audio sample rate.
723  *  @param frames_per_second Number of video frames per second.
724  *  @return Equivalent number of audio frames for `v'.
725  */
726 int64_t
727 video_frames_to_audio_frames (VideoContent::Frame v, float audio_sample_rate, float frames_per_second)
728 {
729         return ((int64_t) v * audio_sample_rate / frames_per_second);
730 }
731
732 string
733 audio_channel_name (int c)
734 {
735         assert (MAX_AUDIO_CHANNELS == 6);
736
737         /* TRANSLATORS: these are the names of audio channels; Lfe (sub) is the low-frequency
738            enhancement channel (sub-woofer)./
739         */
740         string const channels[] = {
741                 _("Left"),
742                 _("Right"),
743                 _("Centre"),
744                 _("Lfe (sub)"),
745                 _("Left surround"),
746                 _("Right surround"),
747         };
748
749         return channels[c];
750 }
751
752 FrameRateConversion::FrameRateConversion (float source, int dcp)
753         : skip (false)
754         , repeat (false)
755         , change_speed (false)
756 {
757         if (fabs (source / 2.0 - dcp) < (fabs (source - dcp))) {
758                 skip = true;
759         } else if (fabs (source * 2 - dcp) < fabs (source - dcp)) {
760                 repeat = true;
761         }
762
763         change_speed = !about_equal (source * factor(), dcp);
764
765         if (!skip && !repeat && !change_speed) {
766                 description = _("Content and DCP have the same rate.\n");
767         } else {
768                 if (skip) {
769                         description = _("DCP will use every other frame of the content.\n");
770                 } else if (repeat) {
771                         description = _("Each content frame will be doubled in the DCP.\n");
772                 }
773
774                 if (change_speed) {
775                         float const pc = dcp * 100 / (source * factor());
776                         description += String::compose (_("DCP will run at %1%% of the content speed.\n"), pc);
777                 }
778         }
779 }
780
781 LocaleGuard::LocaleGuard ()
782         : _old (0)
783 {
784         char const * old = setlocale (LC_NUMERIC, 0);
785
786         if (old) {
787                 _old = strdup (old);
788                 if (strcmp (_old, "C")) {
789                         setlocale (LC_NUMERIC, "C");
790                 }
791         }
792 }
793
794 LocaleGuard::~LocaleGuard ()
795 {
796         setlocale (LC_NUMERIC, _old);
797         free (_old);
798 }
799
800 bool
801 valid_image_file (boost::filesystem::path f)
802 {
803         string ext = f.extension().string();
804         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
805         return (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".tga");
806 }
807
808 string
809 tidy_for_filename (string f)
810 {
811         string t;
812         for (size_t i = 0; i < f.length(); ++i) {
813                 if (isalnum (f[i]) || f[i] == '_' || f[i] == '-') {
814                         t += f[i];
815                 } else {
816                         t += '_';
817                 }
818         }
819
820         return t;
821 }
822
823 shared_ptr<const libdcp::Signer>
824 make_signer ()
825 {
826         boost::filesystem::path const sd = Config::instance()->signer_chain_directory ();
827         if (boost::filesystem::is_empty (sd)) {
828                 libdcp::make_signer_chain (sd, openssl_path ());
829         }
830
831         libdcp::CertificateChain chain;
832
833         {
834                 boost::filesystem::path p (sd);
835                 p /= "ca.self-signed.pem";
836                 chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
837         }
838
839         {
840                 boost::filesystem::path p (sd);
841                 p /= "intermediate.signed.pem";
842                 chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
843         }
844
845         {
846                 boost::filesystem::path p (sd);
847                 p /= "leaf.signed.pem";
848                 chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
849         }
850
851         boost::filesystem::path signer_key (sd);
852         signer_key /= "leaf.key";
853
854         return shared_ptr<const libdcp::Signer> (new libdcp::Signer (chain, signer_key));
855 }
856