2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * @brief Utility methods.
28 #include <boost/filesystem.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include <openssl/sha.h>
31 #include <libxml++/nodes/element.h>
32 #include <libxml++/document.h>
33 #include <xmlsec/xmldsig.h>
34 #include <xmlsec/dl.h>
35 #include <xmlsec/app.h>
36 #include <xmlsec/crypto.h>
38 #include "KM_fileio.h"
41 #include "exceptions.h"
43 #include "argb_frame.h"
44 #include "certificates.h"
45 #include "gamma_lut.h"
46 #include "xyz_frame.h"
51 using std::stringstream;
57 using boost::shared_ptr;
58 using boost::lexical_cast;
59 using namespace libdcp;
69 Kumu::GenRandomValue (id);
70 id.EncodeHex (buffer, 64);
71 return string (buffer);
75 /** Create a digest for a file.
76 * @param filename File name.
77 * @param progress Pointer to a progress reporting function, or 0. The function will be called
78 * with a progress value between 0 and 1.
82 libdcp::make_digest (string filename, boost::function<void (float)>* progress)
84 Kumu::FileReader reader;
85 if (ASDCP_FAILURE (reader.OpenRead (filename.c_str ()))) {
86 boost::throw_exception (FileError ("could not open file to compute digest", filename));
92 int const buffer_size = 65536;
93 Kumu::ByteString read_buffer (buffer_size);
95 Kumu::fsize_t done = 0;
96 Kumu::fsize_t const size = reader.Size ();
99 Kumu::Result_t r = reader.Read (read_buffer.Data(), read_buffer.Capacity(), &read);
101 if (r == Kumu::RESULT_ENDOFFILE) {
103 } else if (ASDCP_FAILURE (r)) {
104 boost::throw_exception (FileError ("could not read file to compute digest", filename));
107 SHA1_Update (&sha, read_buffer.Data(), read);
110 (*progress) (float (done) / size);
115 byte_t byte_buffer[SHA_DIGEST_LENGTH];
116 SHA1_Final (byte_buffer, &sha);
119 return Kumu::base64encode (byte_buffer, SHA_DIGEST_LENGTH, digest, 64);
122 /** Convert a content kind to a string which can be used in a
123 * <ContentKind> node.
124 * @param kind ContentKind.
128 libdcp::content_kind_to_string (ContentKind kind)
140 return "transitional";
147 case PUBLIC_SERVICE_ANNOUNCEMENT:
150 return "advertisement";
156 /** Convert a string from a <ContentKind> node to a libdcp ContentKind.
157 * Reasonably tolerant about varying case.
158 * @param type Content kind string.
159 * @return libdcp ContentKind.
162 libdcp::content_kind_from_string (string type)
164 /* XXX: should probably just convert type to lower-case and have done with it */
166 if (type == "feature") {
168 } else if (type == "short") {
170 } else if (type == "trailer" || type == "Trailer") {
172 } else if (type == "test") {
174 } else if (type == "transitional") {
176 } else if (type == "rating") {
178 } else if (type == "teaser" || type == "Teaser") {
180 } else if (type == "policy") {
182 } else if (type == "psa") {
183 return PUBLIC_SERVICE_ANNOUNCEMENT;
184 } else if (type == "advertisement") {
185 return ADVERTISEMENT;
191 /** Decompress a JPEG2000 image to a bitmap.
192 * @param data JPEG2000 data.
193 * @param size Size of data in bytes.
194 * @param reduce A power of 2 by which to reduce the size of the decoded image;
195 * e.g. 0 reduces by (2^0 == 1), ie keeping the same size.
196 * 1 reduces by (2^1 == 2), ie halving the size of the image.
197 * This is useful for scaling 4K DCP images down to 2K.
200 shared_ptr<libdcp::XYZFrame>
201 libdcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
203 opj_dinfo_t* decoder = opj_create_decompress (CODEC_J2K);
204 opj_dparameters_t parameters;
205 opj_set_default_decoder_parameters (¶meters);
206 parameters.cp_reduce = reduce;
207 opj_setup_decoder (decoder, ¶meters);
208 opj_cio_t* cio = opj_cio_open ((opj_common_ptr) decoder, data, size);
209 opj_image_t* image = opj_decode (decoder, cio);
211 opj_destroy_decompress (decoder);
213 boost::throw_exception (DCPReadError ("could not decode JPEG2000 codestream of " + lexical_cast<string> (size) + " bytes."));
216 opj_destroy_decompress (decoder);
219 image->x1 = rint (float(image->x1) / pow (2, reduce));
220 image->y1 = rint (float(image->y1) / pow (2, reduce));
221 return shared_ptr<XYZFrame> (new XYZFrame (image));
224 /** @param s A string.
225 * @return true if the string contains only space, newline or tab characters, or is empty.
228 libdcp::empty_or_white_space (string s)
230 for (size_t i = 0; i < s.length(); ++i) {
231 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
242 if (xmlSecInit() < 0) {
243 throw MiscError ("could not initialise xmlsec");
246 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
247 if (xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
248 throw MiscError ("unable to load default xmlsec-crypto library");
252 if (xmlSecCryptoAppInit(0) < 0) {
253 throw MiscError ("could not initialise crypto");
256 if (xmlSecCryptoInit() < 0) {
257 throw MiscError ("could not initialise xmlsec-crypto");
261 bool libdcp::operator== (libdcp::Size const & a, libdcp::Size const & b)
263 return (a.width == b.width && a.height == b.height);
266 bool libdcp::operator!= (libdcp::Size const & a, libdcp::Size const & b)
271 /** The base64 decode routine in KM_util.cpp gives different values to both
272 * this and the command-line base64 for some inputs. Not sure why.
275 libdcp::base64_decode (string const & in, unsigned char* out, int out_length)
277 BIO* b64 = BIO_new (BIO_f_base64 ());
279 /* This means the input should have no newlines */
280 BIO_set_flags (b64, BIO_FLAGS_BASE64_NO_NL);
282 /* Copy our input string, removing newlines */
283 char in_buffer[in.size() + 1];
285 for (size_t i = 0; i < in.size(); ++i) {
286 if (in[i] != '\n' && in[i] != '\r') {
291 BIO* bmem = BIO_new_mem_buf (in_buffer, p - in_buffer);
292 bmem = BIO_push (b64, bmem);
293 int const N = BIO_read (bmem, out, out_length);
300 libdcp::tm_to_string (struct tm* tm)
303 strftime (buffer, 64, "%Y-%m-%dT%H:%M:%S", tm);
308 offset = tm->tm_gmtoff / 60;
310 TIME_ZONE_INFORMATION tz;
311 GetTimeZoneInformation (&tz);
315 return string (buffer) + utc_offset_to_string (offset);
318 /** @param b Offset from UTC to local time in minutes.
319 * @return string of the form e.g. -01:00.
322 libdcp::utc_offset_to_string (int b)
324 bool const negative = (b < 0);
325 b = negative ? -b : b;
327 int const hours = b / 60;
328 int const minutes = b % 60;
337 o << setw(2) << setfill('0') << hours << ":" << setw(2) << setfill('0') << minutes;
342 libdcp::ptime_to_string (boost::posix_time::ptime t)
344 struct tm t_tm = boost::posix_time::to_tm (t);
345 return tm_to_string (&t_tm);
349 /* Apparently there is no way to create an ofstream using a UTF-8
350 filename under Windows. We are hence reduced to using fopen
354 libdcp::fopen_boost (boost::filesystem::path p, string t)
356 #ifdef LIBDCP_WINDOWS
357 wstring w (t.begin(), t.end());
358 /* c_str() here should give a UTF-16 string */
359 return _wfopen (p.c_str(), w.c_str ());
361 return fopen (p.c_str(), t.c_str ());