Support more than one closed caption asset per reel.
[libdcp.git] / src / util.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 /** @file  src/util.cc
35  *  @brief Utility methods.
36  */
37
38 #include "util.h"
39 #include "exceptions.h"
40 #include "types.h"
41 #include "certificate.h"
42 #include "openjpeg_image.h"
43 #include "dcp_assert.h"
44 #include "compose.hpp"
45 #include <openjpeg.h>
46 #include <asdcp/KM_util.h>
47 #include <asdcp/KM_fileio.h>
48 #include <asdcp/AS_DCP.h>
49 #include <xmlsec/xmldsig.h>
50 #include <xmlsec/dl.h>
51 #include <xmlsec/app.h>
52 #include <xmlsec/crypto.h>
53 #include <libxml++/nodes/element.h>
54 #include <libxml++/document.h>
55 #include <openssl/sha.h>
56 #include <boost/filesystem.hpp>
57 #include <boost/algorithm/string.hpp>
58 #include <stdexcept>
59 #include <iostream>
60 #include <iomanip>
61
62 using std::string;
63 using std::wstring;
64 using std::cout;
65 using std::min;
66 using std::max;
67 using std::list;
68 using std::setw;
69 using std::setfill;
70 using std::ostream;
71 using boost::shared_ptr;
72 using boost::shared_array;
73 using boost::optional;
74 using boost::function;
75 using boost::algorithm::trim;
76 using namespace dcp;
77
78 /** Create a UUID.
79  *  @return UUID.
80  */
81 string
82 dcp::make_uuid ()
83 {
84         char buffer[64];
85         Kumu::UUID id;
86         Kumu::GenRandomValue (id);
87         id.EncodeHex (buffer, 64);
88         return string (buffer);
89 }
90
91
92 /** Create a digest for a file.
93  *  @param filename File name.
94  *  @param progress Optional progress reporting function.  The function will be called
95  *  with a progress value between 0 and 1.
96  *  @return Digest.
97  */
98 string
99 dcp::make_digest (boost::filesystem::path filename, function<void (float)> progress)
100 {
101         Kumu::FileReader reader;
102         Kumu::Result_t r = reader.OpenRead (filename.string().c_str ());
103         if (ASDCP_FAILURE (r)) {
104                 boost::throw_exception (FileError ("could not open file to compute digest", filename, r));
105         }
106
107         SHA_CTX sha;
108         SHA1_Init (&sha);
109
110         int const buffer_size = 65536;
111         Kumu::ByteString read_buffer (buffer_size);
112
113         Kumu::fsize_t done = 0;
114         Kumu::fsize_t const size = reader.Size ();
115         while (1) {
116                 ui32_t read = 0;
117                 Kumu::Result_t r = reader.Read (read_buffer.Data(), read_buffer.Capacity(), &read);
118
119                 if (r == Kumu::RESULT_ENDOFFILE) {
120                         break;
121                 } else if (ASDCP_FAILURE (r)) {
122                         boost::throw_exception (FileError ("could not read file to compute digest", filename, r));
123                 }
124
125                 SHA1_Update (&sha, read_buffer.Data(), read);
126
127                 if (progress) {
128                         progress (float (done) / size);
129                         done += read;
130                 }
131         }
132
133         byte_t byte_buffer[SHA_DIGEST_LENGTH];
134         SHA1_Final (byte_buffer, &sha);
135
136         char digest[64];
137         return Kumu::base64encode (byte_buffer, SHA_DIGEST_LENGTH, digest, 64);
138 }
139
140 /** Convert a content kind to a string which can be used in a
141  *  &lt;ContentKind&gt; node.
142  *  @param kind ContentKind.
143  *  @return string.
144  */
145 string
146 dcp::content_kind_to_string (ContentKind kind)
147 {
148         switch (kind) {
149         case FEATURE:
150                 return "feature";
151         case SHORT:
152                 return "short";
153         case TRAILER:
154                 return "trailer";
155         case TEST:
156                 return "test";
157         case TRANSITIONAL:
158                 return "transitional";
159         case RATING:
160                 return "rating";
161         case TEASER:
162                 return "teaser";
163         case POLICY:
164                 return "policy";
165         case PUBLIC_SERVICE_ANNOUNCEMENT:
166                 return "psa";
167         case ADVERTISEMENT:
168                 return "advertisement";
169         }
170
171         DCP_ASSERT (false);
172 }
173
174 /** Convert a string from a &lt;ContentKind&gt; node to a libdcp ContentKind.
175  *  Reasonably tolerant about varying case.
176  *  @param kind Content kind string.
177  *  @return libdcp ContentKind.
178  */
179 dcp::ContentKind
180 dcp::content_kind_from_string (string kind)
181 {
182         transform (kind.begin(), kind.end(), kind.begin(), ::tolower);
183
184         if (kind == "feature") {
185                 return FEATURE;
186         } else if (kind == "short") {
187                 return SHORT;
188         } else if (kind == "trailer") {
189                 return TRAILER;
190         } else if (kind == "test") {
191                 return TEST;
192         } else if (kind == "transitional") {
193                 return TRANSITIONAL;
194         } else if (kind == "rating") {
195                 return RATING;
196         } else if (kind == "teaser") {
197                 return TEASER;
198         } else if (kind == "policy") {
199                 return POLICY;
200         } else if (kind == "psa") {
201                 return PUBLIC_SERVICE_ANNOUNCEMENT;
202         } else if (kind == "advertisement") {
203                 return ADVERTISEMENT;
204         }
205
206         DCP_ASSERT (false);
207 }
208
209 /** @param s A string.
210  *  @return true if the string contains only space, newline or tab characters, or is empty.
211  */
212 bool
213 dcp::empty_or_white_space (string s)
214 {
215         for (size_t i = 0; i < s.length(); ++i) {
216                 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
217                         return false;
218                 }
219         }
220
221         return true;
222 }
223
224 /** Set up various bits that the library needs.  Should be called one
225  *  by client applications.
226  */
227 void
228 dcp::init ()
229 {
230         if (xmlSecInit() < 0) {
231                 throw MiscError ("could not initialise xmlsec");
232         }
233
234 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
235         if (xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
236                 throw MiscError ("unable to load default xmlsec-crypto library");
237         }
238 #endif
239
240         if (xmlSecCryptoAppInit(0) < 0) {
241                 throw MiscError ("could not initialise crypto");
242         }
243
244         if (xmlSecCryptoInit() < 0) {
245                 throw MiscError ("could not initialise xmlsec-crypto");
246         }
247
248         OpenSSL_add_all_algorithms();
249 }
250
251 /** Decode a base64 string.  The base64 decode routine in KM_util.cpp
252  *  gives different values to both this and the command-line base64
253  *  for some inputs.  Not sure why.
254  *
255  *  @param in base64-encoded string.
256  *  @param out Output buffer.
257  *  @param out_length Length of output buffer.
258  *  @return Number of characters written to the output buffer.
259  */
260 int
261 dcp::base64_decode (string const & in, unsigned char* out, int out_length)
262 {
263         BIO* b64 = BIO_new (BIO_f_base64 ());
264
265         /* This means the input should have no newlines */
266         BIO_set_flags (b64, BIO_FLAGS_BASE64_NO_NL);
267
268         /* Copy our input string, removing newlines */
269         char in_buffer[in.size() + 1];
270         char* p = in_buffer;
271         for (size_t i = 0; i < in.size(); ++i) {
272                 if (in[i] != '\n' && in[i] != '\r') {
273                         *p++ = in[i];
274                 }
275         }
276
277         BIO* bmem = BIO_new_mem_buf (in_buffer, p - in_buffer);
278         bmem = BIO_push (b64, bmem);
279         int const N = BIO_read (bmem, out, out_length);
280         BIO_free_all (bmem);
281
282         return N;
283 }
284
285 /** @param p Path to open.
286  *  @param t mode flags, as for fopen(3).
287  *  @return FILE pointer or 0 on error.
288  *
289  *  Apparently there is no way to create an ofstream using a UTF-8
290  *  filename under Windows.  We are hence reduced to using fopen
291  *  with this wrapper.
292  */
293 FILE *
294 dcp::fopen_boost (boost::filesystem::path p, string t)
295 {
296 #ifdef LIBDCP_WINDOWS
297         wstring w (t.begin(), t.end());
298         /* c_str() here should give a UTF-16 string */
299         return _wfopen (p.c_str(), w.c_str ());
300 #else
301         return fopen (p.c_str(), t.c_str ());
302 #endif
303 }
304
305 optional<boost::filesystem::path>
306 dcp::relative_to_root (boost::filesystem::path root, boost::filesystem::path file)
307 {
308         boost::filesystem::path::const_iterator i = root.begin ();
309         boost::filesystem::path::const_iterator j = file.begin ();
310
311         while (i != root.end() && j != file.end() && *i == *j) {
312                 ++i;
313                 ++j;
314         }
315
316         if (i != root.end ()) {
317                 return optional<boost::filesystem::path> ();
318         }
319
320         boost::filesystem::path rel;
321         while (j != file.end ()) {
322                 rel /= *j++;
323         }
324
325         return rel;
326 }
327
328 bool
329 dcp::ids_equal (string a, string b)
330 {
331         transform (a.begin(), a.end(), a.begin(), ::tolower);
332         transform (b.begin(), b.end(), b.begin(), ::tolower);
333         trim (a);
334         trim (b);
335         return a == b;
336 }
337
338 string
339 dcp::file_to_string (boost::filesystem::path p, uintmax_t max_length)
340 {
341         uintmax_t len = boost::filesystem::file_size (p);
342         if (len > max_length) {
343                 throw MiscError (String::compose ("Unexpectedly long file (%1)", p.string()));
344         }
345
346         FILE* f = fopen_boost (p, "r");
347         if (!f) {
348                 throw FileError ("could not open file", p, errno);
349         }
350
351         char* c = new char[len];
352         /* This may read less than `len' if we are on Windows and we have CRLF in the file */
353         int const N = fread (c, 1, len, f);
354         fclose (f);
355
356         string s (c, N);
357         delete[] c;
358
359         return s;
360 }
361
362 /** @param key RSA private key in PEM format (optionally with -----BEGIN... / -----END...)
363  *  @return SHA1 fingerprint of key
364  */
365 string
366 dcp::private_key_fingerprint (string key)
367 {
368         boost::replace_all (key, "-----BEGIN RSA PRIVATE KEY-----\n", "");
369         boost::replace_all (key, "\n-----END RSA PRIVATE KEY-----\n", "");
370
371         unsigned char buffer[4096];
372         int const N = base64_decode (key, buffer, sizeof (buffer));
373
374         SHA_CTX sha;
375         SHA1_Init (&sha);
376         SHA1_Update (&sha, buffer, N);
377         uint8_t digest[20];
378         SHA1_Final (digest, &sha);
379
380         char digest_base64[64];
381         return Kumu::base64encode (digest, 20, digest_base64, 64);
382 }
383
384 xmlpp::Node *
385 dcp::find_child (xmlpp::Node const * node, string name)
386 {
387         xmlpp::Node::NodeList c = node->get_children ();
388         xmlpp::Node::NodeList::iterator i = c.begin();
389         while (i != c.end() && (*i)->get_name() != name) {
390                 ++i;
391         }
392
393         DCP_ASSERT (i != c.end ());
394         return *i;
395 }
396
397 string
398 dcp::remove_urn_uuid (string raw)
399 {
400         DCP_ASSERT (raw.substr(0, 9) == "urn:uuid:");
401         return raw.substr (9);
402 }
403
404 string
405 dcp::openjpeg_version ()
406 {
407         return opj_version ();
408 }