libdcp
pkl.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018-2021 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 
40 #include "dcp_assert.h"
41 #include "exceptions.h"
42 #include "filesystem.h"
43 #include "pkl.h"
44 #include "raw_convert.h"
45 #include "util.h"
46 #include "warnings.h"
47 #include "verify.h"
48 LIBDCP_DISABLE_WARNINGS
49 #include <libxml++/libxml++.h>
50 LIBDCP_ENABLE_WARNINGS
51 #include <iostream>
52 
53 
54 using std::string;
55 using std::shared_ptr;
56 using std::make_shared;
57 using std::vector;
58 using boost::optional;
59 using namespace dcp;
60 
61 
62 static string const pkl_interop_ns = "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#";
63 static string const pkl_smpte_ns = "http://www.smpte-ra.org/schemas/429-8/2007/PKL";
64 /* I don't know why Resolve are using this namespace but apparently they are */
65 static string const pkl_resolve_smpte_ns = "http://www.smpte-ra.org/schemas/2067-2/2016/PKL";
66 
67 
68 PKL::PKL(boost::filesystem::path file, vector<dcp::VerificationNote>* notes)
69  : _file (file)
70 {
71  cxml::Document pkl ("PackingList");
72  pkl.read_file(dcp::filesystem::fix_long_path(file));
73 
74  if (pkl.namespace_uri() == pkl_interop_ns) {
75  _standard = Standard::INTEROP;
76  } else if (pkl.namespace_uri() == pkl_smpte_ns) {
77  _standard = Standard::SMPTE;
78  } else if (pkl.namespace_uri() == pkl_resolve_smpte_ns) {
79  _standard = Standard::SMPTE;
80  if (notes) {
81  notes->push_back(
83  dcp::VerificationNote::Type::ERROR,
85  pkl.namespace_uri(),
86  file
87  )
88  );
89  }
90  } else {
91  boost::throw_exception(XMLError("Unrecognised packing list namespace " + pkl.namespace_uri()));
92  }
93 
94  _id = remove_urn_uuid (pkl.string_child ("Id"));
95  _annotation_text = pkl.optional_string_child ("AnnotationText");
96  _issue_date = pkl.string_child ("IssueDate");
97  _issuer = pkl.string_child ("Issuer");
98  _creator = pkl.string_child ("Creator");
99 
100  for (auto i: pkl.node_child("AssetList")->node_children("Asset")) {
101  _assets.push_back(make_shared<Asset>(i));
102  }
103 }
104 
105 
106 void
107 PKL::add_asset(std::string id, boost::optional<std::string> annotation_text, std::string hash, int64_t size, std::string type, std::string original_filename)
108 {
109  _assets.push_back(make_shared<Asset>(id, annotation_text, hash, size, type, original_filename));
110 }
111 
112 
113 void
114 PKL::write_xml (boost::filesystem::path file, shared_ptr<const CertificateChain> signer) const
115 {
116  xmlpp::Document doc;
117  xmlpp::Element* pkl;
118  if (_standard == Standard::INTEROP) {
119  pkl = doc.create_root_node("PackingList", pkl_interop_ns);
120  } else {
121  pkl = doc.create_root_node("PackingList", pkl_smpte_ns);
122  }
123 
124  cxml::add_text_child(pkl, "Id", "urn:uuid:" + _id);
125  if (_annotation_text) {
126  cxml::add_text_child(pkl, "AnnotationText", *_annotation_text);
127  }
128  cxml::add_text_child(pkl, "IssueDate", _issue_date);
129  cxml::add_text_child(pkl, "Issuer", _issuer);
130  cxml::add_text_child(pkl, "Creator", _creator);
131 
132  auto asset_list = cxml::add_child(pkl, "AssetList");
133  for (auto i: _assets) {
134  auto asset = cxml::add_child(asset_list, "Asset");
135  cxml::add_text_child(asset, "Id", "urn:uuid:" + i->id());
136  if (i->annotation_text()) {
137  cxml::add_text_child(asset, "AnnotationText", *i->annotation_text());
138  }
139  cxml::add_text_child(asset, "Hash", i->hash());
140  cxml::add_text_child(asset, "Size", raw_convert<string>(i->size()));
141  cxml::add_text_child(asset, "Type", i->type());
142  if (auto filename = i->original_filename()) {
143  cxml::add_text_child(asset, "OriginalFileName", *filename);
144  }
145  }
146 
147  indent (pkl, 0);
148 
149  if (signer) {
150  signer->sign (pkl, _standard);
151  }
152 
153  doc.write_to_file_formatted(dcp::filesystem::fix_long_path(file).string(), "UTF-8");
154  _file = file;
155 }
156 
157 
158 optional<string>
159 PKL::hash (string id) const
160 {
161  for (auto i: _assets) {
162  if (i->id() == id) {
163  return i->hash();
164  }
165  }
166 
167  return {};
168 }
169 
170 
171 optional<string>
172 PKL::type (string id) const
173 {
174  for (auto i: _assets) {
175  if (i->id() == id) {
176  return i->type();
177  }
178  }
179 
180  return {};
181 }
182 
183 
184 void
185 PKL::clear_assets()
186 {
187  _assets.clear();
188 }
boost::optional< boost::filesystem::path > file() const
Definition: pkl.h:75
boost::optional< boost::filesystem::path > _file
Definition: pkl.h:135
An XML error.
Definition: exceptions.h:191
DCP_ASSERT macro.
Exceptions thrown by libdcp.
Namespace for everything in libdcp.
Definition: array_data.h:50
PKL class.
Methods for conversion to/from string.
Utility methods and classes.
dcp::verify() method and associated code