Fix incorrect free and leak.
[libdcp.git] / src / mxf_asset.cc
1 /*
2     Copyright (C) 2012 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 <iostream>
25 #include <fstream>
26 #include <boost/filesystem.hpp>
27 #include <boost/lexical_cast.hpp>
28 #include <libxml++/nodes/element.h>
29 #include "AS_DCP.h"
30 #include "KM_prng.h"
31 #include "KM_util.h"
32 #include "mxf_asset.h"
33 #include "util.h"
34 #include "metadata.h"
35 #include "exceptions.h"
36 #include "kdm.h"
37
38 using std::string;
39 using std::list;
40 using boost::shared_ptr;
41 using boost::lexical_cast;
42 using boost::dynamic_pointer_cast;
43 using namespace libdcp;
44
45 MXFAsset::MXFAsset (string directory, string file_name)
46         : Asset (directory, file_name)
47         , _progress (0)
48         , _encrypted (false)
49         , _encryption_context (0)
50         , _decryption_context (0)
51 {
52
53 }
54
55 MXFAsset::MXFAsset (string directory, string file_name, boost::signals2::signal<void (float)>* progress, int edit_rate, int intrinsic_duration, bool encrypted)
56         : Asset (directory, file_name, edit_rate, intrinsic_duration)
57         , _progress (progress)
58         , _encrypted (encrypted)
59         , _encryption_context (0)
60         , _decryption_context (0)
61 {
62         if (_encrypted) {
63                 _key_id = make_uuid ();
64                 uint8_t key_buffer[ASDCP::KeyLen];
65                 Kumu::FortunaRNG rng;
66                 rng.FillRandom (key_buffer, ASDCP::KeyLen);
67                 char key_string[ASDCP::KeyLen * 4];
68                 Kumu::bin2hex (key_buffer, ASDCP::KeyLen, key_string, ASDCP::KeyLen * 4);
69                 _key_value = key_string;
70                         
71                 _encryption_context = new ASDCP::AESEncContext;
72                 if (ASDCP_FAILURE (_encryption_context->InitKey (key_buffer))) {
73                         throw MiscError ("could not set up encryption context");
74                 }
75
76                 uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE];
77                 
78                 if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) {
79                         throw MiscError ("could not set up CBC initialization vector");
80                 }
81         }
82 }
83
84 MXFAsset::~MXFAsset ()
85 {
86         delete _encryption_context;
87 }
88
89 void
90 MXFAsset::fill_writer_info (ASDCP::WriterInfo* writer_info, string uuid, MXFMetadata const & metadata)
91 {
92         writer_info->ProductVersion = metadata.product_version;
93         writer_info->CompanyName = metadata.company_name;
94         writer_info->ProductName = metadata.product_name.c_str();
95
96         writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
97         unsigned int c;
98         Kumu::hex2bin (uuid.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
99         assert (c == Kumu::UUID_Length);
100
101         if (_encrypted) {
102                 Kumu::GenRandomUUID (writer_info->ContextID);
103                 writer_info->EncryptedEssence = true;
104
105                 unsigned int c;
106                 Kumu::hex2bin (_key_id.c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
107                 assert (c == Kumu::UUID_Length);
108         }
109 }
110
111 bool
112 MXFAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
113 {
114         if (!Asset::equals (other, opt, note)) {
115                 return false;
116         }
117         
118         shared_ptr<const MXFAsset> other_mxf = dynamic_pointer_cast<const MXFAsset> (other);
119         if (!other_mxf) {
120                 note (ERROR, "comparing an MXF asset with a non-MXF asset");
121                 return false;
122         }
123         
124         if (_file_name != other_mxf->_file_name) {
125                 note (ERROR, "MXF names differ");
126                 if (!opt.mxf_names_can_differ) {
127                         return false;
128                 }
129         }
130         
131         return true;
132 }
133
134 void
135 MXFAsset::add_typed_key_id (xmlpp::Element* parent) const
136 {
137         xmlpp::Element* typed_key_id = parent->add_child("TypedKeyId");
138         typed_key_id->add_child("KeyType")->add_child_text(key_type ());
139         typed_key_id->add_child("KeyId")->add_child_text("urn:uuid:" + _key_id);
140 }
141
142 void
143 MXFAsset::write_to_cpl (xmlpp::Node* node) const
144 {
145         xmlpp::Node* a = node->add_child (cpl_node_name ());
146         a->add_child ("Id")->add_child_text ("urn:uuid:" + _uuid);
147         a->add_child ("AnnotationText")->add_child_text (_file_name);
148         a->add_child ("EditRate")->add_child_text (lexical_cast<string> (_edit_rate) + " 1");
149         a->add_child ("IntrinsicDuration")->add_child_text (lexical_cast<string> (_intrinsic_duration));
150         a->add_child ("EntryPoint")->add_child_text (lexical_cast<string> (_entry_point));
151         a->add_child ("Duration")->add_child_text (lexical_cast<string> (_duration));
152         if (_encrypted) {
153                 a->add_child("KeyId")->add_child_text ("urn:uuid:" + _key_id);
154         }
155 }
156
157 void
158 MXFAsset::set_kdm_cipher (KDMCipher cipher)
159 {
160         _decryption_context = new ASDCP::AESDecContext;
161         if (ASDCP_FAILURE (_decryption_context->InitKey (cipher.key_raw ()))) {
162                 throw MiscError ("could not set up decryption context");
163         }
164 }