summaryrefslogtreecommitdiff
path: root/tools/dcpdumpsub.cc
blob: a14d497802095d0ee578f84eb0fc1a9fb63cf112 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
    Copyright (C) 2015 Carl Hetherington <cth@carlh.net>

    This file is part of libdcp.

    libdcp is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libdcp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations
    including the two.

    You must obey the GNU General Public License in all respects
    for all of the code used other than OpenSSL.  If you modify
    file(s) with this exception, you may extend this exception to your
    version of the file(s), but you are not obligated to do so.  If you
    do not wish to do so, delete this exception statement from your
    version.  If you delete this exception statement from all source
    files in the program, then also delete it here.
*/


#include "decrypted_kdm.h"
#include "decrypted_kdm_key.h"
#include "encrypted_kdm.h"
#include "file.h"
#include "smpte_text_asset.h"
#include "util.h"
#include <getopt.h>
#include <cstdlib>
#include <iostream>
#include <string>


using std::cerr;
using std::cout;
using std::map;
using std::string;
using boost::optional;


static void
help (string n)
{
	cerr << "Syntax: " << n << " [OPTION] <MXF>\n"
	     << "  -h, --help         show this help\n"
	     << "  -n, --no-fonts     don't extract fonts\n"
	     << "  -k, --kdm          KDM file\n"
	     << "  -p, --private-key  private key file\n";
}

int
main (int argc, char* argv[])
{
	dcp::init ();

	bool extract_fonts = true;
	optional<boost::filesystem::path> kdm_file;
	optional<boost::filesystem::path> private_key_file;

	int option_index = 0;
	while (1) {
		static struct option long_options[] = {
			{ "help", no_argument, 0, 'h'},
			{ "no-fonts", no_argument, 0, 'n'},
			{ "kdm", required_argument, 0, 'k'},
			{ "private-key", required_argument, 0, 'p'},
			{ 0, 0, 0, 0 }
		};

		int c = getopt_long (argc, argv, "hnk:p:", long_options, &option_index);

		if (c == -1) {
			break;
		}

		switch (c) {
		case 'h':
			help (argv[0]);
			exit (EXIT_SUCCESS);
		case 'n':
			extract_fonts = false;
			break;
		case 'k':
			kdm_file = optarg;
			break;
		case 'p':
			private_key_file = optarg;
			break;
		}
	}

	if (argc <= optind || argc > (optind + 2)) {
		help (argv[0]);
		exit (EXIT_FAILURE);
	}

	dcp::SMPTETextAsset sub (argv[optind]);

	if (sub.key_id() && (!kdm_file || !private_key_file)) {
		cerr << "Subtitle MXF is encrypted so you must provide a KDM and private key.\n";
		exit (EXIT_FAILURE);
	}

	if (sub.key_id()) {
		dcp::EncryptedKDM encrypted_kdm (dcp::file_to_string (kdm_file.get ()));
		dcp::DecryptedKDM decrypted_kdm (encrypted_kdm, dcp::file_to_string (private_key_file.get()));
		bool done = false;
		for (auto const& i: decrypted_kdm.keys()) {
			if (i.id() == *sub.key_id()) {
				sub.set_key (i.key ());
				done = true;
			}
		}
		if (!done) {
			cerr << "Could not find required key in KDM.\n";
			exit (EXIT_FAILURE);
		}
	}

	cout << sub.xml_as_string() << "\n";

	if (extract_fonts) {
		for (auto const& i: sub.font_data()) {
			dcp::File f(i.first + ".ttf", "wb");
			if (!f) {
				cerr << "Could not open font file " << i.first << ".ttf for writing";
				exit (EXIT_FAILURE);
			}
			f.write(i.second.data(), 1, i.second.size());
		}
	}
}