No-op: whitespace.
[libdcp.git] / src / mxf.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  src/asset.cc
21  *  @brief Parent class for assets of DCPs made up of MXF files.
22  */
23
24 #include "raw_convert.h"
25 #include "AS_DCP.h"
26 #include "KM_prng.h"
27 #include "KM_util.h"
28 #include "mxf.h"
29 #include "util.h"
30 #include "metadata.h"
31 #include "exceptions.h"
32 #include "dcp_assert.h"
33 #include "compose.hpp"
34 #include <libxml++/nodes/element.h>
35 #include <boost/filesystem.hpp>
36 #include <iostream>
37
38 using std::string;
39 using std::cout;
40 using std::list;
41 using std::pair;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44 using namespace dcp;
45
46 MXF::MXF ()
47         : _decryption_context (0)
48 {
49
50 }
51
52 MXF::~MXF ()
53 {
54         delete _decryption_context;
55 }
56
57 void
58 MXF::fill_writer_info (ASDCP::WriterInfo* writer_info, string id, Standard standard) const
59 {
60         writer_info->ProductVersion = _metadata.product_version;
61         writer_info->CompanyName = _metadata.company_name;
62         writer_info->ProductName = _metadata.product_name.c_str();
63
64         if (standard == INTEROP) {
65                 writer_info->LabelSetType = ASDCP::LS_MXF_INTEROP;
66         } else {
67                 writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
68         }
69         unsigned int c;
70         Kumu::hex2bin (id.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
71         DCP_ASSERT (c == Kumu::UUID_Length);
72
73         if (_key_id) {
74                 Kumu::GenRandomUUID (writer_info->ContextID);
75                 writer_info->EncryptedEssence = true;
76
77                 unsigned int c;
78                 Kumu::hex2bin (_key_id.get().c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
79                 DCP_ASSERT (c == Kumu::UUID_Length);
80         }
81 }
82
83 /** Set the (private) key that will be used to encrypt or decrypt this MXF's content.
84  *  This is the top-secret key that is distributed (itself encrypted) to cinemas
85  *  via Key Delivery Messages (KDMs).
86  *  @param key Key to use.
87  */
88 void
89 MXF::set_key (Key key)
90 {
91         _key = key;
92
93         if (!_key_id) {
94                 /* No key ID so far; we now need one */
95                 _key_id = make_uuid ();
96         }
97
98         _decryption_context = new ASDCP::AESDecContext;
99         if (ASDCP_FAILURE (_decryption_context->InitKey (_key->value ()))) {
100                 throw MiscError ("could not set up decryption context");
101         }
102 }
103
104 string
105 MXF::read_writer_info (ASDCP::WriterInfo const & info)
106 {
107         char buffer[64];
108
109         if (info.EncryptedEssence) {
110                 Kumu::bin2UUIDhex (info.CryptographicKeyID, ASDCP::UUIDlen, buffer, sizeof (buffer));
111                 _key_id = buffer;
112         }
113
114         _metadata.read (info);
115
116         Kumu::bin2UUIDhex (info.AssetUUID, ASDCP::UUIDlen, buffer, sizeof (buffer));
117         return buffer;
118 }